Published
Edited
Jun 5, 2020
Insert cell
Insert cell
chart = {
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height])
.attr("font-family", "sans-serif")
.attr("font-size", 12);

svg.append("g")
.call(xAxis);

svg.append("g")
.attr("fill", "none")
.attr("stroke", "#000")
.selectAll("path")
.data(d3.groups(data, d => d.name))
.join("path")
.attr("d", ([, group]) => line(group))
.call(path => path.clone(true))
.attr("stroke", "#fff")
.attr("stroke-width", 5);

svg.append("g")
.attr("fill", "#fff")
.selectAll("circle")
.data(data)
.join("circle")
.attr("cx", d => x(d.period))
.attr("cy", d => y(d.value))
.attr("r", 20);

svg.append("g")
.attr("text-anchor", "middle")
.selectAll("text")
.data(data)
.join("text")
.attr("x", d => x(d.period))
.attr("y", d => y(d.value))
.attr("dy", "0.35em")
.text(d => formatValue(d.value));

svg.append("g")
.attr("text-anchor", "end")
.selectAll("text")
.data(d3.groups(data, d => d.name))
.join("text")
.attr("x", margin.left - 12)
.attr("y", ([key, [d]]) => y(d.value) + (key === "Colon") * 10)
.attr("dy", "0.35em")
.attr("dx", "-2em")
.text(([key]) => key);

return svg.node();
}
Insert cell
line = d3.line()
.x(d => x(d.period))
.y(d => y(d.value))
Insert cell
x = d3.scalePoint()
.domain(data.map(d => d.period))
.range([margin.left, width - margin.right])
Insert cell
y = d3.scaleLinear()
.domain(d3.extent(data, d => d.value))
.range([height - margin.bottom, margin.top])
Insert cell
xAxis = g => g
.attr("transform", `translate(0,60)`)
.call(d3.axisTop(x))
.call(g => g.select(".domain").remove())
.attr("font-size", 12)
Insert cell
formatValue = d3.format(",")
Insert cell
margin = ({top: 90, right: Math.max(width - 600, 20), bottom: 20, left: 190})
Insert cell
height = 600
Insert cell
data = {
const data = d3.csvParse(await FileAttachment("compiled flows.csv").text(), d3.autoType);
return data.columns.slice(1).flatMap(column => data.map(d => ({
name: d.Region,
period: column,
value: d[column]
})));
}
Insert cell
d3 = require("d3@5", "d3-array@2")
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