viewof chart = {
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height]);
const brush = d3.brush()
.extent([[margin.left, margin.top], [width - margin.right, height - margin.bottom]])
.on("start brush end", brushed);
const circle = svg.append("g")
.attr("fill-opacity", 0.2)
.selectAll("circle")
.data(data)
.join("circle")
.attr("transform", d => `translate(${x(d[0])},${y(d[1])})`)
.attr("r", 3.5);
svg.append("g")
.classed("y-axis", true)
.call(yAxis)
svg.append("g")
.classed("x-axis", true)
.call(xAxis)
svg.append("text")
.classed("axis-title", true)
.attr("text-anchor", "start")
.attr("font-family", "sans-serif")
.attr("font-size", "1em")
.attr("x", margin.left)
.attr("y", margin.top)
.attr("dx", ".5em")
.attr("dy", "-.4em")
.text(title)
const dx = (x.range()[1] - x.range()[0]) / 10;
const x0 = (d3.mean(x.range()));
const [y0, y1] = y.range();
svg.append("g")
.call(brush)
.call(brush.move, [[x0 - dx / 2, y1], [x0 + dx / 2, y0]])
.call(g => g.select(".overlay")
.datum({type: "selection"})
.on("mousedown touchstart", beforebrushstarted));
function beforebrushstarted(event) {
const [[cx, cy]] = 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, y0], [X1, y1]]
: x0 < X0 ? [[X0, y0], [X0 + dx, y1]]
: [[x0, y1], [x1, y0]]);
}
function brushed(event) {
const selection = event.selection;
if (selection === null) {
circle.attr("stroke", null);
} else {
const [[a0, b0], [a1, b1]] = selection;
const [x0, x1] = [a0, a1].map(x.invert);
const [y0, y1] = [b0, b1].map(y.invert);
circle.attr("stroke", d => x0 <= d[0] && d[0] <= x1 && d[1] <= y0 && y1 <= d[1] ? "red" : null);
svg.node().value = [[x0, y0], [x1, y1]];
svg.node().dispatchEvent(new CustomEvent("input"));
}
}
return svg.node();
}