Public
Edited
Mar 27, 2024
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
width
Insert cell
quantity
Insert cell
quantity
Insert cell
height = 650
Insert cell
chartWidth = 700
Insert cell
NumberFormat = selection === "Absolute values" ? d3.format(",.2~s") : d3.format(".0%")
Insert cell
colors = ["#E27227", "#789B8E", "#BECFC9"]
Insert cell
colorScale = d3
.scaleOrdinal()
.domain(["At-risk", "Not at-risk", "No data"]) // Replace with your actual category names
.range(colors)
Insert cell
import { chart as quantity, legend as legendLci } with {
quantity_risk as data,
keyCol as keyCol,
secondarycolumn as secondaryKeyCol,
value_quantity as valueCol,
valueTitle_prop as valueTitle,
valueUnits_prop as valueUnits,
showValueLabels,
sortBars,
keys,
height,
colorScale as colour,
chartWidth as width,
NumberFormat as numberFormat
} from "@trase/stacked-horizontal-bar-chart-2-0"
Insert cell
import { chart as count, legend as legendLci } with {
quantity_risk as data,
keyCol as keyCol,
secondarycolumn as secondaryKeyCol,
value_count as valueCol,
valueTitle_prop2 as valueTitle,
valueUnits_prop1 as valueUnits,
showValueLabels,
sortBars,
keys,
height,
colorScale as colour,
chartWidth as width,
NumberFormat as numberFormat
} from "@trase/stacked-horizontal-bar-chart-2-0"
Insert cell
margin = ({
top: 20,
right: showValueLabels ? 60 : 10,
bottom: 40,
left: Math.min(150, width / 4)
})
Insert cell
import { swatches } from "@trase/legends"
Insert cell
import { titleCard } from "@trase/title-card"
Insert cell
import { Tooltip, tooltipKeyValue, tooltipOffset } from "@trase/tooltip@440"
Insert cell
quantity_risk = FileAttachment("final_aggregated_table_importer.csv").csv()
Insert cell
viewof selection = Inputs.radio(["Absolute values", "Proportional values"], {
value: "Absolute values"
})
Insert cell
value_quantity = selection === "Absolute values" ? "Imported volume (Tonne)" : "Proportion of volume (%)";
Insert cell
value_count = selection === "Absolute values"
? "Number of countries"
: "Proportion of countries (%)"
Insert cell
keys = [
...new Set([
...quantity_risk.map((d) => d[secondarycolumn]),
...quantity_risk.map((d) => d[secondarycolumn])
])
]
Insert cell
keyCol = "country"
Insert cell
secondarycolumn = "risk"
Insert cell
valueTitle_prop = selection === "Absolute values"
? "Total imported volume"
: "Proportion of volume"
Insert cell
valueTitle_prop2 = selection === "Absolute values"
? "Total number of exporters"
: "Proportion of exporters"
Insert cell
valueUnits_prop = selection === "Absolute values" ? "tonnes" : "%";
Insert cell
valueUnits_prop1 = selection === "Absolute values" ? "" : "%"
Insert cell
showValueLabels = selection === "Absolute values" ? true : false
Insert cell
totals = d3.rollup(quantity_risk, d => d3.sum(d, d => d[value_quantity]), d => d[keyCol])
Insert cell
totals_count = d3.rollup(
quantity_risk,
(d) => Math.round(d3.sum(d, (d) => d[value_count])),
(d) => d[keyCol]
)
Insert cell
sortBars = {
let sortBars;

if (selection === "Absolute values") {
sortBars = ([a], [b]) => {
return totals.get(b) - totals.get(a);
};
} else if (selection === "Proportional values") {
// Sort alphabetically by keyCol
sortBars = ([a], [b]) => {
return a.localeCompare(b);
};
}
return sortBars;
}
Insert cell
sortBars_count = {
let sortBars;

if (selection === "Absolute values") {
sortBars = ([a], [b]) => {
return totals_count.get(b) - totals_count.get(a);
};
} else if (selection === "Proportional values") {
// Sort alphabetically by keyCol
sortBars = ([b], [a]) => {
return b.localeCompare(a);
};
}
return sortBars;
}
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