Modul:Népességdiagram
Megjelenés
Népességdiagram[mi ez?] • [dokumentáció: mutat, ] • [tesztek: létrehozás]
Dokumentáció
[szerkesztés]local getArgs = require('Modul:Arguments').getArgs
local p = {}
local property = 'P1082'
local json = {
version = 2,
width = 500, height = 200,
data = {
{
name = "table",
values = {}
}
},
scales = {
{
name = "x",
type = "ordinal",
range = "width",
domain = { data = "table", field= "x" }
},
{
name = "y",
range = "height",
nice = true,
domain = { data = "table", field = "y" }
}
},
axes = {
{ type = "x", scale = "x" },
{ type = "y", scale = "y" }
},
marks = {
{
type = "rect",
from = { data = "table" },
properties = {
enter = {
x = { scale = "x", field = "x" },
width = { scale = "x", band = true, offset = -1},
y = { scale = "y", field = "y" },
y2 = { scale = "y", value = 0 }
},
update = {
fill = { value = "steelblue" }
},
hover = {
fill = { value = "red" }
}
}
},
{
type = "text",
from = { data = "table" },
properties = {
enter = {
x = { scale = "x", field = "x" },
dx = { scale = "x", band = true, mult = 0.5 },
y = { scale = "y", field = "y" },
fill = { value = "#000" },
align = { value = "center" },
baseline = { value = "top" },
dy = { value = -13 },
text = { field = "y_form" }
}
}
}
}
}
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, { x = d, y = n, y_form = n_form })
end
return tostring(mw.html.create("div")
:css("display", "inline-block") -- style hotfix
:css("max-width", "100%")
:css("overflow-x", "auto") -- too wide on mobile
:css("float", float) -- optional
:wikitext(mw.getCurrentFrame():extensionTag("graph", mw.text.jsonEncode(json)))
)
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