Public
Edited
Dec 24, 2023
Insert cell
Insert cell
Insert cell
y.domain()
Insert cell
viewof chart = {
replay;
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height])
.attr("font-size", "1.5em");

const brush = d3.brush()
.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", 2);

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

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

svg.append("text")
.attr("text-anchor", "start")
.attr("font-family", "sans-serif")
.attr("font-size", ".75em")
.attr("x", margin.left)
.attr("y", margin.top)
.attr("dx", "2em")
.attr("dy", ".5em")
.text("magnitude")

svg.append("text")
.attr("text-anchor", "start")
.attr("font-family", "sans-serif")
.attr("font-size", ".75em")
.attr("x", width - margin.right)
.attr("y", height - margin.bottom)
.attr("dx", "-4em")
.attr("dy", "-.5em")
.text("depth (km)")

svg.append("g")
.call(brush)
.call(brush.move, [[x0 - dx / 2, y1], [x0 + dx / 2, y0]]) // Initialize brush
.call(g => g.select(".overlay")
.datum({type: "selection"})
.on("mousedown touchstart", beforebrushstarted))
.transition()
.ease(d3.easeLinear)
.delay(500)
.duration(5000)
// .call(brush.move, [x1, x1 + dx])
.call(brush.move, [[x1, y1], [x1 + dx, y0]])

function beforebrushstarted(event) {
d3.select(this.parentNode).call(brush.clear)
svg.node().value = [null, null, data];
svg.node().dispatchEvent(new CustomEvent("input"));
// const [[cx, cy]] = 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, y0], [X1, y1]]
// : x0 < X0 ? [[X0, y0], [X0 + dx, y1]]
// : [[x0, y1], [x1, y0]]);
}

function brushed(event) {
const selection = event.selection;
if (selection === null) {
// circle.attr("stroke", null);
} else {
const [[a0, b0], [a1, b1]] = selection;
const [x0, x1] = [a0, a1].map(x.invert);
const [y0, y1] = [b0, b1].map(y.invert);
circle.attr("stroke", d => x0 <= d[0] && d[0] <= x1 && d[1] <= y0 && y1 <= d[1] ? "red" : null);
let a = circle // .attr("stroke", null)
.filter(d => x0 <= d[0] && d[0] <= x1 && d[1] <= y0 && y1 <= d[1])
.attr("stroke", "red")
.data()
.map(d => d[2]);
svg.node().value = [[x0, y0], [x1, y1], a];
svg.node().dispatchEvent(new CustomEvent("input"));
}
}

return svg.node();
}
Insert cell
Insert cell
x0 = x(200)
Insert cell
x1 = x.range()[0]
Insert cell
dx = x(20) - x(0); // brush width
Insert cell
y0 = y.range()[0]
Insert cell
y1 = y(4)
Insert cell
Insert cell
geojson = d3.json("https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/2.5_month.geojson")
Insert cell
data = geojson.features.map(d => [d.geometry.coordinates[2], d.properties.mag, d])
Insert cell
title = "magnitude"
Insert cell
xAxis = g => g
.attr("transform", `translate(0,${height - margin.bottom})`)
.call(d3.axisBottom(x).ticks(6))
Insert cell
yAxis = g => g
.attr("transform", `translate(${margin.left}, 0)`)
.call(d3.axisLeft(y).ticks(3))
Insert cell
// x = d3.scaleTime(d3.extent(data, d => d[0]), [margin.left, width - margin.right])
x = d3.scaleLinear(d3.extent(data, d => d[0]), [margin.left, width - margin.right])
Insert cell
y = d3.scaleLinear(d3.extent(data, d => d[1]), [height - margin.bottom, margin.top])
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