Public
Edited
Oct 24, 2023
3 forks
8 stars
Insert cell
Insert cell
chart = {
const brush = d3.brush()
.on("start brush", ({selection}) => brushed(selection))
.on("end", brushended);

const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height]);

const point = svg.append("g")
.attr("fill", "#ccc")
.attr("stroke", "#777")
.selectAll("circle")
.data(data)
.join("circle")
.attr("cx", d => d[0])
.attr("cy", d => d[1])
.attr("r", 3.5);

svg.append("g")
.call(brush)
.call(brush.move, defaultExtent);

function brushed(selection) {
point.attr("fill", selection && (d => contains(selection, d) ? "red" : null));
}

function brushended({sourceEvent, selection}) {
if (!sourceEvent) return; // Only transition after interaction.
d3.select(this).transition()
.delay(100)
.duration(selection ? 750 : 0)
.call(brush.move, defaultExtent);
}

return svg.node();
}
Insert cell
function contains([[x0, y0], [x1, y1]], [x, y]) {
return x >= x0 && x < x1 && y >= y0 && y < y1;
}
Insert cell
height = 600
Insert cell
defaultExtent = [[width * 0.1, width * 0.1], [width * 0.3, width * 0.3]]
Insert cell
data = Array.from({length: 2000}, () => [Math.random() * width, Math.random() * height])
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more