Module:Croatian population data graph

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)
        : attr('role', 'presentation')
    
    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

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.