Open main menu
Home
Random
Donate
Recent changes
Special pages
Community portal
Preferences
About Stockhub
Disclaimers
Search
User menu
Talk
Contributions
Create account
Log in
Editing
Module:Sandbox/Elli/ResultsTable
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
-- 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
Summary:
Please note that all contributions to Stockhub may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Stockhub:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Templates used on this page:
Template:Module other
(
edit
)
Template:Module rating
(
edit
)
Template:Ombox
(
edit
)
Module:Arguments
(
edit
)
Module:Message box
(
edit
)
Module:Message box/configuration
(
edit
)
Module:Message box/ombox.css
(
edit
)
Module:Sandbox/Elli/ResultsTable/doc
(
edit
)
Module:Yesno
(
edit
)