Modulo:Graph: differenze tra le versioni

Contenuto cancellato Contenuto aggiunto
Moroboshi (discussione | contributi)
+colori+etichette
Moroboshi (discussione | contributi)
comincio a renderlo più modulare
Riga 10:
end
 
-- ===============================================================================
function p.pie_chart(frame)
-- Costruisce una struttura dati da codificare in json per realizzare un grafico
args = require('Modulo:Arguments').getArgs(frame)
-- a torta visualizzabile utilizzando il tag graph
local value_string = cfg.localization.value
-- https://www.mediawiki.org/wiki/Extension:Graph
local label_string = cfg.localization.label
-- i nomi dei parametri sono localizzati in base a chart/Config
local color_string = cfg.localization.color
--
local index = 1
-- ===============================================================================
local data = {}
function build_pie_chart_json(data, args)
local colors = {}
local labelsgraph = {}
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 400200,
height = args[cfg.localization['height']] or 400200,
data = {
{
name = "table",
values = data,
transform = { { type = "pie", value = "data.yx" } }
}
},
Line 53 ⟶ 35:
properties = {
enter = {
x = { field = "data.yx", group = "width", mult = 0.5 },
y = { field = "data.yx", group = "height", mult = 0.5 },
startAngle = {field = "startAngle"},
endAngle = {field = "endAngle"},
innerRadius = {value = 0},
outerRadius = {value = tonumber(args[cfg.localization.radius]) or 150cfg.radius_default },
stroke = {value = "#fff"},
},
update = { fill = { field = "data.color"} },
hover = { fill = {value = "pink"} }
},
},
}
{}
if args[cfg.localization.show_label] then
graph.marks[#graph.marks+1] = {type = "text",
from = { data = "table"},
properties = {
enter = {
x = { field = "data.yx", group = "width", mult = 0.5 },
y = { field = "data.yx", group = "height", mult = 0.5 },
radius = { value = tonumber(args[cfg.localization.radius]) or 100cfg.radius_default, offset = 8 },
theta = { field = "midAngle"},
fill = { value = "#000" },
align = { value = "center" },
baseline = { value = "middle" },
text = { field = "data.xlabel"}
}
}
}
}end
}return graph
end
local json = mw.text.jsonEncode(graph, mw.text.JSON_PRETTY)
 
function p.pie_chart(frame)
return frame:extensionTag("syntaxhighlight", json, {lang="json"}) .. frame:extensionTag( 'graph', json )
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 graphlabels = {}
while true do
local index_s = tostring(index)
local value = tonumber(args[value_string .. index_s])
if value then
data[index] = {
yx = value,
xlabel = args[label_string .. index_s] or args[value_string .. index_s],
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 = build_pie_chart_json(data, args)
local json
-- se il parametro debug_json è stato valorizzato ritorna il codice json generato invece di generare il grafico
if args.debug_json then
local json = return frame:extensionTag('syntaxhighlight', mw.text.jsonEncode(graph, mw.text.JSON_PRETTY), {lang='json'})
end
return frame:extensionTag( 'graph', mw.text.jsonEncode(graph) )
end