Published
Edited
Jan 25, 2021
1 fork
5 stars
Insert cell
Insert cell
Insert cell
Insert cell
{
const svg = d3.select(DOM.svg(width, height));
svg.append("g")
.call(xAxis);
svg.append("g")
.call(yAxis);

svg.append(() => dataGroup.node());
svg.append("g")
.attr("fill", "none")
.attr("stroke", "orange")
.attr("fill", "orange")
.attr("fill-opacity", "0.1")
.attr("stroke-linejoin", "round")
.attr("stroke-width", "3")
.attr("pointer-events", "none")
.selectAll("path")
.data(contours(contourSize) || contours.last.contour)
.enter().append("path")
.attr("d", d3.geoPath());
return svg.node();
}
Insert cell
dataGroup = {
const g = d3.create("svg:g")
g
.attr("stroke", "#000")
.attr("stroke-opacity", 0.2)
.selectAll("circle")
.data(data)
.join("circle")
.attr("cx", d => x(d.date))
.attr("cy", d => y(d.value))
.attr("fill", d => z(d.value))
.attr("r", 2.5)
.append("title")
.text(d => `${d.value}°F on ${d.date.toLocaleDateString()}`)
return g
}
Insert cell
contours = {
const contours = new Map()
window._contours = contours
return bandwidth => {
return contours.get(bandwidth) || (contours.set(bandwidth, d3.contourDensity()
.x(d => x(d.date))
.y(d => y(d.value))
.size([width, height])
.thresholds([0.15])
.bandwidth(bandwidth)
(data)), contours.get(bandwidth))
}
}
Insert cell
data = {
const data = [];
const csv = await fetch(
"https://cors-anywhere.herokuapp.com/https://data.giss.nasa.gov/gistemp/tabledata_v4/GLB.Ts%2BdSST.csv"
).then(r => r.text());
console.log(
csv
.split('\n')
.slice(1)
.join('\n')
);
d3.csvParse(
csv
.split('\n')
.slice(1)
.join('\n'),
(d, i, columns) => {
for (let i = 1; i < 13; ++i) {
if (Number.isNaN(+d[columns[i]])) continue;
data.push({
date: new Date(d.Year, i - 1, 1),
value: +d[columns[i]]
});
}
}
);
return data;
}
Insert cell
height = 600
Insert cell
margin = ({top: 20, right: 30, bottom: 30, left: 40})
Insert cell
x = d3.scaleTime()
.domain(d3.extent(data, d => d.date))
.range([margin.left, width - margin.right])
Insert cell
y = d3.scaleLinear()
.domain(d3.extent(data, d => d.value)).nice()
.range([height - margin.bottom, margin.top])
Insert cell
z = {
const max = d3.max(data, d => Math.abs(d.value));
return d3.scaleSequential(d3.interpolateRdBu).domain([max, -max]);
}
Insert cell
xAxis = g => g
.attr("transform", `translate(0,${height - margin.bottom})`)
.call(d3.axisBottom(x).ticks(width / 80))
.call(g => g.select(".domain").remove())
Insert cell
yAxis = g => g
.attr("transform", `translate(${margin.left},0)`)
.call(d3.axisLeft(y).ticks(null, "+"))
.call(g => g.select(".domain").remove())
.call(g => g.selectAll(".tick line")
.filter(d => d === 0)
.clone()
.attr("x2", width - margin.right - margin.left)
.attr("stroke", "#ccc"))
.call(g => g.append("text")
.attr("fill", "#000")
.attr("x", 5)
.attr("y", margin.top)
.attr("dy", "0.32em")
.attr("text-anchor", "start")
.attr("font-weight", "bold")
.text("Anomaly (°C)"))
Insert cell
d3 = require("d3@5")
Insert cell
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