Published
Edited
Aug 31, 2021
2 stars
Insert cell
Insert cell
chart = {
const path = d3.geoPath();
const contours = d3.tricontour();

const svg = d3
.create("svg")
.attr("viewBox", wide ? [0, 0, width, height] : [0, 0, height, width])
.style("display", "block")
.style("margin", "0 -14px")
.style("width", "calc(100% + 28px)")
.style("height", "auto");

const g = svg
.append("g")
.attr(
"transform",
wide
? null
: `
rotate(90 ${width / 2},${height / 2})
translate(${(width - height) / 2},${(width - height) / 2})
`
)
.attr("stroke", "white")
.attr("stroke-width", 0.03);

for (const threshold of thresholds) {
g.append("path")
.attr("d", path(contours.contour(points, threshold)))
.attr("fill", color(threshold));

yield svg.node();
}
}
Insert cell
width = data.width
Insert cell
height = data.height
Insert cell
wide = Generators.observe(notify => {
let wide;
function resized() {
let w = window.innerWidth > 640;
if (w !== wide) notify(wide = w);
}
resized();
window.addEventListener("resize", resized);
return () => window.removeEventListener("resize", resized);
})
Insert cell
color = d3.scaleSequential(interpolateTerrain).domain(d3.extent(data.values)).nice()
Insert cell
thresholds = color.ticks(20)
Insert cell
points = Array.from(data.values, (d, i) => [
(i % data.width) + 0.5,
((i / data.width) | 0) + 0.5,
d
])
Insert cell
data = d3.json("https://gist.githubusercontent.com/mbostock/4241134/raw/ee3244eaf405667623241b3d5e1477424623d12b/volcano.json")
Insert cell
interpolateTerrain = {
const i0 = d3.interpolateHsvLong(d3.hsv(120, 1, 0.65), d3.hsv(60, 1, 0.90));
const i1 = d3.interpolateHsvLong(d3.hsv(60, 1, 0.90), d3.hsv(0, 0, 0.95));
return t => t < 0.5 ? i0(t * 2) : i1((t - 0.5) * 2);
}
Insert cell
d3 = require("d3@7", "d3-tricontour@1", "d3-hsv@0.1")
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