Published
Edited
Jul 3, 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([new Date(2019, 0, 1), new Date(2019, 11, 31)])
//.domain([data[0].values[0].date, data[0].values[data[0].values.length - 1].date])
.range([0, width])
Insert cell
[new Date(2019, 0, 1), new Date(2019, 11, 31)]
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/vpascual/a3638e4e8b5731380ece3012661bad6c/raw/2892f1ca7c664ff2a70503ad72bd6a9e26c5c184/Data_horizon_chart.csv", ({Country, value, date}) => ({name: Country, date: new parseTime(date), value: +value}));
//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
parseTime = d3.timeParse("%d/%m/%Y");
Insert cell
d3 = require("d3@5")
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