Published
Edited
Sep 27, 2022
8 forks
13 stars
Insert cell
Insert cell
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(Float64Array.from({length: 800}, rx))
.join("circle")
.attr("transform", d => `translate(${x(d)},${ry()})`)
.attr("r", 3.5);

svg.append("g")
.call(xAxis);

svg.append("g")
.call(brush)
.call(brush.move, [3, 5].map(x))
.call(g => g.select(".overlay")
.datum({type: "selection"})
.on("mousedown touchstart", beforebrushstarted));

function beforebrushstarted(event) {
const dx = x(1) - x(0); // Use a fixed width when recentering.
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 && d <= x1 ? "red" : null);
}
}

return svg.node();
}
Insert cell
xAxis = g => g
.attr("transform", `translate(0,${height - margin.bottom})`)
.call(d3.axisBottom(x))
Insert cell
x = d3.scaleLinear([0, 10], [margin.left, width - margin.right])
Insert cell
rx = d3.randomUniform(...x.domain())
Insert cell
ry = d3.randomNormal(height / 2, height / 12)
Insert cell
margin = ({top: 10, right: 20, bottom: 20, left: 20})
Insert cell
height = 120
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