Module:Sandbox/Elli/CountyResults

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 = "Michigan/Washtenaw" -- location or local fileFullPath = "Election results/County/" .. fileToGet .. ".tab" local electionData = mw.ext.data.get(fileFullPath) local electionResults = electionData["data"] resultsWikitable = "{| align=\"center\" border=\"2\" cellpadding=\"4\" cellspacing=\"0\" style=\"float:right; margin: 1em 1em 1em 0; border: 1px #aaa solid; border-collapse: collapse; font-size: 95%;\"\n|+ Presidential election results\n|- bgcolor=lightgrey\n! Year\n! Democratic\n! Republican\n! Third 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|-\n| style=\"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