Public
Edited
May 10
Insert cell
Insert cell
Insert cell
md`#### Chun Hang Chan`
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
format = d => `${d}%`
Insert cell
Insert cell
iowa = FileAttachment("PMHDShapeFileProjected.json").json()
Insert cell
counties = topojson.feature(iowa, iowa.objects.PMHDShapeFileProjected)
Insert cell
csv_data = d3.csvParse(await FileAttachment("PMHDShapeFileProjected.csv").text(),({FIPS, County_Val, CountyName}) => [+FIPS, +County_Val, CountyName])
Insert cell
data = Object.assign(new Map(csv_data), {title: "Menatlly Unhealth Days Experienced by Adults Per Month in Iowa"})
Insert cell
data.get(19119)
Insert cell
UnHealthyDays = Array.from(csv_data.values(), d => d[1])
Insert cell
Oranges = [d3.color("#fff5eb"), d3.color("#fed9a6"), d3.color("#fd8d3c"), d3.color("#e6550d"), d3.color("#a63603")]


Insert cell
naturalbreaks = simple.ckmeans(UnHealthyDays, Oranges.length).map(v => v.pop())
Insert cell
//more information on sequential scales: https://observablehq.com/@d3/sequential-scales
// color = d3.scaleSequentialQuantile([...data.values()], d3.interpolateBlues)

// color = d3.scaleQuantile()
// .domain(med_age)
// .range()

color = d3.scaleThreshold()
.domain(naturalbreaks)
.range(Oranges)
Insert cell
width = 975
Insert cell
height = 610
Insert cell
margin = 50
Insert cell
//Rotate the map sets the longitude of origin for our UTM Zone 15N projection.
//projection = d3.geoTransverseMercator().rotate([94,0]).fitExtent([[margin, margin], [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.geoTransverseMercator().rotate([94,0]).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(365,0)")
.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){
// console.log(color(data.get(d.properties.FIPS)[0]))
// return color(data.get(d.properties.FIPS)[0]);
// })
.attr("fill", d => color(data.get(+d.properties.FIPS)))
.attr("d", path)
.append("title")
.text(d => " Mentally Unhealthy Days per Month: " + data.get(+d.properties.FIPS));

return svg.node();
}
Insert cell
Type Markdown, then Shift-Enter. Ctrl-space for more options. Arrow ↑/↓ to switch modes.

Insert cell
data.get(19005)
Insert cell
color(data.get(19005))
Insert cell
color(40)
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