viewof chart = {
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height]);
const brush = d3.brushX()
.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")
.call(yAxis)
.call(g => g.style("font-size", "x-large"));
svg.append("g")
.call(xAxis)
.call(g => g.style("font-size", "x-large"));
svg.append("text")
.attr("text-anchor", "start")
.style("font-family", "sans-serif")
.style("font-size", "x-large")
.attr("x", margin.left)
.attr("y", margin.top)
.attr("dx", ".5em")
.attr("dy", ".5em")
.text(title)
const dx = (x.range()[1] - x.range()[0]) / 10;
const x0 = (d3.mean(x.range()));
svg.append("g")
.call(brush)
.call(brush.move, [x0 - dx / 2, x0 + dx / 2])
.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);
circle.attr("stroke", d => x0 <= d[0] && d[0] <= x1 ? "red" : null);
svg.node().value = [x0, x1];
svg.node().dispatchEvent(new CustomEvent("input"));
}
}
return svg.node();
}