Module:Sandbox/Ythlev: Difference between revisions
Appearance
Content deleted Content added
No edit summary |
No edit summary |
||
Line 19: | Line 19: | ||
end |
end |
||
args['votes' .. i] = tonumber(args['votes' .. i]) |
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] |
valid = valid + args['votes' .. i] |
||
end |
end |
||
Line 79: | Line 83: | ||
:wikitext(string.format('\[\[%s\]\]', args['cand' .. v])) |
:wikitext(string.format('\[\[%s\]\]', args['cand' .. v])) |
||
end |
end |
||
if v == winner then |
|||
⚫ | |||
:tag('td') |
row |
||
:tag('td') |
|||
:css(' |
:css('font-weight', 'bold') |
||
: |
:css('text-align', 'right') |
||
: |
:wikitext(fmt(args['votes' .. v])) |
||
: |
:tag('td') |
||
:css('font-weight', 'bold') |
|||
⚫ | |||
:css('text-align', 'right') |
|||
⚫ | |||
else |
|||
⚫ | |||
:tag('td') |
|||
:css('text-align', 'right') |
|||
:wikitext(fmt(args['votes' .. v])) |
|||
:tag('td') |
|||
:css('text-align', 'right') |
|||
:wikitext(string.format('%.2f', args['votes' .. v] / valid * 100)) |
|||
end |
|||
root:node(row) |
root:node(row) |
||
end |
end |
Revision as of 18:05, 17 June 2020
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 = 0
for i = 1, 20 do
if args['cand' .. i] ~= nil and args['votes' .. i] ~= nil
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
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
local root = mw.html.create('table')
root
:addClass('wikitable')
local row = mw.html.create('tr')
if party then
row
:tag('th')
:wikitext('Party')
:attr('colspan', 2)
end
if vp then
row
:tag('th')
:wikitext('President')
:tag('th')
:wikitext('Vice president')
else
row
:tag('th')
:wikitext('Candidate')
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 args['party_name' .. v] ~= nil then
row
:tag('td')
:wikitext(string.format('\[\[%s\]\]', args['party_name' .. v]))
else
row
:tag('td')
:wikitext(string.format('\[\[%s\]\]', args['party' .. v]))
end
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 v == winner then
row
:tag('td')
:css('font-weight', 'bold')
:css('text-align', 'right')
:wikitext(fmt(args['votes' .. v]))
:tag('td')
:css('font-weight', 'bold')
:css('text-align', 'right')
:wikitext(string.format('%.2f', args['votes' .. v] / valid * 100))
else
row
:tag('td')
:css('text-align', 'right')
:wikitext(fmt(args['votes' .. v]))
:tag('td')
:css('text-align', 'right')
:wikitext(string.format('%.2f', args['votes' .. v] / valid * 100))
end
root:node(row)
end
args.invalid, args.electorate = tonumber(args.invalid), tonumber(args.electorate)
root
:tag('tr')
:css('background', '#eaecf0')
:css('text-align', 'right')
:tag('td')
:wikitext('Valid votes')
:attr('colspan', cols - 2)
:tag('td')
:wikitext(fmt(valid))
:tag('td')
:wikitext(string.format('%.2f', valid / (valid + args.invalid) * 100))
:tag('tr')
:css('background', '#eaecf0')
:css('text-align', 'right')
:tag('td')
:wikitext('Invalid votes')
:attr('colspan', cols - 2)
:tag('td')
:wikitext(fmt(args.invalid))
:tag('td')
:wikitext(string.format('%.2f', args.invalid / (valid + args.invalid) * 100))
:tag('tr')
:css('font-weight', 'bold')
:css('background', '#eaecf0')
:css('text-align', 'right')
:tag('td')
:wikitext('Total votes')
:attr('colspan', cols - 2)
:tag('td')
:wikitext(fmt(valid + args.invalid))
:tag('td')
:wikitext('100.00')
:tag('tr')
:css('background', '#eaecf0')
:css('text-align', 'right')
:tag('td')
:wikitext('Electorate–turnout')
:attr('colspan', cols - 2)
:tag('td')
:wikitext(fmt(args.electorate))
:tag('td')
:wikitext(string.format('%.2f', (valid + args.invalid) / args.electorate * 100))
return tostring(root)
end
return p