Skip to content

Commit

Permalink
refactor(frontend): add variables to plot data (#621)
Browse files Browse the repository at this point in the history
Add a `variable` property to plot data, then use that when we need to display just the variable name in a plot.
  • Loading branch information
eatyourgreens authored Dec 13, 2024
1 parent 2b0c561 commit d3f6a67
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions frontend-v2/src/features/simulation/SimulationPlotView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ import { RootState } from "../../app/store";
import useDataset from "../../hooks/useDataset";
import useSubjectGroups from "../../hooks/useSubjectGroups";

type ScatterDataWithVariable = ScatterData & { variable: string };

const Plot = createPlotlyComponent(Plotly);
// https://github.com/plotly/plotly.js/blob/8c47c16daaa2020468baf9376130e085a4f01ec6/src/components/color/attributes.js#L4-L16
const plotColours = [
Expand Down Expand Up @@ -202,6 +204,7 @@ function generatePlotData(
x: d.time.map((t) => t * xconversionFactor),
y: variableValues.map((v) => v * yconversionFactor),
name: name.trim(),
variable: variableName,
visible: visible ? true : "legendonly",
line: {
color: colour,
Expand All @@ -216,6 +219,7 @@ function generatePlotData(
y: [],
type: "scatter",
name: name.trim(),
variable: y_axis.variable,
visible: visible ? true : "legendonly",
};
}
Expand Down Expand Up @@ -426,7 +430,7 @@ const SimulationPlotView: FC<SimulationPlotProps> = ({
}),
);
})
.flat() as Partial<ScatterData>[];
.flat() as Partial<ScatterDataWithVariable>[];

const concentrationUnit = units.find((unit) => unit.symbol === "pmol/L");
if (concentrationUnit === undefined) {
Expand All @@ -442,10 +446,10 @@ const SimulationPlotView: FC<SimulationPlotProps> = ({

const yAxisVariables = plotData
.filter((d) => !d.yaxis)
.map((d) => d.name?.replace("REF ", "").split(" ")[0]);
.map((d) => d.variable);
const y2AxisVariables = plotData
.filter((d) => d.yaxis)
.map((d) => d.name?.replace("REF ", "").split(" ")[0]);
.map((d) => d.variable);
let yAxisTitle = [...new Set(yAxisVariables)].join(", ");
let y2AxisTitle = [...new Set(y2AxisVariables)].join(", ");
let xAxisTitle = "Time";
Expand Down

0 comments on commit d3f6a67

Please sign in to comment.