Module:Chart data/doc
This is the documentation page for Module:Chart data
Helps create the json to generate charts using Chart.js through MediaWiki:Gadget-Charts-core.js.
This module is a helper module to be used by other modules; it may not designed to be invoked directly. See RuneScape:Lua/Helper modules for a full list and more information.
Function | Type | Use |
---|---|---|
_main( args ) | table/chart | Turns a table/chart object into a json string. |
convertToXYFormat( ys, [xs|{}] ) | table, table | Converts the ys array into an array of {x = n, y = y[n]} tables. If xs is already partially filled it will use {x = x[n], y = y[n]} until all values in xs are used, then it will use {x = n, y = y[n]} again for the remaining values in ys . |
generateXYFromFunc( func, start_x, end_x, [step|1] ) | function, number, number, number | Returns an array of {x = n, y = fun(n)} tables where n ranges from start_x to end_x in step increments. Be careful when using decimal step values as floating point error can cause the generator to stop one element too soon. |
jagexInterpolation( low_chance, high_chance, start_level, end_level ) | number, number, number, number | |
newChart( [options] ) | table | Returns a new chart object. options is a table with options in the Chart.js format. Most options will be set automatically or will be set later with other functions if not already defined. Usually all you need to define here is the chart type e.g. newChart{ type = 'scatter' } . Check the modules documentation for more info. |
chart:addDataSets( ... ) | table/dataSet object | Appends all given data sets to the chart.data.datasets table. |
chart:addDataLabels( labels ) | table | Appends all items in labels to the chart.data.labels table. |
chart:setDimensions( width, [height|width], [minWidth|400], [minHeight|400], [resizable|false] ) | number/string, number/string, number/string, number/string, boolean | Sets the dimensions of the chart. If a number is given to width , height , minWidth and minHeight it will be assumed you meant pixels. You can also use strings like 100% to fill the available space, 40vw /40vh to dynamically scale the simensions to the viewport size (i.e. 40vw = 40% of browser's window width). If resizable is true, the chart can be dragged by the lower right corner to change its size. |
chart:setTitle( [text|nil], [position|'top'] ) | string, string | Sets the label title of the chart. A value of nil will remove the current title. |
chart:setXLabel( [label|nil] ) | string | Sets the label for the x axis. Only works on chart types 'line', 'bar', 'horizontalBar', 'bubble' and 'scatter'. If used without arguments it will remove the current label. |
chart:setYLabel( [label|nil] ) | string | Sets the label for the y axis. Only works on chart types 'line', 'bar', 'horizontalBar', 'bubble' and 'scatter'. If used without arguments it will remove the current label. |
chart:setXLimits( [min|nil], [max|nil], [step|nil] ) | number, number, number | Sets the start, stop and step size of the x axis. Any argument with a value of nil will remove that setting. Only works on chart types 'horizontalBar', 'bubble' and 'scatter'. |
chart:setYLimits( [min|nil], [max|nil], [step|nil] ) | number, number, number | Sets the start, stop and step size of the y axis. Any argument with a value of nil will remove that setting. Only works on chart types 'line', 'bar', 'bubble' and 'scatter'. |
chart:setRadialLimits( [min|nil], [max|nil], [step|nil] ) | number, number, number | Sets the start, stop and step size of the r axis on polar plots. Any argument with a value of nil will remove that setting. Only works on chart types 'radar' and 'polarArea'. |
chart:setXAxisType( [type|nil] ) | string | Sets the axis type. Supported values are 'linear', 'logarithmic', 'category' and 'time'. If called without arguments it will reset back to the default value 'linear'. Only works on chart types 'bubble', 'scatter' and 'horizontalBar'. |
chart:setYAxisType( [type|nil] ) | string | Same as chart:setXAxisType() but for the y axis. Only works on chart types 'line', 'bubble', 'scatter' and 'bar'. |
chart:setOptions( options ) | table | Sets options using Chart.js format but makes sure you only change the given settings and not accidentally delete already existing settings. i.e. using chart:setOptions{ options = {scales = {ticks = {max = 100}}} } while {options = {scales = {ticks = {min = 0}}}} already exists will result in {options = {scales = {ticks = {min = 0, max = 100}}}} . |
chart:makeMwLoadDataCompatible() | N/A | Strips metatables so that it can be loaded by mw.loadData() making it possible to display the table using {{Chart data|<module name>}} . This should only be used at the very end when you are done creating your chart. |
chart:newDataSet( [options] ) | table | Returns a new dataSet object which is also automatically added to the chart datasets table. |
dataSet:addData( data ) | table | Appends the values of data to the already existing data stored in the dataSet.data array. Using dataset.data = data will overwrite any stored data. |
dataSet:addDataPoint( data ) | number/table | Append a single value to the dataSet.data array. Same as table.insert( dataSet.data, data ) . |
dataSet:setOptions( options ) | table | Sets options using Chart.js format. |
Example:
local p = {}
local chart = require( 'Module:Chart data' )
-- This chart can then be added to a page using {{Chart data|<module name>}}
function p.pie()
local plot = chart.newChart{ type = 'pie' }
plot:setDimensions( '40vw', nil, 400, nil, true ) -- Pie chart is always square
plot.colorPallet = chart.colorPallets.green
local labels = {}
local set = plot:newDataSet()
for i = 1, 6 do
set:addDataPoint( math.floor( math.sqrt( i ) * 10 + 0.5 ) / 10 )
table.insert( labels, 'Value ' .. i )
end
plot:addDataLabels( labels )
return plot:makeMwLoadDataCompatible()
end
return p.pie()
-------------------------------------------------------------
local p = {}
local chart = require( 'Module:Chart data' )
-- This chart cound be used by another module or drawn using {{#Invoke:<module name>|bar}}
function p.bar()
local plot = chart.newChart{ type = 'bar' }
plot:setDimensions( '40%', 600, 400, 400, true )
plot:setXLabel( 'x axis label' )
plot:setYLabel( 'y axis label' )
for i = 1, 2 do
local set = plot:newDataSet()
for j = 1, 6 do
set:addDataPoint( math.sqrt( i*j ) )
end
set.label = 'Set ' .. i
if i == 1 then
set.color = chart.colorPallets.green[3]
else
set.color = chart.colorPallets.orange[3]
end
end
local labels = {}
for i = 1, 6 do
table.insert( labels, 'Value ' .. i )
end
plot:addDataLabels( labels )
return plot
end
return p
Usage
To create a chart we need to create JSON in a format described by chart.js.
To make this a bit easier we can write a lua table instead which is then converted to a JSON using chart._main()
.
An example of this is Module:Chart data/xp chart, which can then be displayed on a page using {{Chart data|Chart data/xp chart|height=40vh|width=40vw}}
resulting in:
{"type":"scatter","data":{"datasets":[{"data":[{"y":0,"x":1},{"y":83,"x":2},{"y":174,"x":3},{"y":276,"x":4},{"y":388,"x":5},{"y":512,"x":6},{"y":650,"x":7},{"y":801,"x":8},{"y":969,"x":9},{"y":1154,"x":10},{"y":1358,"x":11},{"y":1584,"x":12},{"y":1833,"x":13},{"y":2107,"x":14},{"y":2411,"x":15},{"y":2746,"x":16},{"y":3115,"x":17},{"y":3523,"x":18},{"y":3973,"x":19},{"y":4470,"x":20},{"y":5018,"x":21},{"y":5624,"x":22},{"y":6291,"x":23},{"y":7028,"x":24},{"y":7842,"x":25},{"y":8740,"x":26},{"y":9730,"x":27},{"y":10824,"x":28},{"y":12031,"x":29},{"y":13363,"x":30},{"y":14833,"x":31},{"y":16456,"x":32},{"y":18247,"x":33},{"y":20224,"x":34},{"y":22406,"x":35},{"y":24815,"x":36},{"y":27473,"x":37},{"y":30408,"x":38},{"y":33648,"x":39},{"y":37224,"x":40},{"y":41171,"x":41},{"y":45529,"x":42},{"y":50339,"x":43},{"y":55649,"x":44},{"y":61512,"x":45},{"y":67983,"x":46},{"y":75127,"x":47},{"y":83014,"x":48},{"y":91721,"x":49},{"y":101333,"x":50},{"y":111945,"x":51},{"y":123660,"x":52},{"y":136594,"x":53},{"y":150872,"x":54},{"y":166636,"x":55},{"y":184040,"x":56},{"y":203254,"x":57},{"y":224466,"x":58},{"y":247886,"x":59},{"y":273742,"x":60},{"y":302288,"x":61},{"y":333804,"x":62},{"y":368599,"x":63},{"y":407015,"x":64},{"y":449428,"x":65},{"y":496254,"x":66},{"y":547953,"x":67},{"y":605032,"x":68},{"y":668051,"x":69},{"y":737627,"x":70},{"y":814445,"x":71},{"y":899257,"x":72},{"y":992895,"x":73},{"y":1096278,"x":74},{"y":1210421,"x":75},{"y":1336443,"x":76},{"y":1475581,"x":77},{"y":1629200,"x":78},{"y":1798808,"x":79},{"y":1986068,"x":80},{"y":2192818,"x":81},{"y":2421087,"x":82},{"y":2673114,"x":83},{"y":2951373,"x":84},{"y":3258594,"x":85},{"y":3597792,"x":86},{"y":3972294,"x":87},{"y":4385776,"x":88},{"y":4842295,"x":89},{"y":5346332,"x":90},{"y":5902831,"x":91},{"y":6517253,"x":92},{"y":7195629,"x":93},{"y":7944614,"x":94},{"y":8771558,"x":95},{"y":9684577,"x":96},{"y":10692629,"x":97},{"y":11805606,"x":98},{"y":13034431,"x":99},{"y":14391160,"x":100},{"y":15889109,"x":101},{"y":17542976,"x":102},{"y":19368992,"x":103},{"y":21385073,"x":104},{"y":23611006,"x":105},{"y":26068632,"x":106},{"y":28782069,"x":107},{"y":31777943,"x":108},{"y":35085654,"x":109},{"y":38737661,"x":110},{"y":42769801,"x":111},{"y":47221641,"x":112},{"y":52136869,"x":113},{"y":57563718,"x":114},{"y":63555443,"x":115},{"y":70170840,"x":116},{"y":77474828,"x":117},{"y":85539082,"x":118},{"y":94442737,"x":119},{"y":104273167,"x":120},{"y":115126838,"x":121},{"y":127110260,"x":122},{"y":140341028,"x":123},{"y":154948977,"x":124},{"y":171077457,"x":125},{"y":188884740,"x":126}],"label":"Standard skill","borderColor":"rgba(166,206,227,1)","showLine":true,"backgroundColor":"rgba(166,206,227,0.2)","fill":false}]},"options":{"maintainAspectRatio":false,"scales":{"y":{"ticks":{"beginAtZero":true},"scaleLabel":{"display":true,"labelString":"Experience"}},"x":{"ticks":{"beginAtZero":true},"scaleLabel":{"display":true,"labelString":"Level"}}},"tooltips":{"mode":"x","intersect":false,"position":"nearest"}}}
To make the construction of this table a bit easier, the chart class is provided which internally has the extact same structure as if we manually created the table. The chart class just sets a bunch of default settings based on what chart type you are creating; it also makes it a lot easier to deal with the colors of your data and provides a bunch of functions to easily set axis labels, axis start and stop values, axis type, etc.
Color pallets
chart.colorPallets has a few pre-defined color pallets. These pallets are made up of Module:Rgba objects. They also have a metatable set which allows them to automatically use the next color pallet in case the current pallet has less colors than your number of datasets.
You can set your preferred pallet for your chart with myChart.colorPallet = chart.collorPallets.blue
, in case of chart type bar of horizontalbar your can also set it per data set myDataSet.colorPallet = chart.collorPallets.orange
or if you want all bars of a given set to be the same color myDataSet.color = chart.collorpallets.orange[3]
.
It is possible to define your own color pallet as an array of Module:Rgba objects.
The chart class has the following color opions:
backgroundAlpha
- sets thergba:fade()
value for background colors.hoverLightenValue
- sets thergba:lighten()
value for when you hover over a data point.hoverAlpha
- sets thergba:fade()
value for when you hover over a data point.hoverSaturateValue
- sets thergba:saturate()
value for when you hover over a data point.
Their default value depend on the cart type but you can manually set them using:
chart:setOptions{ backgroundAlpha = <number>, hoverLightenValue = <number>, hoverAlpha = <number>, hoverSaturateValue = <number> }
Code |
function p.colorQualitative()
local plot = chart.newChart{ type = 'scatter' }
plot:setDimensions( '10vw', '10vh', 300, 300, true )
plot:setTitle( 'Qualitative' )
plot.fill = false
plot.colorPallet = chart.colorPallets.qualitative
for i = 1, #plot.colorPallet do
local set = plot:newDataSet()
set.data = chart.generateXYFromFunc( function(x) return i*x^2 end, 0, 50 )
set.label = 'Set ' .. i
end
return plot:makeMwLoadDataCompatible()
end
|
Code |
function p.colorBlue()
local plot = chart.newChart{ type = 'scatter' }
plot:setDimensions( '10vw', '10vh', 300, 300, true )
plot:setTitle( 'Blue' )
plot.fill = false
plot.colorPallet = chart.colorPallets.blue
for i = 1, #plot.colorPallet do
local set = plot:newDataSet()
set.data = chart.generateXYFromFunc( function(x) return i*x^2 end, 0, 50 )
set.label = 'Set ' .. i
end
return plot:makeMwLoadDataCompatible()
end
|
Code |
function p.colorGreen()
local plot = chart.newChart{ type = 'scatter' }
plot:setDimensions( '10vw', '10vh', 300, 300, true )
plot:setTitle( 'Green' )
plot.fill = false
plot.colorPallet = chart.colorPallets.green
for i = 1, #plot.colorPallet do
local set = plot:newDataSet()
set.data = chart.generateXYFromFunc( function(x) return i*x^2 end, 0, 50 )
set.label = 'Set ' .. i
end
return plot:makeMwLoadDataCompatible()
end
|
Code |
function p.colorBlueGreen()
local plot = chart.newChart{ type = 'scatter' }
plot:setDimensions( '10vw', '10vh', 300, 300, true )
plot:setTitle( 'BlueGreen' )
plot.fill = false
plot.colorPallet = chart.colorPallets.blueGreen
for i = 1, #plot.colorPallet do
local set = plot:newDataSet()
set.data = chart.generateXYFromFunc( function(x) return i*x^2 end, 0, 50 )
set.label = 'Set ' .. i
end
return plot:makeMwLoadDataCompatible()
end
|
Code |
function p.colorOrange()
local plot = chart.newChart{ type = 'scatter' }
plot:setDimensions( '10vw', '10vh', 300, 300, true )
plot:setTitle( 'Orange' )
plot.fill = false
plot.colorPallet = chart.colorPallets.orange
for i = 1, #plot.colorPallet do
local set = plot:newDataSet()
set.data = chart.generateXYFromFunc( function(x) return i*x^2 end, 0, 50 )
set.label = 'Set ' .. i
end
return plot:makeMwLoadDataCompatible()
end
|
Code |
function p.colorOverflow()
local plot = chart.newChart{ type = 'scatter' }
plot:setDimensions( '10vw', '10vh', 300, 300, true )
plot:setTitle( 'Color overflow' )
plot:setOptions{
options = {
legend = {
display = false
}
}
}
plot.fill = false
plot.colorPallet = chart.colorPallets.orange
for i = 1, 25 do
local set = plot:newDataSet()
set.data = chart.generateXYFromFunc( function(x) return i*x^2 end, 0, 50 )
set.label = 'Set ' .. i
end
return plot:makeMwLoadDataCompatible()
end
|
Code |
function p.customColorPallet()
local plot = chart.newChart{ type = 'scatter' }
plot:setDimensions( '10vw', '10vh', 300, 300, true )
plot:setTitle( 'Custom color pallet' )
plot.fill = false
plot.colorPallet = {
chart.rgba.new(0,0,0),
chart.rgba.new(126,0,0),
chart.rgba.new(0,126,0),
chart.rgba.new(0,0,126),
chart.rgba.new(255,0,0),
chart.rgba.new(0,255,0),
chart.rgba.new(0,0,255),
}
for i = 1, #plot.colorPallet do
local set = plot:newDataSet()
set.data = chart.generateXYFromFunc( function(x) return i*x^2 end, 0, 50 )
set.label = 'Set ' .. i
end
return plot:makeMwLoadDataCompatible()
end
|
Code |
function p.colorOptions()
local plot = chart.newChart{ type = 'bar' }
plot:setDimensions( '10vw', '10vh', 300, 300, true )
plot:setTitle( 'Color options' )
plot:setOptions{
backgroundAlpha = 0.8,
hoverLightenValue = 0.5,
hoverAlpha = 0.4,
hoverSaturateValue = 1.5,
}
for i = 1, 2 do
local set = plot:newDataSet()
for j = 1, 6 do
set:addDataPoint( math.sqrt( i*j ) )
end
set.label = 'Set ' .. i
if i == 1 then
set.color = chart.colorPallets.green[3]
else
set.color = chart.colorPallets.orange[3]
end
end
local labels = {}
for i = 1, 6 do
table.insert( labels, 'Value ' .. i )
end
plot:addDataLabels( labels )
return plot:makeMwLoadDataCompatible()
end
|
Examples
Code |
function p.line()
local plot = chart.newChart{ type = 'line' }
plot:setDimensions( '10vw', '10vh', 300, 300, true )
plot:setTitle( 'Line chart' )
plot:setXLabel( 'x axis label' )
plot:setYLabel( 'y axis label' )
for i = 1, 2 do
local set = plot:newDataSet()
set.data = { i^2, (i+1)^2, (i+2)^2, (i+3^2), (i+4)^2 }
set.label = 'Set ' .. i
set.borderDash = {5, 5}
end
local labels = {}
for i = 1, 5 do
table.insert( labels, 'Value ' .. i )
end
plot:addDataLabels( labels )
return 'Prefix text message' .. plot .. 'Affix text message'
end
|
Code |
function p.bar()
local plot = chart.newChart{ type = 'bar' }
plot:setDimensions( '10vw', '10vh', 300, 300, true )
plot:setTitle( 'Bar chart' )
plot:setXLabel( 'x axis label' )
plot:setYLabel( 'y axis label' )
for i = 1, 2 do
local set = plot:newDataSet()
for j = 1, 6 do
set:addDataPoint( math.sqrt( i*j ) )
end
set.label = 'Set ' .. i
if i == 1 then
set.color = chart.colorPallets.green[3]
else
set.color = chart.colorPallets.orange[3]
end
end
local labels = {}
for i = 1, 6 do
table.insert( labels, 'Value ' .. i )
end
plot:addDataLabels( labels )
return plot:makeMwLoadDataCompatible()
end
|
Code |
function p.horizontalBar()
local plot = chart.newChart{ type = 'horizontalBar' }
plot:setDimensions( '10vw', '10vh', 300, 300, true )
plot:setTitle( 'HorizontalBar chart' )
plot:setXLabel( 'x axis label' )
plot:setYLabel( 'y axis label' )
for i = 1, 2 do
local set = plot:newDataSet()
for j = 1, 6 do
set:addDataPoint( math.sqrt( i*j ) )
end
set.label = 'Set ' .. i
if i == 1 then
set.color = chart.colorPallets.green[3]
else
set.color = chart.colorPallets.orange[3]
end
end
local labels = {}
for i = 1, 6 do
table.insert( labels, 'Value ' .. i )
end
plot:addDataLabels( labels )
return plot:makeMwLoadDataCompatible()
end
|
Code |
function p.radar()
local plot = chart.newChart{ type = 'radar' }
plot:setDimensions( '10vw', nil, 300, nil, true ) -- Radar chart is always square. height = width
plot:setTitle( 'Radar chart' )
plot:setRadialLimits( 0, 5 )
plot.fill = false
for i = 1, 5 do
local set = plot:newDataSet()
for j = 1, 6 do
set:addDataPoint( math.sqrt( i*j*(math.random()+1) ) )
end
set.label = 'Set ' .. i
if i == 3 then
set.fill = true
end
end
local labels = {}
for i = 1, 6 do
table.insert( labels, 'Value ' .. i )
end
plot:addDataLabels( labels )
return plot:makeMwLoadDataCompatible()
end
|
Code |
function p.pie()
local plot = chart.newChart{ type = 'pie' }
plot:setDimensions( '10vw', nil, 300, nil, true ) -- Pie chart is always square. height = width
plot:setTitle( 'Pie chart' )
local labels = {}
local set = plot:newDataSet()
for i = 1, 6 do
set:addDataPoint( math.floor( math.sqrt( i ) * 10 + 0.5 ) / 10 )
table.insert( labels, 'Value ' .. i )
end
plot:addDataLabels( labels )
return plot:makeMwLoadDataCompatible()
end
|
Code |
function p.doughnut()
local plot = chart.newChart{ type = 'doughnut' }
plot:setDimensions( '10vw', nil, 300, nil, true ) -- doughnut chart is always square. height = width
plot:setTitle( 'Doughnut chart' )
local labels = {}
local set = plot:newDataSet()
for i = 1, 6 do
set:addDataPoint( math.floor( math.sqrt( i ) * 10 + 0.5 ) / 10 )
table.insert( labels, 'Value ' .. i )
end
plot:addDataLabels( labels )
return plot:makeMwLoadDataCompatible()
end
|
Code |
function p.polarArea()
local plot = chart.newChart{ type = 'polarArea' }
plot:setDimensions( '10vw', nil, 300, nil, true ) -- polarArea chart is always square. height = width
plot:setTitle( 'PolarArea chart' )
local labels = {}
local set = plot:newDataSet()
for i = 1, 6 do
set:addDataPoint( math.floor( math.sqrt( i ) * 10 + 0.5 ) / 10 )
table.insert( labels, 'Value ' .. i )
end
plot:addDataLabels( labels )
return plot:makeMwLoadDataCompatible()
end
|
Code |
function p.bubble()
local plot = chart.newChart{ type = 'bubble' }
plot:setDimensions( '10vw', '10vh', 300, 300, true )
plot:setTitle( 'Bubble chart' )
plot:setXLabel( 'x axis label' )
plot:setYLabel( 'y axis label' )
for i = 1, 5 do
local set = plot:newDataSet()
for j = 1, 5 do
set:addDataPoint{ x = math.random() * 10, y = math.random() * 20, r = math.random() * 15 + 5 }
end
set.label = 'Set ' .. i
end
return plot:makeMwLoadDataCompatible()
end
|
Code |
function p.scatter()
local plot = chart.newChart{ type = 'scatter' }
plot:setDimensions( '10vw', '10vh', 300, 300, true )
plot:setTitle( 'Scatter chart' )
plot:setXLabel( 'x axis label' )
plot:setYLabel( 'y axis label' )
plot:setYAxisType( 'logarithmic' )
plot.fill = false
for i = 1, 5 do
local set = plot:newDataSet()
set.data = chart.generateXYFromFunc( function(x) return (math.sin( x/5 ) * 5 + x)^i end, 0, 100 )
set.label = 'Set ' .. i
if i == 2 then
set.fill = '+2'
end
end
return plot:makeMwLoadDataCompatible()
end
|