Skip to content
This repository was archived by the owner on Oct 20, 2022. It is now read-only.

Commit

Permalink
Add unaffiliated voters to partisan graph (#11)
Browse files Browse the repository at this point in the history
* Add unaffiliated voters to partisan graph

* Remove extra print

* Better handled of unaffiliated voters
  • Loading branch information
benweissmann authored Dec 23, 2020
1 parent 7ab4e98 commit 8ec2ebc
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 81 deletions.
26 changes: 18 additions & 8 deletions data.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,33 @@ def statewide_by_day():

return results


def statewide_by_party_day():
sql = """
SELECT * FROM cumulative_stats_by_party_day
WHERE days_before < 45 AND days_before >= (select '2021-01-05' - max(date(file_update_time - interval '18 hours')) from updated_times);
"""

results = {}
results['combined'] = {}
results["combined"] = {}
for result in db.engine.execute(text(sql)):
results.setdefault(result['party'] or '', {})[result['days_before']] = {
if result["party"] in ("D", "R"):
party = result["party"]
else:
party = "U"

results.setdefault(party, {})[result["days_before"]] = {
"total_general": int(result["total_returned_general"]),
"total_special": int(result["total_returned_special"]),
}
results['combined'].setdefault(result['days_before'], {
'total_general': 0, 'total_special': 0
})
results['combined'][result['days_before']]['total_general'] += int(result["total_returned_general"])
results['combined'][result['days_before']]['total_special'] += int(result["total_returned_special"])
results["combined"].setdefault(
result["days_before"], {"total_general": 0, "total_special": 0}
)
results["combined"][result["days_before"]]["total_general"] += int(
result["total_returned_general"]
)
results["combined"][result["days_before"]]["total_special"] += int(
result["total_returned_special"]
)

return results
return results
147 changes: 79 additions & 68 deletions static/homepage.js
Original file line number Diff line number Diff line change
@@ -1,53 +1,67 @@

(function () {
var daysBeforeSeries = Object.keys(window.HOMEPAGE_GRAPH_DATA['combined']);
var daysBeforeCol = ['Days Before'];
var combinedGeneral = ['combinedGeneral'];
var combinedRunoff = ['combinedRunoff'];
var demGeneral = ['demGeneral'];
var demRunoff = ['demRunoff'];
var repGeneral = ['repGeneral'];
var repRunoff = ['repRunoff'];
var daysBeforeSeries = Object.keys(window.HOMEPAGE_GRAPH_DATA["combined"]);
var daysBeforeCol = ["Days Before"];
var combinedGeneral = ["combinedGeneral"];
var combinedRunoff = ["combinedRunoff"];
var demGeneral = ["demGeneral"];
var demRunoff = ["demRunoff"];
var repGeneral = ["repGeneral"];
var repRunoff = ["repRunoff"];
var unaffiliatedGeneral = ["unaffiliatedGeneral"];
var unaffiliatedRunoff = ["unaffiliatedRunoff"];

daysBeforeSeries.forEach(function (daysBefore) {
// use negative value -- c3 automatically sorts ascending
daysBeforeCol.push(-daysBefore);
combinedGeneral.push(window.HOMEPAGE_GRAPH_DATA['combined'][daysBefore].total_general);
combinedRunoff.push(window.HOMEPAGE_GRAPH_DATA['combined'][daysBefore].total_special);
demGeneral.push(window.HOMEPAGE_GRAPH_DATA['D'][daysBefore].total_general);
demRunoff.push(window.HOMEPAGE_GRAPH_DATA['D'][daysBefore].total_special);
repGeneral.push(window.HOMEPAGE_GRAPH_DATA['R'][daysBefore].total_general);
repRunoff.push(window.HOMEPAGE_GRAPH_DATA['R'][daysBefore].total_special);
combinedGeneral.push(
window.HOMEPAGE_GRAPH_DATA["combined"][daysBefore].total_general
);
combinedRunoff.push(
window.HOMEPAGE_GRAPH_DATA["combined"][daysBefore].total_special
);
demGeneral.push(window.HOMEPAGE_GRAPH_DATA["D"][daysBefore].total_general);
demRunoff.push(window.HOMEPAGE_GRAPH_DATA["D"][daysBefore].total_special);
repGeneral.push(window.HOMEPAGE_GRAPH_DATA["R"][daysBefore].total_general);
repRunoff.push(window.HOMEPAGE_GRAPH_DATA["R"][daysBefore].total_special);
unaffiliatedGeneral.push(
window.HOMEPAGE_GRAPH_DATA["U"][daysBefore].total_general
);
unaffiliatedRunoff.push(
window.HOMEPAGE_GRAPH_DATA["U"][daysBefore].total_special
);
});

var graph = c3.generate({
bindto: "#homepage-graph",
data: {
x: "Days Before",
columns: [
daysBeforeCol, combinedGeneral, combinedRunoff
],
columns: [daysBeforeCol, combinedGeneral, combinedRunoff],
colors: {
combinedGeneral: '#6f42c1a0',
combinedRunoff: '#6f42c1',
demGeneral: '#007bffa0',
demRunoff: '#007bff',
repGeneral: '#dc3545a0',
repRunoff: '#dc3545'
combinedGeneral: "#6f42c1a0",
combinedRunoff: "#6f42c1",
demGeneral: "#007bffa0",
demRunoff: "#007bff",
repGeneral: "#dc3545a0",
repRunoff: "#dc3545",
unaffiliatedGeneral: "#6f42c1a0",
unaffiliatedRunoff: "#6f42c1",
},
regions: {
combinedGeneral: [ {style: 'dashed'} ],
demGeneral: [ {style: 'dashed'} ],
repGeneral: [ {style: 'dashed'} ]
combinedGeneral: [{ style: "dashed" }],
demGeneral: [{ style: "dashed" }],
repGeneral: [{ style: "dashed" }],
unaffiliatedGeneral: [{ style: "dashed" }],
},
names: {
combinedGeneral: "General 2020",
combinedRunoff: "Runoff 2021",
demGeneral: "Democrats General 2020",
demRunoff: "Democrats Runoff 2021",
repGeneral: "Republicans General 2020",
repRunoff: "Republicans Runoff 2021"
}
repRunoff: "Republicans Runoff 2021",
unaffiliatedGeneral: "Unaffiliated General 2020",
unaffiliatedRunoff: "Unaffiliated Runoff 2021",
},
},
axis: {
x: {
Expand All @@ -64,22 +78,20 @@
max: 4000000,
padding: {
top: 20,
bottom: 0
}
bottom: 0,
},
},
},
grid: {
x: {
lines: [
{ value: -23, text: "In person early voting starts" },
],
lines: [{ value: -23, text: "In person early voting starts" }],
},
y: {
lines: [
{
value: 4015475,
text: "4,015,475 Early Votes Cast In General",
position: 'middle'
position: "middle",
},
],
},
Expand All @@ -93,37 +105,36 @@
},
});

document.getElementById('partisan-toggle').addEventListener('click', function(e) {
if (e.toElement.checked) {
graph.load({
columns: [
daysBeforeCol,
demGeneral,
demRunoff,
repGeneral,
repRunoff
],
unload: [
'combinedGeneral',
'combinedRunoff'
]
});
graph.axis.max(undefined);
} else {
graph.load({
columns: [
daysBeforeCol,
combinedGeneral,
combinedRunoff
],
unload: [
'demGeneral',
'demRunoff',
'repGeneral',
'repRunoff'
]
});
graph.axis.max(4000000);
}
});
document
.getElementById("partisan-toggle")
.addEventListener("click", function (e) {
if (e.toElement.checked) {
graph.load({
columns: [
daysBeforeCol,
demGeneral,
demRunoff,
repGeneral,
repRunoff,
unaffiliatedGeneral,
unaffiliatedRunoff,
],
unload: ["combinedGeneral", "combinedRunoff"],
});
graph.axis.max(undefined);
} else {
graph.load({
columns: [daysBeforeCol, combinedGeneral, combinedRunoff],
unload: [
"demGeneral",
"demRunoff",
"repGeneral",
"repRunoff",
"unaffiliatedGeneral",
"unaffiliatedRunoff",
],
});
graph.axis.max(4000000);
}
});
})();
10 changes: 5 additions & 5 deletions templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,24 @@
sizes="16x16">
<link rel="mask-icon" href="https://www.voteamerica.com/img/safari-pinned-tab.svg"
color="#ff4400">
<meta name="description" content="Track your Georgia mail-in ballot">
<meta name="og:description" content="Track your Georgia mail-in ballot">
<meta name="description" content="Help make sure every voice in Georgia is heard">
<meta name="og:description" content="Help make sure every voice in Georgia is heard">
<meta name="theme-color" content="#fff">
<meta property="og:type" content="website">
<meta property="og:title" content="gaballot.org - Track your Georgia mail-in ballot">
<meta property="og:title" content="gaballot.org - Help make sure every voice in Georgia is heard">
<meta property="fb:app_id" content="218198872691029">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:site" content="@VoteAmerica">
<meta property="og:image"
content="https://s3.amazonaws.com/appforest_uf/f1605654010173x452403538515414600/turnout.png">
content="https://www.voteamerica.com/share/v3/generic.png">

<!-- Bootstrap CSS -->
<link rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
integrity="sha384-TX8t27EcRE3e/ihU7zmQxVncDAy5uIKz4rEkgIXeMed4M0jlfIDPvg6uqKI2xXr2"
crossorigin="anonymous">
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<link href="/static/styles.css?1" rel="stylesheet">
<link href="/static/styles.css" rel="stylesheet">

<title>VoteAmerica Georgia Ballot Tracker</title>
{% endblock %}
Expand Down

0 comments on commit 8ec2ebc

Please sign in to comment.