Public
Edited
Mar 11, 2023
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("data.json").json().then(data => data.map((d, i) => ({id: i + 1, ...d})))
Insert cell
groups = [...new Set(data.map(d => d.group))]
Insert cell
x = d3.scaleLinear()
.domain(d3.extent(data, d => d.deforestation))
.range([margin.left, width - margin.right])
Insert cell
y = d3.scaleBand()
.domain(groups)
.range([height / 2, height / 2])
Insert cell
xAxis = g =>
g
.attr("transform", `translate(0, ${margin.top})`)
.call(d3.axisTop(x).ticks(4))
.call(g => g.select(".domain").remove())
.call(g =>
g
.append("text")
.attr("x", width - margin.right)
.attr("fill", "currentColor")
.attr("text-anchor", "middle")
.text("Commodity deforestation (ha) →")
)
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.volume))
.range([3, 30])
Insert cell
colour = d3.scaleSequential(d3.extent(data, d => d.deforestation), d3.interpolateViridis)
Insert cell
d3 = require("d3@5")
Insert cell
html = (await require("htl@0.2")).html
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

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