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)))}`);
svg.append("g")
.selectAll("path")
.data(states.features)
.enter()
.append("path")
.attr("d", path)
.attr("stroke", "white")
.attr("stroke-width", 1)
.attr("fill", "none");
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 });
}