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 1: Redak 1:
local p = {}
local p = {}


-- Napredna funkcija koja agresivno čisti sve ne-numeričke skrivene znakove
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
   
         value = mw.text.trim(value)
    -- Ukloni HTML komentare ako ih ima
    str = string.gsub(str, "<!--.--->", "")
   
     -- Razlomi niz preko zareza
     for value in string.gmatch(str, "([^,]+)") do
        -- Očisti sve razmake, prijelome redaka (\n), tabulatore (\t) i skrivene znakove
         value = string.gsub(value, "[%s%c]", "")
       
         local num = tonumber(value)
         local num = tonumber(value)
         if num then
         if num then
Redak 15: Redak 23:


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 28:
     local n = #years
     local n = #years


     -- Validacija podataka
     -- Ako modul ne nađe podatke, ispisat će točan broj koji je uspio pročitati radi lakšeg debugiranja
     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 učitanih godina (' .. n .. ') i broj stanovnika (' .. #pops .. ') se ne podudaraju. Provjerite jesu li svi zarezi ispravno postavljeni.</strong>'
     end
     end


     -- Konfiguracija
     -- Konfiguracija grafikona
     local svgWidth  = tonumber(args.svg_width)  or 800
     local svgWidth  = tonumber(args.svg_width)  or 800
     local svgHeight  = tonumber(args.svg_height)  or 300
     local svgHeight  = tonumber(args.svg_height)  or 300
     local svgPadding = tonumber(args.svg_padding) or 20
     local svgPadding = tonumber(args.svg_padding) or 25 -- Malo povećan padding za bolji izgled točaka
     local lineColor  = args.line_color            or "#007bff"
     local lineColor  = args.line_color            or "#007bff"


     -- Statistika
     -- Pronalaženje statistike
     local maxv = pops[1]
     local maxv = pops[1]
     local minv = pops[1]
     local minv = pops[1]
Redak 42: Redak 49:
     end
     end


    -- Izračun raspona za SVG (unutar paddinga)
     local innerWidth  = svgWidth - (svgPadding * 2)
     local innerWidth  = svgWidth - (svgPadding * 2)
     local innerHeight = svgHeight - (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
     -- Izrada SVG-a
     local points = {}
     local points = {}
     local circles = {}
     local circles = {}
     for i, v in ipairs(pops) do
     for i, v in ipairs(pops) do
        -- X se raspoređuje linearno unutar innerWidth, počevši od svgPadding
         local x = svgPadding
         local x = svgPadding
         if n > 1 then
         if n > 1 then
             x = svgPadding + ((i - 1) * (innerWidth / (n - 1)))
             x = svgPadding + ((i - 1) * (innerWidth / (n - 1)))
         end
         end
        -- Y ide odozgo prema dolje (0 je vrh SVG-a)
         local y = svgHeight - svgPadding - ((v / divisor) * innerHeight)
         local y = svgHeight - svgPadding - ((v / divisor) * innerHeight)
          
          
         table.insert(points, string.format("%.1f,%.1f", x, y))
         table.insert(points, string.format("%.1f,%.1f", x, y))
         table.insert(circles, string.format(
         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>',
             '<circle cx="%.1f" cy="%.1f" r="5" fill="%s" stroke="#ffffff" stroke-width="2"><title>Godina %d: %d stanovnika</title></circle>',
             x, y, lineColor, years[i], v
             x, y, lineColor, years[i], v
         ))
         ))
     end
     end


     local svg = string.format([[<svg class="population-linechart" viewBox="0 0 %d %d" style="width:100%%; height:auto;">
     local svg = string.format([[<svg class="population-linechart" viewBox="0 0 %d %d" style="width:100%%; height:auto;" xmlns="http://www.w3.org/2000/svg">
   <polyline fill="none" stroke="%s" stroke-width="3" stroke-linecap="round" stroke-linejoin="round" points="%s" />
   <polyline fill="none" stroke="%s" stroke-width="3" stroke-linecap="round" stroke-linejoin="round" points="%s" />
   %s
   %s
</svg>]], svgWidth, svgHeight, lineColor, table.concat(points, " "), table.concat(circles, "\n"))
</svg>]], svgWidth, svgHeight, lineColor, table.concat(points, " "), table.concat(circles, "\n"))


     -- Bar chart (Stupci)
     -- Izrada Bar Chart-a (Stupaca)
     local bars = {}
     local bars = {}
     for i = 1, n do
     for i = 1, n do
