Public
Edited
Nov 20, 2022
1 star
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
LineChart(cumulative_vote_share, {
x: d => d.date,
y: d => d.share,
z: d => d.choice,
yLabel: "Vote balance",
yFormat: "%",
yDomain: d3.extent(cumulative_vote_share, d => (d.share - 0.5) * 1.20 + 0.5),
width,
strokeWidth: 3,
strokeLinecap: "round",
strokeLinecap: "round",
height: 500,
color: z => choice_color[z],
marginLeft: 50
})
Insert cell
Insert cell
cumulative_race_data = race_data.filter(d => d.value).map(d => ({
...d,
value: d3.sum(race_data.filter(o => d.choice == o.choice && d.date >= o.date && d.value).map(o => o.value))
}))
Insert cell
cumulative_vote_share = cumulative_race_data.map(d => ({
...d,
total: d3.sum(cumulative_race_data.filter(o => d.formattedDate == o.formattedDate).map(o => o.value))
})).map(d => ({...d, share: d.value / d.total}))
Insert cell
colors = ["#cc00cc", "#00cc66"]
Insert cell
choice_color = [... new Set(race_data.map(d => d.choice))].reduce((agg, cur, i) => Object.assign(agg, {[cur]: colors[i]}), {})
Insert cell
viewof legend = Swatches(d3.scaleOrdinal().domain(Object.keys(choice_color)).range(colors))
Insert cell
first_data = data.filter(d =>
d.contest == contest_selection.contest &&
d.jurisdiction == contest_selection.jurisdiction &&
d.type == "Votes" &&
d.factor == "OVERALL" &&
d.date <= (new Date("November 8 2022"))
)
.map(d => ({...d, formattedDate: d3.timeFormat("%b. %d")(d.date), factor: "CURRENT"}))
.filter(d => d.value)
Insert cell
race_data = first_data.concat(data.filter(d =>
d.contest == contest_selection.contest &&
d.jurisdiction == contest_selection.jurisdiction &&
d.type == "Votes" &&
d.factor == "CURRENT"
)
.map(d => ({...d, formattedDate: d3.timeFormat("%b. %d")(d.date)}))
.filter(d => d.value))
Insert cell
contest_selection
Insert cell
Insert cell
workbook = november2022LaCountyElection4300//FileAttachment("google://8 NOVEMBER 2022 - LA County - Election 4300").xlsx()
Insert cell
workbook.sheetNames
Insert cell
Inputs.table(
workbook.sheet(1, {
headers: false,
// range: "A1:J10"
})
)
Insert cell
Insert cell
Insert cell
h1 = workbook.sheet(1)[1]
Insert cell
h2 = workbook.sheet(1)[2]
Insert cell
h3 = workbook.sheet(1)[3]
Insert cell
data = [1,2,4,5].map(d => {
let sheet = workbook.sheet(d)
return sheet.map(row => {
let column_keys = Object.keys(h1)

let contest = row["B"]
let choice = row["D"]

// make an object for each data point
return column_keys.map((col, i) => ({
"date": new Date(h1[col].split("- ").pop() + " 2022"),
"jurisdiction": workbook.sheetNames[d],
"contest": contest,
"choice": choice,
"factor": h2[col],
"type": h3[col],
"value": row[col] == "N/A" ? undefined : row[col],
}))
})})
.flat(Infinity)
.filter(d => d.contest)
.sort((a,b) => d3.ascending(a.date, b.date))
Insert cell
contests = [...new Set(data.map(d => JSON.stringify({jurisdiction: d.jurisdiction, contest: d.contest})))].map(JSON.parse)
Insert cell
import {Swatches} from "@d3/color-legend"
Insert cell
import {LineChart} from "@d3/multi-line-chart"
Insert cell
import {GroupedBarChart} from "@d3/grouped-bar-chart"
Insert cell
race_data
Insert cell
november2022LaCountyElection4300 = FileAttachment("8 NOVEMBER 2022 - LA County - Election 4300 (3).xlsx").xlsx()
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