Public
Edited
Jun 21, 2023
4 forks
31 stars
Insert cell
Insert cell
Insert cell
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; font: 10px sans-serif;");

const g = svg.append("g")
.selectAll("g")
.data(data)
.enter().append("g")
.attr("transform", (d, i) => `translate(0,${i * (step + 1) + margin.top})`);

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

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

g.append("g")
.attr("clip-path", d => d.clip)
.selectAll("use")
.data(d => Array.from(
{length: overlap * 2},
(_, i) => Object.assign({index: i < overlap ? -i - 1 : i - overlap}, d)
))
.enter().append("use")
.attr("fill", d => color(d.index))
.attr("transform", d => mirror && d.index < 0
? `scale(1,-1) translate(0,${d.index * step})`
: `translate(0,${(d.index + 1) * step})`)
.attr("xlink:href", d => d.path.href);

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

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

return svg.node();
}
Insert cell
height = data.length * (step + 1) + margin.top + margin.bottom
Insert cell
margin = ({top: 30, right: 10, bottom: 0, left: 10})
Insert cell
step = 59
Insert cell
color = i => d3[scheme][overlap * 2 + 1][i + (i >= 0) + overlap]
Insert cell
x = d3.scaleUtc()
.domain([data[0].values[0].date, data[0].values[data[0].values.length - 1].date])
.range([0, width])
Insert cell
y = {
const max = d3.max(data, d => d3.max(d.values, d => Math.abs(d.value)));
return d3.scaleLinear()
.domain([-max, +max])
.range([overlap * step, -overlap * step]);
}
Insert cell
xAxis = g => g
.attr("transform", `translate(0,${margin.top})`)
.call(d3.axisTop(x).ticks(width / 80).tickSizeOuter(0))
.call(g => g.selectAll(".tick").filter(d => x(d) < margin.left || x(d) >= width - margin.right).remove())
.call(g => g.select(".domain").remove())
Insert cell
area = d3.area()
.curve(d3.curveStep)
.defined(d => !isNaN(d.value))
.x(d => x(d.date))
.y0(0)
.y1(d => y(d.value))
Insert cell
data = Promise.all([FileAttachment("AAPL.csv"), FileAttachment("AMZN.csv"), FileAttachment("GOOG.csv"), FileAttachment("IBM.csv"), FileAttachment("MSFT.csv")].map(async file => {
const values = d3.csvParse(await file.text(), d => {
const date = parseDate(d["Date"]);
return {date, value: +d["Close"]};
});
const v = values[0].value;
return {
key: file.name.slice(0, -4),
values: values.map(({date, value}) => ({date, value: Math.log(value / v)}))
};
}))
Insert cell
parseDate = d3.utcParse("%Y-%m-%d")
Insert cell
d3 = require("d3@6")
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more