Published
Edited
May 11, 2021
1 star
Insert cell
Insert cell
Insert cell
chart = {
const svg = d3
.create("svg")
.attr("viewBox", [0, 0, width, height])
.attr("font-family", "sans-serif")
.attr("font-size", 10);

const g = svg
.append("g")
.selectAll("g")
.data(
data.series.map(d =>
Object.assign(
{
clipId: DOM.uid("clip"),
pathId: DOM.uid("path")
},
d
)
)
)
.join("g")
.attr("transform", (d, i) => `translate(0,${i * (step + 1) + margin.top})`);

g.append("clipPath")
.attr("id", d => d.clipId.id)
.append("rect")
.attr("width", width)
.attr("height", step);

g.append("defs")
.append("path")
.attr("id", d => d.pathId.id)
.attr("d", d => area(d.values));

g.append("g")
.attr("clip-path", d => d.clipId)
.selectAll("use")
.data(d => new Array(overlap).fill(d))
.join("use")
.attr("fill", (d, i) => color(i))
.attr("transform", (d, i) => `translate(0,${(i + 1) * step})`)
.attr("xlink:href", d => d.pathId.href);

g.append("text")
.attr("x", 4)
.attr("y", step / 2)
.attr("dy", "0.35em")
.text(d => d.name);

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

return svg.node();
}
Insert cell
Insert cell
height = data.series.length * (step + 1) + margin.top + margin.bottom
Insert cell
margin = ({top: 30, right: 10, bottom: 0, left: 10})
Insert cell
step = 59
Insert cell
overlap = 5
Insert cell
band = 0.5
Insert cell
color = i => d3.schemeOrRd[Math.max(3, overlap)][i + Math.max(0, 3-overlap)]
Insert cell
x = d3.scaleLinear([1, 365], [0, width])
Insert cell
y = d3.scaleLinear([0, band], [0, -step])
Insert cell
xDateScale = d3.scaleUtc(
[new Date(2020, 0, 1), new Date(2020, 11, 31)],
[0, width]
)
Insert cell
xAxisDates = g => g
.attr("transform", `translate(0,${margin.top})`)
.call(d3.axisTop(xDateScale).tickFormat(d3.timeFormat("%B")))
.call(g => g.selectAll(".tick").filter((d,i) => i == 0).remove())
.call(g => g.select(".domain").remove())
Insert cell
area = d3
.area()
.curve(d3.curveStep)
.defined(d => !isNaN(d) && d !== null)
.x((d, i) => x(i))
.y0(0)
.y1(d => y(d))
Insert cell
d3 = require("d3@6")
Insert cell
import {legend} from "@d3/color-legend"
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