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( args.data, '\n' )
local iframe = 1mw.getCurrentFrame()
local lang = mw.getContentLanguage()
if (not args.divisor) or (args.divisor == '') then
 
local total1, total2 = 0, 0
local bars, rows, months, prevRow, maxparam = {}, {}, {}, '', 1
for parameter in mw.text.gsplit( lines[#lines], ';' ) do
for k, line in pairs(lines) do
if not string.find( parameter, '^ *%a' ) then
iflocal barargs, i == 7{}, then1
for parameter in mw.text.gsplit(line, ';') do
total1 = tonumber( (string.gsub( parameter, '%D', '' )) )
parameter = mw.text.trim(parameter)
if not total1 then
if string.find(parameter, '^%a') then
total1 = 0
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
elseif i == 9 then
total2 = tonumber( (string.gsub( parameter, '%D', '' )) )
if not total2 then
total2 = 0
end
break
end
i = i + 1
end
end
args.divisor = math.max( total1, total2 ) / ( 0.95 * args.barwidth )
end
local bars = {}
local barargs
for k, line in pairs( lines ) do
barargs, i = {}, 1
for parameter in mw.text.gsplit( line, ';' ) do
if string.find( parameter, '^ *%a' ) then
parameter = mw.text.split( parameter, '=' )
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
barargs.divisor = args.divisor
function format_thousand(v)
barargs.numwidth = args.numwidth
local s = string.format("%d", math.floor(v))
barargs.collapsible = args.collapsible
local pos = string.len(s) % 3
bars[#bars+1] = p._row(barargs)
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
 
return table.concat(bars)
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.firstright1) == true then
barargs[8]local change = '(n.a.)'
if yesno(args['firstright' .. firstright]) == true then
elseif (yesno(args.firstright1) == false) or (args.firstright1 == '') or (not args.firstright1) then
change = '(n.a.)'
if args[1] then
elseif yesno(args['firstright' .. firstright]) == false or not is(args['firstright' .. firstright]) then
if args[8] then
barargs[8]if =not 'is('args[1]) ..and string.gsubis(args[8valuecol],"%.",",") .. ')'then
change = '(=)'
else
change = is(args[changecol]) and '(' .. args[changecol] .. ')' or ''
barargs[8] = ''
change = string.gsub(change,"%.",",")
end
else
if args[7] then
barargs[8] = '(=)'
elseif args[8] then
barargs[8] = '(' .. string.gsub(args[8],"%.",",") .. ')'
else
barargs[8] = ''
end
end
 
else
return change
barargs[8] = ''
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)
if yesno(args.firstright2) == true then
barargs[10] = '(n.a.)'
elseif (yesno(args.firstright2) == false) or (args.firstright2 == '') or (not args.firstright2) then
if args[1] then
if args[10] then
barargs[10] = '(' .. string.gsub(args[10],"%.",",") .. ')'
else
barargs[10] = ''
end
else
if args[9] then
barargs[10] = '(=)'
elseif args[10] then
barargs[10] = '(' .. string.gsub(args[10],"%.",",") .. ')'
else
barargs[10] = ''
end
end
else
barargs[10] = ''
end
barargs.divisor = args.divisor or 1
Riga 227 ⟶ 295:
end
barargs[2] =
'<span class="nowrap">' ..