require("Module:No globals")
local getArgs = require('Module:Arguments').getArgs
local p = {}
-- Converte una stringa di valori numerici separati da "," in una lista
-- Eventuali valori non numerici sono ignorati
local function read_array(base_name, list_name)
local array = {}
if args[list_name] then
array = mw.text.split(string.gsub(args[list_name], "%s", ""), ",")
end
-- se l'array ha lunghezza nulla uso il vecchio metodo di inserimento
if #array == 0 then
for i= 1,20 do
local number = tonumber(args[base_name .. tonumber(i)])
if number then
array[#array+1] = number
end
end
end
return array
end
function p._demografia(args)
local graph = {
version = 2,
width = 450,
height = 250,
padding = "auto",
data = {
{
name = "table",
transform = {
{ type = "formula", field = "population", expr = "round(datum.population)" },
{ type = "sort", by = "year" }
},
values = { }
},
{
name = "table2",
transform = {
{ type = "formula", field = "population", expr = "round(datum.population)" },
{ type = "sort", by = "year" }
},
values = { }
}
},
scales = {
{
name = "x",
type = "linear",
range = "width",
zero = false,
___domain = { fields = { {data =" table", field ="year"}, { data ="table2", field ="year"} } }
},
{
name = "y",
type = "linear",
range = "height",
nice = true,
___domain = { fields ={ {data = "table", field = "population"}, {data = "table2", field ="population"} } }
}
},
axes = {
{ type = "x", scale = "x", format = "d", title = "Anno", grid = true, },
{ type = "y", scale = "y", title = "Abitanti", grid = true, layer ="back" }
},
marks = {
{
type = "line",
from = {data = "table" },
properties = {
enter = {
interpolate = { value = "linear"},
x ={ scale = "x", field = "year"},
y ={ scale = "y", field = "population"},
stroke ={ value = "#fc8"},
strokeWidth = { value = 3}
}
}
},
{
type = "symbol",
from = { data = "table2" },
properties ={
enter ={
x ={ scale = "x", field = "year"},
y ={ scale = "y", field = "population"},
stroke ={ value = "#f80"},
fill ={ value = "#fff"},
size ={ value = 12}
}
}
},
{
type = "symbol",
from = { data = "table"},
properties = {
enter = {
x = { scale = "x", field = "year"},
y = { scale = "y", field = "population"},
stroke = { value ="#800"},
fill = { value ="#fff"},
size = { value =30}
}
}
},
{
type = "text",
from = {data = "table"},
properties = {
enter = {
x = { scale ="x", field = "year"},
y = { scale = "y", field = "population", offset = -8},
align = { value = "center" },
fill = { value = "#000"},
text = { field = "population" }
}
}
},
{
type = "text",
from = {
data = "table",
transform = {
{
type = "aggregate",
summarize = { year = {"min","max"} }
}
}
},
properties ={
enter ={
x = { signal = "width", mult = 0.5},
y = { value = -10},
text ={
template = "Censimenti dal \u007b{datum.min_year}\u007d al \u007b{datum.max_year}\u007d {{#if:{{{b1|}}}|e dati annuali}}"
},
fill = {value = "black"},
fontSize ={value = 16},
align ={value = "center"},
fontWeight = { value = "bold"}
}
}
}
}
}
local raw_data = read_array(args)
if #raw_data == 0 then return '' end
-- generazione del grafico
local titolo = args.titolo or args[1] or "Abitanti censiti<br />"
local populations = read_array("p", "popolazione")
local years = read_array("a", "anni")
local populations2 = read_array("pb", "popolazione2")
local years2 = read_array("b", "anni2")
--TODO error checking
local values1 = { }
for i =1,#populations do
values[i] = {year = years[i], population = populations[i] }
end
graph['data'][1]['values'] = values1
local values2 = { }
for i =1,#populations2 do
values[i] = {year = years2[i], population = populations2[i] }
end
graph['data'][2]['values'] = values2
local width = tonumber(args.dimx)
if width then graph['width'] = width end
local heigth = tonumber(args.dimy)
if height then graph['height'] = height end
return titolo .. mw.getCurrentFrame():extensionTag('graph', mw.text.jsonEncode(graph))
end
function p.demografia(frame)
local args = getArgs(frame)
return p._demografia(args)
end
return p