Published
Edited
Jan 7, 2022
Insert cell
Insert cell
chart = {
const svg = d3
.create("svg")
.attr("width", width)
.attr("height", height)
.attr("viewBox", [0, 0, width, height])
.attr("style", "max-width: 100%; height: auto; height: intrinsic;")
.attr("font-family", "sans-serif")
.attr("font-size", 10);

const startGroup = svg.append("g").attr("transform", `translate(300,0)`);

startGroup
.append("text")
.attr("text-anchor", "end")
.attr("y", height - 10)
.attr("font-weight", 700)
.text(d3.timeFormat("%B %d, %Y")(startDate));

for (var i = 0; i < connections.length; i++) {
const d = connections[i];
startGroup
.append("line")
.attr("stroke", "black")
.attr("opacity", d.name == "Croatia" ? 1 : 0.1)
.attr("x1", 0)
.attr("x2", 300)
.attr("y1", y(d.start))
.attr("y2", y(d.end));
}

for (var i = 0; i < start.length; i++) {
const d = start[i];

startGroup
.append("circle")
.attr("r", 3)
.attr("cx", 0)
.attr("opacity", d.name == "Croatia" ? 1 : 0.1)
.attr("cy", y(d.rate));

startGroup
.append("text")
.attr("alignment-baseline", "middle")
.attr("dx", -5)
.attr("y", y(d.rate))
.attr("stroke-linejoin", "round")
.attr("stroke-width", 5)
.attr("text-anchor", "end")
.attr("stroke", "white")
.text(`${d.name} ${d3.format(",.2r")(d.rate)}`);

startGroup
.append("text")
.attr("alignment-baseline", "middle")
.attr("dx", -5)
.attr("y", y(d.rate))
.attr("text-anchor", "end")
.attr("opacity", d.name == "Croatia" ? 1 : 0.2)
.text(`${d.name} ${d3.format(",.2r")(d.rate)}`);
}

const endGroup = svg.append("g").attr("transform", `translate(600,0)`);

endGroup
.append("text")
.attr("y", height - 10)
.attr("font-weight", 700)
.text(d3.timeFormat("%B %d, %Y")(new Date(2022, 0, 1)));

for (var i = 0; i < end.length; i++) {
const d = end[i];

endGroup
.append("circle")
.attr("r", 3)
.attr("cx", 0)
.attr("opacity", d.name == "Croatia" ? 1 : 0.1)
.attr("cy", y(d.rate));

endGroup
.append("text")
.attr("alignment-baseline", "middle")
.attr("dx", 5)
.attr("y", y(d.rate))
.attr("stroke-linejoin", "round")
.attr("stroke-width", 5)
.attr("stroke", "white")
.text(`${d.name} ${d3.format(",.2r")(d.rate)}`);

endGroup
.append("text")
.attr("alignment-baseline", "middle")
.attr("dx", 5)
.attr("y", y(d.rate))
.attr("opacity", d.name == "Croatia" ? 1 : 0.2)
.text(`${d.name} ${d3.format(",.2r")(d.rate)}`);
}

return svg.node();
}
Insert cell
connections = {
let retval = [];

for (var i = 0; i < start.length; i++) {
let d = start[i];
let startIndex = start.indexOf(d);
let endIndex = end.indexOf(end.find((endD) => endD.name == d.name));

retval.push({
name: d.name,
start: start[startIndex].rate,
end: end[endIndex].rate
});
}

return retval;
}
Insert cell
y = d3
.scaleLinear()
.domain([0, max])
.range([height - 30, 30])
Insert cell
height = 1200
Insert cell
max = d3.max(end, (d) => d.rate)
Insert cell
end.push(end.splice(endIndex, 1)[0])
Insert cell
start
Insert cell
end = endGroup.map((d) => {
return {
name: d.location,
rate: d.total_deaths_per_million
};
})
Insert cell
endIndex = end.indexOf(endCroatia)
Insert cell
endCroatia = end.find((d) => d.name == "Croatia")
Insert cell
endGroup = data.filter(
(d) =>
d.date > new Date(2022, 0, 1) &&
d.date < new Date(2022, 0, 2) &&
d.total_deaths_per_million !== null
)
Insert cell
start.push(start.splice(startIndex, 1)[0])
Insert cell
startIndex = start.indexOf(startCroatia)
Insert cell
startCroatia = start.find((d) => d.name == "Croatia")
Insert cell
start = startGroup.map((d) => {
return {
name: d.location,
rate: d.total_deaths_per_million
};
})
Insert cell
startGroup = data.filter(
(d) =>
d.date > new Date(2020, 5, 1) &&
d.date < new Date(2020, 5, 2) &&
d.total_deaths_per_million !== null
)
Insert cell
item = data.find((d) => d.iso_code == "ALB" && d.date > startDate)
Insert cell
startDate = new Date(2020, 5, 1)
Insert cell
data = d3.csvParse(
await FileAttachment("owid-covid-data@1.csv").text(),
d3.autoType
)
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