Module:Sandbox/Timbaaa/test/1

local p = {}

local baseChartParams = {} local baseChartParams = {} local activeChartParams = {} local deltaChartParams = {}

local function csvToArray(csv) if not csv then return end local list = mw.text.split(csv, "%s*,%s*") local result = {} local isInteger = true for i = 1, #list do		if list[i] == "" then result[i] = nil else result[i] = tonumber(list[i]) if not result[i] then return end end end -- return result, isInteger return result end

local function getDelta(list) local result = {} for i = 1, #list do		if list[i] == nil then result[i] = nil else if i == 1 then result[i] = list[i] else result[i] = list[i] - list[i-1] end end end return result end

local function arrayTocsv(array) local result = '' for i = 1, #array do		result = result..array[i]..', ' end return result end

local function getMovingAverage(list) local result = {} for i = 1, #list do		if i == 1 then result[1] = list[1] elseif i == 2 then result[i] = (list[i]+list[i-1])/2 elseif i == 3 then result[i] = (list[i]+list[i-1]+list[i-2])/3 elseif i == 4 then result[i] = (list[i]+list[i-1]+list[i-2]+list[i-3])/4 elseif i == 5 then result[i] = (list[i]+list[i-1]+list[i-2]+list[i-3]+list[i-4])/5 elseif i == 6 then result[i] = (list[i]+list[i-1]+list[i-2]+list[i-3]+list[i-4]+list[i-5])/6 elseif i == 7 then result[i] = (list[i]+list[i-1]+list[i-2]+list[i-3]+list[i-4]+list[i-5]+list[i-6])/7 else result[i] = (list[i]+list[i-1]+list[i-2]+list[i-3]+list[i-4]+list[i-5]+list[i-6])/7 end end return result end

local function makewikitext(expandedtemplate) local div = mw.html.create( 'div' ) div :attr( 'class', 'toccolours' ) :css('display','inline-block') :wikitext(expandedtemplate) return tostring(div) end

local function makeWikiPlainText(header) local h = mw.html.create h:wikitext(""..header.."") return tostring(h) end

p.main = function( frame ) local array = csvToArray(frame.args.total) local deltaArray = getDelta(array) local deltaCSV = arrayTocsv(deltaArray) baseChartParams.width = 500 baseChartParams.height = 300 baseChartParams.xType = 'date' baseChartParams.yType = 'number' baseChartParams.xAxisTitle = 'Date' baseChartParams.yAxisTitle = 'Count' baseChartParams.yScaleType = 'linear' baseChartParams.xGrid = '' baseChartParams.yGrid = '' baseChartParams.type = 'line' baseChartParams.x = frame.args.date baseChartParams.y1 = frame.args.total baseChartParams.y1Title = 'Reported cases' baseChartParams.y2 = frame.args.active baseChartParams.y2Title = 'Active cases' baseChartParams.y3 = frame.args.dead baseChartParams.y3Title = 'Deaths' baseChartParams.colors = '#FF6347,#FFC000,#343132' baseChartParams.legend ='' baseChartParams = baseChartParams graphWikiText1 = frame:expandTemplate{title='Graph:Chart',args = baseChartParams} baseChartParams.yAxisMin = 1 baseChartParams.yScaleType = 'log' graphWikiText2 = frame:expandTemplate{title='Graph:Chart',args = baseChartParams} activeChartParams.width = 500 activeChartParams.height = 300 activeChartParams.xType = 'date' activeChartParams.yType = 'number' activeChartParams.xAxisTitle = 'Date' activeChartParams.yAxisTitle = 'Count' activeChartParams.yScaleType = 'linear' activeChartParams.xGrid = '' activeChartParams.yGrid = '' activeChartParams.type = 'line' activeChartParams.x = frame.args.date activeChartParams.y1 = frame.args.active activeChartParams.y1Title = 'Active cases' activeChartParams.colors = '#FFC000' deltaChartParams.width = 500 deltaChartParams.height = 300 deltaChartParams.xType = 'date' deltaChartParams.yType = 'number' deltaChartParams.xAxisTitle = 'Date' deltaChartParams.yAxisTitle = 'Count' deltaChartParams.yScaleType = 'linear' deltaChartParams.xGrid = '' deltaChartParams.yGrid = '' deltaChartParams.type = 'line' deltaChartParams.x = frame.args.date deltaChartParams.y1 = arrayTocsv(getDelta(csvToArray(frame.args.total))) deltaChartParams.y1Title = 'Reported Cases' deltaChartParams.y2 = arrayTocsv(getDelta(csvToArray(frame.args.dead))) deltaChartParams.y2Title = 'Deaths' deltaChartParams.y3 = arrayTocsv(getMovingAverage(getDelta(csvToArray(frame.args.total)))) deltaChartParams.y3Title = 'Moving average of reported cases' deltaChartParams.y4 = arrayTocsv(getMovingAverage(getDelta(csvToArray(frame.args.dead)))) deltaChartParams.y4Title = 'Moving average of deaths' deltaChartParams.linewidths = '2,2,1,1' deltaChartParams.legend ='' deltaChartParams = deltaChartParams deltaChartWikiText1 = frame:expandTemplate{title='Graph:Chart',args = deltaChartParams} deltaChartParams.yAxisMin = 1 deltaChartParams.yScaleType = 'log' deltaChartWikiText2 = frame:expandTemplate{title='Graph:Chart',args = deltaChartParams} mainWikiText = frame:expandTemplate{title='Switcher',args={graphWikiText1, 'Linear scale', graphWikiText2,'Log scale'}} activeWikiText = frame:expandTemplate{title='Graph:Chart',args = activeChartParams} dailyWikiText = frame:expandTemplate{title='Switcher',args={deltaChartWikiText1, 'Linear scale', deltaChartWikiText2,'Log scale'}} return makeWikiPlainText('Cumulative cases')..makewikitext(mainWikiText)..makeWikiPlainText('Active cases')..makewikitext(activeWikiText)..makeWikiPlainText('Daily change')..makewikitext(dailyWikiText) end return p