Public
Edited
Mar 5, 2023
Importers
Insert cell
Insert cell
Inputs.button('deep to surface', {reduce: viewof chart.deep_to_surface})
Insert cell
Inputs.button("all of them", {reduce: viewof chart.all_of_them})
Insert cell
viewof chart = {
const w = width / 2;
const dx = w/ 20;
const svg = d3.create("svg")
.attr("viewBox", [0, 0, w, height])
.attr("width", "50%");

const brush = d3.brushX()
.extent([[margin.left, margin.top], [w - margin.right, height - margin.bottom]])
.on("start brush end", brushed);

const circle = svg.append("g")
.selectAll("circle")
.data(data)
.join("circle")
.attr("transform", d => `translate(${x(d[0])},${y(d[1])})`)
.attr("r", 1);

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

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

svg.append("text")
.attr("text-anchor", "end")
.attr("font-family", "sans-serif")
.attr("font-size", "1em")
.attr("x", w - margin.right)
.attr("y", height - margin.bottom)
.attr("dx", "-.5em")
.attr("dy", "-.5em")
.text(xlabel)
svg.append("text")
.attr("text-anchor", "start")
.attr("font-family", "sans-serif")
.attr("font-size", "em")
.attr("x", margin.left)
.attr("y", margin.top)
.attr("dx", "1em")
.attr("dy", "0em")
.text(ylabel)

const mybrush = svg.append("g")
.call(brush)
.call(brush.move, [w - margin.right - dx, w - margin.right])
.call(g => g.select(".overlay")
.datum({type: "selection"})
.on("mousedown touchstart", beforebrushstarted));

function beforebrushstarted(event) {
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);
svg.node().value = [x0, x1, circle
.attr("stroke", null)
.filter(d => (x0 <= d[0] && d[0] <= x1))
.attr("stroke", "black").data() ];
svg.node().dispatchEvent(new CustomEvent("input"));
}
}

const deep_to_surface = () => mybrush.transition()
.duration(2000)
.call(brush.move, [w - margin.right - dx, w - margin.right])
.transition()
.delay(100)
.ease(d3.easeLinear)
.duration(5000)
.call(brush.move, [margin.left, margin.left + dx])

const all_of_them = () => mybrush.transition()
.ease(d3.easeLinear)
.duration(5000)
.call(brush.move, [margin.left, width / 2 - margin.right]);

deep_to_surface();
return Object.assign(svg.node(), {all_of_them: all_of_them, deep_to_surface: deep_to_surface});
}
Insert cell
ylabel = "magnitude"
Insert cell
xlabel = "depth (km)"
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
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.scaleLinear(d3.extent(data, d => d[0]), [margin.left, width / 2 - margin.right])
Insert cell
y = d3.scaleLinear(d3.extent(data, d => d[1]), [height - margin.bottom, margin.top])
Insert cell
margin = ({top: 15, right: 20, bottom: 20, left: 20})
Insert cell
height = 120
Insert cell
d3 = require("d3@6")
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more