Electoral College results and calculator

The Electoral College calculator was the Map Center’s first module, and it contains the Electoral College results of all presidential elections since 1964. Users can view the results of all of those elections and make predictions of the 2012 election.

This module lives in lib/map_center/modules/electoral_college.js.

Operation

This module has two different but related functionalities: a list of previous electoral results from elections since 1968 and a calculator allowing users to predict the electoral results of the 2012 election.

Historical results

The tab available to the user contains a dropdown list of all election years for which we have election results; selecting any of those options colors each state according to its electoral votes that year and shows the total number of electoral votes won by each candidate according to the number of electoral votes cast by each state in that year (including split votes and faithless electors where applicable).

Note

Because of the fact that the same module runs both this functionality and that of the calculator described in the next section, the module determines which functionality to use based on the presence of tabs. If any tab exists and contains a dropdown list (such as the one used for past years’ results), it shows historical results; otherwise, it acts as a calculator instead.

Calculator

This allows users to predict electoral results for the 2012 election. Each state has a click handler bound to it that lets users change the party winning that state’s electoral votes. How this works depends on the state:

  • Maine and Nebraska use the Congressional District Method to allocate their electoral votes. In practice, then, this means at least three electoral votes in each of those states will go to one candidate, but any remaining electoral votes in that state could be assigned to any other candidate.

    To let users predict scenarios in which either or both of these states can split votes, clicking one of these states brings up a dialog box that lets users assign these votes.

  • All other states and the District of Columbia award all of their electoral votes to the winner of the statewide popular vote, so clicking one of those areas toggles the winner in the following order: Republican, Democratic, tossup, no votes.

Changing any state’s vote updates the vote totals in the sidebar, including marking one candidate or the other as winning if that candidate has obtained a majority of electoral votes.

Architecture

This module has a lot of moving parts, so they’ve been integrated into a small publish/subscribe system.

The pub/sub system lives in the ecMap object in the module source. That object has the following methods:

  • on binds subscriber functions to published events. It takes the same arguments as jQuery.bind.

    The only event type is change, which fires whenever the map’s status is changed. To subscribe to this event:

    ecMap.on("change", function(event, status) {
        // Handler body goes here. status is a status object, described below.
    });
    
  • off unbinds subscriber functions. It takes the same arguments as jQuery.unbind.

  • set takes one argument: a new status object (described below) with which to replace the map’s entire internal status object.

  • reset takes no arguments and removes all currently set electoral votes.

  • get takes no arguments and returns a copy of the map’s current status object.

  • modifyVotes takes one argument: an object of changes to state electoral votes in order to modify totals without having to know the map’s entire current state.

    For example, if you just wanted to add five Democratic electoral votes for Nebraska:

    ecMap.modifyVotes({
        "Nebraska": {
            dem: 5,
            rep: 0,
            toss: 0
        }
    });
    

The status objects that are ultimately used all throughout this system have the following properties:

  • stateVotes is an object that contains the state-by-state electoral vote breakdown. The keys are full state names, and the values are objects with the keys dem, rep and toss and integer values with the electoral votes received by each political party in that state.
  • totals is simply an object with the keys dem, rep and toss and integer values with the total electoral votes received by each political party.

Data structure

The data for this module is stored in two objects toward the top of the module source:

  • electoralVotes is an object with election years as keys and objects as values. The object for each year contains:

    • states is an object with the number of electoral votes each state had in that election. The keys are full state names, and the values are numbers of votes.

    • republican, democratic and tossup are arrays containing the names of states whose full electoral votes went to each of those respective parties in that election.

      Note

      Throughout this module, tossup is used to refer both to predictions that keep states in play for either party and to third-party candidates receiving Electoral College votes such as George Wallace in 1968.

    • States that did not award their full electoral votes to any party in that election are not listed in the republican, democratic and tossup arrays; instead, they have their own objects in the election data (using their full state names as keys) containing their electoral vote breakdowns by party. For example, since Nebraska split its vote in 2008:

      electoralVotes["2008"]["Nebraska"] = {
          "republican": 4,
          "democratic": 1,
          "tossup": 0
      }
      

      Instances of faithless electors are also represented this way. For example, since an elector from the District of Columbia left her 2000 ballot blank in protest:

      electoralVotes["2000"]["District of Columbia"] = {
          "republican": 0,
          "democratic": 2,
          "tossup": 0
      }
      
  • candidateNames is an object with election years as keys and objects as values. The object for each year contains the names of the parties’ candidates participating in that election, using the same democratic, republican and tossup (where applicable) key names as before and string values with each candidate’s last name. For example, for the 2012 election:

    candidateNames["2012"] = {
        "democratic": "Obama",
        "republican": "Romney"
    }
    

Project Versions

Table Of Contents

Previous topic

Module reference

Next topic

Incumbent governors’ election results

This Page