Unlisted
Edited
Jan 2, 2023
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
viewof bubbleYearSelect = Inputs.select([2018, 2019, 2020], {
step: 1,
value: 2018,
label: "Select year:",
format: (d) => d
})
Insert cell
draw = chart.update(keys[0])
Insert cell
keys = Object.keys(data[0]).filter((d) => d != "key" && d != "zdc_share")
Insert cell
legendItems = [
{
type: "dot",
stroke: "#bdbdbd",
fill: "#bdbdbd",
text: "No data"
}
]
Insert cell
// height = width
height = 906
Insert cell
width
Insert cell
margin = ({ top: 5, bottom: 5, right: 5, left: 5 })
Insert cell
// colourScheme = traseOranges[6]
colourScheme = traseOranges[7]
Insert cell
colourSchemeRev = colourScheme.reverse()
Insert cell
colourSchemeZdc = (d) => {
if ((d.data.key === "DOMESTIC") | (d.data.key === "UNKNOWN COUNTRY"))
return "#bdbdbd";
return d3.scaleThreshold().domain(thresholds).range(colourSchemeRev)(
d.data[colourKey]
);
}
Insert cell
id_palm_zdc_shr_vols_imp_ctry1 = FileAttachment("id_palm_zdc_shr_vols_imp_ctry@4.csv").csv()
Insert cell
colourKey = "zdc_share"
Insert cell
defriskKey = "deforestation"
Insert cell
root = d3
.hierarchy(
d3.rollup(
data,
(v) => ({ value: d3.sum(v, (d) => d.value), data: v }),
...keys.map((k) => (d) => d[k])
)
)
.sum(([, d]) => d.value)
.sort((a, b) => b.value - a.value)
Insert cell
pack = d3
.pack()
.size([
width - (margin.left + margin.right),
height - (margin.top + margin.bottom)
])
.padding(3)
Insert cell
thresholds = [0.2, 0.4, 0.6, 0.8, 0.9, 0.95].map((d) =>
Math.floor(d * d3.max(data, (d) => d.zdc_share))
)
Insert cell
full = FileAttachment("id_palm_zdc_shr_vols_imp_ctry@4.csv").csv({
typed: true
})
Insert cell
data = full
.filter((d) => d.year === bubbleYearSelect)
.map((d) => ({
key: d.key,
volume: d.volume,
deforestation: d.def_risk,
zdc_share: d.zdc_share
}))
Insert cell
format = d3.format(".03s")
Insert cell
formatBubbleText = (d) => {
let key = d.data.key;
const length = getLabelLength(key, "font: 10px var(--trase-sans-serif)");
const perimeter = d.r * 2 - 20;
const prop = (length - perimeter) / length;
if (perimeter < 50) return "";
if (length > perimeter)
key =
key.substring(0, Math.floor(key.length * (1 - prop)) - 3).trim() + "...";
return [...key.split(" "), format(d.value)];
}
Insert cell
annotate = g => {}
Insert cell
function hover(bubbles, svg, selected_value) {
const tooltip = new Tooltip();
const tooltipKey = selected_value.replace(/_/g, " ");
bubbles
.style("cursor", "pointer")
.on("touchstart", (event) => event.preventDefault())
.on("pointerenter", (event, d) => {
tooltip.position(
...tooltipOffset(tooltip, d3.pointer(event, svg.node()), width, height)
);
tooltip.show(d.data.key, tooltipKeyValueMultiple(d));
})
.on("pointermove", function (event) {
let [x, y] = tooltipOffset(
tooltip,
d3.pointer(event, svg.node()),
width,
height
);
tooltip.position(x, y);
this.releasePointerCapture(event.pointerId);
})
.on("pointerout", () => tooltip.hide());
svg.append(() => tooltip.node);
}
Insert cell
tooltipKeyValueMultiple = (d) => svg`<text>
<tspan font-size="10px" fill="#839095" x="0" dy="0em">VOLUME:</tspan>
<tspan font-family="var(--trase-sans-serif)" font-size="14px" font-weight="700" fill="#31464e" x="0" dy="1.4em">${format(
d.data.volume
)} t</tspan>
<tspan font-size="10px" fill="#839095" x="0" dy="2.2em">DEFORESTATION RISK:</tspan>
<tspan font-family="var(--trase-sans-serif)" font-size="14px" font-weight="700" fill="#31464e" x="0" dy="1.4em">${d3.format(
".03s"
)(d.data.deforestation)} ha</tspan>
<tspan font-size="10px" fill="#839095" x="0" dy="2.2em">SHARE COVERED BY ZDC:</tspan>
<tspan font-family="var(--trase-sans-serif)" font-size="14px" font-weight="700" fill="#31464e" x="0" dy="1.4em">${
d.data[colourKey] >= 0 ? d3.format(".1%")(d.data[colourKey] / 100) : "N/A"
}</tspan>
</text>`
Insert cell
formatTooltipValue = d3.format(".03s")
Insert cell
import {
traseColours,
traseCategory1,
fonts,
traseOranges,
trasePurples,
traseGreens,
traseOrPu
} from "@trase/visual-id"
Insert cell
import { getLabelLength, legend, swatches } from "@trase/legends@376"
Insert cell
import { Tooltip, tooltipKeyValue, tooltipOffset } from "@trase/tooltip@440";
Insert cell
import { pickTextColorBasedOnBackground } from "@trase/pick-text-color-based-on-bg"
Insert cell
import { slider, select, checkbox, radio } from "@jashkenas/inputs"
Insert cell
import { titleCard } from "@trase/title-card"
Insert cell
d3 = require("d3@6")
Insert cell
htl = require("htl@0.2")
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