Published
Edited
Jan 12, 2022
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
office="Office"
Insert cell
locality=" Locality"
Insert cell
pollingplaces=FileAttachment("columbiaprecincts.json").json()
Insert cell
precincts = topojson.feature(pollingplaces, pollingplaces.objects[Object.keys(pollingplaces.objects)[0]])
Insert cell
projection = d3.geoIdentity()
.reflectY(true)
.fitSize([width, width* 1/3], precincts)
Insert cell
margin=new Map(data.map((d) => [d["precinct_id"].toString(), (d[candidates[0]] + d[candidates[1]]) == 0? 0.5 : d[candidates[1]]/(d[candidates[0]] +d[candidates[1]])]))
Insert cell
path = d3.geoPath(projection)
Insert cell
color = d3.scaleDiverging([0, 0.5, 1], d3.interpolatePuOr)
Insert cell
Type JavaScript, then Shift-Enter. Ctrl-space for more options. Arrow ↑/↓ to switch modes.

Insert cell
legend = g => {
const width = 240;

g.append("image")
.attr("width", width)
.attr("height", 8)
.attr("preserveAspectRatio", "none")
.attr("xlink:href", ramp(color.interpolator()).toDataURL());

g.append("text")
.attr("class", "caption")
.attr("y", -6)
.attr("fill", "#000")
.attr("text-anchor", "start")
.attr("font-weight", "bold")
.text(candidates[0] +"% (purple) vs. " + candidates[1] + "% (orange)" );

g.call(d3.axisBottom(d3.scaleLinear(color.domain(), [0, width / 2, width]))
.ticks(6)
.tickFormat(d => `+${(Math.abs((d-0.5) * 100)+50).toFixed(0)}%`)
.tickSize(13))
.select(".domain")
.remove();
}
Insert cell
data=d3.csvParse(await file.text(),d3.autoType)
Insert cell
results =
{
if(precinct==null)
return aq.from(data).select(candidates).fold(aq.all()

).pivot('key', { value: d => op.sum(d.value) }).objects()
else
return aq.from(data).filter(aq.escape(d=>d.precinct_id==precinct.properties.PCode)).select(candidates).objects()
}
Insert cell
file = FileAttachment("columbia@5.csv")
Insert cell
candidates = data.columns.slice(2)
Insert cell
import {vl} from "@vega/vega-lite-api"
Insert cell
import {Legend, Swatches} from "@d3/color-legend"
Insert cell
import {ramp} from "@mbostock/color-ramp"
Insert cell
import { aq, op } from '@uwdata/arquero'
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