local p = {}
    local mWikidata = require('Modulo:Wikidata')
    local mChart = require('Modulo:Chart')
    local getArgs = require('Module:Arguments').getArgs

local function read_array(raw_data)
    if not(raw_data) then return {} end
    local list = mw.text.split(raw_data, ",")
    local data = {}
    for _,s in ipairs(list) do
        data[#data+1] = s
    end
    return data
end


function p.demograph(frame)
    local args = getArgs(frame)
    local populations = mWikidata._getProperty({'P1082', from=args[1], rank='normal'}, true)
    local years = read_array(mWikidata._getQualifier({'P1082', 'P585', from=args[1], rank='normal'}, true) )
    --if #populations ~= #years then return "Dati da wikidata non congruenti" end
    if true then
        local temp = {}
        for i = 1, #populations do
            temp[i] = (years[i] or '') .. ': ' .. tostring(populations[i]) .. '<br/>'
        end
        return table.concat(temp)
    end
    if true then return mw.text.jsonEncode(populations, mw.text.JSON_PRETTY)  end
    for i = 1, math.floor(#populations/2) do
        local j=#populations-i+1
        years[i], years[j] = years[j], years[i]
        populations[i], populations[j] = populations[j], populations[i]
    end
    
    local graph_args = {
        ygrid = true,
        xtitle = 'anni',
        ytitle = 'popolazione',
        y = { populations },
        x = { years },
        graph_type = 'rect',
        seriesTitles = {'Popolazione'},
        colors = {'#4682B4'}
    }
    local graph_json = mChart.chart_json(graph_args)
    --if true then return  graph_json end
    return  frame:extensionTag('graph', graph_json)
end

    return p