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 values = {}
    local colors = {}
    local labels = {}
    while true do
        local index_s = tostring(index)
        local value = tonumber(args[value_string .. index_s])
        if  value then
            values[index] = value
            colors[index] = args[color_string .. index_s] or cfg.default_colors[index] or cfg.default_color
            labels[index] = args[label_string .. index_s] or 'xxx'
            index = index + 1
        else
            break
        end
    end
    if #values == 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 = values,
                transform = { { type = "pie", value = "data" } }
            }
        },
        marks = {
            {
                type = "arc",
                from = { data = "table"},
                properties = {
                    enter = {
                        x = { group = "width", mult = 0.5 },
                        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 = {value = "#ccc"} },
                    hover = { fill = {value = "pink"} }
                },
             },
            {
                type = "text",
                from = { data = "table"},
                properties = {  
                    enter = {
                        x = { group = "width", mult = 0.5 },
                        y = { group = "height", mult = 0.5 },
                        radius = { value = tonumber(args[cfg.localization.radius])  or 150 , offset = 8 },
                        theta = { field = "midAngle"},
                        fill = { value = "#000" },
                        align = { value = "center" },
                        baseline = { value = "middle" },
                        text = { field = "data"}
                    }
                }
            }
        }
    }
    local json = mw.text.jsonEncode(graph, mw.text.JSON_PRETTY)

    return frame:extensionTag("syntaxhighlight", json, {lang="json"}) ..  frame:extensionTag( 'graph', json )
end

return p