Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 14 additions & 24 deletions frontend-v2/src/features/simulation/SimulationPlotView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@ import {
generateScatterPlots,
genIcLines,
getICLineShapes,
getAxisTitles,
getPlotDimensions,
getPlotLayout,
getYRanges,
ScatterDataWithVariable,
getPlotAxes,
} from "./utils";
import { useConfig } from "./config";

Expand Down Expand Up @@ -139,27 +141,11 @@ const SimulationPlotView: FC<SimulationPlotProps> = ({
const maxX = Math.max(...convertedTime);
const icLineShapes = getICLineShapes({ icLines, minX, maxX, plot });

const yAxisVariables = plotData
.filter((d) => !d.yaxis)
.map((d) => d.variable);
const y2AxisVariables = plotData
.filter((d) => d.yaxis)
.map((d) => d.variable);
let yAxisTitle = [...new Set(yAxisVariables)].join(", ");
let y2AxisTitle = [...new Set(y2AxisVariables)].join(", ");
let xAxisTitle = "Time";
const yUnit = units.find((u) => u.id === plot.y_unit);
const y2Unit = units.find((u) => u.id === plot.y_unit2);
const xUnit = units.find((u) => u.id === plot.x_unit);
if (yUnit) {
yAxisTitle = `${yAxisTitle} [${yUnit.symbol}]`;
}
if (y2Unit) {
y2AxisTitle = `${y2AxisTitle} [${y2Unit.symbol}]`;
}
if (xUnit) {
xAxisTitle = `${xAxisTitle} [${xUnit.symbol}]`;
}
const { xAxisTitle, yAxisTitle, y2AxisTitle } = getAxisTitles({
plot,
plotData,
units,
});

const plotDimensions = getPlotDimensions({
isVertical,
Expand All @@ -168,18 +154,22 @@ const SimulationPlotView: FC<SimulationPlotProps> = ({
plotCount,
});

const basePlotLayout: Partial<Layout> = getPlotLayout({
const plotAxes: Partial<Layout> = getPlotAxes({
plot,
plotDimensions,
shouldShowLegend,
xAxisTitle,
yAxisTitle,
y2AxisTitle,
yRanges,
});

const basePlotLayout: Partial<Layout> = getPlotLayout({
plotDimensions,
shouldShowLegend,
});

const plotLayout: Partial<Layout> = {
...basePlotLayout,
...plotAxes,
shapes: icLineShapes,
};

Expand Down
80 changes: 61 additions & 19 deletions frontend-v2/src/features/simulation/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,40 @@ export const getYRanges = ({
return { minY, maxY, minY2, maxY2 };
};

export const getAxisTitles = ({
plot,
plotData,
units,
}: {
plot: FieldArrayWithId<Simulation, "plots", "id">;
plotData: Partial<ScatterDataWithVariable>[];
units: UnitRead[];
}) => {
const yAxisVariables = plotData
.filter((d) => !d.yaxis)
.map((d) => d.variable);
const y2AxisVariables = plotData
.filter((d) => d.yaxis)
.map((d) => d.variable);
let yAxisTitle = [...new Set(yAxisVariables)].join(", ");
let y2AxisTitle = [...new Set(y2AxisVariables)].join(", ");
let xAxisTitle = "Time";
const yUnit = units.find((u) => u.id === plot.y_unit);
const y2Unit = units.find((u) => u.id === plot.y_unit2);
const xUnit = units.find((u) => u.id === plot.x_unit);
if (yUnit) {
yAxisTitle = `${yAxisTitle} [${yUnit.symbol}]`;
}
if (y2Unit) {
y2AxisTitle = `${y2AxisTitle} [${y2Unit.symbol}]`;
}
if (xUnit) {
xAxisTitle = `${xAxisTitle} [${xUnit.symbol}]`;
}

return { yAxisTitle, y2AxisTitle, xAxisTitle };
};

export const getPlotDimensions = ({
dimensions,
isHorizontal,
Expand Down Expand Up @@ -428,10 +462,8 @@ export const getICLineShapes = ({
return shapes;
};

type PlotLayoutProps = {
type PlotAxesProps = {
plot: FieldArrayWithId<Simulation, "plots", "id">;
plotDimensions: { height: number; width: number };
shouldShowLegend: boolean;
xAxisTitle: string;
yAxisTitle: string;
y2AxisTitle: string;
Expand All @@ -442,15 +474,13 @@ type PlotLayoutProps = {
maxY2: number | undefined;
};
};
export const getPlotLayout: (props: PlotLayoutProps) => Partial<Layout> = ({
export const getPlotAxes = ({
plot,
plotDimensions,
shouldShowLegend,
xAxisTitle,
yAxisTitle,
y2AxisTitle,
yRanges: { minY, maxY, minY2, maxY2 },
}) => {
}: PlotAxesProps) => {
const axisScaleOptions: {
[key: string]: { type: "linear" | "log"; dtick?: number | string };
} = {
Expand All @@ -462,18 +492,6 @@ export const getPlotLayout: (props: PlotLayoutProps) => Partial<Layout> = ({
// setup range for y-axis
const { rangey, rangey2 } = ranges(minY, maxY, minY2, maxY2, plot);
return {
autosize: false,
width: plotDimensions?.width,
height: plotDimensions?.height,
dragmode: "pan",
showlegend: shouldShowLegend,
legend: {
orientation: "v",
yanchor: "top",
xanchor: "right",
y: 1,
x: 1,
},
xaxis: {
title: xAxisTitle,
automargin: true,
Expand All @@ -498,6 +516,30 @@ export const getPlotLayout: (props: PlotLayoutProps) => Partial<Layout> = ({
exponentformat: "power",
...axisScaleOptions[plot.y2_scale || "lin"],
},
} as Partial<Layout>;
};

type PlotLayoutProps = {
plotDimensions: { height: number; width: number };
shouldShowLegend: boolean;
};
export const getPlotLayout: (props: PlotLayoutProps) => Partial<Layout> = ({
plotDimensions,
shouldShowLegend,
}) => {
return {
autosize: false,
width: plotDimensions?.width,
height: plotDimensions?.height,
dragmode: "pan",
showlegend: shouldShowLegend,
legend: {
orientation: "v",
yanchor: "top",
xanchor: "right",
y: 1,
x: 1,
},
margin: {
l: 50,
r: 50,
Expand Down