Public
Edited
Jun 16, 2023
Insert cell
Insert cell
Insert cell
Insert cell
map = {
const svg = d3.create("svg")
.attr("viewBox", [0, 0, 975, 610]);

svg.append(legend)
.attr("transform", "translate(870,450)");

var countyMap = svg.append("g")
.selectAll("path")
.data(counties.features)
.join("path")
.attr("fill", d => color(data.get(d.id+" "+dateToString(date))))
.attr("d", path)
.append("title")
.text(d => `${d.properties.name}, ${states.get(d.id.slice(0, 2)).name}
${format(data.get(d.id+" "+ dateToString(date)))}`);

// Draw the state boundaries (could do this in a cleaner way with topoJSON)
svg.append("g")
.selectAll("path")
.data(states.features)
.enter()
.append("path")
.attr("d", path)
.attr("stroke", "white")
.attr("stroke-width", 1)
.attr("fill", "none");

// Add legend
var legendGroup = svg.append("g")
.attr("transform", "translate("+50+","+50+")")
.attr("width", 400)
.attr("height", 60);

var legendElement = legendGroup.append(() => legend({
color,
title: var1_select,
ticks: bins,
tickFormat: ",.2f",
width: 400,
height: 60
}));

// Update the fill based on date scrubber
function update(date) {
countyMap.attr("fill", function(d) {
var fclass = data.get(d.id+" "+dateToString(date));
//console.log(d.id+" "+dateToString(date))
//console.log(fclass)
//console.log(data.get(d.id+" "+dateToString(date)))
//if (fclass) {
// var fcolor = color(fclass);
// } else {
// var fcolor = "#cccc";
// }
// return fcolor ;

});
}

// Function to handle legend hover
function handleLegendHover(d, i) {
// Get the corresponding bin range from the color scale
var range = color.range();
var binIndex = i;
var binColor = range[binIndex];

// Highlight the corresponding selection on the map
countyMap.attr("fill", function(d) {
var map_var = data.get(d.properties["COUNTY_FIPS"] + " " + dateToString(date));

if (map_var) {
var fcolor = (color(map_var) === binColor) ? binColor : "#cccc";
} else {
var fcolor = "#cccc";
}

return fcolor;
});
}

// Function to handle mouseover event
function handleMouseOver(d) {
// Example: Change fill color on hover
d3.select(this)
.attr("fill", "red");
// Show the tooltip
var tooltip = d3.select("body")
.append("div")
.attr("class", "tooltip")
.style("position", "absolute")
.style("opacity", 0)
.html("County: " + d.properties["NAME"] + "<br/>Data: " + d.var1_select)
.style("left", d3.event.pageX + "px")
.style("top", d3.event.pageY + "px");
tooltip.transition()
.duration(300)
.style("opacity", 0.9);
}

// Function to handle mouseout event
function handleMouseOut(d) {
// Example: Revert fill color after hover
d3.select(this)
.attr("fill", function(d) {
var map_var = data.get(d.properties["COUNTY_FIPS"] + " " + dateToString(date));

if (map_var) {
var fcolor = color(map_var);
} else {
var fcolor = "#cccc";
}

return fcolor;
});
// Remove the tooltip
d3.select(".tooltip").remove();
}

// Function to handle legend click
function handleLegendClick(d, i) {
var binInfoBox = legendGroup.select(".bin-info-box");
if (binInfoBox.empty()) {
binInfoBox = legendGroup
.append("text")
.attr("class", "bin-info-box")
.attr("x", 500)
.attr("y", 30)
.style("font-size", "12px")
.style("fill", "black")
.text("You are viewing the bin information of this legend");
} else {
binInfoBox.remove();
}
}

// Attach event listeners to the legend elements
legendElement.selectAll("rect")
.on("mouseover", handleLegendHover)
.on("mouseout", handleLegendMouseOut)
.on("click", handleLegendClick);

return Object.assign(svg.node(), { update });
}

