Public
Edited
Apr 11, 2024
1 fork
Insert cell
Insert cell
chart = htl.html`
${titleCard1}
${legendOnly}
${chartOnly}
`
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
data_ori = Object.assign(
await FileAttachment(
"global_beef_deforestation_deduce_fao_update_24@4.csv"
).csv({ typed: false }),
{ d: "volume_EU_2022" }
)
Insert cell
data = data_ori.filter((d) => {
// Check if deforestation_cptc is a number and is less than or equal to valuePct
return d["deforestation_cptc"] !== '' && d["deforestation_cptc"] != null && !isNaN(d["deforestation_cptc"]) && d["deforestation_cptc"] <= valuePct;
});
Insert cell
dataMap = new Map(
data.map(({ key, risk_categories, volume_EU_2022 }) => [
String(key),
risk_categories,
Number(volume_EU_2022)
])
)
Insert cell
viewof valuePctSelect = Inputs.select(["Total", "99%", "95%"], {
value: "Total",
label: ""
})
Insert cell
pct_select = [
1,
0.99,
0.95,
{
total: "Total",
trase: "99%",
cgf: "95%"
}
]
Insert cell
valuePct = pct_select[Object.values(pct_select[3]).indexOf(valuePctSelect)]
Insert cell
iso = new Map(data.map((d) => [String(d.M49_code), d.key]))
Insert cell
{
const countriesGeom = world.objects.countries.geometries.map((d) => {
return {
...d,
properties: { ...d.properties, trase_name: iso.get(String(d.id)) }
};
});
return {
...world,
objects: {
...world.objects,
countries: { ...world.objects.countries, geometries: countriesGeom }
}
};
}
Insert cell
world = FileAttachment("countries-110m-minus-antarctica@1.json").json()
Insert cell
height = width * 0.6
Insert cell
margin = ({ top: 0, right: 0, bottom: 0, left: 0 })
Insert cell
projection = d3
.geoNaturalEarth1()
.fitExtent(
[[margin.left, margin.top], [width - margin.right, height - margin.bottom]],
topojson.mesh(world, world.objects.countries)
)
.rotate([-15, 0]) // keep russia together
Insert cell
path = d3.geoPath(projection);
Insert cell
risk_code = 'risk_categories'
Insert cell
keys = [
...new Set([
...data.map((d) => d[risk_code]),
])
]
Insert cell
colour1 = d3
.scaleOrdinal()
.domain(keys)
.range(["#E27227", "#789B8E", "#BECFC9"])
Insert cell
colour = d3
.scaleOrdinal()
.domain(["At-risk", "Not at-risk", "No data"])
.range(["#E27227", "#789B8E", "#BECFC9"])
Insert cell
format = d3.format(",.2r")
Insert cell
annotate = g => {}
Insert cell
volume = new Map(
data.map(({ key, volume_EU_2022 }) => [String(key), Number(volume_EU_2022)])
)
Insert cell
// viewof selected_value = Inputs.radio(["Global", "European Union"], {
// value: "Global"
// })
Insert cell
maxDeforestation = d3.max(data_ori, (d) => d.volume_cum)
Insert cell
function hover(g, svg) {
const tooltip = new Tooltip();
g.style("cursor", "pointer")
.on("touchstart", (event) => event.preventDefault())
.on("pointerenter", function (event, f) {
if (!volume.has(iso.get(f.id))) return;
tooltip.position(...d3.pointer(event));
tooltip.show(
f.properties.name,
tooltipKeyValue(
tooltipKey.toUpperCase(),
format(volume.get(iso.get(f.id))),
f
)
);
// highlight jurisdiction
d3.select(this).transition().attr("opacity", 0.7);
})
.on("pointermove", function (event) {
let [x, y] = tooltipOffset(tooltip, d3.pointer(event), width, height);
tooltip.position(x, y);
this.releasePointerCapture(event.pointerId);
})
.on("pointerout", function () {
tooltip.hide();
d3.select(this).transition().attr("opacity", 1);
});
svg.append(() => tooltip.node);
}
Insert cell
tooltipKey = data["volume_EU_2022"] || "Volume placed on the EU market (tonnes)"
Insert cell
import { swatches, getLabelLength } from "@trase/legends-2-0"
Insert cell
import { Tooltip, tooltipKeyValue, tooltipOffset } from "@trase/tooltip@440"
Insert cell
d3 = require("d3@6")
Insert cell
topojson = require("topojson-client@3")
Insert cell
simple = require("simple-statistics@7")
Insert cell
import { traseColours, traseCategory1, fonts } from "@trase/visual-id@1366"
Insert cell
import { titleCard } from "@trase/title-card"
Insert cell
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