Modul:Diagramă populație

De la Wikipedia, enciclopedia liberă
Jump to navigation Jump to search

Documentația acestui modul poate fi creată la Modul:Diagramă populație/doc

local getArgs = require('Modul:Arguments').getArgs
local p = {}
local property = 'P1082'

local subpageJson = {
  version = 2,		
  width = 500,
  height = 300,
  padding = {top = 10, left = 60, bottom = 30, right = 10},
  data = {
    {
      name = "table",
      url = "wikiraw:///",
      format = {type = "csv", parse = {populatie = "number"} }
    }
  },
  scales = {
    {
      name = "x",
      type = "ordinal",
      range = "width",
      domain = {data = "table", field = "an"}
    },
    {
      name = "y",
      type = "linear",
      range = "height",
      domain = {data = "table", field = "populatie"},
      nice = true
    }
  },
  axes = {
    {type = "x", scale = "x"},
    {type = "y", scale = "y"}
  },
  marks = {
    {
      type = "rect",
      from = {data = "table"},
      properties = {
        enter = {
          x = {scale = "x", field = "an"},
          width = {scale = "x", band = true, offset = -2},
          y = {scale = "y", field = "populatie"},
          y2 = {scale = "y", value = 0}
        },
        update = {
          fill = {
            r = { value = 153 },
            g = { value = 178 },
            b = { value = 204 }
          }
        },
        hover = {
          fill = {
            r = { value = 133 },
            g = { value = 158 },
            b = { value = 184 }
          }
        }
      }
    },
    {
      type = "text",
      from = {data = "table"},
      properties = {
        enter = {
          align = {value = "center"},
          fill = {value = "#333"},
          x = {scale = "x", field = "an"},
          dx = {scale = "x", band = true, mult = 0.5},
          y = {scale = "y", field = "populatie", offset = -5},
          text = {field = "populatie"}
        }
      }
    }
  }
}

local json = {
	version = 2,
	width = 400, 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 = {
                        r = { value = 153 },
                        g = { value = 178 },
                        b = { value = 204 }
                    }
                },
                hover = {
                    fill = {
                        r = { value = 133 },
                        g = { value = 158 },
                        b = { value = 184 }
                    }
                }
			}
		},
		{
			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" }
				}
			}
		}
	}
}

local function isNilOrEmpty(argument)
	return argument == nil or mw.text.trim(argument) == ''
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  -- dată
			d = require('Modul: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, { tostring(v), numbers_raw[v] } )
	end
	return numbers
end

local function drawGraph(json, subpage, args)
	local width = nil
	local height = nil
	mw.logObject(args["lățime"])
	mw.logObject(args["înălțime"])
	if args ~= nil and args["lățime"] ~= nil then
		width = tonumber(args["lățime"])
	end
	if args ~= nil and args["înălțime"] ~= nil then
		height = tonumber(args["înălțime"])
	end
	
	if not isNilOrEmpty(subpage) then
		json.data[1].url = json.data[1].url .. mw.uri.encode(subpage, "PATH")
	end
	if not isNilOrEmpty(width) then
		json.width = width
	end
	if not isNilOrEmpty(height) then
		json.height = height
	end
	
	return tostring(mw.html.create("div")
		:css("display", "inline-block")  -- style hotfix
		:wikitext(mw.getCurrentFrame():extensionTag("graph", mw.text.jsonEncode(json))))
end

local function prepareGraph(data, args)
	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 drawGraph(json, nil, args)
end

function p.main(frame)
	local args = {}
	if type(frame) ~= 'table' then
		args = { id = frame }
	else
		args = getArgs(frame)
	end
	if args ~= nil and args[1] ~= nil then
		local subpage = "Format:Grafic demografie/" .. args[1]
		title = mw.title.new(subpage)
		if title.exists then
			return drawGraph(subpageJson, subpage, args)
		end
	end
	
	local statements = getStatements(args.id)
	if type(statements) ~= 'table' then
		return ''
	end
	statements = prepareStatements(statements)
	if type(statements) ~= 'table' then
		return ''
	end
	return prepareGraph(statements, args)
end

return p