Modulo:Graph

Versione del 3 lug 2015 alle 09:34 di Moroboshi (discussione | contributi) (Nuova pagina: local p = {} local cfg = mw.loadData( 'Modulo:Sandbox/moroboshi/Chart/Configurazione' ); local function dump(t, ...) local args = {...} for _, s in ipairs(arg...)
(diff) ← Versione meno recente | Versione attuale (diff) | Versione più recente → (diff)

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 = tointeger(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
    local graph = {}
    graph.name = args[cfg.localization.name] or 'grafico a torta'
    grap.width = args[cfg.localization['width']] or 400
    graph.height = args[cfg.localization['height']] or 400
    grahp.data = {}
    graph.data[1] = { name = "table"}
    graph.data[1].values = values
    graph.data[1].transform = { { type = "pie", value = "data" } } 
    graph.marks[1] = {}
    graph.marks[1].type = "arc"
    graph.marks[1].from = { data = "table"}
    graph.marks[1].properties = {}
    graph.marks[1].properties.enter = {} 
    graph.marks[1].properties.enter.x = { group = "width", mult = 0.5 }
    graph.marks[1].properties.enter.y = { group = "width", mult = 0.5 }
    graph.marks[1].properties.enter.startAngle = {field = "startAngle"}
    graph.marks[1].properties.enter.endAngle = {field = "endAngle"}
    graph.marks[1].properties.enter.innerRadius = {value = 0}
    graph.marks[1].properties.enter.outerRadius = {value = tonumber(args[cfg.localization.radius]) or 200 }
    graph.marks[1].properties.enter.outerRadius = {value = "#000"}
    graph.marks[1].properties.update = { fill = {value = "#ccc"} }
    graph.marks[1].properties.hover = { fill = {value = "pink"} }
    graph.marks[2] = { }
    graph.marks[2].type = "text"
    graph.marks[2].from = { data = "table"}
    graph.marks[2].properties = {}
    graph.marks[2].properties.enter = {
        x = { group = "width", mult = 0.5 },
        y = { group = "width", mult = 0.5 },
        radius = { scale = {value = 200, 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