Module:Graph production
La documentation de ce module est générée par le modèle {{Documentation module}}.
Les éditeurs peuvent travailler dans le bac à sable (créer).
Voir les statistiques d'appel depuis le wikicode sur l'outil wstat et les appels depuis d'autres modules.
local getArgs = require('Module:Arguments').getArgs
local p = {}
local defaultproperty = 'P1092' -- propriété "quantité produite"
local json = {
version = 2,
width = 600, height = 300,
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" }
}
}
}
}
}
local function getStatements(id, property)
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 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 = {}
if #dates <= 20 then
for _, v in ipairs(dates) do
table.insert(numbers, { tostring(v), numbers_raw[v] } )
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]})
end
if numbers_raw[dates[n + 1]] >= 1000000 then
json.marks[2].properties.enter.angle = { value = -45 }
end
end
return numbers
end
local function drawGraph(data, float)
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") -- style hotfix
: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
local property = args["propriété"] or args.property or defaultproperty
local statements = getStatements(args.id, property)
if type(statements) ~= 'table' then
return ''
end
statements = prepareStatements(statements)
if type(statements) ~= 'table' then
return ''
end
return drawGraph(statements, args.float)
end
return p