local p = {}
local function is(v)
return (v or '') ~= ''
end
function p._barColors(n)
if n == 1 then
return 'Black' --morti
elseif n == 2 then
return 'Lightgreen' --guarigioni
elseif n == 3 then
return 'Red' --casi o altlbl1
elseif n == 4 then
return 'Gold' --altlbl2
elseif n == 5 then
return 'OrangeRed' --altlbl3
end
return nil
end
function p._legend0(args)
return '<span style="margin:0px; font-size:90%;">' .. '<span style="' .. 'border:' .. (args.border or 'none') .. '; background-color:' .. (args[1] or 'none') .. '; color:' .. (args[1] or 'none') .. ';">' .. ' ' .. '</span>' .. ' ' .. (args[2] or '') .. '</span>'
end
function p._buildBars(args)
local lines = mw.text.split(args.data, '\n')
local frame = mw.getCurrentFrame()
local lang = mw.getContentLanguage()
local bars, rows, months, prevRow, maxparam = {}, {}, {}, '', 1
for k, line in pairs(lines) do
local barargs, i = {}, 1
for parameter in mw.text.gsplit(line, ';') do
parameter = mw.text.trim(parameter)
if string.find(parameter, '^%a') then
parameter = mw.text.split(parameter, '=')
if parameter[1] == 'alttot1' or parameter[1] == 'alttot2' then
parameter[2] = tonumber(frame:callParserFunction('#expr', parameter[2]))
if is(parameter[2]) then
maxparam = math.max(maxparam, parameter[2])
end
end
barargs[parameter[1]] = parameter[2]
else
if is(parameter) then
if i >= 2 and i <= 6 then
parameter = tonumber(frame:callParserFunction('#expr', frame:callParserFunction('formatnum',parameter,'R')))
maxparam = math.max(maxparam, parameter or 1)
end
barargs[i] = parameter
if i == 7 or i == 9 then
parameter = tonumber(mw.ustring.match(frame:callParserFunction('formatnum',parameter,'R'), '^%d*'))
maxparam = math.max(maxparam, parameter or 1)
end
end
i = i + 1
end
end
function format_thousand(v)
local s = string.format("%d", math.floor(v))
local pos = string.len(s) % 3
if pos == 0 then pos = 3 end
return string.sub(s, 1, pos)
.. string.gsub(string.sub(s, pos+1), "(...)", ".%1")
end
local function fillCols(col, change)
local data
local changetype
local value, num, prevnum
if col == 1 then
data = args.right1data or 3
changetype = args.changetype1 or 'p'
else
data = args.right2data or 1
changetype = args.changetype2 or 'p'
end
num = tonumber(barargs[tonumber(data) + 1])
prevnum = tonumber(prevRow[tonumber(data) + 1])
if num then -- nothing in column, source found, and data exists
value = num
if not change and yesno(barargs['firstright' .. col] ~= true) 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(mw.ustring.format(rounding, 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
value = format_thousand(value)
end
return value, change
end
if not is(barargs[7]) then
barargs[7], barargs[8] = fillCols(1, barargs[8])
end
if not is(barargs[9]) then
barargs[9], barargs[10] = fillCols(2, barargs[10])
end
if is(barargs[1]) then
local e,f = pcall(function () return mw.getContentLanguage('it'):formatDate('M',barargs[1]) end)
if e then
months[#months+1] = f
end
end
barargs.prevDate = prevRow[1]
rows[#rows + 1] = barargs
prevRow = barargs
end
for i=1,#rows do -- build rows
rows[i].divisor = tonumber(args.divisor) and tonumber(args.divisor) or maxparam / (0.95 * args.barwidth)
rows[i].numwidth = args.numwidth
rows[i].collapsible = args.collapsible
rows[i].rowsToEnd = #rows - i
rows[i].rowheight = args.rowheight
rows[i].duration = args.duration
end
monthsOut = {}
for i=1,#months do -- deduplicate months
if months[i] ~= monthsOut[#monthsOut] then
monthsOut[#monthsOut+1] = months[i]
end
end
return table.concat(bars), monthsOut
end
|