Public
Edited
Nov 16, 2023
1 fork
Insert cell
Insert cell
{
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height]);

const brushWidth = 50;

const brush = d3.brushY()
.extent([
[-(brushWidth / 2), margin.top],
[brushWidth / 2, height - margin.bottom]
])
.on("start brush end", brushed);

const path = svg.append("g")
.selectAll("path")
.data(data.slice(0, 2000))
.join("path")
.style("fill", "none")
.style("stroke-width", 1)
.style("stroke-opacity", 0.25)
.style("stroke", "steelblue")
.attr("d", d => line(d3.cross(keys, [d], (key, d) => [key, d[key]])));

svg.append("g")
.selectAll("g")
.data(keys)
.join("g")
.attr("transform", d => `translate(${x(d)}, 0)`)
.each(function(d) { d3.select(this).call(d3.axisLeft(y.get(d))); })
.call(g => g.append("text")
.attr("x", 0)
.attr("y", margin.top-10)
.style("text-anchor", "middle")
.style("dominant-baseline", "text-bottom")
.style("fill", "black")
.style("font-size", "100%")
.text(d => d))
.call(brush);

const selections = new Map();

function brushed({selection}, key) {
if (selection === null) selections.delete(key);
else selections.set(key, selection.map(y.get(key).invert).sort());
const selected = [];
path.each(function(d) {
const active = Array.from(selections).every(([key, [min, max]]) => d[key] >= min && d[key] <= max);
d3.select(this).style("stroke", active ? "steelblue": "lightgrey");
if (active) {
d3.select(this).raise();
selected.push(d);
}
});
svg.property("value", selected).dispatch("input");
}
return svg.node()
}
Insert cell
margin = ({top: 50, right: 50, bottom: 50, left: 50})
Insert cell
data = d3.csv(url, d3.autoType)
Insert cell
url = FileAttachment("nutrients.csv").url()
Insert cell
keys = data.columns.slice(2, 16);
Insert cell
y = new Map(Array.from(keys, key => [key, d3.scaleLinear(d3.extent(data, d => d[key]), [height-margin.bottom, margin.top])]))
Insert cell
x = d3.scalePoint(keys, [margin.left, width - margin.right])
Insert cell
line = d3.line()
.defined(([, value]) => value != null)
.y(([key, value]) => y.get(key)(value))
.x(([key]) => x(key))
Insert cell
height = width * .6
Insert cell
d3.cross(keys, [data[0]], (key, d) => [key, d[key]])
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