Published
Edited
Apr 30, 2020
Insert cell
Insert cell
Insert cell
chart = {
const svg = d3.select(DOM.svg(width, height));

svg.append("g")
.attr("fill", "none")
.attr("stroke-width", 1.5)
.selectAll("path")
.data(data.slice().sort((a, b) => d3.ascending(a[keyz], b[keyz])))
.join("path")
.attr("stroke", d => z(d[keyz]))
.attr("stroke-opacity", 0.4)
.attr("d", d => d3.line()
.defined(([, value]) => value != null)
.x(([key, value]) => x.get(key)(value))
.y(([key]) => y(key))
(d3.cross(keys, [d], (key, d) => [key, d[key]])))
.append("title")
.text(d => d.name);

svg.append("g")
.selectAll("g")
.data(keys)
.join("g")
.attr("transform", d => `translate(0,${y(d)})`)
.each(function(d) { d3.select(this).call(d3.axisBottom(x.get(d))); })
.call(g => g.append("text")
.attr("x", margin.left)
.attr("y", -6)
.attr("text-anchor", "start")
.attr("fill", "currentColor")
.text(d => d))
.call(g => g.selectAll("text")
.clone(true).lower()
.attr("fill", "none")
.attr("stroke-width", 5)
.attr("stroke-linejoin", "round")
.attr("stroke", "white"));

return svg.node();
}
Insert cell
data = d3.csvParse(await FileAttachment("cars.csv").text(), d3.autoType)
Insert cell
keys = data.columns.slice(1)
Insert cell
x = new Map(
Array.from(
keys,
key => [key, d3.scaleLinear(d3.extent(data, d => d[key]), [margin.left, width - margin.right])]
)
)
Insert cell
y = d3.scalePoint(keys, [margin.top, height - margin.bottom])
Insert cell
z = d3.scaleSequential(x.get(keyz).domain().reverse(), d3.interpolateBrBG)
Insert cell
margin = ({top: 20, right: 10, bottom: 20, left: 10})
Insert cell
height = keys.length * 120
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