Module:Croatian population data graph
This module implements the {{Croatian population data graph }} template.
It processes parameters in the format pYYYY=value
taken from the template, and displays the population data as a bar chart. Here, YYYY represents a year and value the corresponding population. In older syntax, the template also accepts up to 99 of annum/population pairs: aN=YYYY | pN=value
.
The chart is rendered as a table with bars made of <div>...</div>
elements, having the heights proportional to each value for a given year YYYY.
Other parameters taken from the template (or module invocation) include, in Croatian (for historical reasons) or English:
English | Croatian |
---|---|
title | naslov |
entity | područje |
note | napomena |
sources | izvori |
censuses=HRV | popisi=HRV |
The following labels can be modified only from the module invocation:
parameter | English text | Croatian text |
---|---|---|
trend | Population trends |
Kretanje broja stanovnika od
|
trendsep | – |
do
|
ord | population |
broj stanovnika
|
nb | '''Note: ''' |
'''Napomena:'''
|
ref | '''Sources: ''' |
'''Izvori:'''
|
YYYYsuf | .
|
Examples
{{Croatian population data graph|p2001=1|p2011=3|p2021=5|title=Title|entity=Entity|note=Note|sources=Sources}}
{{Croatian population data graph|a1=2001|p1=1|a2=2011|p2=3|a3=2021|p3=5|title=Title|entity=Entity|note=Note|sources=Sources}}
When censuses=HRV
only the years of the known Croatian population censuses are shown (1857, 1869, 1880, 1890, 1900, 1910, 1921, 1931, 1948, 1953, 1961, 1971, 1981, 1991, 2001, 2011, 2021), starting and ending with the years for which data are given.
If unset, any years can be shown.
{{Croatian population data graph|censuses=|p2025=5|p2020=9|p2015=12|p2010=8|p2005=4|p2000=2|p1995=1|p1990=0|title=Title|entity=Entity|note=Note|sources=Sources}}
Editors can experiment in this module's sandbox (create | mirror) and testcases (create) pages.
Subpages of this module.
local p = {} -- if the template has censuses=HRV it will show all known censuses from 1857 to 2021 -- regardless of whether the article contains data -- without this parameter in the template, it will only show the years entered in the article local godine_hr = {1857, 1869, 1880, 1890, 1900, 1910, 1921, 1931, 1948, 1953, 1961, 1971, 1981, 1991, 2001, 2011, 2021, } -- translations begin local view_template_link = "[[Template:Croatian population data graph|v]]" local izvori_hr = "[[Template:Croatian population data graph/Sources|Croatian Bureau of Statistics publications]]" local max_height = 8 -- that's for 8em; also set the cell height in the template's css -- all valid param names: for historical reasons, on enwiki some are in Croatian and some in English param_names = { title = {"title", "naslov"}, entity = {"entity", "područje"}, note = {"note", "napomena"}, sources = {"sources", "izvori"}, censuses = {"censuses", "popisi"} } labels = { trend = "Population trends ", -- EN:"Population trends " HR:"Kretanje broja stanovnika od " trendsep = "–", -- EN:"–" HR:" do " ord = "population", -- EN:"population" HR:"broj stanovnika" nb = "'''Note:'''", -- EN:"'''Note:'''" HR:"'''Napomena:" ref = "'''Sources:'''", -- EN:"'''Sources:'''" HR:"'''Izvori:" YYYYsuf = "", -- EN:"" HR:"." } -- translations end function p.dijagram(frame) local data = {} local targs = frame:getParent().args --template arguments in template call local margs = frame.args --module arguments in #invoke param_values = {} local tmp for param, names in pairs(param_names) do tmp = nil for _, name in ipairs(names) do tmp = tmp or targs[name] or margs[name] end param_values[param] = tmp end for label, tx in pairs(labels) do labels[label] = margs[label] or labels[label] --add possibility to change labels from module invocation end mw.logObject(param_values) --loop through all parameters; most of them are parameters of the form pYYYY or aN/pN pairs for k, v in pairs(targs) do local Y = tonumber(string.match(k, "^p(%d%d%d%d)$")) -- parameters like p2021, for population in 2021 if Y then local p = tonumber(v) --mw.log(g,p) if p then data[Y] = p end end local n = string.match(k, "^a(%d%d?)$") -- up to 99 pairs of year/population parameters: a1/p1 ... a99/p99 (old param syntax) if n then local Y = tonumber(v) local p = tonumber(targs["p"..n]) --mw.log(g,p) if Y and p then data[Y] = p end end end --years present in the template; we need them in the table for sorting local years = {} local data_max = 0 for k, v in pairs(data) do table.insert(years, k) if data[k]>data_max then data_max=data[k] end end table.sort(years) --html table where each cell will contain one column (div) of the bar chart --here, we create cells in two rows local tr1 = mw.html.create( "tr" ) tr1 : tag("td") : addClass("kbs-ordinate") : tag("span") : addClass("kbs-ordinate-text") : wikitext(labels["ord"]) : done() local tr2 = mw.html.create( "tr" ) tr2 : tag("td") : done() if (param_values["censuses"]=="HRV") then yrs_to_show = godine_hr else yrs_to_show = years end local first, last = Nil, Nil for _, Y in ipairs(yrs_to_show) do if years[1]<=Y and Y<=years[#years] then --don't show left and right of the only known ones, but yrs_to_show all between first = first or Y last = Y local value = data[Y] or 0 local data_class = value<10000 and "kbs-data" or "kbs-data-smaller" local mark = (value>0 and value) or "" -- do not show 0 or Nil tr1 : tag("td") : addClass("kbs-for-columns") : tag("div") : addClass(data_class) : wikitext(mark) : done() : tag("div") : addClass("kbs-columns") : cssText("height:"..0.01*math.floor(100*value*max_height/data_max).."em;") : done() tr2 : tag("td") : addClass("kbs-years") : wikitext(Y .. labels["YYYYsuf"]) : done() end end -- title above table with diagram -- in template, enter: Settlement X or Municipality Y or City Z local data_for = param_values["entity"] and (param_values["entity"] ~= "") and ("'''" .. param_values["entity"] .. "''': ") or "" local kbs = labels["trend"] .. first..labels["YYYYsuf"] .. labels["trendsep"] .. last..labels["YYYYsuf"] local title = data_for .. (param_values["title"] and (param_values["title"] ~= "") and param_values["title"] or kbs) -- table for bar chart local tbl = mw.html.create( "table" ) tbl : addClass("kbs-table") : node(tr1) : node(tr2) local ttl = mw.html.create( "div" ) ttl : addClass("kbs-title") local ttl_left = mw.html.create( "div" ) ttl_left : addClass("kbs-title-left") : wikitext(title) local ttl_right = mw.html.create( "div" ) ttl_right : addClass("kbs-title-right") : wikitext(view_template_link) ttl : node(ttl_left) : node(ttl_right) local tbl_ttl = mw.html.create( "div" ) tbl_ttl : addClass("kbs-title-table-scrollable") : tag("div") : addClass("kbs-title-table") : node(ttl) : node(tbl) : done() -- Note:... and Sources:... below the diagram local note = param_values["note"] or "" local sources = "" if param_values["censuses"] == "HRV" then sources = izvori_hr end sources = param_values["sources"] or sources local nte = mw.html.create( "div" ) : addClass("kbs-note") if note ~="" then nte : wikitext(labels["nb"] .. note .. " ") end if sources ~="" then nte : wikitext(labels["ref"] .. sources) end local nte_src = mw.html.create( "div" ) nte_src : addClass("kbs-note-nonscrollable") : node(nte) local everything = mw.html.create() : node(tbl_ttl) : node(nte_src) return everything end return p