Published unlisted
Edited
Aug 13, 2021
Fork of Choropleth
Insert cell
Insert cell
Insert cell
Insert cell
color=colorCovid
Insert cell
Insert cell
// // No tooltip
// chart2 = {
// const svg = d3.create("svg")
// .attr("viewBox", [0, 0, 975, 610]);

// svg.append("g")
// .attr("transform", "translate(610,20)")
// .append(() => legend({color: colorCovid, title: data2Map.title, width: 260, tickFormat: ".1f"}));

// svg.append("g")
// .selectAll("path")
// .data(topojson.feature(us, us.objects.counties).features)
// .join("path")
// .attr("fill", d => colorCovid(data2Map.get(d.id)))
// .attr("d", path)
// .append("title")
// .text(d => `${d.properties.name}, ${states.get(d.id.slice(0, 2)).name}
// ${format(data2Map.get(d.id))}`);
// svg.append("path")
// .datum(topojson.mesh(us, us.objects.states, (a, b) => a !== b))
// .attr("fill", "none")
// .attr("stroke", "white")
// .attr("stroke-linejoin", "round")
// .attr("d", path);

// return svg.node();
// }
Insert cell
data=data2Map

Insert cell
viewof mytext = text()

Insert cell
chartToolTipCovid = {
const width = 975;
const height = 610;
const margin = 30;

const svg = d3
.create("svg")
.attr("viewBox", [-margin, 0, width + margin, height + margin]);

svg
.append("g")
.attr("transform", "translate(610,20)")
.append(() =>
legend({ color: colorCovid, title: data.title, width: 260, tickFormat: ".1f" })
);

svg
.append("g")
.attr("id", "map")
.selectAll("path")
.data(topojson.feature(us, us.objects.counties).features)
.join("path")
.attr("class", "county")
.attr("fill", d => colorCovid(data.get(d.id)))
.attr("d", path);

svg
.select("#map")
.append("path")
.attr("fill", "none")
.attr("stroke", "white")
.attr("d", path(topojson.mesh(us, us.objects.states, (a, b) => a !== b)));

const tooltip = svg.append("g");

svg
.selectAll(".county")
.on("touchmove mousemove", function(event, d) {
let message= ((data.get(d.id)===undefined) ?
`No Data
${d.properties.name}, ${states.get(d.id.slice(0, 2)).name}` :
`${format(data.get(d.id))}
${d.properties.name}, ${states.get(d.id.slice(0, 2)).name}`);
// Thanks to @mootari for the tip on the nodata for tooltip.
tooltip.call(
callout,
message
);
tooltip.attr("transform", `translate(${d3.pointer(event, this)})`);
d3.select(this)
.attr("stroke", "red")
.raise();
})
.on("touchend mouseleave", function() {
tooltip.call(callout, null);
d3.select(this)
.attr("stroke", null)
.lower();
})
.on("click", function(event, d) {
viewof mytext.setValue(d.id);
});

return svg.node();
}
Insert cell
import {text} from "@severo/inputs-setter"
Insert cell
data2Map.get("01001")
Insert cell
dataOrig
= Object.assign(new Map(d3.csvParse(await FileAttachment("unemployment-x.csv").text(), ({id, rate}) => [id, +rate])), {title: "Unemployment rate (%)"})
Insert cell
COVIDurl="https://raw.githubusercontent.com/youyanggu/covid19-infection-estimates-latest/main/counties/1_latest_percent_infected_counties.csv"
Insert cell
data2=d3.csv(COVIDurl)
Insert cell
data2fips_infect= data2.map( d => {
return [d.fips, Math.floor(Number(d.perc_current_infected_mean)*10000)/100]
}).filter( d => { return d[1]>0 })
// filter out the 0's
Insert cell
lodash.max(data2fips_infect.map(d => d[1]))
Insert cell
data2Map = Object.assign(mymap, {title: "Covid Rate (%)"})
Insert cell
mymap=new Map(data2fips_infect)
Insert cell
data2Map.get("title")
Insert cell
[ "51011", "51620", "01001" ].map(d=> [data2Map.get(d), data.get(d)])
Insert cell
colorOrig = d3.scaleQuantize([1, 10], d3.schemeBlues[9])
Insert cell
colorCovid = d3.scaleQuantize([0, 5], d3.schemeBlues[9])
Insert cell
path = d3.geoPath()
Insert cell
format = d => `${d}%`
Insert cell
states = new Map(us.objects.states.geometries.map(d => [d.id, d.properties]))
Insert cell
us = FileAttachment("counties-albers-10m.json").json()
Insert cell
topojson = require("topojson-client@3")
Insert cell
topojson.feature(us, us.objects.counties).features
Insert cell
d3 = require("d3@6")
Insert cell
import {legend} from "@d3/color-legend"
Insert cell
lodash = require("lodash")
Insert cell
import { callout } from "@d3/line-chart-with-tooltip"
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