Difference between revisions of "Module:Election results"
Jump to navigation
Jump to search
blackwiki>Frietjes (add optional |colour1= or |color1= and sanity check for existence of meta/color templates per request on talk page) |
blackwiki>Frietjes (per request on the talk page) |
||
| Line 104: | Line 104: | ||
if args['votes' .. v] then | if args['votes' .. v] then | ||
cell | cell | ||
| − | :wikitext(string.format('%.2f | + | :wikitext(string.format('%.2f', args['votes' .. v] / valid * 100)) |
end | end | ||
row:node(cell) | row:node(cell) | ||
| Line 121: | Line 121: | ||
:wikitext(fmt(valid)) | :wikitext(fmt(valid)) | ||
:tag('td') | :tag('td') | ||
| − | :wikitext(string.format('%.2f | + | :wikitext(string.format('%.2f', valid / (valid + invalid) * 100)) |
:tag('tr') | :tag('tr') | ||
:addClass('sortbottom') | :addClass('sortbottom') | ||
| Line 133: | Line 133: | ||
:wikitext(fmt(invalid)) | :wikitext(fmt(invalid)) | ||
:tag('td') | :tag('td') | ||
| − | :wikitext(string.format('%.2f | + | :wikitext(string.format('%.2f', invalid / (valid + invalid) * 100)) |
:tag('tr') | :tag('tr') | ||
:addClass('sortbottom') | :addClass('sortbottom') | ||
| Line 146: | Line 146: | ||
:wikitext(fmt(valid + invalid)) | :wikitext(fmt(valid + invalid)) | ||
:tag('td') | :tag('td') | ||
| − | :wikitext('100 | + | :wikitext('100.00') |
:tag('tr') | :tag('tr') | ||
:addClass('sortbottom') | :addClass('sortbottom') | ||
| Line 158: | Line 158: | ||
:wikitext(fmt(electorate)) | :wikitext(fmt(electorate)) | ||
:tag('td') | :tag('td') | ||
| − | :wikitext(string.format('%.2f | + | :wikitext(string.format('%.2f', (valid + invalid) / electorate * 100)) |
:tag('tr') | :tag('tr') | ||
:addClass('sortbottom') | :addClass('sortbottom') | ||
Revision as of 00:17, 19 August 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')
local color = args['colour' .. v] or args['color' .. v]
or (mw.title.new('Template:' .. args['party' .. v] .. '/meta/color').exists
and frame:expandTemplate{title = args['party' .. v] .. '/meta/color'})
or nil
row
:tag('td')
:css('background-color', 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
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
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.00')
: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