Public
Edited
Dec 4
1 fork
1 star
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
legend = () => {
const wrapper = d3.create("div")
.style("width", `${w}px`)

wrapper.append("div")
.style("font-family", franklinLight)
.style("font-size", "16px")
.style("font-weight", "bold")
.style("margin-bottom", "-4px")
.style("text-align", "center")
.text(select.name);

wrapper.append("div")
.style("font-family", franklinLight)
.style("font-size", "16px")
.style("text-align", "center")
.style("margin-bottom", "8px")
.text("Compared to 1993-2016 average");
const tickSize = 0;
const margin = {left: 0, right: 0, top: 0, bottom: tickSize + 18};
const width = 300 - margin.left - margin.right;
const height = 12;
const svg = wrapper.append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.style("display", "table")
.style("margin", "0 auto")
.style("overflow", "visible");

svg.call(gradient);

const values = extent;
const scale = d3.scaleLinear(extent, [0, width]);

const g = svg.append("g")
.attr("transform", `translate(${[margin.left, margin.top]})`);
g.append("rect")
.attr("fill", `url(#${gradient.id()})`)
.attr("height", height)
.attr("width", width);

const tick = g.selectAll(".tick")
.data(values)
.join("g")
.attr("class", "tick")
.attr("transform", d => `translate(${[scale(d), height]})`);

tick.append("line")
.attr("stroke", "black")
.attr("y2", tickSize);

tick.append("text")
.attr("dy", tickSize)
.attr("font-family", franklinLight)
.attr("font-size", 14)
.attr("text-anchor", (d, i) => i === 0 ? "start" : "end")
.attr("y", 14)
.text((d, i) => select.legend[i]);
return wrapper.node();
}
Insert cell
map = function*() {
const canvas = d3.create("canvas");
canvas.node().width = w;
canvas.node().height = h;
const context = canvas.node().getContext("2d");
path.context(context);

context.beginPath();
context.fillStyle = color(color.domain()[0])
context.arc(w / 2, h / 2, w / 2, 0, Math.PI * 2);
context.fill();

for (let i = 0, l = contours.features.length; i < l; i++) {
const d = contours.features[i];
context.beginPath();
path(d);
const c = color(d.value);
context.fillStyle = c;
context.fill();
context.strokeStyle = c;
context.stroke();

yield canvas.node();
}

if (variable === "sst") {
context.fillStyle = "white";
countriesGeo.features.forEach(feature => {
context.beginPath();
path(feature);
context.fill();
})
}
context.fillStyle = "none";
context.lineWidth = 1;
context.beginPath();
path(countriesGeoInner);
context.strokeStyle = "#494949";
context.stroke();
context.beginPath();
path(countriesGeoOuter);
context.strokeStyle = "#2a2a2a";
context.stroke();

yield canvas.node();
}
Insert cell
Insert cell
extent = variable === "jet" ? [-6.5, 6.5] : [-3.5, 3.5]
Insert cell
absmax = d3.max(extent, d => Math.abs(d))
Insert cell
interpolateJet = interpolatePalette([
"#2a2a2a", "#494949", "#666666", "#808080", "#aaaaaa", "#d5d5d5", "#e9e9e9",
"#fdfdfd", "#f9c296", "#f1a26a", "#e6823d", "#d86100", "#a64a00", "#763500"
])
Insert cell
interpolator = variable === "jet" ? interpolateJet : d3.interpolateRdBu
Insert cell
color = d3.scaleDiverging(variable === "jet" ? [-absmax, 0, absmax] : [absmax, 0, -absmax], interpolator)
Insert cell
Insert cell
interval = 10
Insert cell
offsets = d3.range(0, 100 + (100 / interval), 100 / interval)
Insert cell
colors = offsets.map(d3.scaleLinear(d3.extent(offsets), extent)).map(color)
Insert cell
gradient = gradientLinear()
.id("legend-gradient")
.offsets(offsets)
.colors(colors)
Insert cell
Insert cell
projection = d3.geoOrthographic()
.rotate([120, -30, 0])
.fitSize([w, h], {type: "Sphere"})
Insert cell
path = d3.geoPath(projection)
Insert cell
countriesTopo = FileAttachment("countries@1.topo.json").json()
Insert cell
countriesGeoInner = topojson.mesh(countriesTopo, countriesTopo.objects.country, (a, b) => a !== b)
Insert cell
countriesGeoOuter = topojson.mesh(countriesTopo, countriesTopo.objects.country, (a, b) => a === b)
Insert cell
countriesGeo = topojson.feature(countriesTopo, countriesTopo.objects.country)
Insert cell
Insert cell
options = [{name: "Jet stream", property: "jet", legend: ["Weaker than normal", "Stronger"]}, {name: "Sea surface temperature", property: "sst", legend: ["Colder than normal", "Warmer"]}]
Insert cell
variable = select.property
Insert cell
raw = FileAttachment("sfara-jet-sst.csv").csv({ typed: true })
Insert cell
data = JSON.parse(JSON.stringify(raw)).filter(f => f[variable] !== -9999)
Insert cell
contour = d3.geoContour().thresholds(40);
Insert cell
points = JSON.parse(JSON.stringify(data)).map(d => [d.longitude, d.latitude, d[variable]])
Insert cell
contours = ({
type: "FeatureCollection",
features: contour(points)
})
Insert cell
Insert cell
w = 640
Insert cell
h = w
Insert cell
Insert cell
import { franklinLight } from "@climatelab/fonts@46"
Insert cell
import { gradientLinear } from "@climatelab/gradient@60"
Insert cell
import { interpolatePalette } from "@climatelab/roll-your-own-color-palette-interpolator@346";
Insert cell
import { toc } from "@climatelab/toc@45"
Insert cell
d3 = require("d3@7", "d3-geo-voronoi@2")
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