Public
Edited
Nov 8, 2022
11 forks
28 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
height = 400 // initial height
Insert cell
margin = ({top: 50, right: 100, bottom: 50, left: 200})
Insert cell
data = FileAttachment("data2.json").json().then(data => data.map((d, i) => ({id: i + 1, ...d})))
Insert cell
continents = [...new Set(data.map(d => d.continent))]
Insert cell

x = d3.scaleLinear()
.domain(d3.extent(data, d => d.year))
.range([margin.left, width - margin.right])
Insert cell
y = d3.scaleBand()
.domain(continents)
.range([height / 2, height / 2])
Insert cell
xAxis = g =>
g
.attr("transform", `translate(0, ${margin.top})`)
.call(d3.axisTop(x).ticks(10))
.call(g => g.select(".domain").remove())
.call(g =>
g
.append("text")
.attr("x", width - margin.right)
.attr("fill", "currentColor")
.attr("text-anchor", "middle")
.text("Years →")
)
Insert cell
yAxis = (g, scale = y, ticks = y.domain()) =>
g
.attr("transform", `translate(${30}, 0)`)
.call(d3.axisLeft(scale).tickValues(ticks))
.call(g => g.style("text-anchor", "start"))
.call(g => g.select(".domain").remove())
Insert cell
r = d3.scaleSqrt()
.domain(d3.extent(data, d => d.emission))
.range([6, 20])
Insert cell
colour = d3.scaleSequential(d3.extent(data, d => d.year), d3.interpolatePlasma)
Insert cell
Insert cell
Insert cell
// data = client.query(`
// WITH top AS (
// SELECT exporter_group, SUM(volume) AS volume
// FROM views.datasets
// WHERE latest_dataset = TRUE
// AND country_of_production = 'INDONESIA'
// AND commodity = 'PALM OIL'
// AND country_of_production != country_of_import
// AND year = 2015
// GROUP BY exporter_group
// ORDER BY volume DESC
// LIMIT 5
// )
// SELECT
// CASE WHEN exporter_group = ANY(SELECT exporter_group FROM top)
// THEN exporter_group
// ELSE 'OTHER' END AS group,
// exporter,
// SUM(volume) AS volume,
// SUM(commodity_deforestation) AS deforestation
// FROM views.datasets
// WHERE latest_dataset = TRUE
// AND country_of_production = 'INDONESIA'
// AND commodity = 'PALM OIL'
// AND country_of_production != country_of_import
// AND year = 2015
// GROUP BY exporter_group, exporter
// ORDER BY volume DESC`)
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