Published
Edited
Nov 11, 2020
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
compareCandidates = (candidate1, candidate2) => {
const results1 = candidateResultsByCounty(candidate1)
const results2 = candidateResultsByCounty(candidate2)
const totals1 = totalVotesByCounty(results1[0].RaceName, results1[0].year)
const totals2 = totalVotesByCounty(results2[0].RaceName, results2[0].year)
return counties.map(county => {
const candidate1Votes = results1.find(d => d.CountyName === county).votes
const candidate1Total = totals1.find(d => d.county === county).totalVotes
const candidate2Votes = results2.find(d => d.CountyName === county).votes
const candidate2Total = totals2.find(d => d.county === county).totalVotes
return {
county: county,
candidate1Votes: candidate1Votes,
candidate1Percent: candidate1Votes / candidate1Total,
candidate2Votes: candidate2Votes,
candidate2Percent: candidate2Votes / candidate2Total,
difference: candidate2Votes - candidate1Votes,
differencePercent: (candidate2Votes / candidate2Total) - (candidate1Votes / candidate1Total),
// percentChange: (candidate2Votes - candidate1Votes) / candidate1Votes
}
})
}
Insert cell
// colorScales = {
// // From https://colorbrewer2.org/
// 'D': ['#f1eef6','#d0d1e6','#a6bddb', '#3690c0','#0570b0','#034e7b'],
// 'R': ['#fee5d9','#fcbba1','#fc9272','#ef3b2c','#cb181d','#99000d'],
// 'L': ['#edf8e9','#c7e9c0','#a1d99b','#41ab5d','#238b45','#005a32'],
// 'default': ['#f2f0f7', '#dadaeb', '#bcbddc', '#9e9ac8', '#756bb1', '#54278f'],
// }
Insert cell
voteShiftColor = d3.scaleLinear()
.domain([-5000,-1000,0,1000,5000])
.range(['#b35806','#f1a340','#fee0b6','#f7f7f7','#d8daeb','#998ec3','#542788'])
Insert cell
voteShiftColor(10)
Insert cell
demVotesColor = d3.scaleLinear()
.domain([0, 100, 500, 1000, 10000, 50000])
.range(['#f1eef6','#d0d1e6','#a6bddb', '#3690c0','#0570b0','#034e7b'])
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
totalVotesByCounty("UNITED STATES SENATOR","2018")
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
mergeInData(countyFeatures, candidateResultsByCounty("JON TESTER (D) - 2018"), 'NAMELABEL', 'CountyName')
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more