Published
Edited
Mar 1, 2022
Importers
13 stars
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
colorChangeRamp(d3.interpolateRdBu)
Insert cell
colorChangeRamp(interpolateFiveThirtyEightRdBu)
Insert cell
colorChangeRamp(interpolateFiveThirtyEightRdPuBu)
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
interpolateFiveThirtyEightRdBu = d3.interpolateRgbBasis(
schemeFiveThirtyEightRdBu
)
Insert cell
schemeFiveThirtyEightRdBu = [
...['#af3f38', '#fa5a50', '#ff998a', '#ffcec5'], // reds, dark to light
'#eae3ea', // gray
...['#c8cae3', '#9197c8', '#5768ac', '#3d4978'] // blues, light to dark
]
Insert cell
Insert cell
interpolateFiveThirtyEightRdPuBu = d3.interpolateRgbBasis(
schemeFiveThirtyEightRdPuBu
)
Insert cell
schemeFiveThirtyEightRdPuBu = [
"#e85847", // red
"#8265ae", // purple
"#4591e3" // blue
]
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
marginScale = d3.scaleDiverging(colors).domain([-maxMargin, 0, maxMargin])
Insert cell
viewof maxMargin = Inputs.range([1, 100], {
step: 1,
value: 80,
label: "Max Margin"
})
Insert cell
viewof maxMargin2 = Inputs.range([1, 100], {
step: 1,
value: 30,
label: "Max Margin"
})
Insert cell
viewof colors = Inputs.select(
new Map([
['d3.interpolateRdBu', d3.interpolateRdBu],
['interpolateFiveThirtyEightRdBu', interpolateFiveThirtyEightRdBu],
['interpolateFiveThirtyEightRdPuBu', interpolateFiveThirtyEightRdPuBu]
]),
{ value: interpolateFiveThirtyEightRdBu, label: "Color Interpolator" }
)
Insert cell
viewof colors2 = Inputs.select(
new Map([
['d3.schemeRdBu', d3.schemeRdBu[11]],
['schemeFiveThirtyEightRdBu', schemeFiveThirtyEightRdBu],
['schemeFiveThirtyEightRdPuBu', schemeFiveThirtyEightRdPuBu]
]),
{ value: schemeFiveThirtyEightRdPuBu, label: "Color Scheme" }
)
Insert cell
counties = topojson
.feature(us, us.objects.counties)
.features.map(county => {
const countyData = countyMap[county["id"]];
if (!countyData) return null;
return {
...county,
margin: countyData
};
})
.filter(d => d)
Insert cell
stateData = nytData.data.races.map(s => {
return {
name: s.state_name,
abbr: s.state_id,
countyCount: s.counties.length,
margin:
s.leader_margin_value * (s.leader_party_id == "republican" ? -1 : 1),
votes: s.votes
};
})
Insert cell
countyMap = {
const map = {};
nytData.data.races.forEach(s => {
s.counties.forEach(c => {
map[c.fips] =
c.leader_margin_value * (c.leader_party_id == "republican" ? -1 : 1);
});
});
return map;
}
Insert cell
Insert cell
nytData = d3.json(
"https://static01.nyt.com/elections-assets/2020/data/api/2020-11-03/national-map-page/national/president.json"
)
Insert cell
import { PINNED } from '@mootari/signature'
Insert cell
import { us, states } from '@mbostock/ancestry-by-u-s-county'
Insert cell
topojson = require("topojson-client@3")
Insert cell
import { ramp as colorChangeRamp } from '@fil/perceptually-uniform'
Insert cell
import { ramp } from '@mbostock/color-ramp'
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