Published
Edited
Jun 19, 2019
Insert cell
Insert cell
Insert cell
Insert cell
chart = {
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height])
.style("font", "10px sans-serif");

const g = svg.append("g")
.selectAll("g")
.data(data)
.join("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 => 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.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 = 23
Insert cell
color = i => d3[scheme][Math.max(3, overlap)][i + Math.max(0, 3 - 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 = d3.scaleLinear()
.domain([0, d3.max(data, d => d3.max(d.values, d => d.value))])
.range([0, -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.curveBasis)
.defined(d => !isNaN(d.value))
.x(d => x(d.date))
.y0(0)
.y1(d => y(d.value))
Insert cell
data = {
const data = await d3.csv("https://gist.githubusercontent.com/chrtze/c74efb46cadb6a908bbbf5227934bfea/raw/c32d94689dd432609c55d1758d8e69c59f94f802/traffic_weekly.csv", ({name, date, total_1, total_2}) => ({name, date: new Date(date * 1000), value: +total_1 + +total_2}));
return d3.nest()
.key(d => d.name)
.sortValues((a, b) => a.date - b.date)
.entries(data)
.map(d => (d.sum = d3.sum(d.values, d => d.value), d))
.sort((a, b) => b.sum - a.sum);
}
Insert cell
d3 = require("d3@5")
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