Published
Edited
Feb 28, 2021
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
data = {
const position = []
const cdata = []
const maxCO2 = d3.max(envData.filter(x => countries.features.some(f => f.properties.name === x.country)), x => +x.co2_per_capita)

for (const feature of countries.features) {
let coordinates;
if (feature.geometry.type == "Polygon") {
coordinates = feature.geometry.coordinates
} else if (feature.geometry.type == "MultiPolygon") {
coordinates = feature.geometry.coordinates[0]
} else {
throw `All elements must be polygons or multipolgyons. ${feature.geometry}`
}
const center = (() => {
const centers = coordinates.map(coords =>
[
d3.mean(coords, x => x[0]),
d3.mean(coords, x => x[1]),
]
)
return centers[0]
})();
// console.log(center)
position.push(polarToCartesian(center[0], center[1], 1.01))
position.push(polarToCartesian(center[0], center[1], 1.03))
position.push(polarToCartesian(center[0], center[1], 1.05))
position.push(polarToCartesian(center[0], center[1], 1.07))
// console.log(feature)
const cenv = envData.filter(x => +x.year === year && x.country === feature.properties.name);
if (cenv.length > 0) {
const obs = cenv[cenv.length - 1];
// console.log(obs)
const v = +obs.co2_per_capita;
const color = d3.color(interpolateColor(v))
// console.log(color)
cdata.push({
center,
value: v,
color: [color.r / 256, color.g / 256, color.b / 256],
})
}
}
const scale = d3.scaleQuantize()
.domain([0, 1])
.range(d3.range(1, 100))
return {
position: gl.regl.buffer(cdata.flatMap(x => {
const reps = scale(x.value)
return d3.range(0, reps + 1).map((_, rep) => polarToCartesian(x.center[0], x.center[1], 1 + 0.002 * rep))
})),
count: d3.sum(cdata, x => scale(x.value) + 1),
color: gl.regl.buffer(cdata.flatMap(x => {
const reps = scale(x.value)
return d3.range(0, reps + 1).map(() => x.color)
})),
}
return {
position: gl.regl.buffer(position),
color: gl.regl.buffer(position.map(([x, y, z], i) => ([Math.sin(x), Math.sin(y), Math.cos(z)]))),
count: position.length,
}
}
Insert cell
Insert cell
Insert cell
drawCountry = gl.regl({
vert: `
precision mediump float;

attribute vec3 position;
uniform mat4 model, view, projection;

void main () {
gl_Position = projection * view * model * vec4(position, 1);
}
`,
frag: `
precision mediump float;

void main () {
gl_FragColor = vec4(0, 0, 0, 1);
}
`,
attributes: {
position: gl.regl.prop("position")
},
uniforms: {
model: gl.regl.prop("model"),
},
elements: gl.regl.prop("elements"),
})
Insert cell
drawCountryData = gl.regl({
vert: `
precision mediump float;

attribute vec3 position, color;
uniform mat4 model, view, projection;

varying vec3 vColor;

void main () {
gl_PointSize = 2.;
vColor = color;
gl_Position = projection * view * model * vec4(position, 1);
}
`,
frag: `
precision mediump float;

varying vec3 vColor;

void main () {
gl_FragColor = vec4(vColor, 1);
}
`,
attributes: {
position: gl.regl.prop("position"),
color: gl.regl.prop("color"),
},
uniforms: {
model: gl.regl.prop("model"),
},
primitive: 'points',
count: gl.regl.prop("count"),
})
Insert cell
setupCamera = gl.regl({
context: {
projection: (context, props) =>
mat4.perspective(
[],
Math.PI / 4,
context.viewportWidth / context.viewportHeight,
0.01,
4 * 0.92 * Math.sqrt(Math.pow(3 * Math.cos(props.time), 2) + Math.pow(3 * Math.sin(props.time), 2)),
),

view: (context, props) =>
mat4.lookAt(
[],
[
3 * Math.cos(props.time),
3 * Math.sin(props.time),
0,
],
[0, 0, 0],
[0, 0, 1],
)
},

uniforms: {
view: gl.regl.context('view'),
projection: gl.regl.context('projection')
}
})
Insert cell
function draw(time) {
gl.regl.clear({
color: [1, 1, 1, 1],
depth: 1,
})
setupCamera({ time }, () => {
const model = mat4.identity([])

const { position, elements } = features
drawCountry({
model,
position,
elements
})
drawCountryData({
model,
position: data.position,
color: data.color,
count: data.count,
})

// drawGraticule(graticule.map(points => ({
// model,
// position: points,
// count: points.length,
// })))
})
}
Insert cell
draw(now / 5000)
Insert cell
function polarToCartesian(lon0, lat0, R) {
const lat = lat0 * Math.PI / 180
const lon = lon0 * Math.PI / 180
const x = R * Math.cos(lat) * Math.cos(lon)
const y = R * Math.cos(lat) * Math.sin(lon)
const z = R * Math.sin(lat)

return [x, y, z];
}
Insert cell
Insert cell
drawGraticule = gl.regl({
vert: `
precision mediump float;

attribute vec3 position;
uniform mat4 model, view, projection;

void main () {
gl_Position = projection * view * model * vec4(position, 1);
}
`,
frag: `
precision mediump float;

void main () {
gl_FragColor = vec4(0, 0, 0, 0.1);
}
`,
attributes: {
position: gl.regl.prop("position")
},
uniforms: {
model: gl.regl.prop("model"),
view: gl.regl.context('view'),
projection: gl.regl.context('projection'),
},
primitive: 'line strip',
count: gl.regl.prop("count"),
})
Insert cell
interpolateColor = d3.interpolateYlOrRd
Insert cell
envData = d3.csvParse(await fetch("https://raw.githubusercontent.com/owid/co2-data/master/owid-co2-data.csv").then(res => res.text()))
Insert cell
Insert cell
countries = {
const world = await fetch("https://cdn.jsdelivr.net/npm/world-atlas@2/countries-10m.json")
.then(res => res.json())
return topojson.feature(world, world.objects.countries)
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
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