tonumber(os.date('%m', args.nStartDate or args.nAltStartDate)),
tonumber(os.date('%Y', args.nEndDate or args.nAltEndDate )),
tonumber(os.date('%m', args.nEndDate or args.nAltEndDate ))
dates = {nStartDate=args.nStartDate, nAltStartDate=args.nAltStartDate, nEndDate=args.nEndDate, nAltEndDate=args.nAltEndDate}  
repeat
id[#id+1] = (args.multiyear and y or '') .. lang:lc(monthAbbrs[m])
dates[#dates+1] = {year=y, mon=monthAbbrs[m]}
y = y + math.floor(m / 12)
m = m % 12 + 1
until y == ey and m > em or y > ey  
dates.l = args.daysToEnd < duration and '-l' .. duration
id = table.concat(id, '-') .. (dates.l or '')
barargs.id = id
dates.id = id
end
else
barargs.collapsed = ''
barargs.id = ''
end  
return p._customBarStacked(barargs), dates
end  
function p._buildBars(args)
local = mw.getCurrentFrame
local updatePeriod = 86400 -- temporary implementation only supports daily updates  
local function getUnix(timestamp)
return lang:formatDate('U', timestamp)
end  
local rows, prevRow = {}, {}
for line in mw.text.gsplit(args.data, '\n') do
local i, barargs = 1, {}
-- parameter parsing, basic type/missing value handling
for parameter in mw.text.gsplit(line, ';') do
if parameter:find('^%s*%a') then
parameter = mw.text.split(parameter, '=')
parameter[1] = mw.text.trim(parameter[1])
if parameter[1]:find('^alttot') then
parameter[2] = tonumber(:callParserFunction('#expr', parameter[2]))
else
parameter[2] = mw.text.trim(parameter[2])
if parameter[1]:find('^firstright') then
parameter[2] = yesno(parameter[2])
elseif parameter[2] == '' then
parameter[2] = nil
end
end
barargs[parameter[1]] = parameter[2]
else
parameter = mw.text.trim(parameter)
if parameter ~= '' then
if i >= 2 and i <= 6 then
parameter = tonumber(:callParserFunction('#expr', parameter))
assert(parameter, 'data parameters 2 to 6 must not be formatted')
end
barargs[i] = parameter
end
i = i + 1
end
end  
local bValid, nDateDiff
-- get relevant date info based on previous row
if barargs[1] then
bValid, barargs.nDate = pcall(getUnix, barargs[1])
assert(bValid, 'invalid date "' .. barargs[1] .. '"')
if prevRow.nDate or prevRow.nEndDate then
nDateDiff = barargs.nDate - (prevRow.nDate or prevRow.nEndDate)
if nDateDiff > updatePeriod then
if nDateDiff == 2 * updatePeriod then
prevRow = {nDate=barargs.nDate-updatePeriod}
prevRow[1] = os.date('%Y-%m-%d', prevRow.nDate)
else
prevRow = {nStartDate=(prevRow.nDate or prevRow.nEndDate)+updatePeriod, nEndDate=barargs.nDate-updatePeriod}
end
rows[#rows+1] = prevRow
end
else
prevRow.nEndDate = barargs.nDate - updatePeriod
if prevRow.nStartDate == prevRow.nEndDate then
prevRow.nDate = prevRow.nEndDate
prevRow[1] = os.date('%Y-%m-%d', prevRow.nDate)
-- as nAltStartDate assumes a minimal multiday interval, it's possible for it to be greater if a true previous span is 1 day
elseif prevRow.nAltStartDate and prevRow.nAltStartDate >= prevRow.nEndDate then
error('a row in a consecutive intervals group is 1 day long and misses the date parameter')
end
end
else
if barargs.enddate then
bValid, barargs.nEndDate = pcall(getUnix, barargs.enddate)
assert(bValid, 'invalid enddate "' .. barargs.enddate .. '"')
end
if prevRow.nDate or prevRow.nEndDate then
barargs.nStartDate = (prevRow.nDate or prevRow.nEndDate) + updatePeriod
if barargs.nStartDate == barargs.nEndDate then
barargs.nDate = barargs.nEndDate
barargs[1] = os.date('%Y-%m-%d', barargs.nDate)
end
else
prevRow.nAltEndDate = (prevRow.nStartDate or prevRow.nAltStartDate) + updatePeriod
barargs.nAltStartDate = prevRow.nAltEndDate + updatePeriod
if barargs.nEndDate and barargs.nAltStartDate >= barargs.nEndDate then
error('a row in a consecutive intervals group is 1 day long and misses the date parameter')
end
end
end  
local function fillCols(col, change)
local data = args['' .. col .. 'data']
local changetype = args['changetype' .. col]
local value, num, prevnum  
if data == 'alttot1' then
num = barargs.alttot1 or barargs[4]
prevnum = prevRow.alttot1 or prevRow[4]
elseif data == 'alttot2' then
num = barargs.alttot2 or barargs[6]
prevnum = prevRow.alttot2 or prevRow[6]
elseif data then
num = barargs[data+1]
prevnum = prevRow[data+1]
end  
if data and num then -- nothing in column, source found, and data exists
value = changetype == 'o' and '' or lang:formatNum(num) -- set value to num if changetype isn't 'o'  
if not change and not barargs['firstright' .. col] then
if prevnum and prevnum ~= 0 then -- data on previous row
if num - prevnum ~= 0 then --data has changed since previous row
change = num-prevnum
if changetype == 'a' then -- change type is "absolute"
if change > 0 then
change = '+' .. lang:formatNum(change)
end
else -- change type is "percent", "only percent" or undefined
local percent = 100 * change / prevnum -- calculate percent
local rounding = math.abs(percent) >= 10 and '%.0f' or math.abs(percent) >= 1 and '%.1f' or '%.2f'
percent = tonumber(rounding:format(percent)) -- round to two sigfigs  
if percent > 0 then
change = '+' .. lang:formatNum(percent) .. '%'
elseif percent < 0 then
change = lang:formatNum(percent) .. '%'
else
change = '='
end
end
else -- data has not changed since previous row
change = '='
end
else -- no data on previous row
barargs['firstright' .. col] = true -- set to (n.a.)
end
end
end  
return value, change
end  
if not barargs[7] then
barargs[7], barargs[8] = fillCols(1, barargs[8])
end
if not barargs[9] then
barargs[9], barargs[10] = fillCols(2, barargs[10])
end  
rows[#rows+1] = barargs
prevRow = barargs
end  
-- calculate and pass repetitive (except daysToEnd) parameters to each row
local lastRow = rows[#rows]
local total = {lastRow[2] or 0, lastRow[3] or 0, [4]=lastRow[5] or 0}
total[3] = lastRow.alttot1 or lastRow[4] and lastRow[4] - total[1] - total[2] or 0
total[5] = lastRow.alttot2 or lastRow[6] and lastRow[6] - total[1] - total[2] or 0
local divisor = (total[1] + total[2] + total[3] + total[4] + total[5]) / (0.95 * args.barwidth)
local firstDate, lastDate = rows[1].nDate, lastRow.nDate or lastRow.nEndDate
local multiyear = os.date('%Y', firstDate) ~= os.date('%Y', lastDate - (args.nooverlap and args.duration * 86400 or 0))
if args.collapsible ~= false then
args.collapsible = (lastDate - firstDate) / 86400 >= args.duration
end  
local bars, dateList = {}, {}
for i, row in ipairs(rows) do -- build rows
row.divisor = divisor
row.numwidth = args.numwidth
row.rowheight = args.rowheight
row.collapsible = args.collapsible
row.duration = args.duration
row.nooverlap = args.nooverlap
row.daysToEnd = (lastDate - (row.nDate or row.nEndDate or row.nAltEndDate)) / 86400
row.multiyear = multiyear  
bars[#bars+1], dateList[#dateList+1] = p._row(row)
end  
return table.concat(bars), dateList
end  
function p._legend0(args)
return
'<span>' ..
'<span>' ..
'&nbsp;&nbsp;&nbsp;&nbsp;' .. '</span>' ..
'&nbsp;' .. (args[2] or '') .. '</span>'
end  
function p._chart(args)
for key, value in pairs(args) do
if ({float=1, barwidth=1, numwidth=1, changetype=1})[key:gsub('%d', '')] then
args[key] = value:lower
end
end  
local barargs = {}  
barargs.css = 'Module:Medical cases chart/styles.css'
barargs.float = args.float or ''  
args.barwidth = args.barwidth or 'medium'
local barwidth
if args.barwidth == 'thin' then
barwidth = 120
elseif args.barwidth == 'medium' then
barwidth = 280
elseif args.barwidth == 'wide' then
barwidth = 400
elseif args.barwidth == 'auto' then
bar
else
error('unrecognized barwidth')
end  
local function _numwidth(i)
local nw = args.numwidth:sub(i, i)
if nw == 'n' then
return 0
elseif nw == 't' then
return 40
elseif nw == 'm' or nw == 'd' then
return 55
elseif nw == 'w' then
return 70
elseif nw == 'x' then
return 85
else
error('unrecognized numwidth[' .. i .. ']')
end
end  
args.numwidth = args.numwidth or 'mm'
local numwidth = _numwidth(1) + 10 + _numwidth(2)
local right1width = numwidth
if #args.numwidth == 4 then
numwidth = numwidth + _numwidth(3) + _numwidth(4)
if args.numwidth:sub(3, 3) == 'n' then
numwidth = numwidth + 6
else
numwidth = numwidth + 10
end
if not args.right2 then