Redak 100: Redak 102:
   <strong>Najmanje:</strong> %d stanovnika<br>
   <strong>Najmanje:</strong> %d stanovnika<br>
   <strong>Prosjek:</strong> %d stanovnika<br>
   <strong>Prosjek:</strong> %d stanovnika<br>
   <strong>Ukupno godina:</strong> %d
   <strong>Ukupno popisa:</strong> %d
</div>]], maxv, minv, math.floor(sum / n), n)
</div>]], maxv, minv, math.floor(sum / n), n)



Inačica od 19. srpanj 2026. u 09:54

Dokumentacija modula


local p = {}

-- Napredna funkcija koja agresivno čisti sve ne-numeričke skrivene znakove
local function splitCSV(str)
    local t = {}
    if not str then return t end
    
    -- Ukloni HTML komentare ako ih ima
    str = string.gsub(str, "<!--.--->", "")
    
    -- Razlomi niz preko zareza
    for value in string.gmatch(str, "([^,]+)") do
        -- Očisti sve razmake, prijelome redaka (\n), tabulatore (\t) i skrivene znakove
        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

    -- Ako modul ne nađe podatke, ispisat će točan broj koji je uspio pročitati radi lakšeg debugiranja
    if n == 0 or #pops ~= n then
        return '<strong class="error">Greška u grafikonu: Broj učitanih godina (' .. n .. ') i broj stanovnika (' .. #pops .. ') se ne podudaraju. Provjerite jesu li svi zarezi ispravno postavljeni.</strong>'
    end

    -- Konfiguracija grafikona
    local svgWidth   = tonumber(args.svg_width)   or 800
    local svgHeight  = tonumber(args.svg_height)  or 300
    local svgPadding = tonumber(args.svg_padding) or 25 -- Malo povećan padding za bolji izgled točaka
    local lineColor  = args.line_color            or "#007bff"

    -- Pronalaženje 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 innerWidth  = svgWidth - (svgPadding * 2)
    local innerHeight = svgHeight - (svgPadding * 2)
    local divisor = maxv > 0 and maxv or 1

    -- Izrada SVG-a
    local points = {}
    local circles = {}
    for i, v in ipairs(pops) do
        local x = svgPadding
        if n > 1 then
            x = svgPadding + ((i - 1) * (innerWidth / (n - 1)))
        end
        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="#ffffff" stroke-width="2"><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;" xmlns="http://www.w3.org/2000/svg">
  <polyline fill="none" stroke="%s" stroke-width="3" stroke-linecap="round" stroke-linejoin="round" points="%s" />
  %s
</svg>]], svgWidth, svgHeight, lineColor, table.concat(points, " "), table.concat(circles, "\n"))

    -- Izrada Bar Chart-a (Stupaca)
    local bars = {}
    for i = 1, n do
        local trend = "equal"
        if i > 1 then
            if pops[i] > pops[i-1] then
                trend = "up"
            elseif pops[i] < pops[i-1] then
                trend = "down"
            end
        end
        
        local width = math.floor((pops[i] / divisor) * 100)
        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

    local stats = string.format([[<div class="population-stats">
  <strong>Najviše:</strong> %d stanovnika<br>
  <strong>Najmanje:</strong> %d stanovnika<br>
  <strong>Prosjek:</strong> %d stanovnika<br>
  <strong>Ukupno popisa:</strong> %d
</div>]], maxv, minv, math.floor(sum / n), n)

    return '<div class="population-wrapper">\n' .. table.concat(bars, "\n") .. '\n' .. svg .. '\n' .. stats .. '\n' .. '</div>'
end

return p