Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

front: restore miniGET sync by storing scale domain #5601

Merged
merged 5 commits into from
Jan 26, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ import TimeButtons from 'modules/simulationResult/components/TimeButtons';
// TIMELINE DISABLED // import TimeLine from 'modules/simulationResult/components/TimeLine/TimeLine';
import TrainDetails from 'modules/simulationResult/components/TrainDetails';
import DriverTrainSchedule from 'modules/trainschedule/components/DriverTrainSchedule/DriverTrainSchedule';
import getScaleDomainFromValues from 'modules/simulationResult/components/ChartHelpers/getScaleDomainFromValues';
import SpaceCurvesSlopes from 'modules/simulationResult/components/SpaceCurvesSlopes';
import SpaceTimeChart from 'modules/simulationResult/components/SpaceTimeChart/SpaceTimeChart';
import SpeedSpaceChart from 'modules/simulationResult/components/SpeedSpaceChart/SpeedSpaceChart';
import type { PositionScaleDomain } from 'modules/simulationResult/consts';
import type { PositionScaleDomain, TimeScaleDomain } from 'modules/simulationResult/types';
import { Train } from 'reducers/osrdsimulation/types';
import { useStoreDataForSpaceTimeChart } from 'modules/simulationResult/components/SpaceTimeChart/useStoreDataForSpaceTimeChart';
import getScaleDomainFromValues from 'modules/simulationResult/components/ChartHelpers/getScaleDomainFromValues';

const MAP_MIN_HEIGHT = 450;

