×
新規記事を作成
ここにページのタイトルを記入:
We currently have 2 articles on 政治ボールwiki 日本語版. Type your article name above or click on one of the titles below and start writing!



    政治ボールwiki 日本語版

    このモジュールについての説明文ページを モジュール:Pie chart/doc に作成できます

    local p = {}
    
    function p.render(frame)
        local args = frame.args
        local caption = args["caption"] or ""
        local values = {}
        local labels = {}
        
        for i = 1, 10 do
            if args["label" .. i] and args["value" .. i] then
                table.insert(labels, args["label" .. i])
                table.insert(values, tonumber(args["value" .. i]) or 0)
            end
        end
        
        local total = 0
        for _, v in ipairs(values) do
            total = total + v
        end
        
        if total == 0 then
            return "<div style='color: red;'>Error: No valid values provided.</div>"
        end
    
        local data = {}
        for i, v in ipairs(values) do
            table.insert(data, string.format('{ "category": "%s", "value": %d }', labels[i], v))
        end
        
        local graph = string.format([[
        <graph>
        {
          "type": "pie",
          "data": {
            "values": [ %s ]
          },
          "encode": {
            "tooltip": { "field": "category", "type": "nominal" }
          }
        }
        </graph>
        ]], table.concat(data, ", "))
    
        return "<div style='text-align: center;'>" ..
            (caption ~= "" and "<b>" .. caption .. "</b><br>" or "") ..
            graph .. "</div>"
    end
    
    return p