Published
Edited
Apr 21, 2020
Insert cell
Insert cell
Insert cell
d3 = require("d3@5", "d3-svg-legend@2")
Insert cell
function unique(array) {
return array.filter((v, i) => array.indexOf(v) === i);
}
Insert cell
format = d => `${d}%`
Insert cell
topojson = require("topojson-client@3")
Insert cell
featureCollection = FileAttachment("prec2018_wgs84.json").json()
Insert cell
isarithmicPolygons = topojson.feature(featureCollection, featureCollection.objects.prec2018_wgs84)
Insert cell
polygonValues = {
const values = []
for (let i = 0; i < isarithmicPolygons.features.length; i++) {
let d = isarithmicPolygons.features[i];
values[i] = d.properties.range;
}
return values;
}
Insert cell
uniqueValues = unique(polygonValues.sort())
Insert cell
//load iowa counties reference dataset
iowa = FileAttachment("iowa_counties_topo@1.json").json()
Insert cell
counties = topojson.feature(iowa, iowa.objects.iowa_counties)
Insert cell
projection_counties = d3.geoTransverseMercator().rotate([94,0]).fitExtent([[80, 80], [width, height]], counties);
Insert cell
path = d3.geoPath().projection(projection_counties);
Insert cell
colors = d3.quantize(d3.interpolateHcl("#fafa6e", "#2A4858"), uniqueValues.length)
Insert cell
color = d3.scaleOrdinal(polygonValues.sort(), colors)
Insert cell
margin = 20
Insert cell
width = 975
Insert cell
height = 610
Insert cell
legend = d3.legendColor()
.shapeWidth(110)
.orient("horizontal")
.scale(x)
// .title("Avg yearly precipitation in 2018")
Insert cell
x = d3.scaleOrdinal(legendValues, colors)
Insert cell
legendValues = {
const values = []
for (let i = 0; i < uniqueValues.length; i++) {
let d = uniqueValues[i];
let d2 = d.split(" - ")
let startVal = Math.round(d2[0] * 100) / 100
let endVal = Math.round(d2[1] * 100) / 100
if(i > 0)
startVal += 0.01
startVal = Math.round(startVal * 100) / 100
values[i] = startVal + " - " + endVal
}
return values;
}
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