Modul:PopulationChart

Izvor: Hrvatska internetska enciklopedija
Inačica 787806 od 19. srpanj 2026. u 07:46 koju je unio WikiSysop (razgovor | doprinosi) (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...«.)
(razl) ←Starija inačica | vidi trenutačnu inačicu (razl) | Novija inačica→ (razl)
Prijeđi na navigaciju Prijeđi na pretraživanje
Dokumentacija modula


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 godina i broj stanovnika se ne podudara.</strong>"
    end

    -- Statistika
    local maxv = pops[1]
    local minv = pops[1]
    local sum  = 0

    for i,v in ipairs(pops) do
        if v > maxv then maxv = v end
        if v < minv then minv = v end
        sum = sum + v
    end

    -- SVG polyline
    local points = {}
    for i,v in ipairs(pops) do
        local x = (i-1) * (800 / (n-1))
        local y = 300 - (v / maxv * 280)
        table.insert(points, string.format("%.1f,%.1f", x, y))
    end

    local svg = '<svg class="population-linechart" viewBox="0 0 800 300" preserveAspectRatio="none">'
        .. '<polyline fill="none" stroke="#007bff" stroke-width="3" points="'
        .. table.concat(points, " ")
        .. '" /></svg>'

    -- Bar chart
    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] / maxv) * 100)

        table.insert(bars,
            string.format([[
<div class="population-bar">
  <div class="population-year">%d</div>
  <div class="population-value %s" style="width:%d%%;" data-tooltip="Godina %d: %d stanovnika">
    <span>%d</span>
  </div>
</div>
]], years[i], trend, width, years[i], pops[i], pops[i])
        )
    end

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

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

return p