Difference between revisions of "Module:Election results"
Jump to navigation
Jump to search
blackwiki>Number 57 (Give up :() |
blackwiki>Number 57 |
||
| Line 163: | Line 163: | ||
:tag('td') | :tag('td') | ||
:wikitext(string.format('%.2f%%', (valid + invalid) / electorate * 100)) | :wikitext(string.format('%.2f%%', (valid + invalid) / electorate * 100)) | ||
| + | :tag('tr') | ||
| + | :addClass('sortbottom') | ||
| + | :css('background', '#eaecf0') | ||
| + | :css('text-align', 'right') | ||
| + | :tag('td') | ||
| + | :wikitext('Source: ', args.source) | ||
| + | :attr('colspan', cols - 0) | ||
| + | :css('text-align', 'left') | ||
return tostring(root) | return tostring(root) | ||
end | end | ||
return p | return p | ||
Revision as of 20:11, 24 July 2020
Implements {{Election results}}
local p = {}
function p.main(frame)
local args = require('Module:Arguments').getArgs(frame)
local index, cols = {}, 3
local party, vp = false, false
local winner, winner_votes = 0, 0
local valid, invalid, electorate = 0, tonumber(args.invalid) or 0, tonumber(args.electorate) or 0
for i = 1, 20 do
if args['cand' .. i] then
table.insert(index, i)
if not party and args['party' .. i] then
party = true
cols = cols + 2
end
if not vp and args['vp' .. i] then
vp = true
cols = cols + 1
end
if args['votes' .. i] then
args['votes' .. i] = tonumber(args['votes' .. i])
if args['votes' .. i] > winner_votes then
winner = i
winner_votes = args['votes' .. i]
end
valid = valid + args['votes' .. i]
end
end
end
local root = mw.html.create('table')
root
:addClass('wikitable sortable')
:tag('caption')
:wikitext(args.caption)
local row = mw.html.create('tr')
if vp then
row
:tag('th')
:wikitext('President')
:attr('colspan', 2)
:tag('th')
:wikitext('Vice president')
else
row
:tag('th')
:wikitext('Candidate')
:attr('colspan', 2)
end
if party then
row
:tag('th')
:wikitext('Party')
end
row
:tag('th')
:wikitext('Votes')
:tag('th')
:wikitext('%')
root:node(row)
local lang = mw.getContentLanguage()
local function fmt(n)
return lang:formatNum(n)
end
for i, v in ipairs(index) do
local row = mw.html.create('tr')
row
:tag('td')
:css('background-color', frame:expandTemplate{title = args['party' .. v] .. '/meta/color'})
if vp then
row
:tag('td')
:wikitext(string.format('\[\[%s\]\]', args['cand' .. v]))
row
:tag('td')
:wikitext(string.format('\[\[%s\]\]', args['vp' .. v]))
else
row
:tag('td')
:wikitext(string.format('\[\[%s\]\]', args['cand' .. v]))
end
if args['party_name' .. v] then
row
:tag('td')
:wikitext(string.format('\[\[%s\|%s\]\]', args['party' .. v], args['party_name' .. v]))
else
row
:tag('td')
:wikitext(string.format('\[\[%s\]\]', args['party' .. v]))
end
local cell = mw.html.create('td')
cell
:css('text-align', 'right')
if args['votes' .. v] then
cell
:wikitext(fmt(args['votes' .. v]))
end
if v == winner then
cell
:css('font-weight', 'bold')
end
row:node(cell)
local cell = mw.html.create('td')
cell
:css('text-align', 'right')
if args['votes' .. v] then
cell
:wikitext(string.format('%.2f%%', args['votes' .. v] / valid * 100))
end
if v == winner then
cell
:css('font-weight', 'bold')
end
row:node(cell)
root:node(row)
end
root
:tag('tr')
:addClass('sortbottom')
:css('background', '#eaecf0')
:css('text-align', 'right')
:tag('td')
:wikitext('Valid votes')
:attr('colspan', cols - 2)
:css('text-align', 'left')
:tag('td')
:wikitext(fmt(valid))
:tag('td')
:wikitext(string.format('%.2f%%', valid / (valid + invalid) * 100))
:tag('tr')
:addClass('sortbottom')
:css('background', '#eaecf0')
:css('text-align', 'right')
:tag('td')
:wikitext('Invalid votes')
:attr('colspan', cols - 2)
:css('text-align', 'left')
:tag('td')
:wikitext(fmt(invalid))
:tag('td')
:wikitext(string.format('%.2f%%', invalid / (valid + invalid) * 100))
:tag('tr')
:addClass('sortbottom')
:css('font-weight', 'bold')
:css('background', '#eaecf0')
:css('text-align', 'right')
:tag('td')
:wikitext('Total votes')
:attr('colspan', cols - 2)
:css('text-align', 'left')
:tag('td')
:wikitext(fmt(valid + invalid))
:tag('td')
:wikitext('100%')
:tag('tr')
:addClass('sortbottom')
:css('background', '#eaecf0')
:css('text-align', 'right')
:tag('td')
:wikitext('Registered voters/turnout')
:attr('colspan', cols - 2)
:css('text-align', 'left')
:tag('td')
:wikitext(fmt(electorate))
:tag('td')
:wikitext(string.format('%.2f%%', (valid + invalid) / electorate * 100))
:tag('tr')
:addClass('sortbottom')
:css('background', '#eaecf0')
:css('text-align', 'right')
:tag('td')
:wikitext('Source: ', args.source)
:attr('colspan', cols - 0)
:css('text-align', 'left')
return tostring(root)
end
return p