Expand Down Expand Up @@ -58,6 +58,11 @@ export default function SimulationResults({
const [initialHeightOfSpaceCurvesSlopesChart, setInitialHeightOfSpaceCurvesSlopesChart] =
useState(heightOfSpaceCurvesSlopesChart);

const [timeScaleDomain, setTimeScaleDomain] = useState<TimeScaleDomain>({
range: undefined,
source: undefined,
});

// X scale domain shared between SpeedSpace and SpaceCurvesSlopes charts.
const [positionScaleDomain, setPositionScaleDomain] = useState<PositionScaleDomain>({
initial: [],
Expand Down Expand Up @@ -151,13 +156,15 @@ export default function SimulationResults({
</div>

{/* SIMULATION: TIMELINE — TEMPORARILY DISABLED
{simulation.trains.length && chart && (
{simulation.trains.length && (
<TimeLine
chart={chart}
timeScaleDomain={timeScaleDomain}
selectedTrainId={selectedTrain?.id || simulation.trains[0].id}
trains={simulation.trains as SimulationReport[]}
onTimeScaleDomainChange={setTimeScaleDomain}
/>
)} */}
)}
*/}

{/* SIMULATION : SPACE TIME CHART */}
<div className="simulation-warped-map d-flex flex-row align-items-stretch mb-2 bg-white">
Expand Down Expand Up @@ -185,6 +192,8 @@ export default function SimulationResults({
dispatchUpdateSelectedTrainId={dispatchUpdateSelectedTrainId}
dispatchPersistentUpdateSimulation={dispatchPersistentUpdateSimulation}
setTrainResultsToFetch={setTrainResultsToFetch}
timeScaleDomain={timeScaleDomain}
setTimeScaleDomain={setTimeScaleDomain}
/>
)}
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,7 @@ import {
SimulationD3Scale,
PositionsSpeedTimes,
} from 'reducers/osrdsimulation/types';
import {
ChartAxes,
GRADIENT,
ListValues,
TIME,
XAxis,
Y2Axis,
YAxis,
} from 'modules/simulationResult/consts';
import { ChartAxes, ListValues, XAxis, Y2Axis, YAxis } from 'modules/simulationResult/consts';

export function sec2d3datetime(time: number) {
return d3.timeParse('%H:%M:%S')(sec2time(time));
Expand Down Expand Up @@ -383,9 +375,9 @@ const specificInterpolateOnTime =
return positionInterpolated;
};

export const isSpaceTimeChart = (keyValues: ChartAxes) => keyValues[0] === TIME;
export const isSpaceTimeChart = (keyValues: ChartAxes) => keyValues[0] === 'time';

export const isSpaceSlopesCurves = (keyValues: ChartAxes) => keyValues[1] === GRADIENT;
export const isSpaceSlopesCurves = (keyValues: ChartAxes) => keyValues[1] === 'gradient';

export function trainWithDepartureAndArrivalTimes(train: Train, dragOffset = 0) {
const firstStop = train.base.stops[0];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,8 @@ import {
getAxis,
isSpaceTimeChart,
} from 'modules/simulationResult/components/ChartHelpers/ChartHelpers';
import {
CHART_AXES,
LIST_VALUES,
type ChartAxes,
type PositionScaleDomain,
} from 'modules/simulationResult/consts';
import { CHART_AXES, LIST_VALUES, type ChartAxes } from 'modules/simulationResult/consts';
import type { PositionScaleDomain } from 'modules/simulationResult/types';
import drawGuideLines from 'modules/simulationResult/components/ChartHelpers/drawGuideLines';
import type { SpaceCurvesSlopesData } from 'modules/simulationResult/components/SpaceCurvesSlopes';
import type {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { useSelector } from 'react-redux';
import * as d3 from 'd3';
import { CgLoadbar } from 'react-icons/cg';

import { CHART_AXES, type PositionScaleDomain } from 'modules/simulationResult/consts';
import { CHART_AXES } from 'modules/simulationResult/consts';
import type { PositionScaleDomain } from 'modules/simulationResult/types';
import {
defineLinear,
interpolateOnPosition,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { noop } from 'lodash';
import React, { useEffect, useRef, useState, useCallback, useMemo } from 'react';
import React, { useEffect, useRef, useState, useCallback } from 'react';
import { CgLoadbar } from 'react-icons/cg';
import { GiResize } from 'react-icons/gi';
import { Rnd } from 'react-rnd';

import { CHART_AXES } from 'modules/simulationResult/consts';
import type { TimeScaleDomain } from 'modules/simulationResult/types';
import {
enableInteractivity,
traceVerticalLine,
Expand Down Expand Up @@ -51,11 +52,13 @@ export type SpaceTimeChartProps = {
selectedProjection?: OsrdSimulationState['selectedProjection'];
simulation?: SimulationSnapshot;
simulationIsPlaying?: boolean;
timeScaleDomain?: TimeScaleDomain;
isDisplayed?: boolean;
onSetBaseHeight?: (newHeight: number) => void;
dispatchUpdateSelectedTrainId: DispatchUpdateSelectedTrainId;
dispatchPersistentUpdateSimulation: DispatchPersistentUpdateSimulation;
setTrainResultsToFetch?: (trainSchedulesIDs?: number[]) => void;
setTimeScaleDomain?: (newTimeScaleDomain: TimeScaleDomain) => void;
};

export default function SpaceTimeChart(props: SpaceTimeChartProps) {
Expand All @@ -69,11 +72,13 @@ export default function SpaceTimeChart(props: SpaceTimeChartProps) {
selectedProjection,
simulation,
simulationIsPlaying = false,
timeScaleDomain,
isDisplayed = true,
onSetBaseHeight = noop,
dispatchUpdateSelectedTrainId,
dispatchPersistentUpdateSimulation,
setTrainResultsToFetch = noop,
setTimeScaleDomain,
} = props;

const [baseHeight, setBaseHeight] = useState(initialHeight);
Expand All @@ -88,15 +93,14 @@ export default function SpaceTimeChart(props: SpaceTimeChartProps) {
SimulationSnapshot['trains'] | undefined
>(undefined);

const timeScaleRange: [Date, Date] = useMemo(() => {
if (chart) return (rotate ? chart.y.domain() : chart.x.domain()) as [Date, Date];
return [new Date(), new Date()];
}, [chart]);

/* coordinate the vertical cursors with other graphs (GEV for instance) */
const { timePosition, updateTimePosition } = useChartSynchronizer(
(newTimePosition, positionValues) => {
if (dateIsInRange(newTimePosition, timeScaleRange)) {
if (
timeScaleDomain &&
timeScaleDomain.range &&
dateIsInRange(newTimePosition, timeScaleDomain.range)
) {
traceVerticalLine(chart, CHART_AXES.SPACE_TIME, positionValues, newTimePosition, rotate);
}
},
Expand Down Expand Up @@ -182,7 +186,8 @@ export default function SpaceTimeChart(props: SpaceTimeChartProps) {
const trainsToDraw = trainSimulations.map((train) =>
createTrain(CHART_AXES.SPACE_TIME, train as Train)
);
drawAllTrains(

const newDrawnedChart = drawAllTrains(
allowancesSettings,
chart,
CHART_ID,
Expand All @@ -193,11 +198,11 @@ export default function SpaceTimeChart(props: SpaceTimeChartProps) {
rotate,
selectedProjection,
selectedTrain as Train,
setChart,
setDragOffset,
trainSimulations as Train[],
trainsToDraw
);
setChart(newDrawnedChart);
setResetChart(false);
}
};
Expand All @@ -211,9 +216,29 @@ export default function SpaceTimeChart(props: SpaceTimeChartProps) {
redrawChart();
}, [resetChart, rotate, selectedTrain, trainSimulations, height]);

/* redraw the trains if the time scale range has changed from Timeline */
useEffect(() => {
if (chart && timeScaleDomain && timeScaleDomain.source !== 'SpaceTimeChart') {
const currentTimeRange = timeScaleDomain.range;
if (currentTimeRange) {
chart.x.domain(currentTimeRange);
redrawChart();
}
}
}, [timeScaleDomain]);

/* add behaviour on zoom and mousemove/mouseover/wheel on the new chart each time the chart changes */
useEffect(() => {
if (trainSimulations && selectedTrain) {
SharglutDev marked this conversation as resolved.
Show resolved Hide resolved
if (chart && selectedTrain) {
const newTimeScaleRange = (rotate ? chart.y.domain() : chart.x.domain()) as [Date, Date];

if (setTimeScaleDomain) {
setTimeScaleDomain({
range: newTimeScaleRange,
source: 'SpaceTimeChart',
});
}

const dataSimulation = createTrain(CHART_AXES.SPACE_TIME, selectedTrain as Train);
enableInteractivity(
chart,
Expand All @@ -223,7 +248,7 @@ export default function SpaceTimeChart(props: SpaceTimeChartProps) {
setChart,
simulationIsPlaying,
updateTimePosition,
timeScaleRange
newTimeScaleRange
);
}
}, [chart]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ const drawAllTrains = (
rotate: boolean,
selectedProjection: OsrdSimulationState['selectedProjection'],
selectedTrain: Train,
setChart: React.Dispatch<React.SetStateAction<Chart | undefined>>,
setDragOffset: React.Dispatch<React.SetStateAction<number>>,
simulationTrains: Train[],
trainsToDraw: SimulationTrain[]
Expand Down Expand Up @@ -96,7 +95,7 @@ const drawAllTrains = (
train
);
});
setChart(chartLocal);
return chartLocal;
};

export { drawOPs, drawAllTrains };
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,8 @@ import { GiResize } from 'react-icons/gi';
import { Rnd } from 'react-rnd';

import { LightRollingStock, SimulationReport } from 'common/api/osrdEditoastApi';
import {
CHART_AXES,
type PositionScaleDomain,
type ChartAxes,
} from 'modules/simulationResult/consts';
import { CHART_AXES, type ChartAxes } from 'modules/simulationResult/consts';
import type { PositionScaleDomain } from 'modules/simulationResult/types';
import {
enableInteractivity,
traceVerticalLine,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
createPowerRestrictionSegment,
DRAWING_KEYS,
} from 'applications/operationalStudies/consts';
import { POSITION, SPEED, HEIGHT, CHART_AXES } from 'modules/simulationResult/consts';
import { CHART_AXES } from 'modules/simulationResult/consts';
import drawArea from 'modules/simulationResult/components/ChartHelpers/drawArea';
import drawCurve from 'modules/simulationResult/components/ChartHelpers/drawCurve';
import defineChart from 'modules/simulationResult/components/ChartHelpers/defineChart';
Expand Down Expand Up @@ -39,19 +39,19 @@ function createChart(
let scaleY2: d3.ScaleLinear<number, number, never> = defineLinear(0, 0, 0);

if (chart === undefined || resetChart) {
const maxX = d3.max(trainSimulation.speed, (speedObject) => speedObject[POSITION]) as number;
const maxX = d3.max(trainSimulation.speed, (speedObject) => speedObject.position) as number;
scaleX = defineLinear(maxX + 100);

const maxY = d3.max(trainSimulation.speed, (speedObject) => speedObject[SPEED]) as number;
const maxY = d3.max(trainSimulation.speed, (speedObject) => speedObject.speed) as number;
scaleY = defineLinear(maxY);

const minY2 = d3.min(
trainSimulation.slopesCurve,
(speedObject) => speedObject[HEIGHT]
(speedObject) => speedObject.height
) as number;
const maxY2 = d3.max(
trainSimulation.slopesCurve,
(speedObject) => speedObject[HEIGHT]
(speedObject) => speedObject.height
) as number;
scaleY2 = chart === undefined ? defineLinear(maxY2, 0, minY2) : chart.y2;
} else {
Expand Down
Loading
Loading