File:Ambox warning blue construction.svg | This module is rated as pre-alpha. It is unfinished, and may or may not be in active development. It should not be used from article namespace pages. Modules remain pre-alpha until the original editor (or someone who takes one over if it is abandoned for some time) is satisfied with the basic structure. |
UsageEdit
This is a draft module with the goal of standardizing "election results" tables.
-- this is a Lua module for working with [[mw:Help:Tabular Data|Tabular Data]] from Wikimedia Commons. the goal is to consistently display "election results by county" tables
local p = {}
function sumRow(row)
total = 0
for i, j in ipairs(row) do
if (i ~= 1) then -- ignore the first entry
total = total + j
end
end
return total
end
function electionTable(data)
local fileToGet = data or "2020 United States presidential election/Connecticut"
local fileFullPath = "Election results/" .. fileToGet .. ".tab"
local electionData = mw.ext.data.get(fileFullPath)
local electionResults = electionData["data"]
local electionSchema = electionData["schema"]["fields"]
resultsWikitable = "{| class=\"wikitable sortable\"" .. "\n! rowspan=2 |" .. electionSchema[1]["title"]
for _, schemaRow in ipairs(electionSchema) do
if (_ ~= 1) then -- this is to avoid duplicating the county entry
resultsWikitable = resultsWikitable .. "\n! colspan=2 |" .. schemaRow["title"]
end
end
resultsWikitable = resultsWikitable .. "\n! rowspan=2 |Total\n|-"
for _, schemaRow in ipairs(electionSchema) do
if (_ ~= 1) then -- this is to avoid duplicating the county entry
resultsWikitable = resultsWikitable .. "\n ! # \n ! %"
end
end
for _, resultsRow in ipairs(electionResults) do
resultsLocal = "\n|-\n|" .. resultsRow[1] -- start of this row's table syntax and the row's name
rowTotal = sumRow(resultsRow)
for _, column in ipairs(resultsRow) do
if (_ ~= 1) then -- this is to avoid duplicating the county entry
resultsLocal = resultsLocal .. "||" .. column -- raw
resultsLocal = resultsLocal .. "||" .. math.floor(column*1000/rowTotal)/10 .. "%" -- one decimal of precision
end
end
resultsWikitable = resultsWikitable .. resultsLocal .. "||" .. rowTotal
end
return resultsWikitable .. "\n|}"
end
function p.electionTable(frame)
return electionTable(frame.args.data)
end
return p