Modul:PopulationChart: razlika između inačica
Prijeđi na navigaciju
Prijeđi na pretraživanje
Stvorena nova stranica sa sadržajem: »local p = {} -- Pretvara string "1857,1869,1880" u {1857,1869,1880} local function splitCSV(str) local t = {} for value in string.gmatch(str, "([^,]+)") do table.insert(t, tonumber(value)) end return t end function p.render(frame) local years = splitCSV(frame.args.years or "") local pops = splitCSV(frame.args.pops or "") local n = #years if n == 0 or #pops ~= n then return "<strong>Greška: broj g...«. |
mNema sažetka uređivanja |
||
| Nije prikazano 6 međuinačica | |||
| Redak 1: | Redak 1: | ||
local p = {} | local p = {} | ||
-- | -- Čišćenje skrivenih znakova i prelamanje po zarezu | ||
local function splitCSV(str) | local function splitCSV(str) | ||
local t = {} | local t = {} | ||
if not str then return t end | |||
str = string.gsub(str, "<!--.--->", "") | |||
for value in string.gmatch(str, "([^,]+)") do | 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 | end | ||
return t | return t | ||
| Redak 11: | Redak 17: | ||
function p.render(frame) | function p.render(frame) | ||
local years = splitCSV( | local args = frame:getParent() and frame:getParent().args or frame.args | ||
local pops = splitCSV( | local years = splitCSV(args.years) | ||
local pops = splitCSV(args.pops) | |||
local n = #years | |||
if n == 0 or #pops ~= n then | if n == 0 or #pops ~= n then | ||
return | return '<strong class="error">Greška u grafikonu: Broj godina i stanovnika se ne podudara.</strong>' | ||
end | end | ||
local barColor = args.line_color or "#3366cc" | |||
-- Statistika | -- Statistika | ||
| Redak 23: | Redak 32: | ||
local minv = pops[1] | local minv = pops[1] | ||
local sum = 0 | local sum = 0 | ||
for _, v in ipairs(pops) do | |||
for | |||
if v > maxv then maxv = v end | if v > maxv then maxv = v end | ||
if v < minv then minv = v end | if v < minv then minv = v end | ||
sum = sum + v | sum = sum + v | ||
end | end | ||
local divisor = maxv > 0 and maxv or 1 | |||
local lang = mw.language.getContentLanguage() | |||
local | |||
-- | -- Početak tablice | ||
local | 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>') | |||
for i=1,n do | -- Redovi (izbjegnut bilo kakav format, čisto spajanje) | ||
local | for i = 1, n do | ||
local percentage = math.floor((pops[i] / divisor) * 100) | |||
local pctString = tostring(percentage) .. "%" | |||
local trendIndicator = "" | |||
if i > 1 then | if i > 1 then | ||
if pops[i] > pops[i-1] then | if pops[i] > pops[i-1] then | ||
elseif 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 | ||
end | end | ||
local | 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 | end | ||
local | -- Dno tablice | ||
< | local formattedAvg = lang:formatNum(math.floor(sum / n)) | ||
local formattedMin = lang:formatNum(minv) | |||
local formattedMax = lang:formatNum(maxv) | |||
Prosjek: | |||
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 | return table.concat(html, "\n") | ||
end | end | ||
return p | return p | ||
Posljednja izmjena od 19. srpanj 2026. u 10:34
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