Published
Edited
Aug 20, 2022
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
map = {
const width = 1200
const height = 600
const margin = 10


// create SVG element
let svg = d3.select(DOM.svg(width, height));
const path = d3.geoPath();

// Add the text box for State
svg.append("text")
.attr("x", margin*14)
.attr("y",height-30)
.attr("dy", ".35em")
.text();
const aa = svg.append("g");
const countyMap = aa.append("g")
.attr("transform","translate("+margin+","+margin+")")
.selectAll("path")
.data(topojson.feature(us, us.objects.counties).features)
.join("path")
.attr("fill", d => color(data.get(d.id+" "+dateToString(date))))
.attr("d", path)
.on("mouseover", function(d) {
d3.select(this)
.attr("stroke" , "orange")
})
.on("mouseout", function(d) {
d3.select(this)
.attr("stroke" , "null")
})
.on('click', function(e, d) {
d3.selectAll("path")
.style('stroke', null);
d3.select(this)
.style('stroke', 'orange');
svg.select("text").text(`${d.properties.name}, ${states.get(d.id.slice(0, 2)).name}, ID: ${d.id}`);
})
.append("title")
.text(d => `${d.properties.name}, ${states.get(d.id.slice(0, 2)).name}
${format(data.get(d.id+" "+ dateToString(date)))}`);

svg.append(legend)
.attr("transform", "translate(100,500)");
//.attr("transform", "translate(870,450)");
aa.append("path")
.attr("transform","translate("+margin+","+margin+") ")
//.datum(topojson.mesh(us, us.objects.states, (a, b) => a !== b))
.attr("fill", "none")
.attr("stroke", "white")
.attr("stroke-linejoin", "round")
.attr("d", path(topojson.mesh(us, us.objects.states, (a, b) => a !== b)));
// 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 ;

});
}
var zoom = d3.zoom()
.scaleExtent([1, 8])
.on('zoom', zoomed);
function zoomed(event) {
const {transform} = event;
aa.attr("transform", transform);
aa.attr("stroke-width", 1 / transform.k);
}
aa.call(zoom);
//return svg.node();
return Object.assign(svg.node(), {update});
}
Insert cell
Insert cell
Insert cell
datestest = {
const data = await d3.csv("https://raw.githubusercontent.com/YuLan1014/covid/main/allcluster.csv", d => {
return d.date;
});
return [...new Set(data)];
}
Insert cell
dates = {
const data = await FileAttachment("allcluster.csv").csv();
var dd = data.map(function(d) { return d.date; });
return [...new Set(dd)];

}
Insert cell
testww = FileAttachment("allcluster.csv").csv()
Insert cell
rrmap= {
const testww = await FileAttachment("allcluster.csv").csv();
return new Map(testww.map(({id,rr_c,rr_l,date}) => [(id+ " "+date), [+rr_c, +rr_l]]))
}
Insert cell
data = Object.assign(
rrmap
,{title: ["RR of cluster", "RR of county"]},)

Insert cell
datatest = Object.assign(new Map(
// await d3.csv("https://raw.githubusercontent.com/YuLan1014/covid/main/allgis_res_2021.csv", ({ID,RR_C,RR_L,date}) => [(ID+ " "+date), [+RR_C, +RR_L]])),
await d3.csv("https://raw.githubusercontent.com/YuLan1014/covid/main/allcluster.csv", ({id,rr_c,rr_l,date}) => [(id+ " "+date), [+rr_c, +rr_l]])),
{title: ["RR of cluster", "RR of county"]}
)

//number of days within the cluster
//https://raw.githubusercontent.com/YuLan1014/Vis/master/biv_1017.csv

//number of days within the cluster with the thresholde of 14 days (rr>0)
//https://raw.githubusercontent.com/YuLan1014/Vis/master/biv14_1001.csv

//number of days within the cluster with the thresholde of 14 days (rr>1)
//https://raw.githubusercontent.com/YuLan1014/Vis/master/biv14RR1_1001.csv
//test
//https://raw.githubusercontent.com/YuLan1014/Vis/master/test.csv
Insert cell
labelsRR= ["Group 1", "Group 2","Group 3", "Group 4"]
Insert cell
labels = ["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
path = d3.geoPath()
Insert cell
color = {
return value => {
if (!value) return "#ccc";
let [a, b] = value;
return colors[y(b) + x(a) * n];
};
}
Insert cell
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
//clean.filter(d => d[1] === dates[dates.length-1]).map(d => d[2])
Insert cell
//d => d[1] === dates[dates.length-1]
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
//color = d3.scaleQuantile(d3.schemeRdPu[bins]).domain(clean.filter(d => d[1] === dates[dates.length-1]).map(d => d[2]));
Insert cell
//path = d3.geoPath().projection(projection);
Insert cell
//projection = d3.geoAlbersUsa().ranslate( [width/2, height/2] );
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
us = FileAttachment("counties-albers-10m@9.json").json()
Insert cell
topojson = require("topojson-client@3")
Insert cell
d3 = require("d3@6")
Insert cell
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
md
`## References

1. Animated Map of COVID-19 Cases:
https://observablehq.com/@onoratod/animated-map-of-covid-19-cases

2. Bivariate Choropleth:
https://observablehq.com/@d3/bivariate-choropleth
`
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