Published
Edited
Mar 29, 2020
Insert cell
Insert cell
Insert cell
chart = {
replay;

const svg = d3.create("svg")
.attr("viewBox", [-30, 0, width+50, height]);
svg.append("g")
.call(xAxis);
svg.append("g")
.call(yAxis);
// add line 1
const l1 = length(line(data_1));
svg.append("path")
.datum(data_1)
.attr("fill", "none")
.attr("stroke", "green")
.attr("stroke-width", 2.5)
.attr("stroke-linejoin", "round")
.attr("stroke-linecap", "round")
.attr("stroke-dasharray", `0,${l1}`)
.attr("d", line)
.transition()
.duration(8000)
.ease(d3.easeLinear)
.attr("stroke-dasharray", `${l1},${l1}`);
// add circles 1
svg.append("g")
.attr("fill", "white")
.attr("stroke", "black")
.attr("stroke-width", 2)
.selectAll("circle")
.data(data_1)
.join("circle")
.attr("cx", d => x(d.x))
.attr("cy", d => y(d.y))
.attr("r", 3);
// add line 2
const l2 = length(line(data_2));
svg.append("path")
.datum(data_2)
.attr("fill", "none")
.attr("stroke", "blue")
.attr("stroke-width", 2.5)
.attr("stroke-linejoin", "round")
.attr("stroke-linecap", "round")
.attr("stroke-dasharray", `0,${l2}`)
.attr("d", line)
.transition()
.duration(8000)
.ease(d3.easeLinear)
.attr("stroke-dasharray", `${l2},${l2}`);
// add circles 2
svg.append("g")
.attr("fill", "white")
.attr("stroke", "black")
.attr("stroke-width", 2)
.selectAll("circle")
.data(data_2)
.join("circle")
.attr("cx", d => x(d.x))
.attr("cy", d => y(d.y))
.attr("r", 3);
// legend
svg.append("circle").attr("cx",85).attr("cy",100).attr("r", 10).style("fill", "green")
svg.append("circle").attr("cx",85).attr("cy",130).attr("r", 10).style("fill", "blue")
svg.append("text").attr("x", 100).attr("y", 100).text("Tested Total").style("font-size", "20px").attr("alignment-baseline","middle")
svg.append("text").attr("x", 100).attr("y", 130).text("Newly Tested").style("font-size", "20px").attr("alignment-baseline","middle")
return svg.node();
}
Insert cell
height = 720
Insert cell
margin = ({top: 20, right: 30, bottom: 30, left: 40})
Insert cell
x = d3.scaleLinear()
.domain(d3.extent(data_1, d => d.x)).nice()
.range([margin.left, width - margin.right])
Insert cell
y = d3.scaleLinear()
.domain(d3.extent(data_1, d => d.y)).nice()
.range([height - margin.bottom, margin.top])
Insert cell
xAxis = g => g
.style("font-size", "22px")
.attr("transform", `translate(0,${height - margin.bottom})`)
.call(d3.axisBottom(x).ticks(width / 80))
.call(g => g.select(".domain").remove())
.call(g => g.selectAll(".tick line").clone()
.attr("y2", -height)
.attr("stroke-opacity", 0.1))
.call(g => g.append("text")
.attr("x", width - 40)
.attr("y", -110)
.attr("font-weight", "bold")
.attr("text-anchor", "end")
.attr("fill", "black")
.text("March 2020")
.call(halo))
Insert cell
yAxis = g => g
.style("font-size", "22px")
.attr("transform", `translate(${margin.left},0)`)
.call(d3.axisLeft(y).ticks(null))
.call(g => g.select(".domain").remove())
.call(g => g.selectAll(".tick line").clone()
.attr("x2", width)
.attr("stroke-opacity", 0.1))
Insert cell
function length(path) {
return d3.create("svg:path").attr("d", path).node().getTotalLength();
}
Insert cell
function halo(text) {
text.select(function() { return this.parentNode.insertBefore(this.cloneNode(true), this); })
.attr("fill", "none")
.attr("stroke", "white")
.attr("stroke-width", 4)
.attr("stroke-linejoin", "round");
}
Insert cell
line = d3.line()
.curve(d3.curveCatmullRom)
.x(d => x(d.x))
.y(d => y(d.y))
Insert cell
data_1 = {
const data = d3.csvParse(await FileAttachment("tested@2.csv").text(), ({day, infected}) => ({
x: +day,
y: +infected}));
return data;
}
Insert cell
data_2 = {
const data = d3.csvParse(await FileAttachment("tested - delta@1.csv").text(), ({day, infected}) => ({
x: +day,
y: +infected}));
return data;
}
Insert cell
d3 = require("d3@5")
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