Modul:Homokozó/Bean49/h001/Modul:Népességdiagram tmp1
Megjelenés
Homokozó/Bean49/h001/Modul:Népességdiagram tmp1[mi ez?] • [dokumentáció: mutat, ] • [tesztek: létrehozás]
local getArgs = require('Modul:Arguments').getArgs
local p = {}
local property = 'P1082'
local json = {
['$schema'] = "https://vega.github.io/schema/vega/v5.json",
description = "Népességdiagram demó.",
width = 400,
height = 200,
padding = 5,
data = {
{
name = "table",
values = {}
}
}
}
local function getStatements(id)
local entity = mw.wikibase.getEntity(id)
if not entity then
return nil
end
if not entity.claims or not entity.claims[property] then
return nil
end
local statements = entity.claims[property]
return statements
end
local function prepareStatements(statements)
local numbers_raw = {}
local ranks = { normal = 1, preferred = 2 }
local function processStatement(i, statement)
local snak = statement.mainsnak
if not snak or snak.snaktype ~= 'value' then
statements[i] = nil
return;
end
local rank = ranks[statement.rank]
if not rank then
return
end
local n = tonumber(snak.datavalue.value.amount)
local d = nil
if statement.qualifiers and statement.qualifiers.P585 then -- dátum
for _, snak in ipairs(statement.qualifiers.P585) do
if snak.snaktype == 'value' then
d = require('Modul:Time').newFromWikidataValue(snak.datavalue.value)
break
end
end
end
if d and d.year and (not numbers_raw[d.year] or numbers_raw[d.year].rank <= rank) then
statements[i] = { d.year, n }
numbers_raw[d.year] = { number = n, rank = rank }
else
statements[i] = nil
end
end
for n, statement in pairs(statements) do
processStatement(n, statement)
end
local dates = {}
for d, v in pairs(statements) do
table.insert(dates, v[1])
end
if #dates < 1 then
return nil
end
table.sort(dates)
local numbers = {}
if #dates <= 10 then
for _, v in ipairs(dates) do
table.insert(numbers, { tostring(v), numbers_raw[v].number } )
end
else
local n = #dates - 1
for i = 0, 9 do
local j = math.floor(i * n / 9 + 0.5) + 1
local year = dates[j]
table.insert(numbers, {tostring(year), numbers_raw[year].number})
end
if numbers_raw[dates[n + 1]].number >= 1000000 then
--json.marks[2].properties.enter.angle = { value = -45 }
end
end
return numbers
end
local function drawGraph(data, float)
local lang = mw.language.getContentLanguage()
for _, v in pairs(data) do
local d, n = v[1], v[2]
local n_form = tostring(n)
if n >= 10000 then
n_form = lang:formatNum(n)
end
table.insert(json.data[1].values, { category = d, amount = n })
end
return '<div class="nepessegdiagram" style="width:500px; height:200px; border:1px solid lightgray; visibility:hidden; margin-left:auto; margin-right:auto">\n' ..
mw.getCurrentFrame():callParserFunction('#tag', 'nowiki', mw.text.jsonEncode(json)) .. '\n</div>\n'
end
function p.main(frame)
local args = {}
if type(frame) ~= 'table' then
args = { id = frame }
else
args = getArgs(frame)
end
if args.property then
property = args.property
end
local statements = getStatements(args.id)
if type(statements) ~= 'table' then
return nil
end
statements = prepareStatements(statements)
if type(statements) ~= 'table' or #statements < 2 then
return nil
end
return drawGraph(statements, args.float)
end
return p