Module:Graph:Population history

Από τη Βικιπαίδεια, την ελεύθερη εγκυκλοπαίδεια
Μετάβαση στην πλοήγηση Πήδηση στην αναζήτηση
Τεκμηρίωση[δημιουργία]
local getArgs = require('Module:Arguments').getArgs
local args = {}
local p = {}
local property = 'P1082'

local json = {
	version = 2,
	width = 600, height = 300,
	data = {
		{
			name = "table",
			values = {}
		}
	},
	scales = {
		{
			name = "x",
			type = "linear",
			domain = { data = "table", field= "x" },
			range = "width",
			zero = false,
			nice = true
		},
		{
			name = "y",
			range = "height",
			nice = true,
			domain = { data = "table", field = "y" }
		}
	},
	axes = {
		{ type = "x", scale = "x", tickcount = 20, format = "d", grid = true },
		{ type = "y", scale = "y", format = "2d", grid = true }
	},
	marks = {
		{
			type = "line",
			from = { data = "table" },
			properties = {
				enter = {
					x = { scale = "x", field = "x" },
					y = { scale = "y", field = "y" },
					strokeWidth = { value = 5 },
					stroke = { value = "steelblue" }
				}
			}
		}
	}
}

if args["labels"] then
	table.insert(json["marks"],
		{
			type = "text",
			from = { data = "table" },
			properties = {
				enter = {
					x = { scale = "x", field = "x" },
					y = { scale = "y", field = "y" },
					fill = { value = "#000" },
					align = { value = "left" },
					baseline = { value = "top" },
					dy = { value = -13 },
					text = { field = "y" }
				}
			}
		}
	)
end

local function getStatements(id)
	local entity = mw.wikibase.getEntity()
	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 function processStatement(i, statement)
		local snak = statement.mainsnak
		if not snak or snak.snaktype ~= 'value' then
			statements[i] = nil
			return;
		end
		local n = tonumber(snak.datavalue.value.amount)
		local d = nil
		if statement.qualifiers and statement.qualifiers.P585 then  -- dátum
			d = require('Module:Time').newFromWikidataValue(statement.qualifiers.P585[1].datavalue.value)
		end
		if d and d.year then
			statements[i] = { d.year, n }
			numbers_raw[d.year] = n
			return;
		else
			statements[i] = nil
			return;
		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 = {}
	for n, v in pairs(dates) do
		table.insert(numbers, { v, numbers_raw[v] } )
	end
	return numbers
end

local function drawGraph(data)
	for _, v in pairs(data) do
		local d, n = v[1], v[2]
		table.insert(json.data[1].values, { x = d, y = n })
	end
	return tostring(mw.html.create("div"):css("display", "inline-block"):wikitext(mw.getCurrentFrame():extensionTag("graph", mw.text.jsonEncode(json))))
end

function p.main(frame)
	if type(frame) ~= 'table' then
		args = { id = frame }
	else
		args = getArgs(frame)
	end
	local statements = getStatements(args.id)
	if type(statements) ~= 'table' then
		return ''
	end
	statements = prepareStatements(statements)
	if type(statements) ~= 'table' then
		return ''
	end
	return drawGraph(statements)
end

return p