Modul:PopulationChart: razlika između inačica
Prijeđi na navigaciju
Prijeđi na pretraživanje
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...«. |
mNema sažetka uređivanja |
||
| Redak 1: | Redak 1: | ||
local p = {} | local p = {} | ||
local function splitCSV(str) | local function splitCSV(str) | ||
local t = {} | local t = {} | ||
for value in string.gmatch(str, "([^,]+)") do | for value in string.gmatch(str or "", "([^,]+)") do | ||
value = mw.text.trim(value) | |||
table.insert(t, tonumber(value)) | table.insert(t, tonumber(value)) | ||
end | end | ||
| Redak 11: | Redak 11: | ||
function p.render(frame) | function p.render(frame) | ||
local years = splitCSV( | local args = frame:getParent() and frame:getParent().args or frame.args | ||
local pops = splitCSV( | |||
local years = splitCSV(args.years) | |||
local pops = splitCSV(args.pops) | |||
local n = #years | local n = #years | ||
| Redak 18: | Redak 20: | ||
return "<strong>Greška: broj godina i broj stanovnika se ne podudara.</strong>" | return "<strong>Greška: broj godina i broj stanovnika se ne podudara.</strong>" | ||
end | 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 | -- Statistika | ||
| Redak 24: | Redak 32: | ||
local sum = 0 | local sum = 0 | ||
for | for _,v in ipairs(pops) do | ||
if v > maxv then maxv = v end | if v > maxv then maxv = v end | ||
if v < minv then minv = v end | if v < minv then minv = v end | ||
| Redak 30: | Redak 38: | ||
end | end | ||
-- SVG polyline | local innerHeight = svgHeight - svgPadding * 2 | ||
-- SVG polyline + tooltipovi | |||
local points = {} | local points = {} | ||
local circles = {} | |||
for i,v in ipairs(pops) do | for i,v in ipairs(pops) do | ||
local x = (i-1) * ( | local x = (i-1) * (svgWidth / math.max(1,(n-1))) | ||
local y = | local y = svgHeight - svgPadding - (v / maxv * innerHeight) | ||
table.insert(points, string.format("%.1f,%.1f", x, y)) | 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 | end | ||
local svg = | 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 | -- Bar chart s animacijom | ||
local bars = {} | local bars = {} | ||
| Redak 49: | Redak 69: | ||
local trend = "equal" | local trend = "equal" | ||
if i > 1 then | if i > 1 then | ||
if pops[i] > pops[i-1] then trend = "up" | if pops[i] > pops[i-1] then | ||
elseif pops[i] < pops[i-1] then trend = "down" | trend = "up" | ||
elseif pops[i] < pops[i-1] then | |||
trend = "down" | |||
end | end | ||
end | end | ||
| Redak 56: | Redak 78: | ||
local width = math.floor((pops[i] / maxv) * 100) | local width = math.floor((pops[i] / maxv) * 100) | ||
table.insert(bars, | table.insert(bars, string.format([[ | ||
<div class="population-bar"> | <div class="population-bar"> | ||
<div class="population-year">%d</div> | <div class="population-year">%d</div> | ||
| Redak 64: | Redak 85: | ||
</div> | </div> | ||
</div> | </div> | ||
]], years[i], trend, width, years[i], pops[i], pops[i]) | ]], years[i], trend, width, years[i], pops[i], pops[i])) | ||
end | end | ||
Inačica od 19. srpanj 2026. u 07:51
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