Public
Edited
Mar 6
5 forks
3 stars
Insert cell
md`# Dealing with null values
2019 Yield of crop
bushel per acre`
Insert cell
d3 = require("d3@5")
Insert cell
import {legend} from "@d3/color-legend"
Insert cell
simple = require("simple-statistics@7.0.7/dist/simple-statistics.min.js")
Insert cell
format = d => `${d}%`
Insert cell
topojson = require("topojson-client@3")
Insert cell
iowacount = FileAttachment("countiesandcropiowa.json").json()
Insert cell
counties = topojson.feature(iowacount, iowacount.objects.countiesandcropiowa)
Insert cell
csv_data = d3.csvParse(await FileAttachment("iowacropcountjoin.csv").text(),({NAME, cropdatano__Yield_2019}) => [NAME, +cropdatano__Yield_2019])
Insert cell
data = Object.assign(new Map(csv_data), {title: "Bushels per Acre"})
Insert cell
//Array(99).filter(d => d==0)
//Array(99).filter(d=>0)
//Yield_2019.object.array(99).filter(d => d==0)
//Yieldthisone=domain[d3.min(50),d3.max(300)]
Insert cell
//Yield_2019 = Array.from(csv_data.values(), d => d[1]).filter(d => d > 0)
Insert cell
Yield_2019 = Array.from(csv_data.values(), d => d[1]).filter(d => !isNaN(d))
Insert cell
d3.min(Yield_2019)
Insert cell
YlGnBu = [d3.color("#ffffcc"), d3.color("#a1dab4"), d3.color("#41b6c4"), d3.color("#2c7fb8"),d3.color("#253494")]
Insert cell
naturalbreaks = simple.ckmeans(Yield_2019, YlGnBu.length).map(v => v.pop())
Insert cell
nancolor = d3.color("gray")
Insert cell
nullcolorforlegend = d3.scaleOrdinal()
.domain([NaN])
.range([nancolor])
Insert cell
//more information on sequential scales: https://observablehq.com/@d3/sequential-scales
//color = d3.scaleSequentialQuantile([...data.values()], d3.interpolateBlues)

//color = d3.scaleQuantile()
//.domain(Yield_2019)
//.range(["#ffffcc","#a1dab4","#41b6c4","#2c7fb8","#253494"])

color = d3.scaleThreshold()
.domain(naturalbreaks)
.range(YlGnBu)
//color= d3.scaleQuantize()
//.domain(["])
//.range(["#ffffcc", "#cbc9e2", "#9e9ac8","#756bb1","#54278f"])
Insert cell
width = 975
Insert cell
height = 610
Insert cell
margin = 100
Insert cell
//Rotate the map sets the longitude of origin for our UTM Zone 15N projection.
projection = d3.geoTransverseMercator().rotate([94,0]).fitExtent([[80, 80], [width, height]], counties);
//d3 reference for projections: https://d3js.org/d3-geo

//use the following url for specific projection settings: https://github.com/veltman/d3-stateplane
//Use this code to set up the map projection (if different than geographic projection)

//projection = d3.geoAlbers().fitExtent([[margin, margin], [width - margin, height - margin]], counties)

//projection = d3.geoMercator().fitExtent([[margin, margin], [width - margin, height - margin]], counties)
Insert cell
//Using a path generator to project geometry onto the map
path = d3.geoPath().projection(projection);
Insert cell
choropleth = {
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height]);

svg.append("g")
.attr("transform", "translate(650,20)")
.append(() =>
legend({
color: nullcolorforlegend,
title: "No Data",
width: 50,
tickFormat: ".1f"
})
);
svg.append("g")
.attr("transform", "translate(360,20)")
.append(() =>
legend({
color: color,
title: data.title,
width: 260,
tickFormat: ".1f"
})
);

svg.append("g")
.selectAll("path")
.data(counties.features)
.join("path")
.attr("stroke", "white")
.attr("stroke-linejoin", "round")
.attr("stroke-width", 1)
.attr("fill", function(d){
if(data.get(d.properties.NAME) > 0){
return color(data.get(d.properties.NAME));
}
else{
return nancolor;
}
})
// .attr("fill", d => color(data.get(d.properties.NAME)[0]))
.attr("d", path)
.append("title")
.text(d => " Yield: " + data.get(d.properties.NAME));
svg.selectAll(".subunit-label")
.data(counties.features)
.enter().append("text")
.attr("class", function(d) { return "subunit-label " + d.id; })
.attr("transform", function(d) { return "translate(" + path.centroid(d) + ")"; })
.attr("fill", function(d){
var value = data.get(d.properties.NAME);
if(value > 0 && value < 210){
return "black";
}
else{
return "white";
}
})
.attr("fill-opacity", "1")
.attr("font-size", "10px")
.attr("font-weight", "300")
.attr("text-anchor", "middle")
.text(function(d) {
return d.properties.NAME;
})

return svg.node();
}
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