Public
Edited
Jun 19, 2024
1 star
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
office="Columbia Mayoral Runoff"
Insert cell
locality = {
if(precinct==null)
return ""
else
return " in " + precinct.properties.Name}
Insert cell
pollingplaces=FileAttachment("columbiaprecincts (2)@1.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().padStart(3,0), (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.csv("https://data.shutdown.network/columbiarunoff/columbia.csv",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("columbiaprecincts (2).json")
Insert cell
updatedAt=moment(updateTime).format("h:mm A on MMMM D")
Insert cell
updateTime = fetch("https://data.shutdown.network/columbiarunoff/columbia.csv",{
method: 'GET'}).then(d=> {return d.headers.get("Last-modified")})
Insert cell
moment=require("moment")
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
FileAttachment("columbia@5.csv").csv()
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more