Insert cell
legend = () => {
const k = 24;
const arrow = DOM.uid();
return svg`<g font-family=sans-serif font-size=10>
<g transform="translate(-${k * n / 2},-${k * n / 2}) rotate(-45 ${k * n / 2},${k * n / 2})">
<marker id="${arrow.id}" markerHeight=10 markerWidth=10 refX=6 refY=3 orient=auto>
<path d="M0,0L9,3L0,6Z" />
</marker>
${d3.cross(d3.range(n), d3.range(n)).map(([i, j]) => svg`<rect width=${k} height=${k} x=${i * k} y=${(n - 1 - j) * k} fill=${colors[j * n + i]}>
<title>${data.title[0]}${ labelsRR[j]&&` (${labelsRR[j]})` }
${data.title[1]}${labels[i] && ` (${labels[i]})`}</title>
</rect>`)}
<line marker-end="${arrow}" x1=0 x2=${n * k} y1=${n * k} y2=${n * k} stroke=black stroke-width=1.5 />
<line marker-end="${arrow}" y2=0 y1=${n * k} stroke=black stroke-width=1.5 />
<text font-weight="bold" dy="0.71em" transform="rotate(90) translate(${n / 2 * k},6)" text-anchor="middle">${data.title[0]}</text>
<text font-weight="bold" dy="0.71em" transform="translate(${n / 2 * k},${n * k + 6})" text-anchor="middle">${data.title[1]}</text>
</g>
</g>`;
}
Insert cell
md`## Bivariate variables`
Insert cell
labels = ["lowest", "low","medium", "high"]
Insert cell
labelsRR= ["Group 1", "Group 2","Group 3", "Group 4"]
Insert cell
n = Math.floor(Math.sqrt(colors.length))
Insert cell
//x = d3.scaleLinear([0.053, 0.128,0.295 ,1000], d3.range(n))
x = d3.scaleQuantile(Array.from(data.values(), d => d[0]), d3.range(n))
Insert cell
//y = d3.scaleLinear([0.053, 0.128,0.295 ,1000], d3.range(n))
//y = d3.scaleQuantile(Array.from(data.values(), d => d[1]), d3.range(n))
y = d3.scaleQuantile([1,14], d3.range(n))
Insert cell
color = {
return value => {
if (!value) return "#ccc";
let [a, b] = value;
return colors[y(b) + x(a) * n];
};
}
Insert cell
format = (value) => {
if (!value) return "N/A";
let [a, b] = value;
return `${data.title[0]} ${a} ${labelsRR[x(a)] && ` (${labelsRR[x(a)]})`}
${data.title[1]} ${b} ${labels[y(b)] && ` (${labels[y(b)]})`}`;
}
Insert cell
schemes = [
{
name: "RdBu",
colors: [
"#D3CFE6", "#A5BEDB", "#76ADD0","#489CC5",
"#D0A2CE", "#A395C5", "#7588BA","#477AB1",
"#CD74B5", "#A06BAD", "#7361A4","#46589C",
"#CA479E", "#9E4196", "#713B8F","#453687"
]
},
{
name: "RdBu",
colors: [
"#D3CFE6", "#A5BEDB", "#76ADD0","#489CC5",
"#D0A2CE", "#A395C5", "#7588BA","#477AB1",
"#CD74B5", "#A06BAD", "#7361A4","#46589C",
"#CA479E", "#9E4196", "#713B8F","#453687"
]
}
]
Insert cell
//states = new Map(us.objects.states.geometries.map(d => [d.id, d.properties]));
//states = new Map(us.objects.states.geometries.map(d => [d.id, d.properties]));

Insert cell
viewof colors = {
const form = html`<form style="display: flex; align-items: center; min-height: 33px; font: 12px sans-serif;"><label><select name=i>${schemes.map(c => Object.assign(html`<option>`, {textContent: c.name}))}</select><span style="margin-left: 0.5em;">color scheme</span></label></form>`;
form.i.selectedIndex = 1;
form.oninput = () => form.value = schemes[form.i.selectedIndex].colors;
form.oninput();
return form;
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
states = JSON.parse(JSON.stringify(rawStates))
Insert cell
states.features = stateFeatures
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
//color = d3.scaleQuantile(d3.schemeRdPu[bins]).domain(clean.filter(d => d[1] === dates[dates.length-1]).map(d => d[2]));
Insert cell
Insert cell
iowaProjection = d3.geoAlbers()
.center([0, 41.5])
.rotate([93, 0])
.parallels([29.5, 45.5])
.scale(6000)
.translate([width / 2, height / 2]);
Insert cell
usStatesData = await d3.json('https://d3js.org/us-10m.v1.json');
Insert cell
path = d3.geoPath().projection(iowaProjection);
Insert cell
projection = d3.geoAlbersUsa()
.translate( [width/2, height/2] );
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
md
` ## Import Useful Tools
Import legend and date scrubber from Mike Bostok, and a general purpose slider from Jeremy Ashkenas.
`
Insert cell
//import {legend} from "@d3/color-legend"
Insert cell
Insert cell
Insert cell
Insert cell
function dateToString(date) {
var year = date.getFullYear();
var month = ("0" + (date.getMonth() + 1)).slice(-2);
var day = ("0" + date.getDate()).slice(-2);
return `${year}-${month}-${day}`;
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
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