Published
Edited
Jun 2, 2020
Importers
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
houseGOP2018Data = fetchData({
key: '2018 Dem. House primary',
type: 'HOUSE',
category: 'SW',
raceId: '0',
osn: '0', // office code
county: '0',
party: 'REP',
electionID: '16',
})
Insert cell
Insert cell
makeTotals = raw => {
const candidateNames = Array.from(new Set(raw.map(d => d.calcCandidate)))
const totals = candidateNames.map(name => {
const matches = raw.filter(d => d.calcCandidate === name)
const candidateVotes = d3.sum(matches, d => d.calcCandidateVotes)
const totalVotes = d3.sum(raw, d => d.calcCandidateVotes)
return {
name: name,
party: matches && matches[0].PartyCode[0],
votes: candidateVotes,
percent: candidateVotes / totalVotes,
precincts: matches[0].TotalPrecincts,
precinctsComplete: matches[0].PrecinctsReporting,
precinctsPartial: matches[0].PrecinctsPartial,
raceVotesIn2016: 'TK'
}
}).sort((a,b) => b.votes - a.votes)
// Add votes behind leader
const leader = totals[0]
totals.forEach(d => {
d.votesBehindLeader = leader.votes - d.votes
})

return totals
}
Insert cell
makeTotals(raceDataRaw)
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
tableStyle = html`<style>
.container {
font-family: Arial, sans-serif;
margin-bottom: 1em;
max-width: 600px;
}
.fig-title {
font-size: 1.1em;
font-weight: bold;
margin-bottom: 0.3em;
}
.note {
font-size: 0.9em;
line-height: 1em;
font-style: italic;
margin-bottom: 0.5em;
}
th {
font-weight: normal;
font-style: italic;
}
th.num, td.num {
text-align: right;
width: 20%;
}
.icon {
display: inline-block;
width: 12px;
height: 12px;
margin-right: 4px;
background-color: #666;
}
</style>
`
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
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