Modulo:Graph: differenze tra le versioni
Contenuto cancellato Contenuto aggiunto
comincio a renderlo più modulare |
Nessun oggetto della modifica |
||
Riga 20:
local graph = {
name = args[cfg.localization.name] or 'grafico a torta',
width = args[cfg.localization['width']] or 200,▼
height = args[cfg.localization['height']] or 200,▼
data = {
{
Line 68 ⟶ 66:
return graph
end
local function legend_item(color, text)
local item = mw.html.create('p'):cssText('margin:0px;font-size:100%;text-align:left')
item:tag('span'):cssText(string.format('border:none;background-color:%s;color:%s;', color, color)):wikitext("██")
item:wikitext(string.format(" %s", text))
return item
end
local function build_legend(data, args)
local legend = mw.html.create('div'):addClass('thumbcaption')
legend:wikitext(args[cfg.localization.caption])
for _,datum in ipairs(data) do
legend:node(legend_item(datum.color, mw.ustring.format('%s %s%%', datum.label, tostring(datum.x))))
end
return legend
end
function p.pie_chart(frame)
Line 84 ⟶ 100:
data[index] = {
x = value,
label = args[label_string .. index_s] or
color = args[color_string .. index_s] or cfg.default_colors[index] or cfg.default_color
}
Line 91 ⟶ 107:
break
end
end
-- Se è definito 'other' assumo che sia calcolato su base %, calcolo il suo valore e l'aggiungo alla tabella dati
if args[cfg.localization.other] then
index = index + 1
local total = 0
for _,datum in ipairs(data) do
total = total + datum.x
end
local other_value = min(0, 100 - total)
data[index] = {
x = other_value,
color = args[cfg.localization.color_other] or cfg.default_colors[index] or cfg.default_color
}
end
if #data == 0 then
Line 101 ⟶ 131:
return frame:extensionTag('syntaxhighlight', mw.text.jsonEncode(graph, mw.text.JSON_PRETTY), {lang='json'})
end
local legend = build_legend(data, args)
return frame:extensionTag( 'graph', mw.text.jsonEncode(graph) )▼
local html = mw.html.create('div')
:addClass(string.format('thumb t%s', args[cfg.localization.thumb] or 'right'))
:tag('div')
:addClass('thumbinner')
:cssText('width:202px')
:tag('div')
:cssText("background-color:white;margin:auto;position:relative;width:200px;height:200px;overflow:hidden")
:done()
:node(legend)
return html
end
|