viewof chart = {
const w = width / 2;
const dx = w/ 20;
const svg = d3.create("svg")
.attr("viewBox", [0, 0, w, height])
.attr("width", "50%");
const brush = d3.brushX()
.extent([[margin.left, margin.top], [w - margin.right, height - margin.bottom]])
.on("start brush end", brushed);
const circle = svg.append("g")
.selectAll("circle")
.data(data)
.join("circle")
.attr("transform", d => `translate(${x(d[0])},${y(d[1])})`)
.attr("r", 1);
svg.append("g")
.call(yAxis);
svg.append("g")
.call(xAxis);
svg.append("text")
.attr("text-anchor", "end")
.attr("font-family", "sans-serif")
.attr("font-size", "1em")
.attr("x", w - margin.right)
.attr("y", height - margin.bottom)
.attr("dx", "-.5em")
.attr("dy", "-.5em")
.text(xlabel)
svg.append("text")
.attr("text-anchor", "start")
.attr("font-family", "sans-serif")
.attr("font-size", "em")
.attr("x", margin.left)
.attr("y", margin.top)
.attr("dx", "1em")
.attr("dy", "0em")
.text(ylabel)
const mybrush = svg.append("g")
.call(brush)
.call(brush.move, [w - margin.right - dx, w - margin.right])
.call(g => g.select(".overlay")
.datum({type: "selection"})
.on("mousedown touchstart", beforebrushstarted));
function beforebrushstarted(event) {
const [[cx]] = d3.pointers(event);
const [x0, x1] = [cx - dx / 2, cx + dx / 2];
const [X0, X1] = x.range();
d3.select(this.parentNode)
.call(brush.move, x1 > X1 ? [X1 - dx, X1]
: x0 < X0 ? [X0, X0 + dx]
: [x0, x1]);
}
function brushed(event) {
const selection = event.selection;
if (selection === null) {
circle.attr("stroke", null);
} else {
const [x0, x1] = selection.map(x.invert);
svg.node().value = [x0, x1, circle
.attr("stroke", null)
.filter(d => (x0 <= d[0] && d[0] <= x1))
.attr("stroke", "black").data() ];
svg.node().dispatchEvent(new CustomEvent("input"));
}
}
const deep_to_surface = () => mybrush.transition()
.duration(2000)
.call(brush.move, [w - margin.right - dx, w - margin.right])
.transition()
.delay(100)
.ease(d3.easeLinear)
.duration(5000)
.call(brush.move, [margin.left, margin.left + dx])
const all_of_them = () => mybrush.transition()
.ease(d3.easeLinear)
.duration(5000)
.call(brush.move, [margin.left, width / 2 - margin.right]);
deep_to_surface();
return Object.assign(svg.node(), {all_of_them: all_of_them, deep_to_surface: deep_to_surface});
}