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, "<!--.--->", "")
for value in string.gmatch(str, "([^,]+)") do
value = string.gsub(value, "[%s%c]", "")
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 i stanovnika se ne podudara.</strong>'
end
local barColor = args.line_color or "#3366cc"
-- Statistika
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
local lang = mw.language.getContentLanguage()
-- Početak tablice
local html = {}
table.insert(html, '<table class="wikitable" style="width:100%; max-width:500px; font-size:90%; margin:10px 0;">')
table.insert(html, '<tr><th style="width:20%; text-align:center;">Godina</th><th style="width:30%; text-align:right;">Stanovnika</th><th style="width:50%;">Trend</th></tr>')
-- Redovi (izbjegnut bilo kakav format, čisto spajanje)
for i = 1, n do
local percentage = math.floor((pops[i] / divisor) * 100)
local pctString = tostring(percentage) .. "%"
local trendIndicator = ""
if i > 1 then
if pops[i] > pops[i-1] then
trendIndicator = ' <span style="color:#28a745;">▲</span>'
elseif pops[i] < pops[i-1] then
trendIndicator = ' <span style="color:#dc3545;">▼</span>'
else
trendIndicator = ' <span style="color:#6c757d;">■</span>'
end
end
local formattedPop = lang:formatNum(pops[i])
local currentYear = tostring(years[i])
local row = '<tr>' ..
'<td style="text-align:center; font-weight:bold; background:#f8f9fa;">' .. currentYear .. '</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:' .. pctString .. '; height:100%; border-radius:2px;"></div>' ..
'</div>' ..
'</td>' ..
'</tr>'
table.insert(html, row)
end
-- Dno tablice
local formattedAvg = lang:formatNum(math.floor(sum / n))
local formattedMin = lang:formatNum(minv)
local formattedMax = lang:formatNum(maxv)
local footer = '<tr style="background:#f8f9fa; font-weight:bold; border-top:2px solid #a2a9b1;">' ..
'<td colspan="3" style="font-size:85%; padding:6px; color:#54595d;">' ..
'Ukupno popisa: ' .. tostring(n) .. ' | Prosjek: ' .. formattedAvg .. ' | Raspon: ' .. formattedMin .. ' - ' .. formattedMax ..
'</td>' ..
'</tr>'
table.insert(html, footer)
table.insert(html, '</table>')
return table.concat(html, "\n")
end
return p