Izvor: Hrvatska internetska enciklopedija
Prijeđi na navigaciju
Prijeđi na pretraživanje
local p = {}
-- Čišćenje skrivenih znakova i prelamanje po zarezu
local function splitCSV(str)
local t = {}
if not str then return t end
str = string.gsub(str, "<!--.--->", "") -- Ukloni HTML komentare
for value in string.gmatch(str, "([^,]+)") do
value = string.gsub(value, "[%s%c]", "") -- Ukloni razmake i skrivene prijelome
local num = tonumber(value)
if num then
table.insert(t, num)
end
end
return t
end
function p.render(frame)
local args = frame:getParent() and frame:getParent().args or frame.args
local years = splitCSV(args.years)
local pops = splitCSV(args.pops)
local n = #years
if n == 0 or #pops ~= n then
return '<strong class="error">Greška u grafikonu: Broj godina (' .. n .. ') i stanovnika (' .. #pops .. ') se ne podudara.</strong>'
end
-- Konfiguracija boje (zadano diskretna plava)
local barColor = args.line_color or "#3366cc"
-- Izračun statistike
local maxv = pops[1]
local minv = pops[1]
local sum = 0
for _, v in ipairs(pops) do
if v > maxv then maxv = v end
if v < minv then minv = v end
sum = sum + v
end
local divisor = maxv > 0 and maxv or 1
-- Korištenje ugrađenog MediaWiki sustava za formatiranje brojeva (npr. 1234 -> 1.234)
local lang = mw.language.getContentLanguage()
-- Prazan tablični string (koristimo "wikitable" klasu)
local html = {}
table.insert(html, '<table class="wikitable population-table" style="width: 100%; max-width: 500px; font-size: 90%; margin: 10px 0; border-collapse: collapse;">')
table.insert(html, '<tr><th style="width: 15%; text-align: center;">Godina</th><th style="width: 25%; text-align: right;">Stanovnika</th><th style="width: 60%;">Trend / Grafikon</th></tr>')
-- Popunjavanje redaka tablice
for i = 1, n do
local percentage = math.floor((pops[i] / divisor) * 100)
-- Određivanje indikatora trenda
local trendIndicator = ""
if i > 1 then
if pops[i] > pops[i-1] then
trendIndicator = ' <span style="color: #28a745; font-size: 85%;">▲</span>'
elseif pops[i] < pops[i-1] then
trendIndicator = ' <span style="color: #dc3545; font-size: 85%;">▼</span>'
else
trendIndicator = ' <span style="color: #6c757d; font-size: 85%;">■</span>'
end
end
local formattedPop = lang:formatNum(pops[i])
-- Sigurno spajanje HTML-a bez string.format postotne greške
local row = '<tr>' ..
'<td style="text-align: center; font-weight: bold; background: #f8f9fa;">' .. years[i] .. '</td>' ..
'<td style="text-align: right; white-space: nowrap; padding-right: 8px;">' .. formattedPop .. trendIndicator .. '</td>' ..
'<td style="vertical-align: middle; padding: 4px 8px;">' ..
'<div style="background: #e9ecef; border-radius: 2px; width: 100%; height: 12px;">' ..
'<div style="background: ' .. barColor .. '; width: ' .. percentage .. '%; height: 100%; border-radius: 2px;" title="Godina ' .. years[i] .. ': ' .. formattedPop .. ' stanovnika"></div>' ..
'</div>' ..
'</td>' ..
'</tr>'
table.insert(html, row)
end
-- Dodavanje kompaktne statistike na dno tablice
local avgPop = math.floor(sum / n)
local formattedAvg = lang:formatNum(avgPop)
local formattedMin = lang:formatNum(minv)
local formattedMax = lang:formatNum(maxv)
table.insert(html, string.format([[<tr style="background: #f8f9fa; font-weight: bold; border-top: 2px solid #a2a9b1;">
<td colspan="3" style="font-size: 85%; padding: 6px; text-align: left; color: #54595d;">
Ukupno popisa: %d | Prosjek: %s | Raspon: %s - %s
</td>
</tr>]], n, formattedAvg, formattedMin, formattedMax))
table.insert(html, '</table>')
return table.concat(html, "\n")
end
return p