Published
Edited
Mar 19, 2019
Insert cell
Insert cell
chart = {
const svg = d3.select(DOM.svg(width, height));

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

svg.append("g")
.call(yAxis);
const group = svg.append("g")
.selectAll("g")
.data(data.series)
.join("g")
.attr("transform", d => `translate(0,${y(d.name) + 1})`);

group.append("path")
.attr("fill", "#ddd")
.attr("d", d => area(d.values));

group.append("path")
.attr("fill", "none")
.attr("stroke", "black")
.attr("d", d => line(d.values));

return svg.node();
}
Insert cell
overlap = 1
Insert cell
height = data.series.length * 17
Insert cell
margin = ({top: 40, right: 20, bottom: 30, left: 120})
Insert cell
x = d3.scaleTime()
.domain(d3.extent(data.dates))
.range([margin.left, width - margin.right])
Insert cell
y = d3.scalePoint()
.domain(data.series.map(d => d.name))
.range([margin.top, height - margin.bottom])
Insert cell
z = d3.scaleLinear()
.domain([0, d3.max(data.series, d => d3.max(d.values))]).nice()
.range([0, -overlap * y.step()])
Insert cell
xAxis = g => g
.attr("transform", `translate(0,${height - margin.bottom})`)
.call(d3.axisBottom(x)
.ticks(width / 80)
.tickSizeOuter(0))
Insert cell
yAxis = g => g
.attr("transform", `translate(${margin.left},0)`)
.call(d3.axisLeft(y).tickSize(0).tickPadding(4))
.call(g => g.select(".domain").remove())
Insert cell
area = d3.area()
.curve(d3.curveBasis)
.defined(d => !isNaN(d))
.x((d, i) => x(data.dates[i]))
.y0(0)
.y1(d => z(d))
Insert cell
line = area.lineY1()
Insert cell
data = {
const data = await d3.csv("https://gist.githubusercontent.com/aepton/488ae236cf71e1b6462997397aebdfbd/raw/338e94bc95c6e1c7bd6868540f2bd928f964b659/march_sensor_health.csv", ({name, date, value}) => ({name, date, value: +value}));

const dates = d3.nest()
.key(d => d.date)
.entries(data)
.map(d => new Date(+d.key))
.sort(d3.ascending)
.slice(1);

const values = d3.nest()
.key(d => d.name)
.key(d => d.date)
.rollup(v => v[0].value)
.map(data);

return {
series: values.entries().map(({key, value}) => ({
name: key,
values: dates.map(d => value.get(+d))
})),
dates
};
}
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