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);
svg.append("g")
.call(xAxis);
svg.append("g")
.call(yAxis);
svg.append("g")
.call(brush);
const gs = svg.append("g")
.attr("stroke-width", 1.5)
.attr("font-family", "sans-serif")
.attr("font-size", 10)
.selectAll("g")
.data(data)
.join("g")
.attr("transform", d => `translate(${x(d.x)},${y(d.y)})`)
.call(g => g.append("circle")
.attr("fill", "steelblue")
.attr("r", 3))
.call(g => g.append("text")
.attr("dy", "0.35em")
.attr("x", 7)
.text(d => d.name));
function brushed() {
const selection = d3.event.selection;
if (selection === null) {
gs.select("circle").attr("stroke", null);
} else {
const [p0, p1] = selection.map(d => [x.invert(d[0]), y.invert(d[1])]);
mutable selected = data.filter(d =>
p0[0] <= d.x && d.x <= p1[0] &&
p0[1] >= d.y && d.y >= p1[1] );
gs.select("circle").attr("stroke", d =>
p0[0] <= d.x && d.x <= p1[0] &&
p0[1] >= d.y && d.y >= p1[1]
? "red" : null);
}
}
return svg.node();
}