prodviz = html`<div style="display: flex; justify-content: center;">${Plot.plot((() => {
const n = 3;
const customProvOrder = ["Alberta", "Saskatchewan", "Manitoba", "Ontario", "Quebec"];
const provKeys = customProvOrder.filter(prov => production.some(d => d.prov === prov));
const provIndex = new Map(provKeys.map((key, i) => [key, i]));
const fx = (key) => provIndex.get(key) % n;
const fy = (key) => Math.floor(provIndex.get(key) / n);
const customGrainOrder = ["Wheat", "Canola", "Corn", "Barley", "Soybeans", "Oats", "Lentils"];
const grainIndex = new Map(customGrainOrder.map((key, i) => [key, i]));
const sortedProduction = production.slice().sort((a, b) => (grainIndex.get(a.grain) ?? Infinity) - (grainIndex.get(b.grain) ?? Infinity));
return {
width,
height: 600,
color: { legend: true, domain: customGrainOrder },
title: "Production trends of major grains and legumes in Canada by province",
subtitle: "Regional differences exist in the type and volume of grain produced.",
caption: "Source: Own calculation using data from Statistics Canada",
fx: { padding: 0.08, axis: null },
fy: { padding: 0.08, axis: null },
marks: [
Plot.frame({ strokeOpacity: 0.1 }),
Plot.areaY(sortedProduction, {
x: (d) => +d.year,
y: "value",
fill: "grain",
fx: (d) => fx(d.prov),
fy: (d) => fy(d.prov),
title: (d) => `${d.prov} produced ${(d.value / 1_000_000).toFixed(1)} million tonnes of ${d.grain.toLowerCase()} in ${d.year}.`,
tip: true
}),
Plot.text(provKeys, {fx, fy, frameAnchor: "top-left", dx: 6, dy: 6}),
],
x: {
label: "Year of production",
domain: [2000, 2023],
ticks: [2000, 2023],
tickFormat: (d) => d.toString(),
},
y: {
label: "Production in metric tonnes",
ticks: [5000000, 10000000, 15000000, 20000000, 25000000, 30000000, 35000000],
tickFormat: (d) => Math.round(d / 1_000_000) + 'M'
}
};
})())}</div>`