Module:Sandbox/Elli/CountyResults explained

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 totalend

function electionTable(data) local fileToGet = "Michigan/Washtenaw" -- location or local fileFullPath = "Election results/County/" .. fileToGet .. ".tab" local electionData = mw.ext.data.get(fileFullPath) local electionResults = electionData["data"] resultsWikitable = "

Presidential election results\n
- bgcolor=lightgrey\n! Year\nDemocratic\n! Republican\nThird Parties" for _, resultsRow in ipairs (electionResults) do year = resultsRow[1] demVotes = resultsRow[2] repVotes = resultsRow[3] otherVotes = resultsRow[4] -- winner is 0 for dems, 1 for republicans, -1 if tied if demVotes > repVotes then winner = 0 elseif repVotes > demVotes then winner = 1 else winner = -1 end totalVotes = demVotes + repVotes + otherVotes demPct = string.format("%.1f%%", (demVotes/totalVotes)*100) repPct = string.format("%.1f%%", (repVotes/totalVotes)*100) otherPct = string.format("%.1f%%", (otherVotes/totalVotes)*100) resultsRowWikitext = "\n-\nstyle=\"text-align:center;\" " .. tostring(year) .. "\n" resultsRowWikitext = resultsRowWikitext .. "style=\"text-align:center;\" " if winner

0 then resultsRowWikitext = resultsRowWikitext .. "" .. demPct .. "" else resultsRowWikitext = resultsRowWikitext .. demPct end resultsRowWikitext = resultsRowWikitext .. " " .. tostring(demVotes) .. "\n" resultsRowWikitext = resultsRowWikitext .. "

style=\"text-align:center;\" " if winner

1 then resultsRowWikitext = resultsRowWikitext .. "" .. repPct .. "" else resultsRowWikitext = resultsRowWikitext .. repPct end resultsRowWikitext = resultsRowWikitext .. " " .. tostring(repVotes) .. "\n" resultsRowWikitext = resultsRowWikitext .. "

style=\"text-align:center;\" " resultsRowWikitext = resultsRowWikitext .. otherPct resultsRowWikitext = resultsRowWikitext .. " " .. tostring(otherVotes) .. "\n" resultsWikitable = resultsWikitable .. resultsRowWikitext end return resultsWikitable .. "
"end

function p.electionTable(frame) return electionTable(frame.args.data)end

return p