function StackedAreaChart(
data,
{
x = ([x]) => x,
y = ([, y]) => y,
z = () => 1,
marginTop = 20,
marginRight = 30,
marginBottom = 30,
marginLeft = 40,
width = 640,
height = 400,
xType = d3.scaleUtc,
xDomain,
xRange = [marginLeft, width - marginRight],
yType = d3.scaleLinear,
yDomain,
yRange = [height - marginBottom, marginTop],
zDomain,
offset = d3.stackOffsetDiverging,
order = d3.stackOrderNone,
xFormat,
yFormat,
yLabel,
colors = d3.schemeTableau10
} = {}
) {
const X = d3.map(data, x);
const Y = d3.map(data, y);
const Z = d3.map(data, z);
if (xDomain === undefined) xDomain = d3.extent(X);
if (zDomain === undefined) zDomain = Z;
zDomain = new d3.InternSet(zDomain);
const I = d3.range(X.length).filter((i) => zDomain.has(Z[i]));
const series = d3
.stack()
.keys(zDomain)
.value(([x, I], z) => Y[I.get(z)])
.order(order)
.offset(offset)(
d3.rollup(
I,
([i]) => i,
(i) => X[i],
(i) => Z[i]
)
)
.map((s) => s.map((d) => Object.assign(d, { i: d.data[1].get(s.key) })));
if (yDomain === undefined) yDomain = d3.extent(series.flat(2));
const xScale = xType(xDomain, xRange);
const yScale = yType(yDomain, yRange);
const color = d3.scaleOrdinal(zDomain, colors);
const xAxis = d3
.axisBottom(xScale)
.ticks(width / 80, xFormat)
.tickSizeOuter(0);
const yAxis = d3.axisLeft(yScale).ticks(height / 50, yFormat);
const yAxisR = d3.axisRight(yScale);
const area = d3
.area()
.x(({ i }) => xScale(X[i]))
.y0(([y1]) => yScale(y1))
.y1(([, y2]) => yScale(y2));
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;");
svg
.append("g")
.attr("transform", `translate(${marginLeft - 1},0)`)
.call(yAxis)
.call((g) =>
g
.selectAll(".tick line")
.clone()
.attr("x2", width - marginLeft - marginRight)
.attr("stroke-opacity", 0.1)
)
.call((g) =>
g
.append("text")
.attr("x", -marginLeft)
.attr("y", 10)
.attr("fill", "currentColor")
.attr("text-anchor", "start")
.text(yLabel)
);
svg
.append("g")
.selectAll("path")
.data(series)
.join("path")
.attr("fill", ([{ i }]) => color(Z[i]))
.attr("d", area)
.append("title")
.text(([{ i }]) => Z[i]);
svg
.append("g")
.attr("transform", `translate(0,${height - marginBottom})`)
.call(xAxis);
svg
.append("g")
.attr("transform", `translate(${width - marginRight}, 0)`)
.call(yAxisR);
return Object.assign(svg.node(), { scales: { color } });
}