{
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()
}