Modul:PopulationChart

Izvor: Hrvatska internetska enciklopedija
Inačica 787808 od 19. srpanj 2026. u 07:51 koju je unio WikiSysop (razgovor | doprinosi)
Prijeđi na navigaciju Prijeđi na pretraživanje
Dokumentacija modula


local p = {}

local function splitCSV(str)
    local t = {}
    for value in string.gmatch(str or "", "([^,]+)") do
        value = mw.text.trim(value)
        table.insert(t, tonumber(value))
    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>Greška: broj godina i broj stanovnika se ne podudara.</strong>"
    end

    -- Konfiguracija (možeš mijenjati iz predloška)
    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
    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 innerHeight = svgHeight - svgPadding * 2

    -- SVG polyline + tooltipovi
    local points = {}
    local circles = {}

    for i,v in ipairs(pops) do
        local x = (i-1) * (svgWidth / math.max(1,(n-1)))
        local y = svgHeight - svgPadding - (v / maxv * innerHeight)

        table.insert(points, string.format("%.1f,%.1f", x, y))

        table.insert(circles, string.format(
            '<circle cx="%.1f" cy="%.1f" r="4" fill="%s"><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" preserveAspectRatio="none">
  <polyline fill="none" stroke="%s" stroke-width="3" points="%s" />
  %s
</svg>
]], svgWidth, svgHeight, lineColor, table.concat(points, " "), table.concat(circles, "\n"))

    -- Bar chart s animacijom
    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