Published
Edited
Mar 16, 2022
1 star
Insert cell
Insert cell
chart = {
const svg = d3.create("svg").attr("viewBox", [0, 0, width, height]);

let previousS0, previousS1;

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 }, Math.random))
.join("circle")
.attr("transform", (d) => `translate(${x(d)},${y()})`)
.attr("r", 3.5);

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

let brushGroup = svg
.append("g")
.call(brush)
.call(brush.move, [0.3, 0.5].map(x));

function brushed({ selection }) {
if (selection === null) {
circle.attr("stroke", null);
} else {
const sx = selection.map(x.invert);
// circle.attr("stroke", d => sx[0] <= d && d <= sx[1] ? "red" : null);
}
// d3.select(this).call(brushHandle, selection);
var s = selection || x.range();
// console.log(s, ((s[1] - s[0]) / width) * 100 > 50);
if (((s[1] - s[0]) / width) * 100 > 50) {
console.log("run");
brushGroup.call(brush.move, [previousS0, previousS1]);
return;
}
previousS0 = s[0];
previousS1 = s[1];
}

return svg.node();
}
Insert cell
brushHandle = (g, selection) => g
.selectAll(".handle--custom")
.data([{type: "w"}, {type: "e"}])
.join(
enter => enter.append("path")
.attr("class", "handle--custom")
.attr("fill", "#666")
.attr("fill-opacity", 0.8)
.attr("stroke", "#000")
.attr("stroke-width", 1.5)
.attr("cursor", "ew-resize")
.attr("d", arc)
)
.attr("display", selection === null ? "none" : null)
.attr("transform", selection === null ? null : (d, i) => `translate(${selection[i]},${(height + margin.top - margin.bottom) / 2})`)
Insert cell
arc = d3.arc()
.innerRadius(0)
.outerRadius((height - margin.top - margin.bottom) / 2)
.startAngle(0)
.endAngle((d, i) => i ? Math.PI : -Math.PI)
Insert cell
xAxis = g => g
.attr("transform", `translate(0,${height - margin.bottom})`)
.call(d3.axisBottom(x))
Insert cell
x = d3.scaleLinear([0, 1], [margin.left, width - margin.right])
Insert cell
x2 = d3.scaleLinear([0, 1], [margin.left, width - margin.right])
Insert cell
y = d3.randomNormal(height / 2, height / 12)
Insert cell
margin = ({top: 10, right: 20, bottom: 20, left: 20})
Insert cell
height = 120
Insert cell
d3 = require("d3@6")
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