Modul:PopulationChart: razlika između inačica

Izvor: Hrvatska internetska enciklopedija
Prijeđi na navigaciju Prijeđi na pretraživanje
mNema sažetka uređivanja
mNema sažetka uređivanja
 
Redak 5: Redak 5:
     local t = {}
     local t = {}
     if not str then return t end
     if not str then return t end
     str = string.gsub(str, "<!--.--->", "") -- Ukloni HTML komentare
     str = string.gsub(str, "<!--.--->", "")
     for value in string.gmatch(str, "([^,]+)") do
     for value in string.gmatch(str, "([^,]+)") do
         value = string.gsub(value, "[%s%c]", "") -- Ukloni razmake i skrivene prijelome
         value = string.gsub(value, "[%s%c]", "")
         local num = tonumber(value)
         local num = tonumber(value)
         if num then
         if num then
Redak 23: Redak 23:


     if n == 0 or #pops ~= n then
     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>'
         return '<strong class="error">Greška u grafikonu: Broj godina i stanovnika se ne podudara.</strong>'
     end
     end


    -- Konfiguracija boje (zadano diskretna plava)
     local barColor = args.line_color or "#3366cc"
     local barColor = args.line_color or "#3366cc"


     -- Izračun statistike
     -- Statistika
     local maxv = pops[1]
     local maxv = pops[1]
     local minv = pops[1]
     local minv = pops[1]
Redak 40: Redak 39:
     local divisor = maxv > 0 and maxv or 1
     local divisor = maxv > 0 and maxv or 1


    -- Korištenje ugrađenog MediaWiki sustava za formatiranje brojeva
     local lang = mw.language.getContentLanguage()
     local lang = mw.language.getContentLanguage()


     -- Prazan tablični string (koristimo "wikitable" klasu)
     -- Početak tablice
     local html = {}
     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, '<table class="wikitable" style="width:100%; max-width:500px; font-size:90%; margin:10px 0;">')
     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>')
     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>')


     -- Popunjavanje redaka tablice
     -- Redovi (izbjegnut bilo kakav format, čisto spajanje)
     for i = 1, n do
     for i = 1, n do
         local percentage = math.floor((pops[i] / divisor) * 100)
         local percentage = math.floor((pops[i] / divisor) * 100)
        local pctString = tostring(percentage) .. "%"
          
          
        -- Određivanje indikatora trenda
         local trendIndicator = ""
         local trendIndicator = ""
         if i > 1 then
         if i > 1 then
             if pops[i] > pops[i-1] then
             if pops[i] > pops[i-1] then
                 trendIndicator = ' <span style="color: #28a745; font-size: 85%;">▲</span>'
                 trendIndicator = ' <span style="color:#28a745;">▲</span>'
             elseif pops[i] < pops[i-1] then
             elseif pops[i] < pops[i-1] then
                 trendIndicator = ' <span style="color: #dc3545; font-size: 85%;">▼</span>'
                 trendIndicator = ' <span style="color:#dc3545;">▼</span>'
             else
             else
                 trendIndicator = ' <span style="color: #6c757d; font-size: 85%;">■</span>'
                 trendIndicator = ' <span style="color:#6c757d;">■</span>'
             end
             end
         end
         end


         local formattedPop = lang:formatNum(pops[i])
         local formattedPop = lang:formatNum(pops[i])
        local currentYear = tostring(years[i])


        -- Sigurno spajanje HTML-a za redove bez string.format-a
         local row = '<tr>' ..
         local row = '<tr>' ..
          '<td style="text-align: center; font-weight: bold; background: #f8f9fa;">' .. years[i] .. '</td>' ..
            '<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="text-align:right; white-space:nowrap; padding-right:8px;">' .. formattedPop .. trendIndicator .. '</td>' ..
          '<td style="vertical-align: middle; padding: 4px 8px;">' ..
            '<td style="vertical-align:middle; padding:4px 8px;">' ..
            '<div style="background: #e9ecef; border-radius: 2px; width: 100%; height: 12px;">' ..
                '<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 style="background:' .. barColor .. '; width:' .. pctString .. '; height:100%; border-radius:2px;"></div>' ..
            '</div>' ..
                '</div>' ..
          '</td>' ..
            '</td>' ..
         '</tr>'
         '</tr>'
          
          
Redak 80: Redak 78:
     end
     end


     -- Dodavanje kompaktne statistike na dno tablice (također prebačeno na sigurno spajanje)
     -- Dno tablice
     local avgPop = math.floor(sum / n)
     local formattedAvg = lang:formatNum(math.floor(sum / n))
    local formattedAvg = lang:formatNum(avgPop)
     local formattedMin = lang:formatNum(minv)
     local formattedMin = lang:formatNum(minv)
     local formattedMax = lang:formatNum(maxv)
     local formattedMax = lang:formatNum(maxv)
      
      
     local footer = '<tr style="background: #f8f9fa; font-weight: bold; border-top: 2px solid #a2a9b1;">' ..
     local footer = '<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;">' ..
        '<td colspan="3" style="font-size:85%; padding:6px; color:#54595d;">' ..
        'Ukupno popisa: ' .. n .. ' | Prosjek: ' .. formattedAvg .. ' | Raspon: ' .. formattedMin .. ' - ' .. formattedMax ..
            'Ukupno popisa: ' .. tostring(n) .. ' | Prosjek: ' .. formattedAvg .. ' | Raspon: ' .. formattedMin .. ' - ' .. formattedMax ..
      '</td>' ..
        '</td>' ..
     '</tr>'
     '</tr>'
      
      

Posljednja izmjena od 19. srpanj 2026. u 10:34

Dokumentacija modula


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