Modulo:Graph

Questo è un modulo scritto in Lua. Le istruzioni che seguono sono contenute nella sottopagina Modulo:Graph/man (modifica · cronologia)
Sandbox: Modulo:Graph/sandbox (modifica · cronologia) · Sottopagine: lista · Test: Modulo:Graph/test (modifica · cronologia · Esegui)
Modulo che implementa i template {{Grafico}} e {{Grafico a torta}}.
Ha una sottopagina di configurazione: Modulo:Graph/Configurazione.
local p = {}
local cfg = mw.loadData( 'Modulo:Sandbox/moroboshi/Chart/Configurazione' );
local function dump(t, ...)
local args = {...}
for _, s in ipairs(args) do
table.insert(t, s)
end
end
function p.pie_chart(frame)
args = require('Modulo:Arguments').getArgs(frame)
local value_string = cfg.localization.value
local label_string = cfg.localization.label
local color_string = cfg.localization.color
local index = 1
local data = {}
local colors = {}
local labels = {}
while true do
local index_s = tostring(index)
local value = tonumber(args[value_string .. index_s])
if value then
data[index] = {
x = args[label_string .. index_s] or args[value_string .. index_s],
y = value,
color = args[color_string .. index_s] or cfg.default_colors[index] or cfg.default_color
}
index = index + 1
else
break
end
end
if #data == 0 then
return ''
end
local graph = {
name = args[cfg.localization.name] or 'grafico a torta',
width = args[cfg.localization['width']] or 400,
height = args[cfg.localization['height']] or 400,
data = {
{
name = "table",
values = data,
transform = { { type = "pie", value = "data.y" } }
}
},
marks = {
{
type = "arc",
from = { data = "table"},
properties = {
enter = {
x = { field = "data.y", group = "width", mult = 0.5 },
y = { field = "data.y", group = "height", mult = 0.5 },
startAngle = {field = "startAngle"},
endAngle = {field = "endAngle"},
innerRadius = {value = 0},
outerRadius = {value = tonumber(args[cfg.localization.radius]) or 150 },
stroke = {value = "#fff"},
},
update = { fill = { field = "data.color"} },
hover = { fill = {value = "pink"} }
},
},
{
type = "text",
from = { data = "table"},
properties = {
enter = {
x = { field = "data.y", group = "width", mult = 0.5 },
y = { field = "data.y", group = "height", mult = 0.5 },
radius = { value = tonumber(args[cfg.localization.radius]) or 100, offset = 8 },
theta = { field = "midAngle"},
fill = { value = "#000" },
align = { value = "center" },
baseline = { value = "middle" },
text = { field = "data.x"}
}
}
}
}
}
local json = mw.text.jsonEncode(graph, mw.text.JSON_PRETTY)
return frame:extensionTag("syntaxhighlight", json, {lang="json"}) .. frame:extensionTag( 'graph', json )
end
return p