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
 
Nisu prikazane 4 međuinačice
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 = {}
     -- Čisti whitespace i prelama po zarezu
     if not str then return t end
     for value in string.gmatch(str or "", "([^,]+)") do
    str = string.gsub(str, "<!--.--->", "")
         value = mw.text.trim(value)
     for value in string.gmatch(str, "([^,]+)") do
         value = string.gsub(value, "[%s%c]", "")
         local num = tonumber(value)
         local num = tonumber(value)
         if num then
         if num then
Redak 15: Redak 17:


function p.render(frame)
function p.render(frame)
    -- Dohvaćanje argumenata iz predloška
     local args = frame:getParent() and frame:getParent().args or frame.args
     local args = frame:getParent() and frame:getParent().args or frame.args
     local years = splitCSV(args.years)
     local years = splitCSV(args.years)
Redak 21: Redak 22:
     local n = #years
     local n = #years


    -- Validacija podataka
     if n == 0 or #pops ~= n then
     if n == 0 or #pops ~= n then
         return '<strong class="error">Greška: Broj godina (' .. n .. ') i broj stanovnika (' .. #pops .. ') se ne podudara ili su podaci neispravni.</strong>'
         return '<strong class="error">Greška u grafikonu: Broj godina i stanovnika se ne podudara.</strong>'
     end
     end


    -- Konfiguracija
     local barColor = args.line_color or "#3366cc"
     local svgWidth  = tonumber(args.svg_width)  or 800
    local svgHeight  = tonumber(args.svg_height)  or 300
    local svgPadding = tonumber(args.svg_padding) or 20
    local lineColor  = args.line_color           or "#007bff"


     -- Statistika
     -- Statistika
Redak 41: Redak 37:
         sum = sum + v
         sum = sum + v
     end
     end
    -- Izračun raspona za SVG (unutar paddinga)
    local innerWidth  = svgWidth - (svgPadding * 2)
    local innerHeight = svgHeight - (svgPadding * 2)
    -- Ako je maxv 0, postavi na 1 da izbjegnemo dijeljenje s nulom
     local divisor = maxv > 0 and maxv or 1
     local divisor = maxv > 0 and maxv or 1


    -- SVG linija i točke
     local lang = mw.language.getContentLanguage()
     local points = {}
    local circles = {}
    for i, v in ipairs(pops) do
        -- X se raspoređuje linearno unutar innerWidth, počevši od svgPadding
        local x = svgPadding
        if n > 1 then
            x = svgPadding + ((i - 1) * (innerWidth / (n - 1)))
        end
        -- Y ide odozgo prema dolje (0 je vrh SVG-a)
        local y = svgHeight - svgPadding - ((v / divisor) * innerHeight)
       
        table.insert(points, string.format("%.1f,%.1f", x, y))
        table.insert(circles, string.format(
            '<circle cx="%.1f" cy="%.1f" r="5" fill="%s" stroke="#fff" stroke-width="1.5"><title>Godina %d: %d stanovnika</title></circle>',
            x, y, lineColor, years[i], v
        ))
    end


     local svg = string.format([[<svg class="population-linechart" viewBox="0 0 %d %d" style="width:100%%; height:auto;">
    -- Početak tablice
  <polyline fill="none" stroke="%s" stroke-width="3" stroke-linecap="round" stroke-linejoin="round" points="%s" />
     local html = {}
  %s
    table.insert(html, '<table class="wikitable" style="width:100%; max-width:500px; font-size:90%; margin:10px 0;">')
</svg>]], svgWidth, svgHeight, lineColor, table.concat(points, " "), table.concat(circles, "\n"))
    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>')


     -- Bar chart (Stupci)
     -- Redovi (izbjegnut bilo kakav format, čisto spajanje)
    local bars = {}
     for i = 1, n do
     for i = 1, n do
         local trend = "equal"
         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
                 trend = "up"
                 trendIndicator = ' <span style="color:#28a745;">▲</span>'
             elseif pops[i] < pops[i-1] then
             elseif pops[i] < pops[i-1] then
                 trend = "down"
                 trendIndicator = ' <span style="color:#dc3545;">▼</span>'
            else
                trendIndicator = ' <span style="color:#6c757d;">■</span>'
             end
             end
         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>'
          
          
        local width = math.floor((pops[i] / divisor) * 100)
         table.insert(html, row)
         table.insert(bars, string.format([[<div class="population-bar">
  <div class="population-year">%d</div>
  <div class="population-bar-wrapper">
    <div class="population-value %s" style="width:%d%%;" title="Godina %d: %d stanovnika">
      <span>%d</span>
    </div>
  </div>
</div>]], years[i], trend, width, years[i], pops[i], pops[i]))
     end
     end


     local stats = string.format([[<div class="population-stats">
    -- Dno tablice
  <strong>Najviše:</strong> %d stanovnika<br>
     local formattedAvg = lang:formatNum(math.floor(sum / n))
  <strong>Najmanje:</strong> %d stanovnika<br>
    local formattedMin = lang:formatNum(minv)
  <strong>Prosjek:</strong> %d stanovnika<br>
    local formattedMax = lang:formatNum(maxv)
  <strong>Ukupno godina:</strong> %d
   
</div>]], maxv, minv, math.floor(sum / n), n)
    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 '<div class="population-wrapper">\n' .. table.concat(bars, "\n") .. '\n' .. svg .. '\n' .. stats .. '\n' .. '</div>'
     return table.concat(html, "\n")
end
end


return p
return p

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