render({
data: { values: portfolioData },
transform: [
{ calculate: "year(datum.Date)", as: "Year" },
{ calculate: "datum.Shares * datum.Price", as: "TotalValue" },
{
aggregate: [
{ op: "sum", field: "TotalValue", as: "YearValue" }
],
groupby: ["Investor", "Year"]
},
{ pivot: "Year", value: "YearValue", groupby: ["Investor"] },
{
calculate: "(datum['2023'] - datum['2019']) / datum['2019'] * 100",
as: "PctGain"
}
],
mark: { type: "bar", cornerRadiusEnd: 3 },
encoding: {
x: { field: "Investor", type: "nominal", sort: "-y" },
y: { field: "PctGain", type: "quantitative", title: "% Gain (2019–2023)" },
color: {
field: "PctGain",
type: "quantitative",
scale: { scheme: "blues" }
},
tooltip: [
{ field: "Investor", type: "nominal" },
{ field: "PctGain", type: "quantitative", format: ".2f", title: "% Gain" },
{ field: "2019", type: "quantitative", title: "Value 2019 ($)" },
{ field: "2023", type: "quantitative", title: "Value 2023 ($)" }
]
},
title: "Portfolio % Gain by Investor (2019 → 2023)"
});