Skip to content

Instantly share code, notes, and snippets.

@vnegi10
Created May 10, 2024 10:15
Show Gist options
  • Save vnegi10/fb2c8b7033133ea8b08b7444d827e7df to your computer and use it in GitHub Desktop.
Save vnegi10/fb2c8b7033133ea8b08b7444d827e7df to your computer and use it in GitHub Desktop.
export function plotValueChange(breakdown, date, {width} = {}) {
    const bisector = d3.bisector((i) => breakdown[i].time);
    const basis = (I, Y) => Y[I[bisector.center(I, date)]];
    return Plot.plot({
    style: "overflow: visible;",
    y: {
        //type: "log",
        grid: true,
        label: "Change in total value (%)",
        tickFormat: ((f) => (x) => f((x - 1) * 100))(d3.format("+d")),
        //domain: [0, 100]
    },
    width,
    //title: "",
    x: {label: "Time [days]"},
    color: {legend: true},
    marks: [
      Plot.ruleY([1]),
      Plot.ruleX([date]),
      Plot.lineY(breakdown, Plot.normalizeY(basis, {
        x: "time",
        y: "value",
        interval: "day",
        //stroke: "name",
        //marker: true
        //tip: true
        })),
      ]
    });
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment