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)
const leader = totals[0]
totals.forEach(d => {
d.votesBehindLeader = leader.votes - d.votes
})
return totals
}