Modulo:Grafico epidemia: differenze tra le versioni
Contenuto cancellato Contenuto aggiunto
m Per riga senza data |
Aggiunta modifiche per inserimento solo 4 dati |
||
Riga 3:
local p = {}
local function is(v)
return (v or '') ~= ''
end
function p._barColors(n)
Riga 25 ⟶ 29:
function p._buildBars(args)
local lines = mw.text.split(
local
local lang = mw.getContentLanguage()
local bars, rows, months, prevRow, maxparam = {}, {}, {}, '', 1
for k, line in pairs(lines) do
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
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
bars[i] = p._row(rows[i])
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
function p._row(args)
local barargs = {}
local rowDate = args.prevDate or ''
frame = mw.getCurrentFrame()
lang=mw.language.getContentLanguage()
Riga 79 ⟶ 172:
if pcall(function () lang:formatDate('', args[1]) end) then
barargs[1] = args[1]
rowDate = args[1]
else
barargs[1] = '<strong class="error">Error: Invalid time.</strong>'
Riga 88 ⟶ 182:
local two = args[2] or 0
barargs[2] = frame:callParserFunction('#expr', two)
local three = args[3] or 0
barargs[3] = frame:callParserFunction('#expr', three)
Riga 116 ⟶ 210:
barargs[7] = ''
end
local function changeArg(firstright, valuecol, changecol)
if yesno(args['firstright' .. firstright]) == true then
change = '(n.a.)'
elseif yesno(args['firstright' .. firstright]) == false or not is(args['firstright' .. firstright]) then
change = '(=)'
else
change = is(args[changecol]) and '(' .. args[changecol] .. ')' or ''
change = string.gsub(change,"%.",",")
end
end
return change
end
barargs[8] = changeArg(1,7,8)
if args[9] then
barargs[9] = string.gsub(args[9],",",".") or ''
Riga 144 ⟶ 233:
barargs[9] = ''
end
barargs[10] = changeArg(2,9,10)
barargs.divisor = args.divisor or 1
Riga 227 ⟶ 295:
end
barargs[2] =
'<span class="nowrap">' ..
|