Published
Edited
Nov 13, 2020
Insert cell
chart = {
const context = DOM.context2d(800, 600);
const path = d3.geoPath(projection, context);
let contours = makeContoursData();
for(let i = 0; i < contours.length; i++){
// when i == 7, some polygon is counterclockwise
// if(i == 7){
// continue;
// }
let one = contours[i];
context.beginPath();
path(one);
context.fillStyle = colorScale(thresholds[i]);
context.fill();
}
return d3.select(context.canvas).node();
}
Insert cell
function makeContoursData(){
let minX = data.x[0];
let maxX = data.x[data.x.length-1];
let minY = data.y[0];
let maxY = data.y[data.y.length-1];
let transform = ({type, value, coordinates}) => {
return {type, value, coordinates: coordinates.map(rings => {
return rings.map(points => {
return points.map(([x, y]) => ([
x / data.width * (maxX - minX) + minX,
y / data.height * (maxY - minY) + minY
]));
});
})};
}
let contours = d3.contours()
.size([data.width, data.height])
.thresholds(thresholds)(varData.values)
.map(transform);
return contours;
}
Insert cell
colorScale = d3.scaleSequential(d3.interpolateTurbo)
.domain([thresholds[0], thresholds[thresholds.length-1]]);
Insert cell
thresholds = d3.range(varData.minVal, varData.maxVal,
(varData.maxVal - varData.minVal) / contourLevel);
Insert cell
contourLevel = 20;
Insert cell
varData = data["anompb"];
Insert cell
projection = d3.geoEquirectangular()
Insert cell
data = FileAttachment("contours.json").json()
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