render({
mark: { type: "line", point: true },
data: { values: portfolioData },
transform: [
{ calculate: "datum.Shares * datum.Price", as: "TotalValue" },
{
window: [
{ op: "first_value", field: "TotalValue", as: "InitialValue" }
],
groupby: ["Investor"],
sort: [{ field: "Date", order: "ascending" }]
},
{
calculate: "(datum.TotalValue - datum.InitialValue) / datum.InitialValue * 100",
as: "PercentReturn"
}
],
encoding: {
x: { field: "Date", type: "T", title: "Date" },
y: {
field: "PercentReturn",
type: "Q",
title: "Portfolio Return (%)",
axis: { format: ".1f" }
},
color: { field: "Investor", type: "N", title: "Investor" },
tooltip: [
{ field: "Investor", type: "N" },
{ field: "Date", type: "T" },
{
field: "PercentReturn",
type: "Q",
title: "Return (%)",
format: ".2f"
}
]
},
width: 500,
height: 300,
title: "Percentage Return Over Time by Investor"
})