@osrd-project/ui-charts 0.0.80 → 0.0.82
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/chronogram/components/Chronogram.d.ts +3 -0
- package/dist/chronogram/index.d.ts +4 -0
- package/dist/chronogram/lib/context.d.ts +6 -0
- package/dist/chronogram/lib/types.d.ts +21 -0
- package/dist/common/consts.d.ts +5 -0
- package/dist/common/context.d.ts +2 -0
- package/dist/{spaceTimeChart/utils → common/helpers}/colors.d.ts +1 -1
- package/dist/common/helpers/time.d.ts +31 -0
- package/dist/common/helpers/utils.d.ts +8 -0
- package/dist/common/hooks/useCanvas.d.ts +16 -0
- package/dist/{spaceTimeChart → common}/hooks/useSize.d.ts +0 -1
- package/dist/common/index.d.ts +5 -0
- package/dist/common/types.d.ts +104 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.esm.js +1893 -1442
- package/dist/index.esm.js.map +1 -1
- package/dist/manchette/assets/sampleData.d.ts +2 -0
- package/dist/manchette/components/Manchette.d.ts +6 -1
- package/dist/manchette/components/ManchetteWithSpaceTimeChart.d.ts +3 -3
- package/dist/manchette/components/Waypoint.d.ts +2 -1
- package/dist/manchette/hooks/useManchetteWithSpaceTimeChart.d.ts +3 -36
- package/dist/manchette/hooks/useSyncManchette.d.ts +94 -0
- package/dist/manchette/index.d.ts +1 -2
- package/dist/manchette/types.d.ts +13 -0
- package/dist/spaceTimeChart/components/ConflictLayer.d.ts +1 -1
- package/dist/spaceTimeChart/components/ConflictTooltip.d.ts +1 -1
- package/dist/spaceTimeChart/components/PathLayer.d.ts +2 -1
- package/dist/spaceTimeChart/components/Quadrilateral.d.ts +2 -1
- package/dist/spaceTimeChart/components/Tooltip.d.ts +1 -1
- package/dist/spaceTimeChart/hooks/useMouseInteractions.d.ts +36 -3
- package/dist/spaceTimeChart/index.d.ts +2 -5
- package/dist/spaceTimeChart/lib/consts.d.ts +0 -3
- package/dist/spaceTimeChart/lib/context.d.ts +3 -6
- package/dist/spaceTimeChart/lib/types.d.ts +4 -91
- package/dist/spaceTimeChart/utils/canvas.d.ts +2 -37
- package/dist/spaceTimeChart/utils/events.d.ts +1 -1
- package/dist/spaceTimeChart/utils/geometry.d.ts +1 -1
- package/dist/spaceTimeChart/utils/scales.d.ts +2 -3
- package/dist/spaceTimeChart/utils/snapping.d.ts +1 -1
- package/dist/spaceTimeChart/utils/vectors.d.ts +1 -1
- package/dist/speedSpaceChart/components/SpeedSpaceChart.d.ts +21 -2
- package/dist/speedSpaceChart/components/common/DetailsBox.d.ts +5 -2
- package/dist/speedSpaceChart/components/common/SettingsPanel.d.ts +2 -1
- package/dist/speedSpaceChart/components/const.d.ts +22 -6
- package/dist/speedSpaceChart/components/helpers/drawElements/reticle.d.ts +2 -1
- package/dist/speedSpaceChart/components/layers/ReticleLayer.d.ts +3 -1
- package/dist/speedSpaceChart/components/utils.d.ts +7 -3
- package/dist/speedSpaceChart/index.d.ts +2 -1
- package/dist/speedSpaceChart/types.d.ts +40 -0
- package/dist/theme.css +410 -195
- package/dist/theme.css.map +1 -1
- package/dist/trackOccupancyDiagram/components/TrackOccupancyCanvas.d.ts +1 -1
- package/dist/trackOccupancyDiagram/components/TrackOccupancyManchette.d.ts +1 -1
- package/dist/trackOccupancyDiagram/components/TrackOccupancyStandalone.d.ts +1 -1
- package/dist/trackOccupancyDiagram/components/helpers/drawElements/drawOccupancyZones.d.ts +8 -2
- package/dist/trackOccupancyDiagram/components/helpers/drawElements/drawOccupancyZonesTexts.d.ts +1 -1
- package/dist/trackOccupancyDiagram/components/helpers/drawElements/drawTracks.d.ts +1 -1
- package/dist/trackOccupancyDiagram/components/layers/OccupancyZonesLayer.d.ts +2 -2
- package/dist/trackOccupancyDiagram/components/layers/TracksLayer.d.ts +1 -1
- package/dist/trackOccupancyDiagram/components/utils.d.ts +1 -1
- package/dist/trackOccupancyDiagram/index.d.ts +2 -2
- package/dist/trackOccupancyDiagram/{components → lib}/types.d.ts +2 -1
- package/package.json +6 -6
- package/dist/manchette/hooks/usePaths.d.ts +0 -11
- package/dist/spaceTimeChart/hooks/useCanvas.d.ts +0 -19
- /package/dist/{spaceTimeChart/utils → common/helpers}/png.d.ts +0 -0
- /package/dist/{spaceTimeChart → common}/hooks/useDevicePixelRatio.d.ts +0 -0
- /package/dist/{spaceTimeChart/components → common/layers}/TimeCaptions.d.ts +0 -0
- /package/dist/{spaceTimeChart/components → common/layers}/TimeGraduations.d.ts +0 -0
- /package/dist/trackOccupancyDiagram/{components → lib}/consts.d.ts +0 -0
package/dist/index.esm.js
CHANGED
|
@@ -1,12 +1,164 @@
|
|
|
1
|
-
import React, { useState, useEffect,
|
|
1
|
+
import React, { createContext, useState, useEffect, useContext, useRef, useCallback, useMemo, Fragment } from 'react';
|
|
2
2
|
import cx from 'classnames';
|
|
3
|
-
import { clamp as clamp$1,
|
|
3
|
+
import { isEqual, inRange, clamp as clamp$1, last, flatten, sortBy, sum } from 'lodash';
|
|
4
4
|
import chroma from 'chroma-js';
|
|
5
|
-
import { ZoomOut, ZoomIn, Iterations,
|
|
6
|
-
import { Checkbox, Slider } from '@osrd-project/ui-core';
|
|
5
|
+
import { ZoomOut, ZoomIn, Iterations, Sliders, X, KebabHorizontal } from '@osrd-project/ui-icons';
|
|
6
|
+
import { Checkbox, CheckboxesTree, Slider } from '@osrd-project/ui-core';
|
|
7
7
|
import * as d3selection from 'd3-selection';
|
|
8
8
|
import * as d3zoom from 'd3-zoom';
|
|
9
9
|
|
|
10
|
+
const TimeChartCanvasContext = createContext(
|
|
11
|
+
// That value should never be used, since the context should always be accessed within a provider
|
|
12
|
+
undefined);
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* @param minT number timestamp
|
|
16
|
+
* @param maxT number timestamp
|
|
17
|
+
* @param timeRanges time frames (24h, 12h, 6h, …)
|
|
18
|
+
* @param gridlinesLevels width of the lines for each time frame
|
|
19
|
+
* @param formatter function to format de values inside the output object
|
|
20
|
+
* Keys are times in ms
|
|
21
|
+
* Values are the highest level on each time
|
|
22
|
+
*/
|
|
23
|
+
function computeVisibleTimeMarkers(minT, maxT, timeRanges, gridlinesLevels, formatter = (level) => ({ level })) {
|
|
24
|
+
const result = {};
|
|
25
|
+
const minTLocalOffset = new Date(minT).getTimezoneOffset() * 60 * 1000;
|
|
26
|
+
timeRanges.forEach((range, i) => {
|
|
27
|
+
const gridlinesLevel = gridlinesLevels[i];
|
|
28
|
+
if (!gridlinesLevel)
|
|
29
|
+
return;
|
|
30
|
+
let t = Math.floor((minT - minTLocalOffset) / range) * range + minTLocalOffset;
|
|
31
|
+
while (t <= maxT) {
|
|
32
|
+
if (t >= minT) {
|
|
33
|
+
result[t] = { ...formatter(gridlinesLevel, i), time: t };
|
|
34
|
+
}
|
|
35
|
+
t += range;
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
return Object.values(result);
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* To get crisp horizontal or vertical lines on a canvas, we must draw them as thin as possible, in
|
|
42
|
+
* terms of actual pixels on screen.
|
|
43
|
+
* The best way for this is:
|
|
44
|
+
* - To center lines 1, 3, 5... pixels wide in the middle of a pixel (0.5, 1.5, 2.5...)
|
|
45
|
+
* - To center lines 2, 4, 6... pixels wide between two pixels (0, 1, 2, 3...)
|
|
46
|
+
* Also, for HiDPi screens:
|
|
47
|
+
* - To center lines of "integer thicknesses" between two screen pixels (0, 0.5, 1, 1.5, 2...)
|
|
48
|
+
* - To center lines with `n + 0.5` thicknesses in the middle of a screen pixel (0.25, 0.75, 1.25, 1.75...)
|
|
49
|
+
* @param rawCoordinate Any input coordinate to fix
|
|
50
|
+
* @param lineWidth The width of the line to draw
|
|
51
|
+
* @param devicePixelRatio
|
|
52
|
+
*/
|
|
53
|
+
function getCrispLineCoordinate(rawCoordinate, lineWidth, devicePixelRatio = window.devicePixelRatio || 1) {
|
|
54
|
+
const centerOffset = Math.ceil(lineWidth * devicePixelRatio) / devicePixelRatio / 2;
|
|
55
|
+
return (Math.round((rawCoordinate - centerOffset) * devicePixelRatio) / devicePixelRatio + centerOffset);
|
|
56
|
+
}
|
|
57
|
+
// ========== Time Scaling ===========
|
|
58
|
+
// The following functions handle data translation to the time referential:
|
|
59
|
+
function getTimeToPixel(timeOrigin, pixelOffset, timeScale) {
|
|
60
|
+
return (time) => pixelOffset + (time - timeOrigin) / timeScale;
|
|
61
|
+
}
|
|
62
|
+
function getPixelToTime(timeOrigin, pixelOffset, timeScale) {
|
|
63
|
+
return (timePixel) => (timePixel - pixelOffset) * timeScale + timeOrigin;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const PICKING_LAYERS = ['paths', 'overlay'];
|
|
67
|
+
const LAYERS = ['background', 'graduations', 'paths', 'overlay', 'captions'];
|
|
68
|
+
// ========== DURATION CONSTANTS ==========
|
|
69
|
+
const SECOND = 1000;
|
|
70
|
+
const MINUTE = 60 * SECOND;
|
|
71
|
+
const HOUR = 60 * MINUTE;
|
|
72
|
+
|
|
73
|
+
const COLORS_TO_INDICES = {};
|
|
74
|
+
const INDICES_TO_COLORS = {};
|
|
75
|
+
const WHITE_ALPHA_75 = '#ffffffbf';
|
|
76
|
+
const BLACK_ALPHA_25 = '#00000025';
|
|
77
|
+
const GREY_50$3 = '#797671';
|
|
78
|
+
/**
|
|
79
|
+
* This function returns a unique hex color corresponding to the given index. The colors are
|
|
80
|
+
* generated as #000001, #000002 ... #0000ff, #000100 etc.
|
|
81
|
+
* These colors aim at representing the given indices on the picking layer.
|
|
82
|
+
*/
|
|
83
|
+
function indexToColor(index) {
|
|
84
|
+
if (INDICES_TO_COLORS[index])
|
|
85
|
+
return INDICES_TO_COLORS[index];
|
|
86
|
+
const color = `#${index.toString(16).padStart(6, '0')}`.toLowerCase();
|
|
87
|
+
COLORS_TO_INDICES[color] = index;
|
|
88
|
+
INDICES_TO_COLORS[index] = color;
|
|
89
|
+
return color;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* This function returns the index corresponding to the given color.
|
|
93
|
+
*/
|
|
94
|
+
function colorToIndex(color) {
|
|
95
|
+
return COLORS_TO_INDICES[color.toLowerCase()];
|
|
96
|
+
}
|
|
97
|
+
function componentToHex(c) {
|
|
98
|
+
return c.toString(16).padStart(2, '0').toLowerCase();
|
|
99
|
+
}
|
|
100
|
+
// The two following functions are just small helpers to help translating colors between RGB and
|
|
101
|
+
// Hex:
|
|
102
|
+
function rgbToHex(r, g, b) {
|
|
103
|
+
return `#${componentToHex(r)}${componentToHex(g)}${componentToHex(b)}`;
|
|
104
|
+
}
|
|
105
|
+
function hexToRgb(color) {
|
|
106
|
+
return chroma(color).rgba();
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* This function captures a list of canvas layers into a PNG image, and returns a blob with the
|
|
111
|
+
* PNG data if possible.
|
|
112
|
+
*/
|
|
113
|
+
async function getPNGBlob(canvases, layers, backgroundColor = '#fff') {
|
|
114
|
+
if (!layers.length)
|
|
115
|
+
throw Error('There must be at least one layer to capture.');
|
|
116
|
+
const firstCanvas = canvases[layers[0]];
|
|
117
|
+
const width = firstCanvas.width;
|
|
118
|
+
const height = firstCanvas.height;
|
|
119
|
+
// Create a new canvas, on which the different layers will be drawn:
|
|
120
|
+
const canvas = document.createElement('CANVAS');
|
|
121
|
+
canvas.setAttribute('width', width + '');
|
|
122
|
+
canvas.setAttribute('height', height + '');
|
|
123
|
+
const ctx = canvas.getContext('2d');
|
|
124
|
+
// Draw a white background first:
|
|
125
|
+
ctx.fillStyle = backgroundColor;
|
|
126
|
+
ctx.fillRect(0, 0, width, height);
|
|
127
|
+
// For each layer, draw it on our canvas:
|
|
128
|
+
layers.forEach((id) => {
|
|
129
|
+
ctx.drawImage(canvases[id], 0, 0, width, height, 0, 0, width, height);
|
|
130
|
+
});
|
|
131
|
+
// Save the canvas as a PNG image:
|
|
132
|
+
return new Promise((resolve, reject) => {
|
|
133
|
+
canvas.toBlob((blob) => {
|
|
134
|
+
if (blob)
|
|
135
|
+
resolve(blob);
|
|
136
|
+
else
|
|
137
|
+
reject();
|
|
138
|
+
}, 'image/png');
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// ============= Canvas ==============
|
|
143
|
+
/**
|
|
144
|
+
* This function returns the picking layers scaling ratio. We basically take the min of the screen
|
|
145
|
+
* pixels and the "HTML pixels", and divide it by two.
|
|
146
|
+
*
|
|
147
|
+
* This allows having a smaller picking stage to fill (so it's faster), while keeping a "good enough
|
|
148
|
+
* precision".
|
|
149
|
+
*/
|
|
150
|
+
function getPickingScalingRatio() {
|
|
151
|
+
const PICKING_DOWNSCALING_RATIO = 0.5;
|
|
152
|
+
const dpr = window.devicePixelRatio || 1;
|
|
153
|
+
// When devicePixelRatio is over 1 (like for Retina displays), we downscale based on the "HTML
|
|
154
|
+
// pixels":
|
|
155
|
+
if (dpr > 1)
|
|
156
|
+
return PICKING_DOWNSCALING_RATIO;
|
|
157
|
+
// When devicePixelRatio is under or equal to 1 (like when the user zooms out for instance), we
|
|
158
|
+
// downscale based on the actual "screen pixels" (to avoid having a too large scene to fill):
|
|
159
|
+
return PICKING_DOWNSCALING_RATIO * dpr;
|
|
160
|
+
}
|
|
161
|
+
|
|
10
162
|
function useDevicePixelRatio() {
|
|
11
163
|
const [ratio, setRatio] = useState(window.devicePixelRatio || 1);
|
|
12
164
|
useEffect(() => {
|
|
@@ -27,7 +179,6 @@ function useDevicePixelRatio() {
|
|
|
27
179
|
|
|
28
180
|
/**
|
|
29
181
|
* This hook takes a DOM element reference as input, and returns always updated width and height.
|
|
30
|
-
* It is an internal hook, and should only be used inside SpaceTimeChart.
|
|
31
182
|
*/
|
|
32
183
|
function useSize(dom) {
|
|
33
184
|
const [size, setSize] = useState({
|
|
@@ -57,761 +208,490 @@ function useSize(dom) {
|
|
|
57
208
|
return size;
|
|
58
209
|
}
|
|
59
210
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
* This context exports everything necessary to draw the chart.
|
|
63
|
-
* It is updated anytime the scales are updated.
|
|
64
|
-
*/
|
|
65
|
-
const SpaceTimeChartContext = createContext(
|
|
66
|
-
// That value should never be used, since the context should always be accessed within a provider
|
|
67
|
-
undefined);
|
|
68
|
-
/**
|
|
69
|
-
* This context only contains necessary helpers to (un)register drawing functions.
|
|
70
|
-
* It is basically never updated.
|
|
71
|
-
*/
|
|
72
|
-
const CanvasContext = createContext(
|
|
73
|
-
// That value should never be used, since the context should always be accessed within a provider
|
|
74
|
-
undefined);
|
|
75
|
-
/**
|
|
76
|
-
* This context exports everything necessary to draw the chart.
|
|
77
|
-
* It is updated anytime the scales are updated or the mouse moves.
|
|
78
|
-
*/
|
|
79
|
-
const MouseContext = createContext(
|
|
80
|
-
// That value should never be used, since the context should always be accessed within a provider
|
|
81
|
-
undefined);
|
|
82
|
-
|
|
83
|
-
const DEFAULT_PATH_END = 'stop';
|
|
84
|
-
// CANVAS SPECIFIC TYPES:
|
|
85
|
-
const PICKING_LAYERS = ['paths', 'overlay'];
|
|
86
|
-
const LAYERS = ['background', 'graduations', 'paths', 'overlay', 'captions'];
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
|
-
* This function returns the picking layers scaling ratio. We basically take the min of the screen
|
|
90
|
-
* pixels and the "HTML pixels", and divide it by two.
|
|
91
|
-
*
|
|
92
|
-
* This allows having a smaller picking stage to fill (so it's faster), while keeping a "good enough
|
|
93
|
-
* precision".
|
|
94
|
-
*/
|
|
95
|
-
function getPickingScalingRatio() {
|
|
96
|
-
const PICKING_DOWNSCALING_RATIO = 0.5;
|
|
97
|
-
const dpr = window.devicePixelRatio || 1;
|
|
98
|
-
// When devicePixelRatio is over 1 (like for Retina displays), we downscale based on the "HTML
|
|
99
|
-
// pixels":
|
|
100
|
-
if (dpr > 1)
|
|
101
|
-
return PICKING_DOWNSCALING_RATIO;
|
|
102
|
-
// When devicePixelRatio is under or equal to 1 (like when the user zooms out for instance), we
|
|
103
|
-
// downscale based on the actual "screen pixels" (to avoid having a too large scene to fill):
|
|
104
|
-
return PICKING_DOWNSCALING_RATIO * dpr;
|
|
105
|
-
}
|
|
211
|
+
const PICKING = 'picking';
|
|
212
|
+
const RENDERING = 'rendering';
|
|
106
213
|
/**
|
|
107
|
-
* This
|
|
108
|
-
* antialiasing. This is very useful to handle picking, since it is not possible to disable
|
|
109
|
-
* antialiasing with the native JavaScript canvas APIs.
|
|
214
|
+
* This hook handles the internal canvas drawing logic of a chart component.
|
|
110
215
|
*/
|
|
111
|
-
function
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
const
|
|
120
|
-
const
|
|
121
|
-
const
|
|
122
|
-
const
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
imageData.data[index] = r;
|
|
165
|
-
imageData.data[index + 1] = g;
|
|
166
|
-
imageData.data[index + 2] = b;
|
|
167
|
-
imageData.data[index + 3] = 255;
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
/**
|
|
173
|
-
* This function takes an integer radius, and returns a flat matrix of 1s and 0s, where the 1s
|
|
174
|
-
* represent the pixels that are within the disc. The shapes are cached, to make it faster to draw
|
|
175
|
-
* a lot of times discs of the same radius.
|
|
176
|
-
*
|
|
177
|
-
* Here are some examples to make it clearer what the output should look like:
|
|
178
|
-
*
|
|
179
|
-
* getAliasedDiscShape(0);
|
|
180
|
-
* [1]
|
|
181
|
-
* getAliasedDiscShape(1);
|
|
182
|
-
* [0, 1, 0,
|
|
183
|
-
* 1, 1, 1,
|
|
184
|
-
* 0, 1, 0]
|
|
185
|
-
* getAliasedDiscShape(2);
|
|
186
|
-
* [0, 0, 1, 0, 0,
|
|
187
|
-
* 0, 1, 1, 1, 0,
|
|
188
|
-
* 1, 1, 1, 1, 1,
|
|
189
|
-
* 0, 1, 1, 1, 0,
|
|
190
|
-
* 0, 0, 1, 0, 0]
|
|
191
|
-
*/
|
|
192
|
-
const DISCS_CACHE = new Map();
|
|
193
|
-
function getAliasedDiscShape(radius) {
|
|
194
|
-
const cachedShape = DISCS_CACHE.get(radius);
|
|
195
|
-
if (cachedShape)
|
|
196
|
-
return cachedShape;
|
|
197
|
-
const diameter = radius * 2 + 1;
|
|
198
|
-
const shape = new Uint8Array(diameter * diameter);
|
|
199
|
-
for (let dy = -radius; dy <= radius; dy++) {
|
|
200
|
-
for (let dx = -radius; dx <= radius; dx++) {
|
|
201
|
-
const distance = dx * dx + dy * dy;
|
|
202
|
-
if (distance <= radius * radius) {
|
|
203
|
-
const x = dx + radius;
|
|
204
|
-
const y = dy + radius;
|
|
205
|
-
shape[y * diameter + x] = 1;
|
|
216
|
+
function useCanvas$1(dom, chartContext, position) {
|
|
217
|
+
// Most things are handled through refs here so that we have a very precise control on when to
|
|
218
|
+
// render anything:
|
|
219
|
+
const canvasesRef = useRef({});
|
|
220
|
+
const contextsRef = useRef({});
|
|
221
|
+
const pickingFunctions = useRef(PICKING_LAYERS.reduce((iter, layer) => ({ ...iter, [layer]: new Set() }), {}));
|
|
222
|
+
const drawingFunctions = useRef(LAYERS.reduce((iter, layer) => ({ ...iter, [layer]: new Set() }), {}));
|
|
223
|
+
const chartContextRef = useRef(chartContext);
|
|
224
|
+
const scheduledRef = useRef(null);
|
|
225
|
+
const [hoveredItem, setHoveredItem] = useState(null);
|
|
226
|
+
const size = useSize(dom);
|
|
227
|
+
const sizeRef = useRef(size);
|
|
228
|
+
/**
|
|
229
|
+
* Keep the sizeRef value up to date (to allow rendering functions not being reset anytime size is
|
|
230
|
+
* updated):
|
|
231
|
+
*/
|
|
232
|
+
useEffect(() => {
|
|
233
|
+
sizeRef.current = size;
|
|
234
|
+
}, [size]);
|
|
235
|
+
const devicePixelRatio = useDevicePixelRatio();
|
|
236
|
+
/**
|
|
237
|
+
* This function renders all picking layers:
|
|
238
|
+
*/
|
|
239
|
+
const drawPicking = useCallback((context, layers) => {
|
|
240
|
+
context.resetPickingElements();
|
|
241
|
+
PICKING_LAYERS.forEach((layer) => {
|
|
242
|
+
if (layers && !layers.has(layer))
|
|
243
|
+
return;
|
|
244
|
+
const ctx = contextsRef.current[`${PICKING}-${layer}`];
|
|
245
|
+
const set = pickingFunctions.current[layer];
|
|
246
|
+
if (ctx && ctx.canvas.width && ctx.canvas.height) {
|
|
247
|
+
const { width, height } = sizeRef.current;
|
|
248
|
+
ctx.clearRect(0, 0, width, height);
|
|
249
|
+
const imageData = ctx.getImageData(0, 0, ctx.canvas.width, ctx.canvas.height);
|
|
250
|
+
const pickingScalingRatio = getPickingScalingRatio();
|
|
251
|
+
set.forEach((fn) => fn(imageData, context, pickingScalingRatio));
|
|
252
|
+
ctx.putImageData(imageData, 0, 0);
|
|
253
|
+
}
|
|
254
|
+
});
|
|
255
|
+
}, []);
|
|
256
|
+
/**
|
|
257
|
+
* This function renders all visible / rendering layers:
|
|
258
|
+
*/
|
|
259
|
+
const drawRendering = useCallback((context, layers) => {
|
|
260
|
+
LAYERS.forEach((layer) => {
|
|
261
|
+
if (layers && !layers.has(layer))
|
|
262
|
+
return;
|
|
263
|
+
const ctx = contextsRef.current[`${RENDERING}-${layer}`];
|
|
264
|
+
const set = drawingFunctions.current[layer];
|
|
265
|
+
if (ctx) {
|
|
266
|
+
const { width, height } = sizeRef.current;
|
|
267
|
+
ctx.clearRect(0, 0, width, height);
|
|
268
|
+
set.forEach((fn) => fn(ctx, context));
|
|
206
269
|
}
|
|
270
|
+
});
|
|
271
|
+
}, []);
|
|
272
|
+
/**
|
|
273
|
+
* This function draws everything that needs to be drawn, and clears the scheduleRef state:
|
|
274
|
+
*/
|
|
275
|
+
const draw = useCallback(() => {
|
|
276
|
+
drawRendering(chartContextRef.current);
|
|
277
|
+
drawPicking(chartContextRef.current);
|
|
278
|
+
if (scheduledRef.current) {
|
|
279
|
+
window.cancelAnimationFrame(scheduledRef.current.frameId);
|
|
280
|
+
scheduledRef.current = null;
|
|
207
281
|
}
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
282
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
283
|
+
}, []);
|
|
284
|
+
/**
|
|
285
|
+
* This function schedules a full render for next frame, if no scheduling has been done yet:
|
|
286
|
+
*/
|
|
287
|
+
const scheduleRendering = useCallback(() => {
|
|
288
|
+
if (!scheduledRef.current) {
|
|
289
|
+
scheduledRef.current = {
|
|
290
|
+
frameId: window.requestAnimationFrame(draw),
|
|
291
|
+
};
|
|
292
|
+
}
|
|
293
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
294
|
+
}, []);
|
|
295
|
+
/**
|
|
296
|
+
* This function helpers registering a drawing function on a given layer:
|
|
297
|
+
*/
|
|
298
|
+
const register = useCallback(({ type, layer, fn }) => {
|
|
299
|
+
if (type === 'picking') {
|
|
300
|
+
const set = pickingFunctions.current[layer];
|
|
301
|
+
if (set.has(fn))
|
|
302
|
+
throw new Error('This picking function has already been registered.');
|
|
303
|
+
set.add(fn);
|
|
304
|
+
}
|
|
305
|
+
else if (type === 'rendering') {
|
|
306
|
+
const set = drawingFunctions.current[layer];
|
|
307
|
+
if (set.has(fn))
|
|
308
|
+
throw new Error('This drawing function has already been registered.');
|
|
309
|
+
set.add(fn);
|
|
310
|
+
}
|
|
311
|
+
scheduleRendering();
|
|
312
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
313
|
+
}, []);
|
|
314
|
+
/**
|
|
315
|
+
* This function helpers unregistering a drawing function from a given layer:
|
|
316
|
+
*/
|
|
317
|
+
const unregister = useCallback(({ type, layer, fn }) => {
|
|
318
|
+
if (type === 'picking') {
|
|
319
|
+
const set = pickingFunctions.current[layer];
|
|
320
|
+
if (!set.has(fn))
|
|
321
|
+
throw new Error('This picking function has not been registered.');
|
|
322
|
+
set.delete(fn);
|
|
323
|
+
}
|
|
324
|
+
else if (type === 'rendering') {
|
|
325
|
+
const set = drawingFunctions.current[layer];
|
|
326
|
+
if (!set.has(fn))
|
|
327
|
+
throw new Error('This drawing function has not been registered.');
|
|
328
|
+
set.delete(fn);
|
|
329
|
+
}
|
|
330
|
+
scheduleRendering();
|
|
331
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
332
|
+
}, []);
|
|
333
|
+
const captureCanvases = useCallback(() => getPNGBlob(canvasesRef.current, LAYERS.map((layer) => `${RENDERING}-${layer}`), chartContextRef.current.theme.background), []);
|
|
334
|
+
// Create all canvas layers:
|
|
335
|
+
useEffect(() => {
|
|
336
|
+
if (!dom)
|
|
337
|
+
return;
|
|
338
|
+
const canvases = canvasesRef.current;
|
|
339
|
+
const contexts = contextsRef.current;
|
|
340
|
+
// Create missing layers:
|
|
341
|
+
const allLayers = [
|
|
342
|
+
...LAYERS.map((layer) => ({ layer, type: 'rendering' })),
|
|
343
|
+
...PICKING_LAYERS.map((layer) => ({ layer, type: 'picking' })),
|
|
344
|
+
];
|
|
345
|
+
allLayers.forEach(({ layer, type }) => {
|
|
346
|
+
const layerId = `${type}-${layer}`;
|
|
347
|
+
if (!canvases[layerId]) {
|
|
348
|
+
const canvas = document.createElement('CANVAS');
|
|
349
|
+
const ctx = canvas.getContext('2d');
|
|
350
|
+
canvas.classList.add(`layer-${type}-${layer}`);
|
|
351
|
+
canvas.style.position = 'absolute';
|
|
352
|
+
canvas.style.inset = '0';
|
|
353
|
+
dom.appendChild(canvas);
|
|
354
|
+
canvases[layerId] = canvas;
|
|
355
|
+
contexts[layerId] = ctx;
|
|
356
|
+
// Draw layer if functions are already registered:
|
|
357
|
+
if (type === 'picking') {
|
|
358
|
+
// (also hide picking layers)
|
|
359
|
+
canvas.style.display = 'none';
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
});
|
|
363
|
+
}, [dom]);
|
|
364
|
+
// Redraw all layers when fingerprint changes:
|
|
365
|
+
useEffect(() => {
|
|
366
|
+
// Cache latest context in ref:
|
|
367
|
+
chartContextRef.current = chartContext;
|
|
368
|
+
scheduleRendering();
|
|
369
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
370
|
+
}, [chartContext.fingerprint]);
|
|
371
|
+
// Handle resizing:
|
|
372
|
+
useEffect(() => {
|
|
373
|
+
const pickingScalingRatio = getPickingScalingRatio();
|
|
374
|
+
for (const id in canvasesRef.current) {
|
|
375
|
+
const canvas = canvasesRef.current[id];
|
|
376
|
+
const ctx = contextsRef.current[id];
|
|
377
|
+
const isPicking = id.split('-')[0] === PICKING;
|
|
378
|
+
if (canvas) {
|
|
379
|
+
const ratio = isPicking ? pickingScalingRatio : devicePixelRatio;
|
|
380
|
+
canvas.style.width = size.width + 'px';
|
|
381
|
+
canvas.style.height = size.height + 'px';
|
|
382
|
+
canvas.setAttribute('width', Math.max(1, size.width * ratio) + 'px');
|
|
383
|
+
canvas.setAttribute('height', Math.max(1, size.height * ratio) + 'px');
|
|
384
|
+
if (isPicking)
|
|
385
|
+
canvas.style.imageRendering = 'pixelated';
|
|
386
|
+
if (!isPicking) {
|
|
387
|
+
// Reset the transform to identity, then apply the new scale
|
|
388
|
+
ctx.setTransform(1, 0, 0, 1, 0, 0);
|
|
389
|
+
ctx.scale(devicePixelRatio, devicePixelRatio);
|
|
237
390
|
}
|
|
238
391
|
}
|
|
239
392
|
}
|
|
240
|
-
|
|
393
|
+
draw();
|
|
394
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
395
|
+
}, [size, devicePixelRatio]);
|
|
396
|
+
// Read picking layer on position change:
|
|
397
|
+
useEffect(() => {
|
|
398
|
+
let newHoveredItem = null;
|
|
399
|
+
const pickingScalingRatio = getPickingScalingRatio();
|
|
400
|
+
PICKING_LAYERS.some((layer) => {
|
|
401
|
+
const ctx = contextsRef.current[`${PICKING}-${layer}`];
|
|
402
|
+
if (ctx && position) {
|
|
403
|
+
const [r, g, b, a] = ctx.getImageData(Math.round(position.x * pickingScalingRatio), Math.round(position.y * pickingScalingRatio), 1, 1).data;
|
|
404
|
+
if (a === 255) {
|
|
405
|
+
const color = rgbToHex(r, g, b);
|
|
406
|
+
const index = colorToIndex(color);
|
|
407
|
+
const element = chartContextRef.current.pickingElements[index];
|
|
408
|
+
newHoveredItem = {
|
|
409
|
+
layer,
|
|
410
|
+
element,
|
|
411
|
+
};
|
|
412
|
+
return true;
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
return false;
|
|
416
|
+
});
|
|
417
|
+
if (!isEqual(newHoveredItem, hoveredItem)) {
|
|
418
|
+
setHoveredItem(newHoveredItem);
|
|
419
|
+
}
|
|
420
|
+
}, [position, hoveredItem]);
|
|
421
|
+
// Keep the canvas context up to date:
|
|
422
|
+
const canvasContext = useMemo(() => ({ register, unregister, captureCanvases }), [register, unregister, captureCanvases]);
|
|
423
|
+
return {
|
|
424
|
+
canvasContext,
|
|
425
|
+
hoveredItem,
|
|
426
|
+
};
|
|
241
427
|
}
|
|
242
428
|
/**
|
|
243
|
-
*
|
|
244
|
-
*
|
|
245
|
-
* vertices[0] ________ vertices[1]
|
|
246
|
-
* / /
|
|
247
|
-
* vertices[3] /________/ vertices[2]
|
|
248
|
-
*
|
|
429
|
+
* This hook helps to bind a picking function to a layer in a Chart.
|
|
249
430
|
*/
|
|
250
|
-
function
|
|
251
|
-
const
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
if (x < xmin)
|
|
259
|
-
xmin = x;
|
|
260
|
-
else if (x > xmax)
|
|
261
|
-
xmax = x;
|
|
262
|
-
if (y < ymin)
|
|
263
|
-
ymin = y;
|
|
264
|
-
else if (y > ymax)
|
|
265
|
-
ymax = y;
|
|
266
|
-
}
|
|
267
|
-
xmin = clamp$1(xmin, 0, imageData.width - 1);
|
|
268
|
-
ymin = clamp$1(ymin, 0, imageData.height - 1);
|
|
269
|
-
xmax = clamp$1(xmax, 0, imageData.width - 1);
|
|
270
|
-
ymax = clamp$1(ymax, 0, imageData.height - 1);
|
|
271
|
-
for (let y = ymin; y < ymax; y++) {
|
|
272
|
-
for (let x = xmin; x < xmax; x++) {
|
|
273
|
-
// Compute whether the point is inside the quadrilateral
|
|
274
|
-
// using the Ray casting algorithm (see https://en.wikipedia.org/wiki/Point_in_polygon)
|
|
275
|
-
let isInside = false;
|
|
276
|
-
for (let i = 0; i < vertices.length; i++) {
|
|
277
|
-
const pointA = vertices[i];
|
|
278
|
-
const pointB = i === vertices.length - 1 ? vertices[0] : vertices[i + 1];
|
|
279
|
-
// Line equation from A to B: y(x) = m * x + c
|
|
280
|
-
const m = (pointB.y - pointA.y) / (pointB.x - pointA.x);
|
|
281
|
-
const c = pointB.y - m * pointB.x;
|
|
282
|
-
// Invert the equation: x(y) = (y - c) / m
|
|
283
|
-
// Look only for points that are on the left of the line, so for a given y, x < (y - c) / m
|
|
284
|
-
if (pointA.y > y != pointB.y > y && x < (y - c) / m) {
|
|
285
|
-
isInside = !isInside;
|
|
286
|
-
}
|
|
287
|
-
}
|
|
288
|
-
// If the point is inside the quadrilateral, allow interactions with it
|
|
289
|
-
if (isInside) {
|
|
290
|
-
const index = (y * imageData.width + x) * 4;
|
|
291
|
-
imageData.data[index] = r;
|
|
292
|
-
imageData.data[index + 1] = g;
|
|
293
|
-
imageData.data[index + 2] = b;
|
|
294
|
-
imageData.data[index + 3] = 255;
|
|
295
|
-
}
|
|
296
|
-
}
|
|
297
|
-
}
|
|
298
|
-
}
|
|
299
|
-
function drawAliasedRect(imageData, { x, y }, width, height, [r, g, b], scalingRatio = 1) {
|
|
300
|
-
x = Math.round(x * scalingRatio);
|
|
301
|
-
y = Math.round(y * scalingRatio);
|
|
302
|
-
width = Math.round(width * scalingRatio);
|
|
303
|
-
height = Math.round(height * scalingRatio);
|
|
304
|
-
const xMin = clamp$1(x, 0, imageData.width);
|
|
305
|
-
const yMin = clamp$1(y, 0, imageData.height);
|
|
306
|
-
const xMax = clamp$1(x + width, 0, imageData.width);
|
|
307
|
-
const yMax = clamp$1(y + height, 0, imageData.height);
|
|
308
|
-
for (let i = xMin; i < xMax; i++) {
|
|
309
|
-
for (let j = yMin; j < yMax; j++) {
|
|
310
|
-
const index = (j * imageData.width + i) * 4;
|
|
311
|
-
imageData.data[index] = r;
|
|
312
|
-
imageData.data[index + 1] = g;
|
|
313
|
-
imageData.data[index + 2] = b;
|
|
314
|
-
imageData.data[index + 3] = 255;
|
|
315
|
-
}
|
|
316
|
-
}
|
|
431
|
+
function usePicking(contextKey, layer, fn) {
|
|
432
|
+
const { register, unregister } = useContext(contextKey);
|
|
433
|
+
useEffect(() => {
|
|
434
|
+
register({ type: 'picking', layer, fn });
|
|
435
|
+
return () => {
|
|
436
|
+
unregister({ type: 'picking', layer, fn });
|
|
437
|
+
};
|
|
438
|
+
}, [layer, fn, register, unregister]);
|
|
317
439
|
}
|
|
318
440
|
/**
|
|
319
|
-
* This function
|
|
320
|
-
* That handles a path that stops or starts exactly in an operational points included in the line
|
|
321
|
-
* represented in the chart.
|
|
441
|
+
* This hook helps to bind a drawing function to a layer in a chart.
|
|
322
442
|
*/
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
ctx.moveTo(spacePixel - STOP_END_SIZE / 2, timePixel);
|
|
332
|
-
ctx.lineTo(spacePixel + STOP_END_SIZE / 2, timePixel);
|
|
333
|
-
}
|
|
334
|
-
ctx.stroke();
|
|
443
|
+
function useDraw(contextKey, layer, fn) {
|
|
444
|
+
const { register, unregister } = useContext(contextKey);
|
|
445
|
+
useEffect(() => {
|
|
446
|
+
register({ type: 'rendering', layer, fn });
|
|
447
|
+
return () => {
|
|
448
|
+
unregister({ type: 'rendering', layer, fn });
|
|
449
|
+
};
|
|
450
|
+
}, [layer, fn, register, unregister]);
|
|
335
451
|
}
|
|
452
|
+
|
|
453
|
+
const MARGIN = 100;
|
|
454
|
+
const MINUTE_OPTIONS = {
|
|
455
|
+
minute: '2-digit',
|
|
456
|
+
hour12: false,
|
|
457
|
+
};
|
|
458
|
+
const HOUR_OPTIONS_SHORT = {
|
|
459
|
+
hour: '2-digit',
|
|
460
|
+
hour12: false,
|
|
461
|
+
};
|
|
462
|
+
const HOUR_OPTIONS_LONG = {
|
|
463
|
+
hour: '2-digit',
|
|
464
|
+
minute: '2-digit',
|
|
465
|
+
hour12: false,
|
|
466
|
+
};
|
|
467
|
+
const DATE_OPTIONS = {
|
|
468
|
+
day: '2-digit',
|
|
469
|
+
month: '2-digit',
|
|
470
|
+
year: 'numeric',
|
|
471
|
+
};
|
|
472
|
+
const MINUTES_FORMATTER = (t) => {
|
|
473
|
+
const minutes = new Date(t).toLocaleTimeString(undefined, MINUTE_OPTIONS);
|
|
474
|
+
return `:${minutes}`;
|
|
475
|
+
};
|
|
476
|
+
const HOURS_FORMATTER = (t, pixelsPerMinute) => new Date(t).toLocaleTimeString(undefined, pixelsPerMinute > 1 ? HOUR_OPTIONS_LONG : HOUR_OPTIONS_SHORT);
|
|
477
|
+
const DATES_FORMATER = (t) => new Date(t).toLocaleDateString(undefined, DATE_OPTIONS);
|
|
478
|
+
const RANGES_FORMATER = [
|
|
479
|
+
() => '',
|
|
480
|
+
() => '',
|
|
481
|
+
MINUTES_FORMATTER,
|
|
482
|
+
MINUTES_FORMATTER,
|
|
483
|
+
MINUTES_FORMATTER,
|
|
484
|
+
MINUTES_FORMATTER,
|
|
485
|
+
HOURS_FORMATTER,
|
|
486
|
+
HOURS_FORMATTER,
|
|
487
|
+
HOURS_FORMATTER,
|
|
488
|
+
HOURS_FORMATTER,
|
|
489
|
+
HOURS_FORMATTER,
|
|
490
|
+
];
|
|
491
|
+
const TimeCaptions = () => {
|
|
492
|
+
const drawingFunction = useCallback((ctx, { timeScale, timeOrigin, timePixelOffset, getTimePixel, width, height, theme: { background, breakpoints, timeRanges, timeCaptionsPriorities, timeCaptionsStyles, timeGraduationsStyles, dateCaptionsStyle, }, captionSize = 40, swapAxis = false, hideTimeCaptions = false, hideDates = false, showTicks = false, }) => {
|
|
493
|
+
if (hideTimeCaptions)
|
|
494
|
+
return;
|
|
495
|
+
const timeAxisSize = !swapAxis ? width : height;
|
|
496
|
+
const spaceAxisSize = (!swapAxis ? height : width) - captionSize;
|
|
497
|
+
// Add some margin, so that captions of times right outside the stage are still visible:
|
|
498
|
+
const minT = timeOrigin - timeScale * (timePixelOffset + MARGIN);
|
|
499
|
+
const maxT = minT + timeScale * (width + MARGIN * 2);
|
|
500
|
+
// Find which styles to apply, relatively to the timescale (i.e. horizontal zoom level):
|
|
501
|
+
const pixelsPerMinute = (1 / timeScale) * MINUTE;
|
|
502
|
+
let labelLevels = [];
|
|
503
|
+
breakpoints.some((breakpoint, i) => {
|
|
504
|
+
if (pixelsPerMinute < breakpoint) {
|
|
505
|
+
labelLevels = timeCaptionsPriorities[i];
|
|
506
|
+
return true;
|
|
507
|
+
}
|
|
508
|
+
return false;
|
|
509
|
+
});
|
|
510
|
+
let labelMarks = computeVisibleTimeMarkers(minT, maxT, timeRanges, labelLevels, (level, i) => ({
|
|
511
|
+
level,
|
|
512
|
+
styles: timeCaptionsStyles[level],
|
|
513
|
+
formatter: RANGES_FORMATER[i],
|
|
514
|
+
}));
|
|
515
|
+
if (!hideDates)
|
|
516
|
+
labelMarks = labelMarks.concat(computeVisibleTimeMarkers(minT, maxT, [24 * HOUR], [1], (level) => ({
|
|
517
|
+
level,
|
|
518
|
+
styles: dateCaptionsStyle,
|
|
519
|
+
formatter: DATES_FORMATER,
|
|
520
|
+
})));
|
|
521
|
+
// Render caption background:
|
|
522
|
+
ctx.fillStyle = background;
|
|
523
|
+
if (!swapAxis) {
|
|
524
|
+
ctx.fillStyle = WHITE_ALPHA_75;
|
|
525
|
+
ctx.fillRect(0, 0, width, 24);
|
|
526
|
+
ctx.fillStyle = background;
|
|
527
|
+
ctx.fillRect(0, spaceAxisSize, timeAxisSize, captionSize);
|
|
528
|
+
}
|
|
529
|
+
else {
|
|
530
|
+
ctx.fillStyle = background;
|
|
531
|
+
ctx.fillRect(0, 0, captionSize, timeAxisSize);
|
|
532
|
+
}
|
|
533
|
+
// Render time captions:
|
|
534
|
+
labelMarks.forEach(({ styles, formatter, time }) => {
|
|
535
|
+
const text = formatter(time, pixelsPerMinute);
|
|
536
|
+
ctx.textAlign = styles.textAlign || 'center';
|
|
537
|
+
ctx.textBaseline = 'top';
|
|
538
|
+
ctx.fillStyle = styles.color;
|
|
539
|
+
ctx.lineWidth = 5;
|
|
540
|
+
ctx.strokeStyle = background;
|
|
541
|
+
ctx.lineCap = 'butt';
|
|
542
|
+
ctx.font = `${styles.fontWeight || 'normal'} ${styles.font}`;
|
|
543
|
+
const timePixel = getCrispLineCoordinate(getTimePixel(time), ctx.lineWidth);
|
|
544
|
+
if (!swapAxis) {
|
|
545
|
+
ctx.beginPath();
|
|
546
|
+
ctx.strokeStyle = timeCaptionsStyles[1].color;
|
|
547
|
+
ctx.lineWidth = 1;
|
|
548
|
+
let tickHeight = 4;
|
|
549
|
+
const mod = time % (60 * 60 * 1000);
|
|
550
|
+
if (mod === 0) {
|
|
551
|
+
tickHeight = 8;
|
|
552
|
+
}
|
|
553
|
+
else if (mod % (30 * 60 * 1000) === 0) {
|
|
554
|
+
tickHeight = 6;
|
|
555
|
+
}
|
|
556
|
+
ctx.moveTo(timePixel, spaceAxisSize);
|
|
557
|
+
ctx.lineTo(timePixel, spaceAxisSize + tickHeight);
|
|
558
|
+
ctx.moveTo(timePixel, 0);
|
|
559
|
+
ctx.lineTo(timePixel, tickHeight);
|
|
560
|
+
ctx.stroke();
|
|
561
|
+
ctx.fillStyle = timeCaptionsStyles[1].color;
|
|
562
|
+
ctx.fillText(text, timePixel, styles.topOffset || 0);
|
|
563
|
+
ctx.fillText(text, timePixel, spaceAxisSize + (styles.topOffset || 0));
|
|
564
|
+
}
|
|
565
|
+
else {
|
|
566
|
+
ctx.save();
|
|
567
|
+
ctx.translate(captionSize - (styles.topOffset || 0), timePixel);
|
|
568
|
+
ctx.rotate(Math.PI / 2);
|
|
569
|
+
ctx.strokeText(text, 0, 0);
|
|
570
|
+
ctx.fillText(text, 0, 0);
|
|
571
|
+
ctx.restore();
|
|
572
|
+
}
|
|
573
|
+
});
|
|
574
|
+
// Render caption top border:
|
|
575
|
+
ctx.strokeStyle = timeGraduationsStyles[1].color;
|
|
576
|
+
ctx.lineWidth = timeGraduationsStyles[1].width;
|
|
577
|
+
if (!showTicks) {
|
|
578
|
+
ctx.beginPath();
|
|
579
|
+
if (!swapAxis) {
|
|
580
|
+
const y = getCrispLineCoordinate(spaceAxisSize, ctx.lineWidth);
|
|
581
|
+
ctx.moveTo(0, y);
|
|
582
|
+
ctx.lineTo(timeAxisSize, y);
|
|
583
|
+
}
|
|
584
|
+
else {
|
|
585
|
+
const x = getCrispLineCoordinate(captionSize, ctx.lineWidth);
|
|
586
|
+
ctx.moveTo(x, 0);
|
|
587
|
+
ctx.lineTo(x, timeAxisSize);
|
|
588
|
+
}
|
|
589
|
+
ctx.stroke();
|
|
590
|
+
}
|
|
591
|
+
}, []);
|
|
592
|
+
useDraw(TimeChartCanvasContext, 'captions', drawingFunction);
|
|
593
|
+
return null;
|
|
594
|
+
};
|
|
595
|
+
|
|
596
|
+
// There are three different contexts because they have very different lifecycles:
|
|
597
|
+
// - MouseContext
|
|
598
|
+
// - SpaceTimeChartContext
|
|
599
|
+
// - SpaceTimeChartCanvasContext
|
|
336
600
|
/**
|
|
337
|
-
* This
|
|
338
|
-
*
|
|
601
|
+
* This context exports everything necessary to draw the chart.
|
|
602
|
+
* It is updated anytime the scales are updated.
|
|
339
603
|
*/
|
|
340
|
-
const
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
let x = timePixel;
|
|
347
|
-
let y = spacePixel;
|
|
348
|
-
if (swapAxis) {
|
|
349
|
-
[horizontalSign, verticalSign] = [verticalSign, horizontalSign];
|
|
350
|
-
[controlX, controlY] = [controlY, controlX];
|
|
351
|
-
[x, y] = [y, x];
|
|
352
|
-
}
|
|
353
|
-
ctx.beginPath();
|
|
354
|
-
ctx.moveTo(x, y);
|
|
355
|
-
ctx.bezierCurveTo(controlX, controlY, controlX, controlY, x + OUT_END_SIZE * horizontalSign, y + OUT_END_SIZE * verticalSign);
|
|
356
|
-
ctx.stroke();
|
|
357
|
-
}
|
|
604
|
+
const SpaceTimeChartContext = createContext(
|
|
605
|
+
// That value should never be used, since the context should always be accessed within a provider
|
|
606
|
+
undefined);
|
|
607
|
+
const SpaceTimeChartCanvasContext = createContext(
|
|
608
|
+
// That value should never be used, since the context should always be accessed within a provider
|
|
609
|
+
undefined);
|
|
358
610
|
/**
|
|
359
|
-
* This
|
|
611
|
+
* This context exports everything necessary to draw the chart.
|
|
612
|
+
* It is updated anytime the scales are updated or the mouse moves.
|
|
360
613
|
*/
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
}
|
|
365
|
-
else {
|
|
366
|
-
drawPathStopExtremity(ctx, timePixel, spacePixel, swapAxis);
|
|
367
|
-
}
|
|
368
|
-
}
|
|
369
|
-
/**
|
|
370
|
-
* @param minT number timestamp
|
|
371
|
-
* @param maxT number timestamp
|
|
372
|
-
* @param timeRanges time frames (24h, 12h, 6h, …)
|
|
373
|
-
* @param gridlinesLevels width of the lines for each time frame
|
|
374
|
-
* @param formatter function to format de values inside the output object
|
|
375
|
-
* Keys are times in ms
|
|
376
|
-
* Values are the highest level on each time
|
|
377
|
-
*/
|
|
378
|
-
function computeVisibleTimeMarkers(minT, maxT, timeRanges, gridlinesLevels, formatter = (level) => ({ level })) {
|
|
379
|
-
const result = {};
|
|
380
|
-
const minTLocalOffset = new Date(minT).getTimezoneOffset() * 60 * 1000;
|
|
381
|
-
timeRanges.forEach((range, i) => {
|
|
382
|
-
const gridlinesLevel = gridlinesLevels[i];
|
|
383
|
-
if (!gridlinesLevel)
|
|
384
|
-
return;
|
|
385
|
-
let t = Math.floor((minT - minTLocalOffset) / range) * range + minTLocalOffset;
|
|
386
|
-
while (t <= maxT) {
|
|
387
|
-
if (t >= minT) {
|
|
388
|
-
result[t] = { ...formatter(gridlinesLevel, i), time: t };
|
|
389
|
-
}
|
|
390
|
-
t += range;
|
|
391
|
-
}
|
|
392
|
-
});
|
|
393
|
-
return Object.values(result);
|
|
394
|
-
}
|
|
395
|
-
/**
|
|
396
|
-
* To get crisp horizontal or vertical lines on a canvas, we must draw them as thin as possible, in
|
|
397
|
-
* terms of actual pixels on screen.
|
|
398
|
-
* The best way for this is:
|
|
399
|
-
* - To center lines 1, 3, 5... pixels wide in the middle of a pixel (0.5, 1.5, 2.5...)
|
|
400
|
-
* - To center lines 2, 4, 6... pixels wide between two pixels (0, 1, 2, 3...)
|
|
401
|
-
* Also, for HiDPi screens:
|
|
402
|
-
* - To center lines of "integer thicknesses" between two screen pixels (0, 0.5, 1, 1.5, 2...)
|
|
403
|
-
* - To center lines with `n + 0.5` thicknesses in the middle of a screen pixel (0.25, 0.75, 1.25, 1.75...)
|
|
404
|
-
* @param rawCoordinate Any input coordinate to fix
|
|
405
|
-
* @param lineWidth The width of the line to draw
|
|
406
|
-
* @param devicePixelRatio
|
|
407
|
-
*/
|
|
408
|
-
function getCrispLineCoordinate(rawCoordinate, lineWidth, devicePixelRatio = window.devicePixelRatio || 1) {
|
|
409
|
-
const centerOffset = Math.ceil(lineWidth * devicePixelRatio) / devicePixelRatio / 2;
|
|
410
|
-
return (Math.round((rawCoordinate - centerOffset) * devicePixelRatio) / devicePixelRatio + centerOffset);
|
|
411
|
-
}
|
|
412
|
-
/**
|
|
413
|
-
* You most likely need to wrap this function call with ctx.save ctx.restore
|
|
414
|
-
* as it modifies ctx current transformation matrix
|
|
415
|
-
*/
|
|
416
|
-
function fillRect(ctx, { timeStart, timeEnd, spaceStart, spaceEnd }, { getPoint }) {
|
|
417
|
-
const startPoint = getPoint({ time: Number(timeStart), position: spaceStart });
|
|
418
|
-
const endPoint = getPoint({ time: Number(timeEnd), position: spaceEnd });
|
|
419
|
-
const width = endPoint.x - startPoint.x;
|
|
420
|
-
const height = endPoint.y - startPoint.y;
|
|
421
|
-
if (width !== 0 && height !== 0) {
|
|
422
|
-
ctx.translate(startPoint.x, startPoint.y);
|
|
423
|
-
ctx.fillRect(0, 0, width, height);
|
|
424
|
-
}
|
|
425
|
-
return { width, height };
|
|
426
|
-
}
|
|
427
|
-
|
|
428
|
-
const COLORS_TO_INDICES = {};
|
|
429
|
-
const INDICES_TO_COLORS = {};
|
|
430
|
-
const WHITE_ALPHA_75 = '#ffffffbf';
|
|
431
|
-
const BLACK_ALPHA_25 = '#00000025';
|
|
432
|
-
const GREY_50$3 = '#797671';
|
|
433
|
-
/**
|
|
434
|
-
* This function returns a unique hex color corresponding to the given index. The colors are
|
|
435
|
-
* generated as #000001, #000002 ... #0000ff, #000100 etc.
|
|
436
|
-
* These colors aim at representing the given indices on the picking layer.
|
|
437
|
-
*/
|
|
438
|
-
function indexToColor(index) {
|
|
439
|
-
if (INDICES_TO_COLORS[index])
|
|
440
|
-
return INDICES_TO_COLORS[index];
|
|
441
|
-
const color = `#${index.toString(16).padStart(6, '0')}`.toLowerCase();
|
|
442
|
-
COLORS_TO_INDICES[color] = index;
|
|
443
|
-
INDICES_TO_COLORS[index] = color;
|
|
444
|
-
return color;
|
|
445
|
-
}
|
|
446
|
-
/**
|
|
447
|
-
* This function returns the index corresponding to the given color.
|
|
448
|
-
*/
|
|
449
|
-
function colorToIndex(color) {
|
|
450
|
-
return COLORS_TO_INDICES[color.toLowerCase()];
|
|
451
|
-
}
|
|
452
|
-
function componentToHex(c) {
|
|
453
|
-
return c.toString(16).padStart(2, '0').toLowerCase();
|
|
454
|
-
}
|
|
455
|
-
// The two following functions are just small helpers to help translating colors between RGB and
|
|
456
|
-
// Hex:
|
|
457
|
-
function rgbToHex(r, g, b) {
|
|
458
|
-
return `#${componentToHex(r)}${componentToHex(g)}${componentToHex(b)}`;
|
|
459
|
-
}
|
|
460
|
-
function hexToRgb(color) {
|
|
461
|
-
return chroma(color).rgba();
|
|
462
|
-
}
|
|
463
|
-
|
|
464
|
-
/**
|
|
465
|
-
* This function captures a list of canvas layers into a PNG image, and returns a blob with the
|
|
466
|
-
* PNG data if possible.
|
|
467
|
-
*/
|
|
468
|
-
async function getPNGBlob(canvases, layers, backgroundColor = '#fff') {
|
|
469
|
-
if (!layers.length)
|
|
470
|
-
throw Error('There must be at least one layer to capture.');
|
|
471
|
-
const firstCanvas = canvases[layers[0]];
|
|
472
|
-
const width = firstCanvas.width;
|
|
473
|
-
const height = firstCanvas.height;
|
|
474
|
-
// Create a new canvas, on which the different layers will be drawn:
|
|
475
|
-
const canvas = document.createElement('CANVAS');
|
|
476
|
-
canvas.setAttribute('width', width + '');
|
|
477
|
-
canvas.setAttribute('height', height + '');
|
|
478
|
-
const ctx = canvas.getContext('2d');
|
|
479
|
-
// Draw a white background first:
|
|
480
|
-
ctx.fillStyle = backgroundColor;
|
|
481
|
-
ctx.fillRect(0, 0, width, height);
|
|
482
|
-
// For each layer, draw it on our canvas:
|
|
483
|
-
layers.forEach((id) => {
|
|
484
|
-
ctx.drawImage(canvases[id], 0, 0, width, height, 0, 0, width, height);
|
|
485
|
-
});
|
|
486
|
-
// Save the canvas as a PNG image:
|
|
487
|
-
return new Promise((resolve, reject) => {
|
|
488
|
-
canvas.toBlob((blob) => {
|
|
489
|
-
if (blob)
|
|
490
|
-
resolve(blob);
|
|
491
|
-
else
|
|
492
|
-
reject();
|
|
493
|
-
}, 'image/png');
|
|
494
|
-
});
|
|
495
|
-
}
|
|
614
|
+
const MouseContext = createContext(
|
|
615
|
+
// That value should never be used, since the context should always be accessed within a provider
|
|
616
|
+
undefined);
|
|
496
617
|
|
|
497
|
-
const
|
|
498
|
-
const
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
const size = useSize(dom);
|
|
514
|
-
const sizeRef = useRef(size);
|
|
515
|
-
/**
|
|
516
|
-
* Keep the sizeRef value up to date (to allow rendering functions not being reset anytime size is
|
|
517
|
-
* updated):
|
|
518
|
-
*/
|
|
519
|
-
useEffect(() => {
|
|
520
|
-
sizeRef.current = size;
|
|
521
|
-
}, [size]);
|
|
522
|
-
const devicePixelRatio = useDevicePixelRatio();
|
|
523
|
-
/**
|
|
524
|
-
* This function renders all picking layers:
|
|
525
|
-
*/
|
|
526
|
-
const drawPicking = useCallback((stcContext, layers) => {
|
|
527
|
-
stcContext.resetPickingElements();
|
|
528
|
-
PICKING_LAYERS.forEach((layer) => {
|
|
529
|
-
if (layers && !layers.has(layer))
|
|
530
|
-
return;
|
|
531
|
-
const ctx = contextsRef.current[`${PICKING}-${layer}`];
|
|
532
|
-
const set = pickingFunctions.current[layer];
|
|
533
|
-
if (ctx && ctx.canvas.width && ctx.canvas.height) {
|
|
534
|
-
const { width, height } = sizeRef.current;
|
|
535
|
-
ctx.clearRect(0, 0, width, height);
|
|
536
|
-
const imageData = ctx.getImageData(0, 0, ctx.canvas.width, ctx.canvas.height);
|
|
537
|
-
const pickingScalingRatio = getPickingScalingRatio();
|
|
538
|
-
set.forEach((fn) => fn(imageData, stcContext, pickingScalingRatio));
|
|
539
|
-
ctx.putImageData(imageData, 0, 0);
|
|
618
|
+
const TimeGraduations = () => {
|
|
619
|
+
const mouseContext = useContext(MouseContext);
|
|
620
|
+
const mouseX = mouseContext?.position.x;
|
|
621
|
+
const isHover = mouseContext?.isHover ?? false;
|
|
622
|
+
const drawingFunction = useCallback((ctx, { timeScale, timeOrigin, timePixelOffset, yPixelOffset, getTimePixel, getTime, swapAxis, width, height, theme: { breakpoints, timeRanges, timeGraduationsStyles, timeGraduationsPriorities }, }) => {
|
|
623
|
+
const timeAxisSize = !swapAxis ? width : height;
|
|
624
|
+
const spaceAxisSize = !swapAxis ? height : width;
|
|
625
|
+
const minT = timeOrigin - timeScale * timePixelOffset;
|
|
626
|
+
const maxT = minT + timeScale * timeAxisSize;
|
|
627
|
+
// Find which styles to apply, relatively to the timescale (i.e. horizontal zoom level):
|
|
628
|
+
const pixelsPerMinute = (1 / timeScale) * MINUTE;
|
|
629
|
+
let gridlinesLevels = [];
|
|
630
|
+
breakpoints.some((breakpoint, i) => {
|
|
631
|
+
if (pixelsPerMinute < breakpoint) {
|
|
632
|
+
gridlinesLevels = timeGraduationsPriorities[i];
|
|
633
|
+
return true;
|
|
540
634
|
}
|
|
635
|
+
return false;
|
|
541
636
|
});
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
637
|
+
const gridMarks = computeVisibleTimeMarkers(minT, maxT, timeRanges, gridlinesLevels);
|
|
638
|
+
// Render grid lines:
|
|
639
|
+
gridMarks.forEach(({ time, level }) => {
|
|
640
|
+
const styles = timeGraduationsStyles[level];
|
|
641
|
+
ctx.strokeStyle = styles.color;
|
|
642
|
+
ctx.lineWidth = styles.width;
|
|
643
|
+
ctx.globalAlpha = styles.opacity ?? 1;
|
|
644
|
+
ctx.setLineDash(styles.dashArray ?? []);
|
|
645
|
+
if (styles.dashArray) {
|
|
646
|
+
ctx.lineDashOffset = -yPixelOffset;
|
|
647
|
+
}
|
|
648
|
+
const timePixel = getCrispLineCoordinate(getTimePixel(time), ctx.lineWidth);
|
|
649
|
+
ctx.beginPath();
|
|
650
|
+
if (!swapAxis) {
|
|
651
|
+
ctx.moveTo(timePixel, 0);
|
|
652
|
+
ctx.lineTo(timePixel, spaceAxisSize);
|
|
653
|
+
}
|
|
654
|
+
else {
|
|
655
|
+
ctx.moveTo(0, timePixel);
|
|
656
|
+
ctx.lineTo(spaceAxisSize, timePixel);
|
|
556
657
|
}
|
|
658
|
+
ctx.stroke();
|
|
557
659
|
});
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
660
|
+
if (!swapAxis && isHover && mouseX != null && !isNaN(mouseX)) {
|
|
661
|
+
const crispX = getCrispLineCoordinate(mouseX, 1);
|
|
662
|
+
const timeValue = getTime(crispX);
|
|
663
|
+
const timeLabel = new Date(timeValue).toLocaleTimeString();
|
|
664
|
+
ctx.globalAlpha = 1;
|
|
665
|
+
// Vertical line:
|
|
666
|
+
ctx.strokeStyle = BLACK_ALPHA_25;
|
|
667
|
+
ctx.lineWidth = 1;
|
|
668
|
+
ctx.setLineDash([]);
|
|
669
|
+
ctx.beginPath();
|
|
670
|
+
ctx.moveTo(crispX, 0);
|
|
671
|
+
ctx.lineTo(crispX, spaceAxisSize);
|
|
672
|
+
ctx.stroke();
|
|
673
|
+
// Label
|
|
674
|
+
const padding = 4;
|
|
675
|
+
const fontSize = 12;
|
|
676
|
+
const fontWeight = '400';
|
|
677
|
+
const fontFamily = 'IBM Plex Sans, sans-regular';
|
|
678
|
+
ctx.font = `${fontWeight} ${fontSize}px ${fontFamily}`;
|
|
679
|
+
const textWidth = ctx.measureText(timeLabel).width;
|
|
680
|
+
const labelX = Math.min(Math.max(crispX - textWidth / 2 - padding, 0), width - textWidth - padding * 2);
|
|
681
|
+
const labelY = spaceAxisSize - 50;
|
|
682
|
+
// Label background
|
|
683
|
+
ctx.fillStyle = '#FFFFFF';
|
|
684
|
+
ctx.fillRect(labelX, labelY - fontSize - 2, textWidth + padding * 2, fontSize + 4);
|
|
685
|
+
// Text
|
|
686
|
+
ctx.fillStyle = GREY_50$3;
|
|
687
|
+
ctx.fillText(timeLabel, labelX + padding, labelY);
|
|
568
688
|
}
|
|
569
|
-
//
|
|
570
|
-
}, []);
|
|
571
|
-
/**
|
|
572
|
-
* This function schedules a full render for next frame, if no scheduling has been done yet:
|
|
573
|
-
*/
|
|
574
|
-
const scheduleRendering = useCallback(() => {
|
|
575
|
-
if (!scheduledRef.current) {
|
|
576
|
-
scheduledRef.current = {
|
|
577
|
-
frameId: window.requestAnimationFrame(draw),
|
|
578
|
-
};
|
|
579
|
-
}
|
|
580
|
-
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
581
|
-
}, []);
|
|
582
|
-
/**
|
|
583
|
-
* This function helpers registering a drawing function on a given layer:
|
|
584
|
-
*/
|
|
585
|
-
const register = useCallback(({ type, layer, fn }) => {
|
|
586
|
-
if (type === 'picking') {
|
|
587
|
-
const set = pickingFunctions.current[layer];
|
|
588
|
-
if (set.has(fn))
|
|
589
|
-
throw new Error('This picking function has already been registered.');
|
|
590
|
-
set.add(fn);
|
|
591
|
-
}
|
|
592
|
-
else if (type === 'rendering') {
|
|
593
|
-
const set = drawingFunctions.current[layer];
|
|
594
|
-
if (set.has(fn))
|
|
595
|
-
throw new Error('This drawing function has already been registered.');
|
|
596
|
-
set.add(fn);
|
|
597
|
-
}
|
|
598
|
-
scheduleRendering();
|
|
599
|
-
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
600
|
-
}, []);
|
|
601
|
-
/**
|
|
602
|
-
* This function helpers unregistering a drawing function from a given layer:
|
|
603
|
-
*/
|
|
604
|
-
const unregister = useCallback(({ type, layer, fn }) => {
|
|
605
|
-
if (type === 'picking') {
|
|
606
|
-
const set = pickingFunctions.current[layer];
|
|
607
|
-
if (!set.has(fn))
|
|
608
|
-
throw new Error('This picking function has not been registered.');
|
|
609
|
-
set.delete(fn);
|
|
610
|
-
}
|
|
611
|
-
else if (type === 'rendering') {
|
|
612
|
-
const set = drawingFunctions.current[layer];
|
|
613
|
-
if (!set.has(fn))
|
|
614
|
-
throw new Error('This drawing function has not been registered.');
|
|
615
|
-
set.delete(fn);
|
|
616
|
-
}
|
|
617
|
-
scheduleRendering();
|
|
618
|
-
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
619
|
-
}, []);
|
|
620
|
-
const captureCanvases = useCallback(() => getPNGBlob(canvasesRef.current, LAYERS.map((layer) => `${RENDERING}-${layer}`), stcContextRef.current.theme.background), []);
|
|
621
|
-
// Create all canvas layers:
|
|
622
|
-
useEffect(() => {
|
|
623
|
-
if (!dom)
|
|
624
|
-
return;
|
|
625
|
-
const canvases = canvasesRef.current;
|
|
626
|
-
const contexts = contextsRef.current;
|
|
627
|
-
// Create missing layers:
|
|
628
|
-
const allLayers = [
|
|
629
|
-
...LAYERS.map((layer) => ({ layer, type: 'rendering' })),
|
|
630
|
-
...PICKING_LAYERS.map((layer) => ({ layer, type: 'picking' })),
|
|
631
|
-
];
|
|
632
|
-
allLayers.forEach(({ layer, type }) => {
|
|
633
|
-
const layerId = `${type}-${layer}`;
|
|
634
|
-
if (!canvases[layerId]) {
|
|
635
|
-
const canvas = document.createElement('CANVAS');
|
|
636
|
-
const ctx = canvas.getContext('2d');
|
|
637
|
-
canvas.classList.add(`layer-${type}-${layer}`);
|
|
638
|
-
canvas.style.position = 'absolute';
|
|
639
|
-
canvas.style.inset = '0';
|
|
640
|
-
dom.appendChild(canvas);
|
|
641
|
-
canvases[layerId] = canvas;
|
|
642
|
-
contexts[layerId] = ctx;
|
|
643
|
-
// Draw layer if functions are already registered:
|
|
644
|
-
if (type === 'picking') {
|
|
645
|
-
// (also hide picking layers)
|
|
646
|
-
canvas.style.display = 'none';
|
|
647
|
-
}
|
|
648
|
-
}
|
|
649
|
-
});
|
|
650
|
-
}, [dom]);
|
|
651
|
-
// Redraw all layers when fingerprint changes:
|
|
652
|
-
useEffect(() => {
|
|
653
|
-
// Cache latest context in ref:
|
|
654
|
-
stcContextRef.current = inputStcContext;
|
|
655
|
-
scheduleRendering();
|
|
656
|
-
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
657
|
-
}, [inputStcContext.fingerprint]);
|
|
658
|
-
// Handle resizing:
|
|
659
|
-
useEffect(() => {
|
|
660
|
-
const pickingScalingRatio = getPickingScalingRatio();
|
|
661
|
-
for (const id in canvasesRef.current) {
|
|
662
|
-
const canvas = canvasesRef.current[id];
|
|
663
|
-
const ctx = contextsRef.current[id];
|
|
664
|
-
const isPicking = id.split('-')[0] === PICKING;
|
|
665
|
-
if (canvas) {
|
|
666
|
-
const ratio = isPicking ? pickingScalingRatio : devicePixelRatio;
|
|
667
|
-
canvas.style.width = size.width + 'px';
|
|
668
|
-
canvas.style.height = size.height + 'px';
|
|
669
|
-
canvas.setAttribute('width', Math.max(1, size.width * ratio) + 'px');
|
|
670
|
-
canvas.setAttribute('height', Math.max(1, size.height * ratio) + 'px');
|
|
671
|
-
if (isPicking)
|
|
672
|
-
canvas.style.imageRendering = 'pixelated';
|
|
673
|
-
if (!isPicking) {
|
|
674
|
-
// Reset the transform to identity, then apply the new scale
|
|
675
|
-
ctx.setTransform(1, 0, 0, 1, 0, 0);
|
|
676
|
-
ctx.scale(devicePixelRatio, devicePixelRatio);
|
|
677
|
-
}
|
|
678
|
-
}
|
|
679
|
-
}
|
|
680
|
-
draw();
|
|
681
|
-
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
682
|
-
}, [size, devicePixelRatio]);
|
|
683
|
-
// Read picking layer on position change:
|
|
684
|
-
useEffect(() => {
|
|
685
|
-
let newHoveredItem = null;
|
|
686
|
-
const pickingScalingRatio = getPickingScalingRatio();
|
|
687
|
-
PICKING_LAYERS.some((layer) => {
|
|
688
|
-
const ctx = contextsRef.current[`${PICKING}-${layer}`];
|
|
689
|
-
if (ctx && position) {
|
|
690
|
-
const [r, g, b, a] = ctx.getImageData(Math.round(position.x * pickingScalingRatio), Math.round(position.y * pickingScalingRatio), 1, 1).data;
|
|
691
|
-
if (a === 255) {
|
|
692
|
-
const color = rgbToHex(r, g, b);
|
|
693
|
-
const index = colorToIndex(color);
|
|
694
|
-
const element = stcContextRef.current.pickingElements[index];
|
|
695
|
-
newHoveredItem = {
|
|
696
|
-
layer,
|
|
697
|
-
element,
|
|
698
|
-
};
|
|
699
|
-
return true;
|
|
700
|
-
}
|
|
701
|
-
}
|
|
702
|
-
return false;
|
|
703
|
-
});
|
|
704
|
-
if (!isEqual(newHoveredItem, hoveredItem)) {
|
|
705
|
-
setHoveredItem(newHoveredItem);
|
|
706
|
-
}
|
|
707
|
-
}, [position, hoveredItem]);
|
|
708
|
-
// Keep the canvas context up to date:
|
|
709
|
-
const canvasContext = useMemo(() => ({ register, unregister, captureCanvases }), [register, unregister, captureCanvases]);
|
|
710
|
-
return {
|
|
711
|
-
canvasContext,
|
|
712
|
-
hoveredItem,
|
|
713
|
-
};
|
|
714
|
-
}
|
|
715
|
-
/**
|
|
716
|
-
* This hook helps to bind a picking function to a layer in the SpaceTimeChart.
|
|
717
|
-
* It is public and can be used outside SpaceTimeChart.
|
|
718
|
-
*/
|
|
719
|
-
function usePicking(layer, fn) {
|
|
720
|
-
const { register, unregister } = useContext(CanvasContext);
|
|
721
|
-
useEffect(() => {
|
|
722
|
-
register({ type: 'picking', layer, fn });
|
|
723
|
-
return () => {
|
|
724
|
-
unregister({ type: 'picking', layer, fn });
|
|
725
|
-
};
|
|
726
|
-
}, [layer, fn, register, unregister]);
|
|
727
|
-
}
|
|
728
|
-
/**
|
|
729
|
-
* This hook helps to bind a drawing function to a layer in the SpaceTimeChart.
|
|
730
|
-
* It is public and can be used outside SpaceTimeChart.
|
|
731
|
-
*/
|
|
732
|
-
function useDraw(layer, fn) {
|
|
733
|
-
const { register, unregister } = useContext(CanvasContext);
|
|
734
|
-
useEffect(() => {
|
|
735
|
-
register({ type: 'rendering', layer, fn });
|
|
736
|
-
return () => {
|
|
737
|
-
unregister({ type: 'rendering', layer, fn });
|
|
738
|
-
};
|
|
739
|
-
}, [layer, fn, register, unregister]);
|
|
740
|
-
}
|
|
741
|
-
|
|
742
|
-
/**
|
|
743
|
-
* This function takes a path, a point index and looks forward in the points order for the first
|
|
744
|
-
* position variation. It then returns "forward" or "backward" according to that variation.
|
|
745
|
-
*
|
|
746
|
-
* If it does not find any variation for some reason, it returns "still" instead.
|
|
747
|
-
*
|
|
748
|
-
* Finally, if `reversed` is true, then, it searches points in the other direction.
|
|
749
|
-
*/
|
|
750
|
-
function getPathDirection({ points }, index, reversed) {
|
|
751
|
-
if (points.length < 2 || !points[index])
|
|
752
|
-
return 'still';
|
|
753
|
-
const reference = points[index].position;
|
|
754
|
-
let i = index;
|
|
755
|
-
let point;
|
|
756
|
-
while ((point = points[i])) {
|
|
757
|
-
if (point.position !== reference) {
|
|
758
|
-
const delta = reversed ? reference - point.position : point.position - reference;
|
|
759
|
-
return delta > 0 ? 'forward' : 'backward';
|
|
760
|
-
}
|
|
761
|
-
i = i + (reversed ? -1 : 1);
|
|
762
|
-
}
|
|
763
|
-
return 'still';
|
|
764
|
-
}
|
|
765
|
-
/**
|
|
766
|
-
* This function takes a SpaceToPixel function and a position, and checks if the function returns
|
|
767
|
-
* the same pixel for the position, starting from both sides. It then returns one or two pixel
|
|
768
|
-
* positions accordingly.
|
|
769
|
-
*/
|
|
770
|
-
function getSpacePixels(getSpacePixel, position) {
|
|
771
|
-
const spacePixelFromStart = getSpacePixel(position);
|
|
772
|
-
const spacePixelFromEnd = getSpacePixel(position, true);
|
|
773
|
-
return spacePixelFromStart === spacePixelFromEnd
|
|
774
|
-
? [spacePixelFromStart]
|
|
775
|
-
: [spacePixelFromStart, spacePixelFromEnd];
|
|
776
|
-
}
|
|
777
|
-
|
|
778
|
-
const SpaceGraduations = () => {
|
|
779
|
-
const drawingFunction = useCallback((ctx, { timePixelOffset, getSpacePixel, operationalPoints, flatSteps, swapAxis, width, height, theme: { spaceGraduationsStyles }, }) => {
|
|
780
|
-
const axisSize = !swapAxis ? width : height;
|
|
781
|
-
// Draw operational point lines:
|
|
782
|
-
operationalPoints.forEach((point) => {
|
|
783
|
-
if (flatSteps.has(point.position))
|
|
784
|
-
return;
|
|
785
|
-
const styles = spaceGraduationsStyles[point.importanceLevel || 0];
|
|
786
|
-
if (!styles)
|
|
787
|
-
return;
|
|
788
|
-
ctx.strokeStyle = styles.color;
|
|
789
|
-
ctx.lineWidth = styles.width;
|
|
790
|
-
ctx.globalAlpha = styles.opacity || 1;
|
|
791
|
-
if (styles.dashArray) {
|
|
792
|
-
ctx.setLineDash(styles.dashArray || []);
|
|
793
|
-
ctx.lineDashOffset = -timePixelOffset;
|
|
794
|
-
}
|
|
795
|
-
// Detect flat steps, and draw two graduations if any (one on each side of the step):
|
|
796
|
-
getSpacePixels(getSpacePixel, point.position).forEach((rawPixel) => {
|
|
797
|
-
const spacePixel = getCrispLineCoordinate(rawPixel, ctx.lineWidth);
|
|
798
|
-
ctx.beginPath();
|
|
799
|
-
if (!swapAxis) {
|
|
800
|
-
ctx.moveTo(0, spacePixel);
|
|
801
|
-
ctx.lineTo(axisSize, spacePixel);
|
|
802
|
-
}
|
|
803
|
-
else {
|
|
804
|
-
ctx.moveTo(spacePixel, 0);
|
|
805
|
-
ctx.lineTo(spacePixel, axisSize);
|
|
806
|
-
}
|
|
807
|
-
ctx.stroke();
|
|
808
|
-
});
|
|
809
|
-
});
|
|
689
|
+
// Reset
|
|
810
690
|
ctx.setLineDash([]);
|
|
811
691
|
ctx.lineDashOffset = 0;
|
|
812
692
|
ctx.globalAlpha = 1;
|
|
813
|
-
}, []);
|
|
814
|
-
useDraw('graduations', drawingFunction);
|
|
693
|
+
}, [mouseX, isHover]);
|
|
694
|
+
useDraw(TimeChartCanvasContext, 'graduations', drawingFunction);
|
|
815
695
|
return null;
|
|
816
696
|
};
|
|
817
697
|
|
|
@@ -823,10 +703,6 @@ const GREY_50$2 = '#797671';
|
|
|
823
703
|
// Fonts:
|
|
824
704
|
const FONT_SIZE = 10;
|
|
825
705
|
const FONT = 'IBM Plex Sans, sans-serif';
|
|
826
|
-
// Here are some helpers to write code about time in ms that is humanly readable:
|
|
827
|
-
const SECOND = 1000;
|
|
828
|
-
const MINUTE = 60 * SECOND;
|
|
829
|
-
const HOUR = 60 * MINUTE;
|
|
830
706
|
const DEFAULT_THEME = {
|
|
831
707
|
background: 'white',
|
|
832
708
|
breakpoints: [0.2, 0.4, 0.7, 2, 6, 20, 60, Infinity],
|
|
@@ -953,221 +829,193 @@ const DEFAULT_THEME = {
|
|
|
953
829
|
},
|
|
954
830
|
};
|
|
955
831
|
|
|
956
|
-
|
|
957
|
-
const
|
|
958
|
-
const
|
|
959
|
-
const
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
};
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
const
|
|
991
|
-
const
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
}
|
|
1028
|
-
else {
|
|
1029
|
-
ctx.fillStyle = background;
|
|
1030
|
-
ctx.fillRect(0, 0, captionSize, timeAxisSize);
|
|
1031
|
-
}
|
|
1032
|
-
// Render time captions:
|
|
1033
|
-
labelMarks.forEach(({ styles, formatter, time }) => {
|
|
1034
|
-
const text = formatter(time, pixelsPerMinute);
|
|
1035
|
-
ctx.textAlign = styles.textAlign || 'center';
|
|
1036
|
-
ctx.textBaseline = 'top';
|
|
1037
|
-
ctx.fillStyle = styles.color;
|
|
1038
|
-
ctx.lineWidth = 5;
|
|
1039
|
-
ctx.strokeStyle = background;
|
|
1040
|
-
ctx.lineCap = 'butt';
|
|
1041
|
-
ctx.font = `${styles.fontWeight || 'normal'} ${styles.font}`;
|
|
1042
|
-
const timePixel = getCrispLineCoordinate(getTimePixel(time), ctx.lineWidth);
|
|
1043
|
-
if (!swapAxis) {
|
|
1044
|
-
ctx.beginPath();
|
|
1045
|
-
ctx.strokeStyle = timeCaptionsStyles[1].color;
|
|
1046
|
-
ctx.lineWidth = 1;
|
|
1047
|
-
let tickHeight = 4;
|
|
1048
|
-
const mod = time % (60 * 60 * 1000);
|
|
1049
|
-
if (mod === 0) {
|
|
1050
|
-
tickHeight = 8;
|
|
1051
|
-
}
|
|
1052
|
-
else if (mod % (30 * 60 * 1000) === 0) {
|
|
1053
|
-
tickHeight = 6;
|
|
1054
|
-
}
|
|
1055
|
-
ctx.moveTo(timePixel, spaceAxisSize);
|
|
1056
|
-
ctx.lineTo(timePixel, spaceAxisSize + tickHeight);
|
|
1057
|
-
ctx.moveTo(timePixel, 0);
|
|
1058
|
-
ctx.lineTo(timePixel, tickHeight);
|
|
1059
|
-
ctx.stroke();
|
|
1060
|
-
ctx.fillStyle = timeCaptionsStyles[1].color;
|
|
1061
|
-
ctx.fillText(text, timePixel, styles.topOffset || 0);
|
|
1062
|
-
ctx.fillText(text, timePixel, spaceAxisSize + (styles.topOffset || 0));
|
|
1063
|
-
}
|
|
1064
|
-
else {
|
|
1065
|
-
ctx.save();
|
|
1066
|
-
ctx.translate(captionSize - (styles.topOffset || 0), timePixel);
|
|
1067
|
-
ctx.rotate(Math.PI / 2);
|
|
1068
|
-
ctx.strokeText(text, 0, 0);
|
|
1069
|
-
ctx.fillText(text, 0, 0);
|
|
1070
|
-
ctx.restore();
|
|
1071
|
-
}
|
|
1072
|
-
});
|
|
1073
|
-
// Render caption top border:
|
|
1074
|
-
ctx.strokeStyle = timeGraduationsStyles[1].color;
|
|
1075
|
-
ctx.lineWidth = timeGraduationsStyles[1].width;
|
|
1076
|
-
if (!showTicks) {
|
|
1077
|
-
ctx.beginPath();
|
|
1078
|
-
if (!swapAxis) {
|
|
1079
|
-
const y = getCrispLineCoordinate(spaceAxisSize, ctx.lineWidth);
|
|
1080
|
-
ctx.moveTo(0, y);
|
|
1081
|
-
ctx.lineTo(timeAxisSize, y);
|
|
1082
|
-
}
|
|
1083
|
-
else {
|
|
1084
|
-
const x = getCrispLineCoordinate(captionSize, ctx.lineWidth);
|
|
1085
|
-
ctx.moveTo(x, 0);
|
|
1086
|
-
ctx.lineTo(x, timeAxisSize);
|
|
1087
|
-
}
|
|
1088
|
-
ctx.stroke();
|
|
1089
|
-
}
|
|
832
|
+
function validateTheme({ breakpoints, timeRanges, timeCaptionsStyles, timeCaptionsPriorities, timeGraduationsStyles, timeGraduationsPriorities, }) {
|
|
833
|
+
const PREFIX = 'SpaceTimeChartTheme is invalid:';
|
|
834
|
+
const breakpointsCount = breakpoints.length;
|
|
835
|
+
const timeRangesCount = timeRanges.length;
|
|
836
|
+
const timeCaptionsValues = new Set(timeCaptionsPriorities.flatMap((arr) => arr));
|
|
837
|
+
const timeGraduationsValues = new Set(timeGraduationsPriorities.flatMap((arr) => arr));
|
|
838
|
+
// Time captions:
|
|
839
|
+
if (timeCaptionsPriorities.length !== breakpointsCount)
|
|
840
|
+
throw new Error(`${PREFIX}: timeCaptionsPriorities should have as much elements as breakpoints.`);
|
|
841
|
+
if (timeCaptionsPriorities.some((arr) => arr.length !== timeRangesCount))
|
|
842
|
+
throw new Error(`${PREFIX}: each array in timeCaptionsPriorities should have as much elements as timeRanges.`);
|
|
843
|
+
if (Array.from(timeCaptionsValues).some((v) => v !== 0 && !timeCaptionsStyles[v]))
|
|
844
|
+
throw new Error(`${PREFIX}: all values (except 0s) from timeCaptionsPriorities should have a matching style in timeCaptionsStyles.`);
|
|
845
|
+
// Time graduations:
|
|
846
|
+
if (timeGraduationsPriorities.length !== breakpointsCount)
|
|
847
|
+
throw new Error(`${PREFIX}: timeGraduationsPriorities should have as much elements as breakpoints.`);
|
|
848
|
+
if (timeGraduationsPriorities.some((arr) => arr.length !== timeRangesCount))
|
|
849
|
+
throw new Error(`${PREFIX}: each array in timeGraduationsPriorities should have as much elements as timeRanges.`);
|
|
850
|
+
if (Array.from(timeGraduationsValues).some((v) => v !== 0 && !timeGraduationsStyles[v]))
|
|
851
|
+
throw new Error(`${PREFIX}: all values (except 0s) from timeGraduationsPriorities should have a matching style in timeGraduationsStyles.`);
|
|
852
|
+
if (!timeGraduationsStyles[1])
|
|
853
|
+
throw new Error(`${PREFIX}: there should be a timeGraduationsStyles for value 1.`);
|
|
854
|
+
}
|
|
855
|
+
|
|
856
|
+
/**
|
|
857
|
+
* This context exports everything necessary to draw the chart.
|
|
858
|
+
* It is updated anytime the scales are updated.
|
|
859
|
+
*/
|
|
860
|
+
const ChronogramContext = createContext(
|
|
861
|
+
// That value should never be used, since the context should always be accessed within a provider
|
|
862
|
+
undefined);
|
|
863
|
+
|
|
864
|
+
const Chronogram = (props) => {
|
|
865
|
+
const { levelCrossingsNames, levelCrossingsOccupancies, timeOrigin, timeScale, xOffset = 0, yOffset = 0, theme, ...attr } = props;
|
|
866
|
+
const [root, setRoot] = useState(null);
|
|
867
|
+
const [canvasesRoot, setCanvasesRoot] = useState(null);
|
|
868
|
+
const fullTheme = useMemo(() => ({ ...DEFAULT_THEME, ...theme }), [theme]);
|
|
869
|
+
const { width, height } = useSize(root);
|
|
870
|
+
const fingerprint = useMemo(() => JSON.stringify({
|
|
871
|
+
levelCrossingsNames,
|
|
872
|
+
levelCrossingsOccupancies,
|
|
873
|
+
width,
|
|
874
|
+
height,
|
|
875
|
+
timeOrigin,
|
|
876
|
+
timeScale,
|
|
877
|
+
xOffset,
|
|
878
|
+
yOffset,
|
|
879
|
+
}), [
|
|
880
|
+
levelCrossingsNames,
|
|
881
|
+
levelCrossingsOccupancies,
|
|
882
|
+
width,
|
|
883
|
+
height,
|
|
884
|
+
timeOrigin,
|
|
885
|
+
timeScale,
|
|
886
|
+
xOffset,
|
|
887
|
+
yOffset,
|
|
888
|
+
]);
|
|
889
|
+
const pickingState = useMemo(() => {
|
|
890
|
+
const pickingElements = [];
|
|
891
|
+
const resetPickingElements = () => {
|
|
892
|
+
pickingElements.length = 0;
|
|
893
|
+
};
|
|
894
|
+
const registerPickingElement = (element) => {
|
|
895
|
+
pickingElements.push(element);
|
|
896
|
+
return pickingElements.length - 1;
|
|
897
|
+
};
|
|
898
|
+
return {
|
|
899
|
+
pickingElements,
|
|
900
|
+
resetPickingElements,
|
|
901
|
+
registerPickingElement,
|
|
902
|
+
};
|
|
1090
903
|
}, []);
|
|
1091
|
-
|
|
1092
|
-
|
|
904
|
+
const contextState = useMemo(() => {
|
|
905
|
+
const getTimePixel = getTimeToPixel(timeOrigin, xOffset, timeScale);
|
|
906
|
+
const getTime = getPixelToTime(timeOrigin, xOffset, timeScale);
|
|
907
|
+
// TODO: Check and fix those functions once we introduce mouse interactions
|
|
908
|
+
const getData = (point) => ({ time: point.x, position: point.y });
|
|
909
|
+
const getPoint = ({ time, position }) => ({
|
|
910
|
+
['x']: getTimePixel(time),
|
|
911
|
+
['y']: position,
|
|
912
|
+
});
|
|
913
|
+
return {
|
|
914
|
+
...pickingState,
|
|
915
|
+
fingerprint,
|
|
916
|
+
width,
|
|
917
|
+
height,
|
|
918
|
+
getTimePixel,
|
|
919
|
+
getTime,
|
|
920
|
+
getData,
|
|
921
|
+
getPoint,
|
|
922
|
+
levelCrossingsNames,
|
|
923
|
+
levelCrossingsOccupancies,
|
|
924
|
+
timeOrigin,
|
|
925
|
+
timeScale,
|
|
926
|
+
timePixelOffset: xOffset,
|
|
927
|
+
yPixelOffset: yOffset,
|
|
928
|
+
theme: fullTheme,
|
|
929
|
+
captionSize: fullTheme.dateCaptionsSize + fullTheme.timeCaptionsSize,
|
|
930
|
+
};
|
|
931
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
932
|
+
}, [fingerprint, pickingState]);
|
|
933
|
+
const { canvasContext } = useCanvas$1(canvasesRoot, contextState);
|
|
934
|
+
// Check theme validity:
|
|
935
|
+
useEffect(() => {
|
|
936
|
+
validateTheme(fullTheme);
|
|
937
|
+
}, [fullTheme]);
|
|
938
|
+
return (React.createElement("div", { ...attr, ref: setRoot, className: cx('relative', attr.className), style: { background: fullTheme.background } },
|
|
939
|
+
React.createElement("div", { ref: setCanvasesRoot, className: "absolute inset-0" }),
|
|
940
|
+
React.createElement(ChronogramContext.Provider, { value: contextState },
|
|
941
|
+
React.createElement(TimeChartCanvasContext.Provider, { value: canvasContext },
|
|
942
|
+
React.createElement(TimeGraduations, null),
|
|
943
|
+
React.createElement(TimeCaptions, null)))));
|
|
1093
944
|
};
|
|
1094
945
|
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
946
|
+
/**
|
|
947
|
+
* This function takes a path, a point index and looks forward in the points order for the first
|
|
948
|
+
* position variation. It then returns "forward" or "backward" according to that variation.
|
|
949
|
+
*
|
|
950
|
+
* If it does not find any variation for some reason, it returns "still" instead.
|
|
951
|
+
*
|
|
952
|
+
* Finally, if `reversed` is true, then, it searches points in the other direction.
|
|
953
|
+
*/
|
|
954
|
+
function getPathDirection({ points }, index, reversed) {
|
|
955
|
+
if (points.length < 2 || !points[index])
|
|
956
|
+
return 'still';
|
|
957
|
+
const reference = points[index].position;
|
|
958
|
+
let i = index;
|
|
959
|
+
let point;
|
|
960
|
+
while ((point = points[i])) {
|
|
961
|
+
if (point.position !== reference) {
|
|
962
|
+
const delta = reversed ? reference - point.position : point.position - reference;
|
|
963
|
+
return delta > 0 ? 'forward' : 'backward';
|
|
964
|
+
}
|
|
965
|
+
i = i + (reversed ? -1 : 1);
|
|
966
|
+
}
|
|
967
|
+
return 'still';
|
|
968
|
+
}
|
|
969
|
+
/**
|
|
970
|
+
* This function takes a SpaceToPixel function and a position, and checks if the function returns
|
|
971
|
+
* the same pixel for the position, starting from both sides. It then returns one or two pixel
|
|
972
|
+
* positions accordingly.
|
|
973
|
+
*/
|
|
974
|
+
function getSpacePixels(getSpacePixel, position) {
|
|
975
|
+
const spacePixelFromStart = getSpacePixel(position);
|
|
976
|
+
const spacePixelFromEnd = getSpacePixel(position, true);
|
|
977
|
+
return spacePixelFromStart === spacePixelFromEnd
|
|
978
|
+
? [spacePixelFromStart]
|
|
979
|
+
: [spacePixelFromStart, spacePixelFromEnd];
|
|
980
|
+
}
|
|
981
|
+
|
|
982
|
+
const SpaceGraduations = () => {
|
|
983
|
+
const drawingFunction = useCallback((ctx, { timePixelOffset, getSpacePixel, operationalPoints, flatSteps, swapAxis, width, height, theme: { spaceGraduationsStyles }, }) => {
|
|
984
|
+
const axisSize = !swapAxis ? width : height;
|
|
985
|
+
// Draw operational point lines:
|
|
986
|
+
operationalPoints.forEach((point) => {
|
|
987
|
+
if (flatSteps.has(point.position))
|
|
988
|
+
return;
|
|
989
|
+
const styles = spaceGraduationsStyles[point.importanceLevel || 0];
|
|
990
|
+
if (!styles)
|
|
991
|
+
return;
|
|
992
|
+
ctx.strokeStyle = styles.color;
|
|
993
|
+
ctx.lineWidth = styles.width;
|
|
994
|
+
ctx.globalAlpha = styles.opacity || 1;
|
|
995
|
+
if (styles.dashArray) {
|
|
996
|
+
ctx.setLineDash(styles.dashArray || []);
|
|
997
|
+
ctx.lineDashOffset = -timePixelOffset;
|
|
1133
998
|
}
|
|
1134
|
-
|
|
999
|
+
// Detect flat steps, and draw two graduations if any (one on each side of the step):
|
|
1000
|
+
getSpacePixels(getSpacePixel, point.position).forEach((rawPixel) => {
|
|
1001
|
+
const spacePixel = getCrispLineCoordinate(rawPixel, ctx.lineWidth);
|
|
1002
|
+
ctx.beginPath();
|
|
1003
|
+
if (!swapAxis) {
|
|
1004
|
+
ctx.moveTo(0, spacePixel);
|
|
1005
|
+
ctx.lineTo(axisSize, spacePixel);
|
|
1006
|
+
}
|
|
1007
|
+
else {
|
|
1008
|
+
ctx.moveTo(spacePixel, 0);
|
|
1009
|
+
ctx.lineTo(spacePixel, axisSize);
|
|
1010
|
+
}
|
|
1011
|
+
ctx.stroke();
|
|
1012
|
+
});
|
|
1135
1013
|
});
|
|
1136
|
-
if (!swapAxis && isHover && mouseX != null && !isNaN(mouseX)) {
|
|
1137
|
-
const crispX = getCrispLineCoordinate(mouseX, 1);
|
|
1138
|
-
const timeValue = getTime(crispX);
|
|
1139
|
-
const timeLabel = new Date(timeValue).toLocaleTimeString();
|
|
1140
|
-
ctx.globalAlpha = 1;
|
|
1141
|
-
// Vertical line:
|
|
1142
|
-
ctx.strokeStyle = BLACK_ALPHA_25;
|
|
1143
|
-
ctx.lineWidth = 1;
|
|
1144
|
-
ctx.setLineDash([]);
|
|
1145
|
-
ctx.beginPath();
|
|
1146
|
-
ctx.moveTo(crispX, 0);
|
|
1147
|
-
ctx.lineTo(crispX, spaceAxisSize);
|
|
1148
|
-
ctx.stroke();
|
|
1149
|
-
// Label
|
|
1150
|
-
const padding = 4;
|
|
1151
|
-
const fontSize = 12;
|
|
1152
|
-
const fontWeight = '400';
|
|
1153
|
-
const fontFamily = 'IBM Plex Sans, sans-regular';
|
|
1154
|
-
ctx.font = `${fontWeight} ${fontSize}px ${fontFamily}`;
|
|
1155
|
-
const textWidth = ctx.measureText(timeLabel).width;
|
|
1156
|
-
const labelX = Math.min(Math.max(crispX - textWidth / 2 - padding, 0), width - textWidth - padding * 2);
|
|
1157
|
-
const labelY = spaceAxisSize - 50;
|
|
1158
|
-
// Label background
|
|
1159
|
-
ctx.fillStyle = '#FFFFFF';
|
|
1160
|
-
ctx.fillRect(labelX, labelY - fontSize - 2, textWidth + padding * 2, fontSize + 4);
|
|
1161
|
-
// Text
|
|
1162
|
-
ctx.fillStyle = GREY_50$3;
|
|
1163
|
-
ctx.fillText(timeLabel, labelX + padding, labelY);
|
|
1164
|
-
}
|
|
1165
|
-
// Reset
|
|
1166
1014
|
ctx.setLineDash([]);
|
|
1167
1015
|
ctx.lineDashOffset = 0;
|
|
1168
1016
|
ctx.globalAlpha = 1;
|
|
1169
|
-
}, [
|
|
1170
|
-
useDraw('graduations', drawingFunction);
|
|
1017
|
+
}, []);
|
|
1018
|
+
useDraw(SpaceTimeChartCanvasContext, 'graduations', drawingFunction);
|
|
1171
1019
|
return null;
|
|
1172
1020
|
};
|
|
1173
1021
|
|
|
@@ -1366,30 +1214,6 @@ function useMouseTracking(dom) {
|
|
|
1366
1214
|
return mouseState;
|
|
1367
1215
|
}
|
|
1368
1216
|
|
|
1369
|
-
function validateTheme({ breakpoints, timeRanges, timeCaptionsStyles, timeCaptionsPriorities, timeGraduationsStyles, timeGraduationsPriorities, }) {
|
|
1370
|
-
const PREFIX = 'SpaceTimeChartTheme is invalid:';
|
|
1371
|
-
const breakpointsCount = breakpoints.length;
|
|
1372
|
-
const timeRangesCount = timeRanges.length;
|
|
1373
|
-
const timeCaptionsValues = new Set(timeCaptionsPriorities.flatMap((arr) => arr));
|
|
1374
|
-
const timeGraduationsValues = new Set(timeGraduationsPriorities.flatMap((arr) => arr));
|
|
1375
|
-
// Time captions:
|
|
1376
|
-
if (timeCaptionsPriorities.length !== breakpointsCount)
|
|
1377
|
-
throw new Error(`${PREFIX}: timeCaptionsPriorities should have as much elements as breakpoints.`);
|
|
1378
|
-
if (timeCaptionsPriorities.some((arr) => arr.length !== timeRangesCount))
|
|
1379
|
-
throw new Error(`${PREFIX}: each array in timeCaptionsPriorities should have as much elements as timeRanges.`);
|
|
1380
|
-
if (Array.from(timeCaptionsValues).some((v) => v !== 0 && !timeCaptionsStyles[v]))
|
|
1381
|
-
throw new Error(`${PREFIX}: all values (except 0s) from timeCaptionsPriorities should have a matching style in timeCaptionsStyles.`);
|
|
1382
|
-
// Time graduations:
|
|
1383
|
-
if (timeGraduationsPriorities.length !== breakpointsCount)
|
|
1384
|
-
throw new Error(`${PREFIX}: timeGraduationsPriorities should have as much elements as breakpoints.`);
|
|
1385
|
-
if (timeGraduationsPriorities.some((arr) => arr.length !== timeRangesCount))
|
|
1386
|
-
throw new Error(`${PREFIX}: each array in timeGraduationsPriorities should have as much elements as timeRanges.`);
|
|
1387
|
-
if (Array.from(timeGraduationsValues).some((v) => v !== 0 && !timeGraduationsStyles[v]))
|
|
1388
|
-
throw new Error(`${PREFIX}: all values (except 0s) from timeGraduationsPriorities should have a matching style in timeGraduationsStyles.`);
|
|
1389
|
-
if (!timeGraduationsStyles[1])
|
|
1390
|
-
throw new Error(`${PREFIX}: there should be a timeGraduationsStyles for value 1.`);
|
|
1391
|
-
}
|
|
1392
|
-
|
|
1393
1217
|
/* eslint-disable @typescript-eslint/no-use-before-define */
|
|
1394
1218
|
/**
|
|
1395
1219
|
* This function helps to index a sequence of consecutive scales into a binary tree, to make it
|
|
@@ -1464,170 +1288,443 @@ function spaceScalesToBinaryTree(spaceOrigin, spaceScales, skipSiblingReferences
|
|
|
1464
1288
|
};
|
|
1465
1289
|
}
|
|
1466
1290
|
}
|
|
1467
|
-
return buildTree(normalizedScales);
|
|
1291
|
+
return buildTree(normalizedScales);
|
|
1292
|
+
}
|
|
1293
|
+
/**
|
|
1294
|
+
* This function takes a sequence of SpaceScales, identifies the flat steps (i.e. the scales that do
|
|
1295
|
+
* not increase the position), and returns them in a set.
|
|
1296
|
+
*/
|
|
1297
|
+
function getFlatSteps(spaceScales) {
|
|
1298
|
+
const flatSteps = [];
|
|
1299
|
+
for (let i = 1; i < spaceScales.length; i++) {
|
|
1300
|
+
const { to: previous } = spaceScales[i - 1];
|
|
1301
|
+
const { to: current } = spaceScales[i];
|
|
1302
|
+
if (previous === current)
|
|
1303
|
+
flatSteps.push(current);
|
|
1304
|
+
}
|
|
1305
|
+
return new Set(flatSteps);
|
|
1306
|
+
}
|
|
1307
|
+
/**
|
|
1308
|
+
* This function takes a NormalizedScaleTree and a position, and returns the leaf node from the
|
|
1309
|
+
* tree that contains that position.
|
|
1310
|
+
*
|
|
1311
|
+
* Also, if the position is lower than the tree's min (tree.from), then the first leaf is returned
|
|
1312
|
+
* and if it is higher than the max (tree.to), the last leaf is returned.
|
|
1313
|
+
*
|
|
1314
|
+
* Finally, if pickLast is truthy, then it returns the last leaf node that contains that position
|
|
1315
|
+
* instead of the first one. It is very important when there are flat sections.
|
|
1316
|
+
*/
|
|
1317
|
+
function getNormalizedScaleAtPosition(position, tree, pickLast) {
|
|
1318
|
+
position = clamp$1(position, tree.from, tree.to);
|
|
1319
|
+
let node = tree;
|
|
1320
|
+
while ('limit' in node) {
|
|
1321
|
+
if (!pickLast) {
|
|
1322
|
+
if (position <= node.limit)
|
|
1323
|
+
node = node.left;
|
|
1324
|
+
else
|
|
1325
|
+
node = node.right;
|
|
1326
|
+
}
|
|
1327
|
+
else {
|
|
1328
|
+
if (position >= node.limit)
|
|
1329
|
+
node = node.right;
|
|
1330
|
+
else
|
|
1331
|
+
node = node.left;
|
|
1332
|
+
}
|
|
1333
|
+
}
|
|
1334
|
+
return node;
|
|
1335
|
+
}
|
|
1336
|
+
/**
|
|
1337
|
+
* This function takes a NormalizedScaleTree and a pixel position, and returns the leaf node from
|
|
1338
|
+
* the tree that contains that pixel.
|
|
1339
|
+
*
|
|
1340
|
+
* Also, if the position is lower than the tree's min pixel (tree.pixelFrom), then the first leaf
|
|
1341
|
+
* is returned and if it is higher than the max pixel (tree.pixelTo), the last leaf is returned.
|
|
1342
|
+
*/
|
|
1343
|
+
function getNormalizedScaleAtPixel(y, tree) {
|
|
1344
|
+
y = clamp$1(y, tree.pixelFrom, tree.pixelTo);
|
|
1345
|
+
let node = tree;
|
|
1346
|
+
while ('pixelLimit' in node) {
|
|
1347
|
+
if (y <= node.pixelLimit)
|
|
1348
|
+
node = node.left;
|
|
1349
|
+
else
|
|
1350
|
+
node = node.right;
|
|
1351
|
+
}
|
|
1352
|
+
return node;
|
|
1353
|
+
}
|
|
1354
|
+
function getSpaceToPixel(pixelOffset, binaryTree) {
|
|
1355
|
+
return (position, fromEnd) => {
|
|
1356
|
+
const { from, pixelFrom, pixelTo, coefficient } = getNormalizedScaleAtPosition(position, binaryTree, fromEnd);
|
|
1357
|
+
// Rare case where coefficient is 0:
|
|
1358
|
+
// (occurs when there is just a flat step, for instance)
|
|
1359
|
+
if (coefficient === 0)
|
|
1360
|
+
return pixelOffset + (fromEnd ? pixelTo : pixelFrom);
|
|
1361
|
+
// Normal case: We simply interpolate
|
|
1362
|
+
return pixelOffset + pixelFrom + (position - from) / coefficient;
|
|
1363
|
+
};
|
|
1364
|
+
}
|
|
1365
|
+
function getPixelToSpace(pixelOffset, binaryTree) {
|
|
1366
|
+
return (spacePixel) => {
|
|
1367
|
+
const { from, pixelFrom, coefficient } = getNormalizedScaleAtPixel(spacePixel - pixelOffset, binaryTree);
|
|
1368
|
+
return from + (spacePixel - pixelOffset - pixelFrom) * coefficient;
|
|
1369
|
+
};
|
|
1370
|
+
}
|
|
1371
|
+
function getPointToData(getTime, getSpace, timeAxis, spaceAxis) {
|
|
1372
|
+
return (point) => ({
|
|
1373
|
+
time: getTime(point[timeAxis]),
|
|
1374
|
+
position: getSpace(point[spaceAxis]),
|
|
1375
|
+
});
|
|
1376
|
+
}
|
|
1377
|
+
function getDataToPoint(getTimePixel, getSpacePixel, timeAxis, spaceAxis) {
|
|
1378
|
+
return ({ time, position }) => ({
|
|
1379
|
+
[timeAxis]: getTimePixel(time),
|
|
1380
|
+
[spaceAxis]: getSpacePixel(position),
|
|
1381
|
+
});
|
|
1382
|
+
}
|
|
1383
|
+
/**
|
|
1384
|
+
* This function takes a path and a time, a returns the position of the train at the given time, or
|
|
1385
|
+
* the position at the closest time to the path existence.
|
|
1386
|
+
*/
|
|
1387
|
+
function getSpaceAtTime(path, time) {
|
|
1388
|
+
const segments = path.points.slice(0, -1).map((p, i) => [p, path.points[i + 1]]);
|
|
1389
|
+
const matchingSegment = segments.find(([p1, p2]) => inRange(time, p1.time, p2.time));
|
|
1390
|
+
if (matchingSegment) {
|
|
1391
|
+
const [p1, p2] = matchingSegment;
|
|
1392
|
+
return p1.position + ((time - p1.time) / (p2.time - p1.time)) * (p2.position - p1.position);
|
|
1393
|
+
}
|
|
1394
|
+
let minPoint = path.points[0];
|
|
1395
|
+
let maxPoint = path.points[0];
|
|
1396
|
+
path.points.slice(1).forEach((point) => {
|
|
1397
|
+
if (point.time < minPoint.time)
|
|
1398
|
+
minPoint = point;
|
|
1399
|
+
if (point.time > maxPoint.time)
|
|
1400
|
+
maxPoint = point;
|
|
1401
|
+
});
|
|
1402
|
+
if (minPoint.time > time)
|
|
1403
|
+
return minPoint.position;
|
|
1404
|
+
return maxPoint.position;
|
|
1405
|
+
}
|
|
1406
|
+
/**
|
|
1407
|
+
* This function helps to find where to "break" a straight line that crosses separations between
|
|
1408
|
+
* different time scales. It takes two positions (in the "space" space), and returns an array of
|
|
1409
|
+
* positions between these the two input positions (the array can be empty).
|
|
1410
|
+
*/
|
|
1411
|
+
function getSpaceBreakpoints(from, to, tree) {
|
|
1412
|
+
if (to < from)
|
|
1413
|
+
return getSpaceBreakpoints(to, from, tree).reverse();
|
|
1414
|
+
from = Math.max(from, tree.from);
|
|
1415
|
+
to = Math.min(to, tree.to);
|
|
1416
|
+
let fromScale = getNormalizedScaleAtPosition(from, tree);
|
|
1417
|
+
const res = [];
|
|
1418
|
+
while (fromScale.to < to) {
|
|
1419
|
+
res.push(fromScale.to);
|
|
1420
|
+
fromScale = fromScale.next;
|
|
1421
|
+
}
|
|
1422
|
+
return res;
|
|
1423
|
+
}
|
|
1424
|
+
/**
|
|
1425
|
+
* in most cases, after the rectangle zoom, the screen will be centered
|
|
1426
|
+
* on the center of the rectangle the user drew.
|
|
1427
|
+
* In case we reach max zoom, not to break this expectation we need a different offset calculation
|
|
1428
|
+
*/
|
|
1429
|
+
function computeRectZoomOffsets({ rect, timeOrigin, spaceOrigin, newTimeScale, newSpaceScale, swapAxes, chartWidth, chartHeight, }) {
|
|
1430
|
+
const chartTimeSizePx = !swapAxes ? chartWidth : chartHeight;
|
|
1431
|
+
const chartSpaceSizePx = !swapAxes ? chartHeight : chartWidth;
|
|
1432
|
+
const timeOffset = sideOffset(timeOrigin, newTimeScale, rect.timeStart, rect.timeEnd, chartTimeSizePx);
|
|
1433
|
+
const spaceOffset = sideOffset(spaceOrigin, newSpaceScale, rect.spaceStart, rect.spaceEnd, chartSpaceSizePx);
|
|
1434
|
+
return !swapAxes
|
|
1435
|
+
? { xOffset: timeOffset, yOffset: spaceOffset }
|
|
1436
|
+
: { xOffset: spaceOffset, yOffset: timeOffset };
|
|
1437
|
+
}
|
|
1438
|
+
function sideOffset(origin, newScale, rectStart, rectEnd, chartSideSizePx, axisPadding = 0) {
|
|
1439
|
+
const rectCenter = (Number(rectStart) + Number(rectEnd)) / 2;
|
|
1440
|
+
const newChartSize = chartSideSizePx * newScale;
|
|
1441
|
+
// newChartBorder is the x or y origin after zoom
|
|
1442
|
+
// it’s normally the same as rectStart (the left or top most part of the rectangle)
|
|
1443
|
+
// but if we reach max zoom we can’t use rectStart as the chart displayed origin
|
|
1444
|
+
// because it doesn’t garentees that the zoom rectangle stays exactly at the center of the chart after the zoom
|
|
1445
|
+
const newChartBorder = rectCenter - newChartSize / 2;
|
|
1446
|
+
return (origin - newChartBorder) / newScale - axisPadding * 2;
|
|
1447
|
+
}
|
|
1448
|
+
|
|
1449
|
+
const DEFAULT_PATH_END = 'stop';
|
|
1450
|
+
|
|
1451
|
+
/**
|
|
1452
|
+
* This function draws a thick lines from "from" to "to" on the given ImageData, with no
|
|
1453
|
+
* antialiasing. This is very useful to handle picking, since it is not possible to disable
|
|
1454
|
+
* antialiasing with the native JavaScript canvas APIs.
|
|
1455
|
+
*/
|
|
1456
|
+
function drawAliasedLine(imageData, { x: fromX, y: fromY }, { x: toX, y: toY }, [r, g, b], thickness, drawOnBottom, scalingRatio = 1) {
|
|
1457
|
+
if (fromX > toX)
|
|
1458
|
+
return drawAliasedLine(imageData, { x: toX, y: toY }, { x: fromX, y: fromY }, [r, g, b], thickness, drawOnBottom, scalingRatio);
|
|
1459
|
+
fromX = Math.round(fromX * scalingRatio);
|
|
1460
|
+
fromY = Math.round(fromY * scalingRatio);
|
|
1461
|
+
toX = Math.round(toX * scalingRatio);
|
|
1462
|
+
toY = Math.round(toY * scalingRatio);
|
|
1463
|
+
thickness = Math.round(thickness * scalingRatio);
|
|
1464
|
+
const width = imageData.width;
|
|
1465
|
+
const height = imageData.height;
|
|
1466
|
+
const dx = toX - fromX;
|
|
1467
|
+
const dy = toY - fromY;
|
|
1468
|
+
const len = Math.sqrt(dx * dx + dy * dy);
|
|
1469
|
+
// Calculate perpendicular vector
|
|
1470
|
+
const normX = -dy / len;
|
|
1471
|
+
const normY = dx / len;
|
|
1472
|
+
// Calculate the four corners of the rectangle
|
|
1473
|
+
const halfThickness = Math.ceil(thickness / 2);
|
|
1474
|
+
const corner1 = {
|
|
1475
|
+
x: fromX + (+normX - dx / len) * halfThickness,
|
|
1476
|
+
y: fromY + (+normY - dy / len) * halfThickness,
|
|
1477
|
+
};
|
|
1478
|
+
const corner2 = {
|
|
1479
|
+
x: fromX + (-normX - dx / len) * halfThickness,
|
|
1480
|
+
y: fromY + (-normY - dy / len) * halfThickness,
|
|
1481
|
+
};
|
|
1482
|
+
const corner3 = {
|
|
1483
|
+
x: toX + (-normX + dx / len) * halfThickness,
|
|
1484
|
+
y: toY + (-normY + dy / len) * halfThickness,
|
|
1485
|
+
};
|
|
1486
|
+
const corner4 = {
|
|
1487
|
+
x: toX + (+normX + dx / len) * halfThickness,
|
|
1488
|
+
y: toY + (+normY + dy / len) * halfThickness,
|
|
1489
|
+
};
|
|
1490
|
+
const ascending = fromY < toY;
|
|
1491
|
+
const top = ascending ? corner4 : corner1;
|
|
1492
|
+
const left = ascending ? corner1 : corner2;
|
|
1493
|
+
const right = ascending ? corner3 : corner4;
|
|
1494
|
+
const bottom = ascending ? corner2 : corner3;
|
|
1495
|
+
const xMin = clamp$1(Math.floor(left.x), 0, width);
|
|
1496
|
+
const xMax = clamp$1(Math.ceil(right.x), 0, width);
|
|
1497
|
+
const yMin = clamp$1(Math.floor(bottom.y), 0, height);
|
|
1498
|
+
const yMax = clamp$1(Math.ceil(top.y), 0, height);
|
|
1499
|
+
for (let y = yMin; y <= yMax; y++) {
|
|
1500
|
+
const xMinRow = clamp$1(y < left.y
|
|
1501
|
+
? Math.floor(bottom.x + ((y - bottom.y) * (left.x - bottom.x)) / (left.y - bottom.y))
|
|
1502
|
+
: Math.floor(left.x + ((y - left.y) * (left.x - top.x)) / (left.y - top.y)), xMin, xMax);
|
|
1503
|
+
const xMaxRow = clamp$1(y < right.y
|
|
1504
|
+
? Math.ceil(bottom.x + ((y - bottom.y) * (right.x - bottom.x)) / (right.y - bottom.y))
|
|
1505
|
+
: Math.ceil(right.x + ((y - right.y) * (right.x - top.x)) / (right.y - top.y)), xMin, xMax);
|
|
1506
|
+
for (let x = xMinRow; x <= xMaxRow; x++) {
|
|
1507
|
+
const index = (y * width + x) * 4;
|
|
1508
|
+
if (!imageData.data[index + 3]) {
|
|
1509
|
+
imageData.data[index] = r;
|
|
1510
|
+
imageData.data[index + 1] = g;
|
|
1511
|
+
imageData.data[index + 2] = b;
|
|
1512
|
+
imageData.data[index + 3] = 255;
|
|
1513
|
+
}
|
|
1514
|
+
}
|
|
1515
|
+
}
|
|
1468
1516
|
}
|
|
1469
1517
|
/**
|
|
1470
|
-
* This function takes
|
|
1471
|
-
*
|
|
1518
|
+
* This function takes an integer radius, and returns a flat matrix of 1s and 0s, where the 1s
|
|
1519
|
+
* represent the pixels that are within the disc. The shapes are cached, to make it faster to draw
|
|
1520
|
+
* a lot of times discs of the same radius.
|
|
1521
|
+
*
|
|
1522
|
+
* Here are some examples to make it clearer what the output should look like:
|
|
1523
|
+
*
|
|
1524
|
+
* getAliasedDiscShape(0);
|
|
1525
|
+
* [1]
|
|
1526
|
+
* getAliasedDiscShape(1);
|
|
1527
|
+
* [0, 1, 0,
|
|
1528
|
+
* 1, 1, 1,
|
|
1529
|
+
* 0, 1, 0]
|
|
1530
|
+
* getAliasedDiscShape(2);
|
|
1531
|
+
* [0, 0, 1, 0, 0,
|
|
1532
|
+
* 0, 1, 1, 1, 0,
|
|
1533
|
+
* 1, 1, 1, 1, 1,
|
|
1534
|
+
* 0, 1, 1, 1, 0,
|
|
1535
|
+
* 0, 0, 1, 0, 0]
|
|
1472
1536
|
*/
|
|
1473
|
-
|
|
1474
|
-
|
|
1475
|
-
|
|
1476
|
-
|
|
1477
|
-
|
|
1478
|
-
|
|
1479
|
-
|
|
1537
|
+
const DISCS_CACHE = new Map();
|
|
1538
|
+
function getAliasedDiscShape(radius) {
|
|
1539
|
+
const cachedShape = DISCS_CACHE.get(radius);
|
|
1540
|
+
if (cachedShape)
|
|
1541
|
+
return cachedShape;
|
|
1542
|
+
const diameter = radius * 2 + 1;
|
|
1543
|
+
const shape = new Uint8Array(diameter * diameter);
|
|
1544
|
+
for (let dy = -radius; dy <= radius; dy++) {
|
|
1545
|
+
for (let dx = -radius; dx <= radius; dx++) {
|
|
1546
|
+
const distance = dx * dx + dy * dy;
|
|
1547
|
+
if (distance <= radius * radius) {
|
|
1548
|
+
const x = dx + radius;
|
|
1549
|
+
const y = dy + radius;
|
|
1550
|
+
shape[y * diameter + x] = 1;
|
|
1551
|
+
}
|
|
1552
|
+
}
|
|
1480
1553
|
}
|
|
1481
|
-
|
|
1554
|
+
DISCS_CACHE.set(radius, shape);
|
|
1555
|
+
return shape;
|
|
1482
1556
|
}
|
|
1483
1557
|
/**
|
|
1484
|
-
* This function
|
|
1485
|
-
* tree that contains that position.
|
|
1486
|
-
*
|
|
1487
|
-
* Also, if the position is lower than the tree's min (tree.from), then the first leaf is returned
|
|
1488
|
-
* and if it is higher than the max (tree.to), the last leaf is returned.
|
|
1489
|
-
*
|
|
1490
|
-
* Finally, if pickLast is truthy, then it returns the last leaf node that contains that position
|
|
1491
|
-
* instead of the first one. It is very important when there are flat sections.
|
|
1558
|
+
* This function draws an aliased disc, using a shape computed by getDiscShape.
|
|
1492
1559
|
*/
|
|
1493
|
-
function
|
|
1494
|
-
|
|
1495
|
-
|
|
1496
|
-
|
|
1497
|
-
|
|
1498
|
-
|
|
1499
|
-
|
|
1500
|
-
|
|
1501
|
-
|
|
1502
|
-
|
|
1503
|
-
|
|
1504
|
-
if (
|
|
1505
|
-
|
|
1506
|
-
|
|
1507
|
-
|
|
1560
|
+
function drawAliasedDisc(imageData, { x: centerX, y: centerY }, radius, [r, g, b], drawOnBottom, scalingRatio = 1) {
|
|
1561
|
+
centerX = Math.round(centerX * scalingRatio);
|
|
1562
|
+
centerY = Math.round(centerY * scalingRatio);
|
|
1563
|
+
radius = Math.ceil(radius * scalingRatio);
|
|
1564
|
+
const { width, height } = imageData;
|
|
1565
|
+
const discShape = getAliasedDiscShape(radius);
|
|
1566
|
+
// Draw the disc on the imageData
|
|
1567
|
+
const diameter = radius * 2 + 1;
|
|
1568
|
+
for (let dy = -radius; dy <= radius; dy++) {
|
|
1569
|
+
for (let dx = -radius; dx <= radius; dx++) {
|
|
1570
|
+
const shapeIndex = (dy + radius) * diameter + (dx + radius);
|
|
1571
|
+
if (discShape[shapeIndex] === 1) {
|
|
1572
|
+
const x = centerX + dx;
|
|
1573
|
+
const y = centerY + dy;
|
|
1574
|
+
if (x >= 0 && x < width && y >= 0 && y < height) {
|
|
1575
|
+
const index = (y * width + x) * 4;
|
|
1576
|
+
{
|
|
1577
|
+
imageData.data[index] = r;
|
|
1578
|
+
imageData.data[index + 1] = g;
|
|
1579
|
+
imageData.data[index + 2] = b;
|
|
1580
|
+
imageData.data[index + 3] = 255;
|
|
1581
|
+
}
|
|
1582
|
+
}
|
|
1583
|
+
}
|
|
1508
1584
|
}
|
|
1509
1585
|
}
|
|
1510
|
-
return node;
|
|
1511
1586
|
}
|
|
1512
1587
|
/**
|
|
1513
|
-
*
|
|
1514
|
-
*
|
|
1588
|
+
* Draws an aliased quadrilateral
|
|
1589
|
+
*
|
|
1590
|
+
* vertices[0] ________ vertices[1]
|
|
1591
|
+
* / /
|
|
1592
|
+
* vertices[3] /________/ vertices[2]
|
|
1515
1593
|
*
|
|
1516
|
-
* Also, if the position is lower than the tree's min pixel (tree.pixelFrom), then the first leaf
|
|
1517
|
-
* is returned and if it is higher than the max pixel (tree.pixelTo), the last leaf is returned.
|
|
1518
1594
|
*/
|
|
1519
|
-
function
|
|
1520
|
-
|
|
1521
|
-
|
|
1522
|
-
|
|
1523
|
-
|
|
1524
|
-
|
|
1525
|
-
|
|
1526
|
-
|
|
1595
|
+
function drawAliasedQuadrilateral(imageData, points, [r, g, b], scalingRatio = 1) {
|
|
1596
|
+
const vertices = points.map((point) => ({
|
|
1597
|
+
x: Math.round(point.x * scalingRatio),
|
|
1598
|
+
y: Math.round(point.y * scalingRatio),
|
|
1599
|
+
}));
|
|
1600
|
+
let { x: xmin, x: xmax, y: ymin, y: ymax } = vertices[0];
|
|
1601
|
+
for (let i = 1; i < vertices.length; i++) {
|
|
1602
|
+
const { x, y } = vertices[i];
|
|
1603
|
+
if (x < xmin)
|
|
1604
|
+
xmin = x;
|
|
1605
|
+
else if (x > xmax)
|
|
1606
|
+
xmax = x;
|
|
1607
|
+
if (y < ymin)
|
|
1608
|
+
ymin = y;
|
|
1609
|
+
else if (y > ymax)
|
|
1610
|
+
ymax = y;
|
|
1611
|
+
}
|
|
1612
|
+
xmin = clamp$1(xmin, 0, imageData.width - 1);
|
|
1613
|
+
ymin = clamp$1(ymin, 0, imageData.height - 1);
|
|
1614
|
+
xmax = clamp$1(xmax, 0, imageData.width - 1);
|
|
1615
|
+
ymax = clamp$1(ymax, 0, imageData.height - 1);
|
|
1616
|
+
for (let y = ymin; y < ymax; y++) {
|
|
1617
|
+
for (let x = xmin; x < xmax; x++) {
|
|
1618
|
+
// Compute whether the point is inside the quadrilateral
|
|
1619
|
+
// using the Ray casting algorithm (see https://en.wikipedia.org/wiki/Point_in_polygon)
|
|
1620
|
+
let isInside = false;
|
|
1621
|
+
for (let i = 0; i < vertices.length; i++) {
|
|
1622
|
+
const pointA = vertices[i];
|
|
1623
|
+
const pointB = i === vertices.length - 1 ? vertices[0] : vertices[i + 1];
|
|
1624
|
+
// Line equation from A to B: y(x) = m * x + c
|
|
1625
|
+
const m = (pointB.y - pointA.y) / (pointB.x - pointA.x);
|
|
1626
|
+
const c = pointB.y - m * pointB.x;
|
|
1627
|
+
// Invert the equation: x(y) = (y - c) / m
|
|
1628
|
+
// Look only for points that are on the left of the line, so for a given y, x < (y - c) / m
|
|
1629
|
+
if (pointA.y > y != pointB.y > y && x < (y - c) / m) {
|
|
1630
|
+
isInside = !isInside;
|
|
1631
|
+
}
|
|
1632
|
+
}
|
|
1633
|
+
// If the point is inside the quadrilateral, allow interactions with it
|
|
1634
|
+
if (isInside) {
|
|
1635
|
+
const index = (y * imageData.width + x) * 4;
|
|
1636
|
+
imageData.data[index] = r;
|
|
1637
|
+
imageData.data[index + 1] = g;
|
|
1638
|
+
imageData.data[index + 2] = b;
|
|
1639
|
+
imageData.data[index + 3] = 255;
|
|
1640
|
+
}
|
|
1641
|
+
}
|
|
1527
1642
|
}
|
|
1528
|
-
return node;
|
|
1529
|
-
}
|
|
1530
|
-
// The following functions handle various kinds of data translation from the pixel space to the
|
|
1531
|
-
// time/space referential:
|
|
1532
|
-
function getTimeToPixel(timeOrigin, pixelOffset, timeScale) {
|
|
1533
|
-
return (time) => pixelOffset + (time - timeOrigin) / timeScale;
|
|
1534
|
-
}
|
|
1535
|
-
function getPixelToTime(timeOrigin, pixelOffset, timeScale) {
|
|
1536
|
-
return (timePixel) => (timePixel - pixelOffset) * timeScale + timeOrigin;
|
|
1537
|
-
}
|
|
1538
|
-
function getSpaceToPixel(pixelOffset, binaryTree) {
|
|
1539
|
-
return (position, fromEnd) => {
|
|
1540
|
-
const { from, pixelFrom, pixelTo, coefficient } = getNormalizedScaleAtPosition(position, binaryTree, fromEnd);
|
|
1541
|
-
// Rare case where coefficient is 0:
|
|
1542
|
-
// (occurs when there is just a flat step, for instance)
|
|
1543
|
-
if (coefficient === 0)
|
|
1544
|
-
return pixelOffset + (fromEnd ? pixelTo : pixelFrom);
|
|
1545
|
-
// Normal case: We simply interpolate
|
|
1546
|
-
return pixelOffset + pixelFrom + (position - from) / coefficient;
|
|
1547
|
-
};
|
|
1548
|
-
}
|
|
1549
|
-
function getPixelToSpace(pixelOffset, binaryTree) {
|
|
1550
|
-
return (spacePixel) => {
|
|
1551
|
-
const { from, pixelFrom, coefficient } = getNormalizedScaleAtPixel(spacePixel - pixelOffset, binaryTree);
|
|
1552
|
-
return from + (spacePixel - pixelOffset - pixelFrom) * coefficient;
|
|
1553
|
-
};
|
|
1554
|
-
}
|
|
1555
|
-
function getPointToData(getTime, getSpace, timeAxis, spaceAxis) {
|
|
1556
|
-
return (point) => ({
|
|
1557
|
-
time: getTime(point[timeAxis]),
|
|
1558
|
-
position: getSpace(point[spaceAxis]),
|
|
1559
|
-
});
|
|
1560
1643
|
}
|
|
1561
|
-
function
|
|
1562
|
-
|
|
1563
|
-
|
|
1564
|
-
|
|
1565
|
-
|
|
1644
|
+
function drawAliasedRect(imageData, { x, y }, width, height, [r, g, b], scalingRatio = 1) {
|
|
1645
|
+
x = Math.round(x * scalingRatio);
|
|
1646
|
+
y = Math.round(y * scalingRatio);
|
|
1647
|
+
width = Math.round(width * scalingRatio);
|
|
1648
|
+
height = Math.round(height * scalingRatio);
|
|
1649
|
+
const xMin = clamp$1(x, 0, imageData.width);
|
|
1650
|
+
const yMin = clamp$1(y, 0, imageData.height);
|
|
1651
|
+
const xMax = clamp$1(x + width, 0, imageData.width);
|
|
1652
|
+
const yMax = clamp$1(y + height, 0, imageData.height);
|
|
1653
|
+
for (let i = xMin; i < xMax; i++) {
|
|
1654
|
+
for (let j = yMin; j < yMax; j++) {
|
|
1655
|
+
const index = (j * imageData.width + i) * 4;
|
|
1656
|
+
imageData.data[index] = r;
|
|
1657
|
+
imageData.data[index + 1] = g;
|
|
1658
|
+
imageData.data[index + 2] = b;
|
|
1659
|
+
imageData.data[index + 3] = 255;
|
|
1660
|
+
}
|
|
1661
|
+
}
|
|
1566
1662
|
}
|
|
1567
1663
|
/**
|
|
1568
|
-
* This function
|
|
1569
|
-
*
|
|
1664
|
+
* This function draws a "stop" path extremity.
|
|
1665
|
+
* That handles a path that stops or starts exactly in an operational points included in the line
|
|
1666
|
+
* represented in the chart.
|
|
1570
1667
|
*/
|
|
1571
|
-
|
|
1572
|
-
|
|
1573
|
-
|
|
1574
|
-
if (
|
|
1575
|
-
|
|
1576
|
-
|
|
1668
|
+
const STOP_END_SIZE = 6;
|
|
1669
|
+
function drawPathStopExtremity(ctx, timePixel, spacePixel, swapAxis) {
|
|
1670
|
+
ctx.beginPath();
|
|
1671
|
+
if (!swapAxis) {
|
|
1672
|
+
ctx.moveTo(timePixel, spacePixel - STOP_END_SIZE / 2);
|
|
1673
|
+
ctx.lineTo(timePixel, spacePixel + STOP_END_SIZE / 2);
|
|
1577
1674
|
}
|
|
1578
|
-
|
|
1579
|
-
|
|
1580
|
-
|
|
1581
|
-
|
|
1582
|
-
|
|
1583
|
-
if (point.time > maxPoint.time)
|
|
1584
|
-
maxPoint = point;
|
|
1585
|
-
});
|
|
1586
|
-
if (minPoint.time > time)
|
|
1587
|
-
return minPoint.position;
|
|
1588
|
-
return maxPoint.position;
|
|
1675
|
+
else {
|
|
1676
|
+
ctx.moveTo(spacePixel - STOP_END_SIZE / 2, timePixel);
|
|
1677
|
+
ctx.lineTo(spacePixel + STOP_END_SIZE / 2, timePixel);
|
|
1678
|
+
}
|
|
1679
|
+
ctx.stroke();
|
|
1589
1680
|
}
|
|
1590
1681
|
/**
|
|
1591
|
-
* This function
|
|
1592
|
-
*
|
|
1593
|
-
* positions between these the two input positions (the array can be empty).
|
|
1682
|
+
* This function draws an "out" path extremity.
|
|
1683
|
+
* That handles a path that leaves or joins the line represented in the chart.
|
|
1594
1684
|
*/
|
|
1595
|
-
|
|
1596
|
-
|
|
1597
|
-
|
|
1598
|
-
|
|
1599
|
-
|
|
1600
|
-
let
|
|
1601
|
-
|
|
1602
|
-
|
|
1603
|
-
|
|
1604
|
-
|
|
1685
|
+
const OUT_END_SIZE = 12;
|
|
1686
|
+
function drawPathOutExtremity(ctx, timePixel, spacePixel, swapAxis, extremityType, pathDirection) {
|
|
1687
|
+
let horizontalSign = extremityType === 'from' ? -1 : 1;
|
|
1688
|
+
let verticalSign = (pathDirection === 'backward' ? -1 : 1) * horizontalSign;
|
|
1689
|
+
let controlX = timePixel + 4 * horizontalSign;
|
|
1690
|
+
let controlY = spacePixel + (OUT_END_SIZE - 2) * verticalSign;
|
|
1691
|
+
let x = timePixel;
|
|
1692
|
+
let y = spacePixel;
|
|
1693
|
+
if (swapAxis) {
|
|
1694
|
+
[horizontalSign, verticalSign] = [verticalSign, horizontalSign];
|
|
1695
|
+
[controlX, controlY] = [controlY, controlX];
|
|
1696
|
+
[x, y] = [y, x];
|
|
1605
1697
|
}
|
|
1606
|
-
|
|
1698
|
+
ctx.beginPath();
|
|
1699
|
+
ctx.moveTo(x, y);
|
|
1700
|
+
ctx.bezierCurveTo(controlX, controlY, controlX, controlY, x + OUT_END_SIZE * horizontalSign, y + OUT_END_SIZE * verticalSign);
|
|
1701
|
+
ctx.stroke();
|
|
1607
1702
|
}
|
|
1608
1703
|
/**
|
|
1609
|
-
*
|
|
1610
|
-
* on the center of the rectangle the user drew.
|
|
1611
|
-
* In case we reach max zoom, not to break this expectation we need a different offset calculation
|
|
1704
|
+
* This function draws a path extremity.
|
|
1612
1705
|
*/
|
|
1613
|
-
function
|
|
1614
|
-
|
|
1615
|
-
|
|
1616
|
-
|
|
1617
|
-
|
|
1618
|
-
|
|
1619
|
-
|
|
1620
|
-
: { xOffset: spaceOffset, yOffset: timeOffset };
|
|
1706
|
+
function drawPathExtremity(ctx, timePixel, spacePixel, swapAxis, extremityType, pathDirection, pathEnd) {
|
|
1707
|
+
if (pathEnd === 'out') {
|
|
1708
|
+
drawPathOutExtremity(ctx, timePixel, spacePixel, swapAxis, extremityType, pathDirection);
|
|
1709
|
+
}
|
|
1710
|
+
else {
|
|
1711
|
+
drawPathStopExtremity(ctx, timePixel, spacePixel, swapAxis);
|
|
1712
|
+
}
|
|
1621
1713
|
}
|
|
1622
|
-
|
|
1623
|
-
|
|
1624
|
-
|
|
1625
|
-
|
|
1626
|
-
|
|
1627
|
-
|
|
1628
|
-
|
|
1629
|
-
const
|
|
1630
|
-
|
|
1714
|
+
/**
|
|
1715
|
+
* You most likely need to wrap this function call with ctx.save ctx.restore
|
|
1716
|
+
* as it modifies ctx current transformation matrix
|
|
1717
|
+
*/
|
|
1718
|
+
function fillRect(ctx, { timeStart, timeEnd, spaceStart, spaceEnd }, { getPoint }) {
|
|
1719
|
+
const startPoint = getPoint({ time: Number(timeStart), position: spaceStart });
|
|
1720
|
+
const endPoint = getPoint({ time: Number(timeEnd), position: spaceEnd });
|
|
1721
|
+
const width = endPoint.x - startPoint.x;
|
|
1722
|
+
const height = endPoint.y - startPoint.y;
|
|
1723
|
+
if (width !== 0 && height !== 0) {
|
|
1724
|
+
ctx.translate(startPoint.x, startPoint.y);
|
|
1725
|
+
ctx.fillRect(0, 0, width, height);
|
|
1726
|
+
}
|
|
1727
|
+
return { width, height };
|
|
1631
1728
|
}
|
|
1632
1729
|
|
|
1633
1730
|
const DEFAULT_PICKING_TOLERANCE = 5;
|
|
@@ -1877,7 +1974,7 @@ const PathLayer = ({ path, color, level = DEFAULT_LEVEL, pickingTolerance = DEFA
|
|
|
1877
1974
|
/**
|
|
1878
1975
|
* This function draws the extremities of the path.
|
|
1879
1976
|
*/
|
|
1880
|
-
const drawExtremities = useCallback((ctx, { getTimePixel, getSpacePixel, swapAxis }) => {
|
|
1977
|
+
const drawExtremities = useCallback((ctx, { getTimePixel, getSpacePixel, swapAxis = false }) => {
|
|
1881
1978
|
if (!path.points.length)
|
|
1882
1979
|
return;
|
|
1883
1980
|
const from = path.points[0];
|
|
@@ -2029,7 +2126,7 @@ const PathLayer = ({ path, color, level = DEFAULT_LEVEL, pickingTolerance = DEFA
|
|
|
2029
2126
|
computePathLength,
|
|
2030
2127
|
drawLabel,
|
|
2031
2128
|
]);
|
|
2032
|
-
useDraw('paths', drawAll);
|
|
2129
|
+
useDraw(SpaceTimeChartCanvasContext, 'paths', drawAll);
|
|
2033
2130
|
const drawPicking = useCallback((imageData, stcContext, scalingRatio) => {
|
|
2034
2131
|
const { registerPickingElement } = stcContext;
|
|
2035
2132
|
// Draw single point:
|
|
@@ -2074,7 +2171,7 @@ const PathLayer = ({ path, color, level = DEFAULT_LEVEL, pickingTolerance = DEFA
|
|
|
2074
2171
|
drawAliasedDisc(imageData, point, (STYLES[level].width + pickingTolerance) * 2, lineColor, false, scalingRatio);
|
|
2075
2172
|
});
|
|
2076
2173
|
}, [getPathLines, getSnapPoints, level, path.id, path.points, pickingTolerance]);
|
|
2077
|
-
usePicking('paths', drawPicking);
|
|
2174
|
+
usePicking(SpaceTimeChartCanvasContext, 'paths', drawPicking);
|
|
2078
2175
|
return null;
|
|
2079
2176
|
};
|
|
2080
2177
|
|
|
@@ -2130,11 +2227,7 @@ function snapPosition(mousePosition, hoveredItem) {
|
|
|
2130
2227
|
}
|
|
2131
2228
|
|
|
2132
2229
|
const SpaceTimeChart = (props) => {
|
|
2133
|
-
const { operationalPoints, spaceOrigin, spaceScales, timeOrigin, timeScale, xOffset = 0, yOffset = 0, swapAxis, onHoveredChildUpdate, children, additionalChildren, enableSnapping, hideTimeCaptions, hideGrid, hidePathsLabels, showTicks, hideDates, theme,
|
|
2134
|
-
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
2135
|
-
onPan, onZoom, onClick, onMouseMove,
|
|
2136
|
-
/* eslint-enable @typescript-eslint/no-unused-vars */
|
|
2137
|
-
...attr } = props;
|
|
2230
|
+
const { operationalPoints, spaceOrigin, spaceScales, timeOrigin, timeScale, xOffset = 0, yOffset = 0, swapAxis, onHoveredChildUpdate, children, additionalChildren, enableSnapping, hideTimeCaptions, hideGrid, hidePathsLabels, showTicks, hideDates, theme, onPan, onZoom, onClick, onMouseMove, ...attr } = props;
|
|
2138
2231
|
const [root, setRoot] = useState(null);
|
|
2139
2232
|
const [canvasesRoot, setCanvasesRoot] = useState(null);
|
|
2140
2233
|
const fullTheme = useMemo(() => ({ ...DEFAULT_THEME, ...theme }), [theme]);
|
|
@@ -2227,7 +2320,7 @@ const SpaceTimeChart = (props) => {
|
|
|
2227
2320
|
timeOrigin,
|
|
2228
2321
|
timeScale,
|
|
2229
2322
|
timePixelOffset,
|
|
2230
|
-
spacePixelOffset,
|
|
2323
|
+
yPixelOffset: spacePixelOffset,
|
|
2231
2324
|
timeAxis,
|
|
2232
2325
|
spaceAxis,
|
|
2233
2326
|
swapAxis: !!swapAxis,
|
|
@@ -2275,14 +2368,15 @@ const SpaceTimeChart = (props) => {
|
|
|
2275
2368
|
return (React.createElement("div", { ...attr, ref: setRoot, className: cx('relative space-time-chart', attr.className), style: { background: fullTheme.background } },
|
|
2276
2369
|
React.createElement("div", { ref: setCanvasesRoot, className: "absolute inset-0" }),
|
|
2277
2370
|
React.createElement(SpaceTimeChartContext.Provider, { value: contextState },
|
|
2278
|
-
React.createElement(
|
|
2279
|
-
React.createElement(
|
|
2280
|
-
|
|
2281
|
-
React.createElement(
|
|
2282
|
-
|
|
2283
|
-
|
|
2284
|
-
|
|
2285
|
-
|
|
2371
|
+
React.createElement(SpaceTimeChartCanvasContext.Provider, { value: canvasContext },
|
|
2372
|
+
React.createElement(TimeChartCanvasContext.Provider, { value: canvasContext },
|
|
2373
|
+
React.createElement(MouseContext.Provider, { value: mouseContext },
|
|
2374
|
+
!hideGrid && (React.createElement(React.Fragment, null,
|
|
2375
|
+
React.createElement(SpaceGraduations, null),
|
|
2376
|
+
React.createElement(TimeGraduations, null),
|
|
2377
|
+
React.createElement(TimeCaptions, null))),
|
|
2378
|
+
children,
|
|
2379
|
+
additionalChildren))))));
|
|
2286
2380
|
};
|
|
2287
2381
|
|
|
2288
2382
|
function isConflictPickingElement(element) {
|
|
@@ -2313,7 +2407,7 @@ const ConflictLayer = ({ conflicts }) => {
|
|
|
2313
2407
|
ctx.fill(paths[i]);
|
|
2314
2408
|
}
|
|
2315
2409
|
}, [conflicts]);
|
|
2316
|
-
useDraw('paths', drawConflictLayer);
|
|
2410
|
+
useDraw(SpaceTimeChartCanvasContext, 'paths', drawConflictLayer);
|
|
2317
2411
|
const drawPicking = useCallback((imageData, { registerPickingElement, getTimePixel, getSpacePixel }, scalingRatio) => {
|
|
2318
2412
|
for (const [conflictIndex, conflict] of conflicts.entries()) {
|
|
2319
2413
|
const x = getTimePixel(conflict.timeStart);
|
|
@@ -2327,7 +2421,7 @@ const ConflictLayer = ({ conflicts }) => {
|
|
|
2327
2421
|
drawAliasedRect(imageData, { x: x - border, y: y - border }, width + 2 * border, height + 2 * border, color, scalingRatio);
|
|
2328
2422
|
}
|
|
2329
2423
|
}, [conflicts]);
|
|
2330
|
-
usePicking('paths', drawPicking);
|
|
2424
|
+
usePicking(SpaceTimeChartCanvasContext, 'paths', drawPicking);
|
|
2331
2425
|
return null;
|
|
2332
2426
|
};
|
|
2333
2427
|
|
|
@@ -2377,7 +2471,7 @@ const OccupancyBlockLayer = ({ occupancyBlocks }) => {
|
|
|
2377
2471
|
ctx.fillRect(x, y, width, height);
|
|
2378
2472
|
}
|
|
2379
2473
|
}, [occupancyBlocks]);
|
|
2380
|
-
useDraw('background', drawOccupancyBlockLayer);
|
|
2474
|
+
useDraw(SpaceTimeChartCanvasContext, 'background', drawOccupancyBlockLayer);
|
|
2381
2475
|
return null;
|
|
2382
2476
|
};
|
|
2383
2477
|
|
|
@@ -2394,7 +2488,7 @@ const PatternRect = ({ imageElement, ...rect }) => {
|
|
|
2394
2488
|
ctx.restore();
|
|
2395
2489
|
}
|
|
2396
2490
|
}, [imageElement, rect]);
|
|
2397
|
-
useDraw('background', drawPatternRect);
|
|
2491
|
+
useDraw(SpaceTimeChartCanvasContext, 'background', drawPatternRect);
|
|
2398
2492
|
return null;
|
|
2399
2493
|
};
|
|
2400
2494
|
|
|
@@ -2448,7 +2542,7 @@ const Quadrilateral = ({ id, vertices, style }) => {
|
|
|
2448
2542
|
ctx.stroke();
|
|
2449
2543
|
ctx.restore();
|
|
2450
2544
|
}, [vertices, style]);
|
|
2451
|
-
useDraw('background', drawRegion);
|
|
2545
|
+
useDraw(SpaceTimeChartCanvasContext, 'background', drawRegion);
|
|
2452
2546
|
const drawPicking = useCallback((imageData, { registerPickingElement, getTimePixel, getSpacePixel }, scalingRatio) => {
|
|
2453
2547
|
const points = vertices.map((vertice) => ({
|
|
2454
2548
|
x: getTimePixel(vertice.time),
|
|
@@ -2459,7 +2553,7 @@ const Quadrilateral = ({ id, vertices, style }) => {
|
|
|
2459
2553
|
const color = hexToRgb(indexToColor(index));
|
|
2460
2554
|
drawAliasedQuadrilateral(imageData, points, color, scalingRatio);
|
|
2461
2555
|
}, [id, vertices]);
|
|
2462
|
-
usePicking('paths', drawPicking);
|
|
2556
|
+
usePicking(SpaceTimeChartCanvasContext, 'paths', drawPicking);
|
|
2463
2557
|
return null;
|
|
2464
2558
|
};
|
|
2465
2559
|
|
|
@@ -2496,7 +2590,7 @@ const ZoomRect = (rect) => {
|
|
|
2496
2590
|
}
|
|
2497
2591
|
ctx.restore();
|
|
2498
2592
|
}, [rect]);
|
|
2499
|
-
useDraw('background', drawZoomRect);
|
|
2593
|
+
useDraw(SpaceTimeChartCanvasContext, 'background', drawZoomRect);
|
|
2500
2594
|
return null;
|
|
2501
2595
|
};
|
|
2502
2596
|
|
|
@@ -2589,20 +2683,19 @@ function getDistance(a, b) {
|
|
|
2589
2683
|
};
|
|
2590
2684
|
}
|
|
2591
2685
|
|
|
2592
|
-
const Waypoint = ({ waypoint: { name, secondaryCode, id, position, onClick }, waypointRef, isActive, isMenuActive, testIdPrefix, }) => (React.createElement("div", { "data-testid": testIdPrefix ? `${testIdPrefix}-base-info` : undefined, className: cx('flex waypoint items-baseline', {
|
|
2686
|
+
const Waypoint = ({ waypoint: { name, secondaryCode, id, position, onClick, styles }, waypointRef, isActive, isMenuActive, testIdPrefix, hidePositions = false, }) => (React.createElement("div", { "data-testid": testIdPrefix ? `${testIdPrefix}-base-info` : undefined, className: cx('flex waypoint items-baseline', {
|
|
2593
2687
|
'waypoint-active': isActive,
|
|
2594
2688
|
'menu-active': isMenuActive,
|
|
2595
|
-
}), id: id, ref: waypointRef, onClick: () => {
|
|
2689
|
+
}), style: styles, id: id, ref: waypointRef, onClick: () => {
|
|
2596
2690
|
if (onClick && !isMenuActive)
|
|
2597
2691
|
onClick(id);
|
|
2598
2692
|
} },
|
|
2599
|
-
React.createElement("div", { "data-testid": testIdPrefix ? `${testIdPrefix}-position` : undefined, className: "waypoint-position justify-self-start text-end" }, positionMmToKm(position)),
|
|
2693
|
+
!hidePositions && (React.createElement("div", { "data-testid": testIdPrefix ? `${testIdPrefix}-position` : undefined, className: "waypoint-position justify-self-start text-end" }, positionMmToKm(position))),
|
|
2600
2694
|
React.createElement("div", { "data-testid": testIdPrefix ? `${testIdPrefix}-name` : undefined, className: "waypoint-name mx-2 justify-self-start" }, name),
|
|
2601
2695
|
React.createElement("div", { className: "waypoint-separator" }),
|
|
2602
|
-
React.createElement(
|
|
2603
|
-
|
|
2604
|
-
|
|
2605
|
-
React.createElement("div", { className: "waypoint-separator" })));
|
|
2696
|
+
secondaryCode && (React.createElement(React.Fragment, null,
|
|
2697
|
+
React.createElement("div", { "data-testid": testIdPrefix ? `${testIdPrefix}-ch` : undefined, className: "waypoint-ch font-mono justify-self-end" }, secondaryCode),
|
|
2698
|
+
React.createElement("div", { className: "waypoint-separator" })))));
|
|
2606
2699
|
|
|
2607
2700
|
const zoomValueToTimeScale = (slider) => MIN_ZOOM_MS_PER_PX * Math.pow(MAX_ZOOM_MS_PER_PX / MIN_ZOOM_MS_PER_PX, slider / 100);
|
|
2608
2701
|
const timeScaleToZoomValue = (timeScale) => (100 * Math.log(timeScale / MIN_ZOOM_MS_PER_PX)) /
|
|
@@ -2644,82 +2737,245 @@ const filterVisibleElements$1 = (elements, totalDistance, heightWithoutFinalWayp
|
|
|
2644
2737
|
if (hasSpace) {
|
|
2645
2738
|
displayedElements.push(element);
|
|
2646
2739
|
}
|
|
2647
|
-
}
|
|
2648
|
-
return displayedElements.sort((a, b) => a.position - b.position);
|
|
2649
|
-
};
|
|
2650
|
-
const selectWaypointsToDisplay = (waypoints, { height, isProportional, yZoom }) => {
|
|
2651
|
-
if (waypoints.length < 2)
|
|
2652
|
-
return [];
|
|
2653
|
-
// display all waypoints in linear mode
|
|
2654
|
-
if (!isProportional)
|
|
2655
|
-
return waypoints;
|
|
2656
|
-
const totalDistance = calcTotalDistance(waypoints);
|
|
2657
|
-
const manchetteHeight = getHeightWithoutLastWaypoint(height);
|
|
2658
|
-
// in proportional mode, hide some waypoints to avoid collisions
|
|
2659
|
-
const minSpace = BASE_WAYPOINT_HEIGHT / yZoom;
|
|
2660
|
-
return filterVisibleElements$1(waypoints, totalDistance, manchetteHeight, minSpace);
|
|
2661
|
-
};
|
|
2662
|
-
/**
|
|
2663
|
-
* 2 modes for space scales
|
|
2664
|
-
* km (isProportional): { coefficient: gives a scale in meter/pixel }
|
|
2665
|
-
* linear: { size: height in pixel } (each point distributed evenly along the height of manchette.)
|
|
2666
|
-
*/
|
|
2667
|
-
const getScales = (waypoints, { isProportional, yZoom, height }, minZoomMillimeterPerPx, maxZoomMillimeterPerPx) => {
|
|
2668
|
-
if (!waypoints.length)
|
|
2669
|
-
return [];
|
|
2670
|
-
if (waypoints.length === 1) {
|
|
2671
|
-
const waypoint = waypoints[0];
|
|
2672
|
-
return [
|
|
2673
|
-
{
|
|
2674
|
-
from: waypoint.position,
|
|
2675
|
-
to: waypoint.position,
|
|
2676
|
-
size: height || 1,
|
|
2677
|
-
},
|
|
2678
|
-
];
|
|
2679
|
-
}
|
|
2680
|
-
if (!isProportional) {
|
|
2681
|
-
return waypoints.slice(0, -1).map((from, index) => {
|
|
2682
|
-
const to = waypoints[index + 1];
|
|
2740
|
+
}
|
|
2741
|
+
return displayedElements.sort((a, b) => a.position - b.position);
|
|
2742
|
+
};
|
|
2743
|
+
const selectWaypointsToDisplay = (waypoints, { height, isProportional, yZoom }) => {
|
|
2744
|
+
if (waypoints.length < 2)
|
|
2745
|
+
return [];
|
|
2746
|
+
// display all waypoints in linear mode
|
|
2747
|
+
if (!isProportional)
|
|
2748
|
+
return waypoints;
|
|
2749
|
+
const totalDistance = calcTotalDistance(waypoints);
|
|
2750
|
+
const manchetteHeight = getHeightWithoutLastWaypoint(height);
|
|
2751
|
+
// in proportional mode, hide some waypoints to avoid collisions
|
|
2752
|
+
const minSpace = BASE_WAYPOINT_HEIGHT / yZoom;
|
|
2753
|
+
return filterVisibleElements$1(waypoints, totalDistance, manchetteHeight, minSpace);
|
|
2754
|
+
};
|
|
2755
|
+
/**
|
|
2756
|
+
* 2 modes for space scales
|
|
2757
|
+
* km (isProportional): { coefficient: gives a scale in meter/pixel }
|
|
2758
|
+
* linear: { size: height in pixel } (each point distributed evenly along the height of manchette.)
|
|
2759
|
+
*/
|
|
2760
|
+
const getScales = (waypoints, { isProportional, yZoom, height }, minZoomMillimeterPerPx, maxZoomMillimeterPerPx) => {
|
|
2761
|
+
if (!waypoints.length)
|
|
2762
|
+
return [];
|
|
2763
|
+
if (waypoints.length === 1) {
|
|
2764
|
+
const waypoint = waypoints[0];
|
|
2765
|
+
return [
|
|
2766
|
+
{
|
|
2767
|
+
from: waypoint.position,
|
|
2768
|
+
to: waypoint.position,
|
|
2769
|
+
size: height || 1,
|
|
2770
|
+
},
|
|
2771
|
+
];
|
|
2772
|
+
}
|
|
2773
|
+
if (!isProportional) {
|
|
2774
|
+
return waypoints.slice(0, -1).map((from, index) => {
|
|
2775
|
+
const to = waypoints[index + 1];
|
|
2776
|
+
return {
|
|
2777
|
+
from: from.position,
|
|
2778
|
+
to: to.position,
|
|
2779
|
+
size: BASE_WAYPOINT_HEIGHT * yZoom,
|
|
2780
|
+
};
|
|
2781
|
+
});
|
|
2782
|
+
}
|
|
2783
|
+
const from = waypoints.at(0).position;
|
|
2784
|
+
const to = waypoints.at(-1).position;
|
|
2785
|
+
const scaleCoeff = isProportional
|
|
2786
|
+
? { coefficient: zoomValueToSpaceScale(minZoomMillimeterPerPx, maxZoomMillimeterPerPx, yZoom) }
|
|
2787
|
+
: { size: BASE_WAYPOINT_HEIGHT * (waypoints.length - 1) * yZoom };
|
|
2788
|
+
return [
|
|
2789
|
+
{
|
|
2790
|
+
from,
|
|
2791
|
+
to,
|
|
2792
|
+
...scaleCoeff,
|
|
2793
|
+
},
|
|
2794
|
+
];
|
|
2795
|
+
};
|
|
2796
|
+
const isInteractiveWaypoint = (item) => item != null && typeof item === 'object' && 'id' in item && 'position' in item;
|
|
2797
|
+
|
|
2798
|
+
const Manchette = ({ zoomYIn, zoomYOut, resetZoom, yZoom = 1, contents, activeWaypointId, activeWaypointRef, isProportional = true, toggleMode, children, height = INITIAL_OP_LIST_HEIGHT, width = 295, hidePositions = false, translations, }) => (React.createElement("div", { className: "ui-manchette-container", style: { width: `${width}px` } },
|
|
2799
|
+
React.createElement("div", { className: "bg-white-100 border-r border-grey-30 relative", style: { height: `${height}px` } },
|
|
2800
|
+
React.createElement("div", { className: "waypoints-list", style: { width: `${width - 1}px` } },
|
|
2801
|
+
contents.map((content, index) => isInteractiveWaypoint(content) ? (React.createElement(Waypoint, { key: content.id, waypoint: content, waypointRef: activeWaypointId === content.id ? activeWaypointRef : undefined, isActive: activeWaypointId === content.id, isMenuActive: !!activeWaypointId, testIdPrefix: "waypoint", hidePositions: hidePositions })) : (React.createElement(Fragment, { key: index }, content))),
|
|
2802
|
+
children)),
|
|
2803
|
+
React.createElement("div", { "data-testid": "manchette-actions", className: "manchette-actions" },
|
|
2804
|
+
React.createElement("div", { className: "zoom-buttons" },
|
|
2805
|
+
React.createElement("button", { "data-testid": "zoom-out-button", className: "zoom-out", onClick: zoomYOut, disabled: yZoom <= MIN_ZOOM_Y || !!activeWaypointId },
|
|
2806
|
+
React.createElement(ZoomOut, null)),
|
|
2807
|
+
React.createElement("button", { "data-testid": "zoom-in-button", className: "zoom-in", onClick: zoomYIn, disabled: yZoom >= MAX_ZOOM_Y || !!activeWaypointId },
|
|
2808
|
+
React.createElement(ZoomIn, null)),
|
|
2809
|
+
React.createElement("button", { "data-testid": "reset-zoom-button", disabled: !!activeWaypointId, className: "zoom-reset", onClick: resetZoom }, "Fit")),
|
|
2810
|
+
React.createElement("div", { className: "flex items-center ml-auto text-sans font-semibold" },
|
|
2811
|
+
React.createElement("button", { disabled: !!activeWaypointId, className: "toggle-mode", onClick: toggleMode },
|
|
2812
|
+
React.createElement("div", { className: "flex flex-col items-end pr-2" },
|
|
2813
|
+
React.createElement("span", { className: cx({ 'text-grey-30': !isProportional }), "data-testid": "km-mode-button" }, "Km"),
|
|
2814
|
+
React.createElement("span", { className: cx({ 'text-grey-30': isProportional }), "data-testid": "linear-mode-button" }, translations?.linearMode || 'Linear')))))));
|
|
2815
|
+
|
|
2816
|
+
const INITIAL_STATE = {
|
|
2817
|
+
xZoom: timeScaleToZoomValue(DEFAULT_ZOOM_MS_PER_PX),
|
|
2818
|
+
yZoom: 1,
|
|
2819
|
+
timeOrigin: 0,
|
|
2820
|
+
spaceOrigin: 0,
|
|
2821
|
+
xOffset: 0,
|
|
2822
|
+
yOffset: 0,
|
|
2823
|
+
scrollTo: null,
|
|
2824
|
+
panning: null,
|
|
2825
|
+
zoomMode: false,
|
|
2826
|
+
rect: null,
|
|
2827
|
+
pixelRect: null,
|
|
2828
|
+
};
|
|
2829
|
+
/**
|
|
2830
|
+
* This hook can be used to synchronize a manchette and a diagram which has a
|
|
2831
|
+
* time scale for X.
|
|
2832
|
+
*
|
|
2833
|
+
* It provides offsets on both axis and functions to handle some
|
|
2834
|
+
* classic behaviors such as zoom, pan and scroll.
|
|
2835
|
+
*/
|
|
2836
|
+
const useSyncManchette = ({ manchetteWithSpaceTimeChartRef, diagramRef, initialState = {
|
|
2837
|
+
timeOrigin: 0,
|
|
2838
|
+
spaceOrigin: 0,
|
|
2839
|
+
xOffset: 0,
|
|
2840
|
+
}, maxYZoom = MAX_ZOOM_Y, enableTimeZoom = true, }) => {
|
|
2841
|
+
const [isShiftPressed, setIsShiftPressed] = useState(false);
|
|
2842
|
+
const [state, setState] = useState({
|
|
2843
|
+
...INITIAL_STATE,
|
|
2844
|
+
timeOrigin: initialState.timeOrigin || 0,
|
|
2845
|
+
spaceOrigin: initialState.spaceOrigin || 0,
|
|
2846
|
+
xOffset: initialState.xOffset || 0,
|
|
2847
|
+
});
|
|
2848
|
+
const { yZoom, yOffset, scrollTo, rect } = state;
|
|
2849
|
+
// =========== Shift pressed ===========
|
|
2850
|
+
const handleKeyDown = useCallback((event) => {
|
|
2851
|
+
if (event.key === 'Shift') {
|
|
2852
|
+
setIsShiftPressed(true);
|
|
2853
|
+
}
|
|
2854
|
+
}, []);
|
|
2855
|
+
const handleKeyUp = useCallback((event) => {
|
|
2856
|
+
if (event.key === 'Shift') {
|
|
2857
|
+
setIsShiftPressed(false);
|
|
2858
|
+
}
|
|
2859
|
+
}, []);
|
|
2860
|
+
useEffect(() => {
|
|
2861
|
+
window.addEventListener('keydown', handleKeyDown);
|
|
2862
|
+
window.addEventListener('keyup', handleKeyUp);
|
|
2863
|
+
return () => {
|
|
2864
|
+
window.removeEventListener('keydown', handleKeyDown);
|
|
2865
|
+
window.removeEventListener('keyup', handleKeyUp);
|
|
2866
|
+
};
|
|
2867
|
+
}, [handleKeyDown, handleKeyUp]);
|
|
2868
|
+
// =========== Y Zoom ===========
|
|
2869
|
+
const zoomYIn = useCallback(() => {
|
|
2870
|
+
const newYZoom = Math.min(yZoom + ZOOM_Y_DELTA, maxYZoom);
|
|
2871
|
+
if (newYZoom !== yZoom) {
|
|
2872
|
+
const newYOffset = yOffset * (newYZoom / yZoom);
|
|
2873
|
+
setState((prev) => ({
|
|
2874
|
+
...prev,
|
|
2875
|
+
yZoom: newYZoom,
|
|
2876
|
+
yOffset: newYOffset,
|
|
2877
|
+
scrollTo: newYOffset,
|
|
2878
|
+
}));
|
|
2879
|
+
}
|
|
2880
|
+
}, [yZoom, maxYZoom, yOffset]);
|
|
2881
|
+
const zoomYOut = useCallback(() => {
|
|
2882
|
+
const newYZoom = Math.max(MIN_ZOOM_Y, yZoom - ZOOM_Y_DELTA);
|
|
2883
|
+
if (newYZoom !== yZoom) {
|
|
2884
|
+
const newYOffset = yOffset * (newYZoom / yZoom);
|
|
2885
|
+
setState((prev) => ({
|
|
2886
|
+
...prev,
|
|
2887
|
+
yZoom: newYZoom,
|
|
2888
|
+
yOffset: newYOffset,
|
|
2889
|
+
scrollTo: newYOffset,
|
|
2890
|
+
}));
|
|
2891
|
+
}
|
|
2892
|
+
}, [yZoom, yOffset]);
|
|
2893
|
+
const resetYZoom = useCallback(() => {
|
|
2894
|
+
setState((prev) => ({ ...prev, yZoom: 1 }));
|
|
2895
|
+
}, []);
|
|
2896
|
+
// =========== X Zoom ===========
|
|
2897
|
+
const handleXZoom = useCallback((newXZoom, xPosition = (diagramRef?.current?.offsetWidth || 0) / 2) => {
|
|
2898
|
+
if (enableTimeZoom)
|
|
2899
|
+
setState((prev) => ({
|
|
2900
|
+
...prev,
|
|
2901
|
+
...zoomX(prev.xZoom, prev.xOffset, newXZoom, xPosition),
|
|
2902
|
+
}));
|
|
2903
|
+
}, [enableTimeZoom, diagramRef]);
|
|
2904
|
+
const handleXZoomOnWheelEvent = useCallback((wheelEvent, newXZoom, xPosition) => {
|
|
2905
|
+
if (isShiftPressed && !rect) {
|
|
2906
|
+
wheelEvent.preventDefault();
|
|
2907
|
+
handleXZoom(newXZoom, xPosition);
|
|
2908
|
+
}
|
|
2909
|
+
}, [handleXZoom, isShiftPressed, rect]);
|
|
2910
|
+
// =========== Y Scroll ===========
|
|
2911
|
+
const handleScrollInManchette = useCallback((e) => {
|
|
2912
|
+
if (rect) {
|
|
2913
|
+
e.preventDefault();
|
|
2914
|
+
return;
|
|
2915
|
+
}
|
|
2916
|
+
if (!isShiftPressed && manchetteWithSpaceTimeChartRef.current) {
|
|
2917
|
+
const { scrollTop } = manchetteWithSpaceTimeChartRef.current;
|
|
2918
|
+
if (scrollTop || scrollTop === 0) {
|
|
2919
|
+
setState((prev) => ({ ...prev, yOffset: scrollTop }));
|
|
2920
|
+
}
|
|
2921
|
+
}
|
|
2922
|
+
}, [isShiftPressed, manchetteWithSpaceTimeChartRef, rect]);
|
|
2923
|
+
useEffect(() => {
|
|
2924
|
+
if (scrollTo !== null && manchetteWithSpaceTimeChartRef.current) {
|
|
2925
|
+
manchetteWithSpaceTimeChartRef.current.scrollTo({
|
|
2926
|
+
top: scrollTo,
|
|
2927
|
+
behavior: 'instant',
|
|
2928
|
+
});
|
|
2929
|
+
}
|
|
2930
|
+
}, [scrollTo, manchetteWithSpaceTimeChartRef]);
|
|
2931
|
+
// =========== On Pan ===========
|
|
2932
|
+
const basicOnPan = useCallback((payload, prev) => {
|
|
2933
|
+
if (!payload.isPanning) {
|
|
2683
2934
|
return {
|
|
2684
|
-
|
|
2685
|
-
|
|
2686
|
-
|
|
2935
|
+
...prev,
|
|
2936
|
+
panning: null,
|
|
2937
|
+
zoomMode: false,
|
|
2687
2938
|
};
|
|
2688
|
-
}
|
|
2689
|
-
|
|
2690
|
-
|
|
2691
|
-
|
|
2692
|
-
|
|
2693
|
-
|
|
2694
|
-
|
|
2695
|
-
|
|
2696
|
-
{
|
|
2697
|
-
|
|
2698
|
-
|
|
2699
|
-
|
|
2700
|
-
|
|
2701
|
-
|
|
2939
|
+
}
|
|
2940
|
+
if (!prev.panning) {
|
|
2941
|
+
return {
|
|
2942
|
+
...prev,
|
|
2943
|
+
panning: { initialOffset: { x: prev.xOffset, y: prev.yOffset } },
|
|
2944
|
+
};
|
|
2945
|
+
}
|
|
2946
|
+
const newState = { ...prev };
|
|
2947
|
+
const { initialOffset } = prev.panning;
|
|
2948
|
+
const diff = getDistance(payload.initialPosition, payload.position);
|
|
2949
|
+
const manchette = manchetteWithSpaceTimeChartRef.current;
|
|
2950
|
+
newState.xOffset = initialOffset.x + diff.x;
|
|
2951
|
+
let newYOffset = initialOffset.y - diff.y;
|
|
2952
|
+
newYOffset = Math.max(newYOffset, 0);
|
|
2953
|
+
if (manchette) {
|
|
2954
|
+
newYOffset = Math.min(newYOffset, manchette.scrollHeight - manchette.offsetHeight);
|
|
2955
|
+
manchette.scrollTop = newYOffset;
|
|
2956
|
+
}
|
|
2957
|
+
newState.yOffset = newYOffset;
|
|
2958
|
+
return newState;
|
|
2959
|
+
}, [manchetteWithSpaceTimeChartRef]);
|
|
2960
|
+
return useMemo(() => ({
|
|
2961
|
+
state,
|
|
2962
|
+
setState,
|
|
2963
|
+
handleScrollInManchette,
|
|
2964
|
+
handleXZoom,
|
|
2965
|
+
handleXZoomOnWheelEvent,
|
|
2966
|
+
yZoomHelpers: { zoomYIn, zoomYOut, resetZoom: resetYZoom },
|
|
2967
|
+
basicOnPan,
|
|
2968
|
+
}), [
|
|
2969
|
+
state,
|
|
2970
|
+
handleScrollInManchette,
|
|
2971
|
+
handleXZoom,
|
|
2972
|
+
handleXZoomOnWheelEvent,
|
|
2973
|
+
zoomYIn,
|
|
2974
|
+
zoomYOut,
|
|
2975
|
+
resetYZoom,
|
|
2976
|
+
basicOnPan,
|
|
2977
|
+
]);
|
|
2702
2978
|
};
|
|
2703
|
-
const isInteractiveWaypoint = (item) => item != null && typeof item === 'object' && 'id' in item && 'position' in item;
|
|
2704
|
-
|
|
2705
|
-
const Manchette = ({ zoomYIn, zoomYOut, resetZoom, yZoom = 1, contents, activeWaypointId, activeWaypointRef, isProportional = true, toggleMode, children, height = INITIAL_OP_LIST_HEIGHT, }) => (React.createElement("div", { className: "ui-manchette-container" },
|
|
2706
|
-
React.createElement("div", { className: "bg-white-100 border-r border-grey-30 relative", style: { minHeight: `${height}px` } },
|
|
2707
|
-
React.createElement("div", { className: "waypoints-list" },
|
|
2708
|
-
contents.map((content, index) => isInteractiveWaypoint(content) ? (React.createElement("div", { key: index, className: "waypoint-wrapper flex justify-start", style: content.styles },
|
|
2709
|
-
React.createElement(Waypoint, { waypoint: content, waypointRef: activeWaypointId === content.id ? activeWaypointRef : undefined, isActive: activeWaypointId === content.id, isMenuActive: !!activeWaypointId, testIdPrefix: "waypoint" }))) : (React.createElement(Fragment, { key: index }, content))),
|
|
2710
|
-
children)),
|
|
2711
|
-
React.createElement("div", { "data-testid": "manchette-actions", className: "manchette-actions" },
|
|
2712
|
-
React.createElement("div", { className: "zoom-buttons" },
|
|
2713
|
-
React.createElement("button", { "data-testid": "zoom-out-button", className: "zoom-out", onClick: zoomYOut, disabled: yZoom <= MIN_ZOOM_Y || !!activeWaypointId },
|
|
2714
|
-
React.createElement(ZoomOut, null)),
|
|
2715
|
-
React.createElement("button", { "data-testid": "zoom-in-button", className: "zoom-in", onClick: zoomYIn, disabled: yZoom >= MAX_ZOOM_Y || !!activeWaypointId },
|
|
2716
|
-
React.createElement(ZoomIn, null)),
|
|
2717
|
-
React.createElement("button", { "data-testid": "reset-zoom-button", disabled: !!activeWaypointId, className: "zoom-reset", onClick: resetZoom }, "Fit")),
|
|
2718
|
-
React.createElement("div", { className: "flex items-center ml-auto text-sans font-semibold" },
|
|
2719
|
-
React.createElement("button", { disabled: !!activeWaypointId, className: "toggle-mode", onClick: toggleMode },
|
|
2720
|
-
React.createElement("div", { className: "flex flex-col items-end pr-2" },
|
|
2721
|
-
React.createElement("span", { className: cx({ 'text-grey-30': !isProportional }), "data-testid": "km-mode-button" }, "Km"),
|
|
2722
|
-
React.createElement("span", { className: cx({ 'text-grey-30': isProportional }), "data-testid": "linear-mode-button" }, "Lin\u00E9aire")))))));
|
|
2723
2979
|
|
|
2724
2980
|
const DEFAULT_MANCHETTE_WITH_SPACE_TIME_CHART_OPTIONS = {
|
|
2725
2981
|
displayTimeCaptions: true,
|
|
@@ -2727,36 +2983,32 @@ const DEFAULT_MANCHETTE_WITH_SPACE_TIME_CHART_OPTIONS = {
|
|
|
2727
2983
|
enableSpacePan: true,
|
|
2728
2984
|
enableTimeZoom: true,
|
|
2729
2985
|
};
|
|
2730
|
-
const useManchetteWithSpaceTimeChart = ({ waypoints, manchetteWithSpaceTimeChartRef, height = INITIAL_SPACE_TIME_CHART_HEIGHT, spaceTimeChartRef, defaultTimeOrigin = 0, defaultSpaceOrigin = 0, defaultXOffset = 0, verticalPadding = BASE_WAYPOINT_HEIGHT
|
|
2731
|
-
const
|
|
2986
|
+
const useManchetteWithSpaceTimeChart = ({ waypoints, manchetteWithSpaceTimeChartRef, height = INITIAL_SPACE_TIME_CHART_HEIGHT, spaceTimeChartRef, defaultTimeOrigin = 0, defaultSpaceOrigin = 0, defaultXOffset = 0, verticalPadding = BASE_WAYPOINT_HEIGHT * 1.5, splitPoints = [], options = {}, }) => {
|
|
2987
|
+
const canvasDrawingHeight = Math.max(1 + BASE_WAYPOINT_HEIGHT, height - FOOTER_HEIGHT); // 521
|
|
2988
|
+
const drawingHeightWithoutTopPadding = canvasDrawingHeight - verticalPadding; // 473
|
|
2989
|
+
const drawingHeightWithoutBothPadding = canvasDrawingHeight - (verticalPadding + BASE_WAYPOINT_HEIGHT); // 441
|
|
2990
|
+
const { displayTimeCaptions, enableTimeZoom } = useMemo(() => ({
|
|
2732
2991
|
...DEFAULT_MANCHETTE_WITH_SPACE_TIME_CHART_OPTIONS,
|
|
2733
2992
|
...options,
|
|
2734
2993
|
}), [options]);
|
|
2735
|
-
const [
|
|
2736
|
-
const
|
|
2737
|
-
|
|
2738
|
-
|
|
2739
|
-
|
|
2740
|
-
|
|
2741
|
-
|
|
2742
|
-
|
|
2743
|
-
|
|
2744
|
-
|
|
2745
|
-
|
|
2746
|
-
|
|
2747
|
-
|
|
2748
|
-
isProportional: true,
|
|
2749
|
-
waypointsChart: [],
|
|
2750
|
-
scales: [],
|
|
2994
|
+
const [isProportional, setIsPropotional] = useState(true);
|
|
2995
|
+
const { state: { xZoom, yZoom, timeOrigin, spaceOrigin, xOffset, yOffset, zoomMode, rect }, setState: setSyncManchetteState, handleScrollInManchette, handleXZoom, handleXZoomOnWheelEvent, yZoomHelpers: { zoomYIn, zoomYOut, resetZoom }, basicOnPan, } = useSyncManchette({
|
|
2996
|
+
manchetteWithSpaceTimeChartRef,
|
|
2997
|
+
diagramRef: spaceTimeChartRef,
|
|
2998
|
+
initialState: {
|
|
2999
|
+
timeOrigin: defaultTimeOrigin,
|
|
3000
|
+
spaceOrigin: defaultSpaceOrigin,
|
|
3001
|
+
xOffset: defaultXOffset,
|
|
3002
|
+
},
|
|
3003
|
+
maxYZoom: isProportional
|
|
3004
|
+
? MAX_ZOOM_Y
|
|
3005
|
+
: (drawingHeightWithoutTopPadding - BASE_WAYPOINT_HEIGHT) / (2 * BASE_WAYPOINT_HEIGHT),
|
|
3006
|
+
enableTimeZoom,
|
|
2751
3007
|
});
|
|
2752
|
-
const { xZoom, yZoom, timeOrigin, spaceOrigin, xOffset, yOffset, scrollTo, panning, zoomMode, rect, isProportional, } = state;
|
|
2753
3008
|
/** used when we change the train dataset for example to center the chart on the new data */
|
|
2754
3009
|
const setTimeOrigin = useCallback((newTimeOrigin) => {
|
|
2755
|
-
|
|
2756
|
-
}, []);
|
|
2757
|
-
const canvasDrawingHeight = Math.max(1 + BASE_WAYPOINT_HEIGHT, height - FOOTER_HEIGHT); // 521
|
|
2758
|
-
const drawingHeightWithoutTopPadding = canvasDrawingHeight - BASE_WAYPOINT_HEIGHT / 2; // 505
|
|
2759
|
-
const drawingHeightWithoutBothPadding = canvasDrawingHeight - BASE_WAYPOINT_HEIGHT; // 489
|
|
3010
|
+
setSyncManchetteState((prev) => ({ ...prev, timeOrigin: newTimeOrigin }));
|
|
3011
|
+
}, [setSyncManchetteState]);
|
|
2760
3012
|
const totalDistance = calcTotalDistance(waypoints);
|
|
2761
3013
|
const { minZoomMillimeterPerPx, maxZoomMillimeterPerPx } = getExtremaScales(drawingHeightWithoutTopPadding, drawingHeightWithoutBothPadding, totalDistance);
|
|
2762
3014
|
const handleRectZoomEnd = useCallback((prev) => {
|
|
@@ -2772,7 +3024,7 @@ const useManchetteWithSpaceTimeChart = ({ waypoints, manchetteWithSpaceTimeChart
|
|
|
2772
3024
|
const newXOffset = sideOffset(prev.timeOrigin, newTimeScale, prev.rect.timeStart, prev.rect.timeEnd, spaceTimeChartRef.current.clientWidth);
|
|
2773
3025
|
let newYZoom = prev.yZoom;
|
|
2774
3026
|
let newYOffset = prev.yOffset;
|
|
2775
|
-
if (
|
|
3027
|
+
if (isProportional) {
|
|
2776
3028
|
const chosenSpaceScale = spaceRange / drawingHeightWithoutTopPadding;
|
|
2777
3029
|
const newSpaceScale = clamp$1(chosenSpaceScale, maxZoomMillimeterPerPx, minZoomMillimeterPerPx);
|
|
2778
3030
|
// we don’t need to handle this case and compute an offset
|
|
@@ -2808,93 +3060,19 @@ const useManchetteWithSpaceTimeChart = ({ waypoints, manchetteWithSpaceTimeChart
|
|
|
2808
3060
|
};
|
|
2809
3061
|
}, [
|
|
2810
3062
|
spaceTimeChartRef,
|
|
3063
|
+
isProportional,
|
|
2811
3064
|
drawingHeightWithoutTopPadding,
|
|
2812
3065
|
maxZoomMillimeterPerPx,
|
|
2813
3066
|
minZoomMillimeterPerPx,
|
|
2814
3067
|
height,
|
|
2815
3068
|
verticalPadding,
|
|
2816
3069
|
]);
|
|
2817
|
-
const zoomYIn = useCallback(() => {
|
|
2818
|
-
const maxZoom = isProportional
|
|
2819
|
-
? MAX_ZOOM_Y
|
|
2820
|
-
: (drawingHeightWithoutTopPadding - BASE_WAYPOINT_HEIGHT) / (2 * BASE_WAYPOINT_HEIGHT);
|
|
2821
|
-
const newYZoom = Math.min(yZoom + ZOOM_Y_DELTA, maxZoom);
|
|
2822
|
-
if (newYZoom !== yZoom) {
|
|
2823
|
-
const newYOffset = yOffset * (newYZoom / yZoom);
|
|
2824
|
-
setState((prev) => ({
|
|
2825
|
-
...prev,
|
|
2826
|
-
yZoom: newYZoom,
|
|
2827
|
-
yOffset: newYOffset,
|
|
2828
|
-
scrollTo: newYOffset,
|
|
2829
|
-
}));
|
|
2830
|
-
}
|
|
2831
|
-
}, [yZoom, yOffset, drawingHeightWithoutTopPadding, isProportional]);
|
|
2832
|
-
const zoomYOut = useCallback(() => {
|
|
2833
|
-
const newYZoom = Math.max(MIN_ZOOM_Y, yZoom - ZOOM_Y_DELTA);
|
|
2834
|
-
if (newYZoom !== yZoom) {
|
|
2835
|
-
const newYOffset = yOffset * (newYZoom / yZoom);
|
|
2836
|
-
setState((prev) => ({
|
|
2837
|
-
...prev,
|
|
2838
|
-
yZoom: newYZoom,
|
|
2839
|
-
yOffset: newYOffset,
|
|
2840
|
-
scrollTo: newYOffset,
|
|
2841
|
-
}));
|
|
2842
|
-
}
|
|
2843
|
-
}, [yZoom, yOffset]);
|
|
2844
|
-
useEffect(() => {
|
|
2845
|
-
if (scrollTo !== null && manchetteWithSpaceTimeChartRef.current) {
|
|
2846
|
-
manchetteWithSpaceTimeChartRef.current.scrollTo({
|
|
2847
|
-
top: scrollTo,
|
|
2848
|
-
behavior: 'instant',
|
|
2849
|
-
});
|
|
2850
|
-
}
|
|
2851
|
-
}, [scrollTo, manchetteWithSpaceTimeChartRef]);
|
|
2852
|
-
const resetZoom = useCallback(() => {
|
|
2853
|
-
setState((prev) => ({ ...prev, yZoom: 1 }));
|
|
2854
|
-
}, []);
|
|
2855
|
-
const handleScroll = useCallback((e) => {
|
|
2856
|
-
if (rect) {
|
|
2857
|
-
e.preventDefault();
|
|
2858
|
-
return;
|
|
2859
|
-
}
|
|
2860
|
-
if (!isShiftPressed && manchetteWithSpaceTimeChartRef.current) {
|
|
2861
|
-
const { scrollTop } = manchetteWithSpaceTimeChartRef.current;
|
|
2862
|
-
if (scrollTop || scrollTop === 0) {
|
|
2863
|
-
setState((prev) => ({ ...prev, yOffset: scrollTop }));
|
|
2864
|
-
}
|
|
2865
|
-
}
|
|
2866
|
-
}, [isShiftPressed, manchetteWithSpaceTimeChartRef, rect]);
|
|
2867
|
-
const handleKeyDown = useCallback((event) => {
|
|
2868
|
-
if (event.key === 'Shift') {
|
|
2869
|
-
setIsShiftPressed(true);
|
|
2870
|
-
}
|
|
2871
|
-
}, []);
|
|
2872
|
-
const handleKeyUp = useCallback((event) => {
|
|
2873
|
-
if (event.key === 'Shift') {
|
|
2874
|
-
setIsShiftPressed(false);
|
|
2875
|
-
}
|
|
2876
|
-
}, []);
|
|
2877
|
-
useEffect(() => {
|
|
2878
|
-
window.addEventListener('keydown', handleKeyDown);
|
|
2879
|
-
window.addEventListener('keyup', handleKeyUp);
|
|
2880
|
-
return () => {
|
|
2881
|
-
window.removeEventListener('keydown', handleKeyDown);
|
|
2882
|
-
window.removeEventListener('keyup', handleKeyUp);
|
|
2883
|
-
};
|
|
2884
|
-
}, [handleKeyDown, handleKeyUp]);
|
|
2885
3070
|
const toggleMode = useCallback(() => {
|
|
2886
|
-
|
|
3071
|
+
setIsPropotional((prev) => !prev);
|
|
2887
3072
|
}, []);
|
|
2888
3073
|
const toggleZoomMode = useCallback(() => {
|
|
2889
|
-
|
|
2890
|
-
}, []);
|
|
2891
|
-
const handleXZoom = useCallback((newXZoom, xPosition = (spaceTimeChartRef?.current?.offsetWidth || 0) / 2) => {
|
|
2892
|
-
if (enableTimeZoom)
|
|
2893
|
-
setState((prev) => ({
|
|
2894
|
-
...prev,
|
|
2895
|
-
...zoomX(prev.xZoom, prev.xOffset, newXZoom, xPosition),
|
|
2896
|
-
}));
|
|
2897
|
-
}, [enableTimeZoom, spaceTimeChartRef]);
|
|
3074
|
+
setSyncManchetteState((prev) => ({ ...prev, zoomMode: !prev.zoomMode }));
|
|
3075
|
+
}, [setSyncManchetteState]);
|
|
2898
3076
|
const spaceScales = useMemo(() => {
|
|
2899
3077
|
// Here, we first compute the base scales, and then we insert a flat step for each split point:
|
|
2900
3078
|
const baseScales = getScales(waypoints, { height, isProportional, yZoom }, minZoomMillimeterPerPx, maxZoomMillimeterPerPx);
|
|
@@ -2973,10 +3151,10 @@ const useManchetteWithSpaceTimeChart = ({ waypoints, manchetteWithSpaceTimeChart
|
|
|
2973
3151
|
}), [waypoints, height, isProportional, yZoom]);
|
|
2974
3152
|
const { manchetteContents, manchetteHeight } = useMemo(() => {
|
|
2975
3153
|
const spaceScaleTree = spaceScalesToBinaryTree(spaceOrigin, spaceScales);
|
|
2976
|
-
const getSpacePixel = getSpaceToPixel(
|
|
3154
|
+
const getSpacePixel = getSpaceToPixel(BASE_WAYPOINT_HEIGHT, spaceScaleTree);
|
|
2977
3155
|
let totalManchetteHeight = height - FOOTER_HEIGHT;
|
|
2978
3156
|
if (spaceScales.length > 0) {
|
|
2979
|
-
totalManchetteHeight = Math.max(totalManchetteHeight, getSpacePixel(spaceScales.at(-1).to, true) +
|
|
3157
|
+
totalManchetteHeight = Math.max(totalManchetteHeight, getSpacePixel(spaceScales.at(-1).to, true) + BASE_WAYPOINT_HEIGHT);
|
|
2980
3158
|
}
|
|
2981
3159
|
// Identify all manchette contents (split sections and waypoints):
|
|
2982
3160
|
const splitPointPositions = new Set(splitPoints.map((point) => point.position) || []);
|
|
@@ -3019,11 +3197,11 @@ const useManchetteWithSpaceTimeChart = ({ waypoints, manchetteWithSpaceTimeChart
|
|
|
3019
3197
|
const top = getSpacePixel(position);
|
|
3020
3198
|
finalContents.push(React.createElement("div", { style: {
|
|
3021
3199
|
// This verticalPadding correction is there because:
|
|
3022
|
-
// - Waypoints are positioned so that their lines (at
|
|
3200
|
+
// - Waypoints are positioned so that their lines (at 16 pixels from
|
|
3023
3201
|
// the top) are aligned with SpaceTimeChart's space lines
|
|
3024
3202
|
// - Split points are positioned so that their top is aligned with SpaceTimeChart's
|
|
3025
3203
|
// space lines
|
|
3026
|
-
top: `${top +
|
|
3204
|
+
top: `${top + BASE_WAYPOINT_HEIGHT / 2}px`,
|
|
3027
3205
|
height: `${getSpacePixel(position, true) - top}px`,
|
|
3028
3206
|
position: 'absolute',
|
|
3029
3207
|
width: '100%',
|
|
@@ -3036,7 +3214,7 @@ const useManchetteWithSpaceTimeChart = ({ waypoints, manchetteWithSpaceTimeChart
|
|
|
3036
3214
|
manchetteHeight: totalManchetteHeight,
|
|
3037
3215
|
manchetteContents: finalContents,
|
|
3038
3216
|
};
|
|
3039
|
-
}, [spaceOrigin, spaceScales,
|
|
3217
|
+
}, [spaceOrigin, spaceScales, height, splitPoints, waypointsToDisplay]);
|
|
3040
3218
|
return useMemo(() => ({
|
|
3041
3219
|
manchetteProps: {
|
|
3042
3220
|
contents: manchetteContents,
|
|
@@ -3063,19 +3241,15 @@ const useManchetteWithSpaceTimeChart = ({ waypoints, manchetteWithSpaceTimeChart
|
|
|
3063
3241
|
spaceOrigin,
|
|
3064
3242
|
spaceScales,
|
|
3065
3243
|
onZoom: ({ delta, position, event, }) => {
|
|
3066
|
-
|
|
3067
|
-
event.preventDefault();
|
|
3068
|
-
handleXZoom(xZoom + delta, position.x);
|
|
3069
|
-
}
|
|
3244
|
+
handleXZoomOnWheelEvent(event, xZoom + delta, position.x);
|
|
3070
3245
|
},
|
|
3071
3246
|
onPan: (payload) => {
|
|
3072
3247
|
const { initialData, data, initialPosition, position, isPanning, context: { width, getData }, } = payload;
|
|
3073
|
-
|
|
3074
|
-
|
|
3075
|
-
if (!isPanning) {
|
|
3248
|
+
setSyncManchetteState((prev) => {
|
|
3249
|
+
if (!isPanning && prev.zoomMode && prev.rect) {
|
|
3076
3250
|
return {
|
|
3077
3251
|
...prev,
|
|
3078
|
-
...
|
|
3252
|
+
...handleRectZoomEnd(prev),
|
|
3079
3253
|
panning: null,
|
|
3080
3254
|
zoomMode: false,
|
|
3081
3255
|
};
|
|
@@ -3107,32 +3281,11 @@ const useManchetteWithSpaceTimeChart = ({ waypoints, manchetteWithSpaceTimeChart
|
|
|
3107
3281
|
pixelRect: newPixelRect,
|
|
3108
3282
|
};
|
|
3109
3283
|
}
|
|
3110
|
-
|
|
3111
|
-
return {
|
|
3112
|
-
...prev,
|
|
3113
|
-
panning: { initialOffset: { x: xOffset, y: yOffset } },
|
|
3114
|
-
};
|
|
3115
|
-
}
|
|
3116
|
-
const newState = { ...prev };
|
|
3117
|
-
const { initialOffset } = panning;
|
|
3118
|
-
const manchette = manchetteWithSpaceTimeChartRef.current;
|
|
3119
|
-
if (enableTimePan) {
|
|
3120
|
-
newState.xOffset = initialOffset.x + diff.x;
|
|
3121
|
-
}
|
|
3122
|
-
if (enableSpacePan) {
|
|
3123
|
-
let newYOffset = initialOffset.y - diff.y;
|
|
3124
|
-
newYOffset = Math.max(newYOffset, 0);
|
|
3125
|
-
if (manchette) {
|
|
3126
|
-
newYOffset = Math.min(newYOffset, manchette.scrollHeight - manchette.offsetHeight);
|
|
3127
|
-
manchette.scrollTop = newYOffset;
|
|
3128
|
-
}
|
|
3129
|
-
newState.yOffset = newYOffset;
|
|
3130
|
-
}
|
|
3131
|
-
return newState;
|
|
3284
|
+
return basicOnPan(payload, prev);
|
|
3132
3285
|
});
|
|
3133
3286
|
},
|
|
3134
3287
|
},
|
|
3135
|
-
handleScroll,
|
|
3288
|
+
handleScroll: handleScrollInManchette,
|
|
3136
3289
|
handleXZoom,
|
|
3137
3290
|
xZoom,
|
|
3138
3291
|
toggleZoomMode,
|
|
@@ -3160,52 +3313,38 @@ const useManchetteWithSpaceTimeChart = ({ waypoints, manchetteWithSpaceTimeChart
|
|
|
3160
3313
|
timeOrigin,
|
|
3161
3314
|
spaceOrigin,
|
|
3162
3315
|
spaceScales,
|
|
3163
|
-
|
|
3316
|
+
handleScrollInManchette,
|
|
3164
3317
|
handleXZoom,
|
|
3318
|
+
handleXZoomOnWheelEvent,
|
|
3165
3319
|
toggleZoomMode,
|
|
3166
3320
|
zoomMode,
|
|
3167
3321
|
rect,
|
|
3168
3322
|
minZoomMillimeterPerPx,
|
|
3169
3323
|
maxZoomMillimeterPerPx,
|
|
3170
3324
|
setTimeOrigin,
|
|
3171
|
-
|
|
3172
|
-
|
|
3173
|
-
manchetteWithSpaceTimeChartRef,
|
|
3174
|
-
enableTimePan,
|
|
3175
|
-
enableSpacePan,
|
|
3325
|
+
setSyncManchetteState,
|
|
3326
|
+
basicOnPan,
|
|
3176
3327
|
handleRectZoomEnd,
|
|
3177
3328
|
canvasDrawingHeight,
|
|
3178
3329
|
]);
|
|
3179
3330
|
};
|
|
3180
3331
|
|
|
3181
|
-
const transformCurve = (curve, departureTime) => curve.positions.map((position, i) => ({
|
|
3182
|
-
time: curve.times[i] + departureTime.getTime(),
|
|
3183
|
-
position,
|
|
3184
|
-
}));
|
|
3185
|
-
const usePaths = (projectPathTrainResult) => useMemo(() => projectPathTrainResult.flatMap((path) => path.spaceTimeCurves.map((spaceTimeCurve) => ({
|
|
3186
|
-
id: path.id,
|
|
3187
|
-
label: path.name,
|
|
3188
|
-
color: PATH_COLOR_DEFAULT,
|
|
3189
|
-
points: transformCurve(spaceTimeCurve, path.departureTime),
|
|
3190
|
-
}))), [projectPathTrainResult]);
|
|
3191
|
-
|
|
3192
3332
|
/**
|
|
3193
3333
|
* A simple component to display a manchette and a space time chart.
|
|
3194
3334
|
*
|
|
3195
3335
|
* This only covers basic usage. For more advanced control over the manchette
|
|
3196
3336
|
* and space time chart, the useManchetteWithSpaceTimeChart() hook can be used.
|
|
3197
3337
|
*/
|
|
3198
|
-
const ManchetteWithSpaceTimeChart = ({ waypoints,
|
|
3338
|
+
const ManchetteWithSpaceTimeChart = ({ waypoints, paths, height = INITIAL_SPACE_TIME_CHART_HEIGHT, children, header, manchetteProps: additionalManchetteProps, spaceTimeChartProps: additionalSpaceTimeChartProps, splitPoints, }) => {
|
|
3199
3339
|
const manchetteWithSpaceTimeChartRef = useRef(null);
|
|
3200
3340
|
const spaceTimeChartRef = useRef(null);
|
|
3201
|
-
const paths = usePaths(projectPathTrainResult);
|
|
3202
3341
|
const { manchetteProps, spaceTimeChartProps, handleScroll } = useManchetteWithSpaceTimeChart({
|
|
3203
3342
|
waypoints,
|
|
3204
3343
|
manchetteWithSpaceTimeChartRef,
|
|
3205
3344
|
height,
|
|
3206
3345
|
spaceTimeChartRef,
|
|
3207
3346
|
splitPoints,
|
|
3208
|
-
defaultTimeOrigin: Math.min(...
|
|
3347
|
+
defaultTimeOrigin: Math.min(...paths.map((p) => p.points[0]?.time)),
|
|
3209
3348
|
});
|
|
3210
3349
|
return (React.createElement("div", { className: "ui-manchette-space-time-chart-wrapper" },
|
|
3211
3350
|
React.createElement("div", { className: "header bg-ambientB-5 w-full border-b border-grey-30", style: { height: '40px' } }, header),
|
|
@@ -3218,17 +3357,26 @@ const ManchetteWithSpaceTimeChart = ({ waypoints, projectPathTrainResult, height
|
|
|
3218
3357
|
};
|
|
3219
3358
|
|
|
3220
3359
|
const InteractionButtons = ({ reset, openSettingsPanel, store, testIdPrefix, }) => (React.createElement("div", { id: "interaction-button-container", className: "z-10" },
|
|
3221
|
-
React.createElement("
|
|
3222
|
-
|
|
3223
|
-
|
|
3224
|
-
|
|
3225
|
-
|
|
3226
|
-
React.createElement(
|
|
3227
|
-
|
|
3228
|
-
|
|
3229
|
-
|
|
3230
|
-
|
|
3231
|
-
|
|
3360
|
+
React.createElement("button", { type: "button", className: cx('reset-button', {
|
|
3361
|
+
'reset-button-disabled': store.ratioX == 1 && store.leftOffset == 0,
|
|
3362
|
+
}), onClick: reset, "data-testid": testIdPrefix ? `${testIdPrefix}-reset` : undefined },
|
|
3363
|
+
React.createElement(Iterations, null)),
|
|
3364
|
+
React.createElement("button", { type: "button", className: "menu-button", "data-testid": testIdPrefix ? `${testIdPrefix}-settings` : undefined, onClick: openSettingsPanel },
|
|
3365
|
+
React.createElement(Sliders, null))));
|
|
3366
|
+
|
|
3367
|
+
var EtcsBrakingType;
|
|
3368
|
+
(function (EtcsBrakingType) {
|
|
3369
|
+
EtcsBrakingType["SLOWDOWN"] = "transition";
|
|
3370
|
+
EtcsBrakingType["STOP"] = "stop";
|
|
3371
|
+
EtcsBrakingType["SPACING"] = "spacing";
|
|
3372
|
+
EtcsBrakingType["ROUTING"] = "routing";
|
|
3373
|
+
})(EtcsBrakingType || (EtcsBrakingType = {}));
|
|
3374
|
+
var EtcsBrakingCurveType;
|
|
3375
|
+
(function (EtcsBrakingCurveType) {
|
|
3376
|
+
EtcsBrakingCurveType["IND"] = "IND";
|
|
3377
|
+
EtcsBrakingCurveType["PS"] = "PS";
|
|
3378
|
+
EtcsBrakingCurveType["GUI"] = "GUI";
|
|
3379
|
+
})(EtcsBrakingCurveType || (EtcsBrakingCurveType = {}));
|
|
3232
3380
|
|
|
3233
3381
|
const SLOPE_FILL_COLOR = '#CFDDCE';
|
|
3234
3382
|
const RIGHT_TICK_HEIGHT_OFFSET = 2;
|
|
@@ -3266,6 +3414,7 @@ const DETAILS_BOX_SELECTION = [
|
|
|
3266
3414
|
'energySource',
|
|
3267
3415
|
'tractionStatus',
|
|
3268
3416
|
'declivities',
|
|
3417
|
+
'etcs',
|
|
3269
3418
|
'electricalProfiles',
|
|
3270
3419
|
'powerRestrictions',
|
|
3271
3420
|
];
|
|
@@ -3277,6 +3426,28 @@ const LAYERS_SELECTION = [
|
|
|
3277
3426
|
'powerRestrictions',
|
|
3278
3427
|
'speedLimitTags',
|
|
3279
3428
|
];
|
|
3429
|
+
const DEFAULT_ETCS_LAYERS_DISPLAY = {
|
|
3430
|
+
etcsBrakingTypes: {
|
|
3431
|
+
stopsAndTransitions: false,
|
|
3432
|
+
spacing: false,
|
|
3433
|
+
routing: false,
|
|
3434
|
+
},
|
|
3435
|
+
etcsBrakingCurveTypes: {
|
|
3436
|
+
indication: true,
|
|
3437
|
+
permittedSpeed: true,
|
|
3438
|
+
guidance: true,
|
|
3439
|
+
},
|
|
3440
|
+
};
|
|
3441
|
+
const ETCS_BRAKING_SELECTION = {
|
|
3442
|
+
stopsAndTransitions: [EtcsBrakingType.SLOWDOWN, EtcsBrakingType.STOP],
|
|
3443
|
+
spacing: [EtcsBrakingType.SPACING],
|
|
3444
|
+
routing: [EtcsBrakingType.ROUTING],
|
|
3445
|
+
};
|
|
3446
|
+
const ETCS_CURVE_SELECTION = {
|
|
3447
|
+
indication: EtcsBrakingCurveType.IND,
|
|
3448
|
+
permittedSpeed: EtcsBrakingCurveType.PS,
|
|
3449
|
+
guidance: EtcsBrakingCurveType.GUI,
|
|
3450
|
+
};
|
|
3280
3451
|
// Colors
|
|
3281
3452
|
const BLACK = chroma(0, 0, 0);
|
|
3282
3453
|
const ERROR_30 = chroma(255, 104, 104);
|
|
@@ -3286,6 +3457,27 @@ const GREY_80$1 = chroma(49, 46, 43);
|
|
|
3286
3457
|
const LIGHT_BLUE = chroma(33, 112, 185);
|
|
3287
3458
|
const WARNING_30 = chroma(234, 167, 43);
|
|
3288
3459
|
const WHITE = chroma(255, 255, 255);
|
|
3460
|
+
const BASE_SPEED_COLOR = chroma(17, 101, 180);
|
|
3461
|
+
const ECO_SPEED_COLOR = chroma(7, 69, 128);
|
|
3462
|
+
const IND_COLOR = chroma(3, 222, 255);
|
|
3463
|
+
const PS_COLOR = chroma(0, 87, 218);
|
|
3464
|
+
const GUI_COLOR = chroma(255, 192, 3);
|
|
3465
|
+
const IND_ALPHA = 0.3;
|
|
3466
|
+
const PS_ALPHA = 0.24;
|
|
3467
|
+
const GUI_ALPHA = 0.4;
|
|
3468
|
+
const BASE_SPEED_FILL_ALPHA = 0.15;
|
|
3469
|
+
// Etcs color dictionary
|
|
3470
|
+
const ETCS_COLOR_DICTIONARY = {
|
|
3471
|
+
[EtcsBrakingCurveType.IND]: IND_COLOR,
|
|
3472
|
+
[EtcsBrakingCurveType.PS]: PS_COLOR,
|
|
3473
|
+
[EtcsBrakingCurveType.GUI]: GUI_COLOR,
|
|
3474
|
+
};
|
|
3475
|
+
// Etcs color transparency dictionary
|
|
3476
|
+
const ETCS_ALPHA_DICTIONARY = {
|
|
3477
|
+
[EtcsBrakingCurveType.IND]: IND_ALPHA,
|
|
3478
|
+
[EtcsBrakingCurveType.PS]: PS_ALPHA,
|
|
3479
|
+
[EtcsBrakingCurveType.GUI]: GUI_ALPHA,
|
|
3480
|
+
};
|
|
3289
3481
|
/**
|
|
3290
3482
|
* COLOR_DICTIONARY maps specific colors to their corresponding secondary colors used for speed limit tags.
|
|
3291
3483
|
*/
|
|
@@ -3301,6 +3493,8 @@ const ZOOM_CONFIG = {
|
|
|
3301
3493
|
MAX_RATIO: 50,
|
|
3302
3494
|
SLIDER_WIDTH: 100,
|
|
3303
3495
|
};
|
|
3496
|
+
const SPEEDS_LINEWIDTH = 0.5;
|
|
3497
|
+
const ETCS_LINEWIDTH = 3;
|
|
3304
3498
|
|
|
3305
3499
|
const getGraphOffsets = (width, height, declivities) => {
|
|
3306
3500
|
const WIDTH_OFFSET = declivities ? width - 102 : width - 60; // +2px so that the tick appears on the right of the chart
|
|
@@ -3378,7 +3572,7 @@ const drawLinearLayerBackground = (ctx, backgroundColor, margins, width, startin
|
|
|
3378
3572
|
ctx.fillRect(MARGIN_LEFT, startingHeight - layerHeight + LINEAR_LAYER_SEPARATOR_HEIGHT, width - MARGIN_LEFT - MARGIN_RIGHT, layerHeight);
|
|
3379
3573
|
};
|
|
3380
3574
|
/**
|
|
3381
|
-
* Return
|
|
3575
|
+
* Return whether a layer should be active or not.
|
|
3382
3576
|
* Depending on the available data, some layers should be disabled.
|
|
3383
3577
|
*/
|
|
3384
3578
|
const isLayerActive = (store, selection) => {
|
|
@@ -3562,9 +3756,17 @@ const getSnappedStop = (cursorX, width, store) => {
|
|
|
3562
3756
|
}
|
|
3563
3757
|
return null;
|
|
3564
3758
|
};
|
|
3759
|
+
/** Retrieve list of active EtcsBrakingTypes. */
|
|
3760
|
+
const getActiveEtcsBrakingTypes = (layers) => Object.entries(layers.etcsBrakingTypes)
|
|
3761
|
+
.filter(([_, isActive]) => isActive)
|
|
3762
|
+
.flatMap(([key]) => ETCS_BRAKING_SELECTION[key]);
|
|
3763
|
+
/** Retrieve list of active EtcsBrakingCurveTypes. */
|
|
3764
|
+
const getActiveEtcsBrakingCurveTypes = (layers) => Object.entries(layers.etcsBrakingCurveTypes)
|
|
3765
|
+
.filter(([_, isActive]) => isActive)
|
|
3766
|
+
.map(([key]) => ETCS_CURVE_SELECTION[key]);
|
|
3565
3767
|
|
|
3566
3768
|
const SETTINGS_PANEL_BASE_HEIGHT = 442;
|
|
3567
|
-
const SettingsPanel = ({ color, store, setStore, setIsMouseHoveringSettingsPanel, adjustHeightOnLayerChange, translations, testIdPrefix, }) => {
|
|
3769
|
+
const SettingsPanel = ({ color, store, setStore, setIsMouseHoveringSettingsPanel, adjustHeightOnLayerChange, translations, testIdPrefix, displayEtcs, }) => {
|
|
3568
3770
|
const closeSettingsPanel = () => {
|
|
3569
3771
|
setIsMouseHoveringSettingsPanel(false);
|
|
3570
3772
|
setStore((prev) => ({
|
|
@@ -3572,6 +3774,22 @@ const SettingsPanel = ({ color, store, setStore, setIsMouseHoveringSettingsPanel
|
|
|
3572
3774
|
isSettingsPanelOpened: false,
|
|
3573
3775
|
}));
|
|
3574
3776
|
};
|
|
3777
|
+
const stopsAndTransitions = 'stopsAndTransitions';
|
|
3778
|
+
const otherEtcsBrakingTypes = ['spacing', 'routing'];
|
|
3779
|
+
const updateEtcsBrakingType = (etcsBrakingType, newValue) => {
|
|
3780
|
+
setStore((prev) => ({
|
|
3781
|
+
...prev,
|
|
3782
|
+
etcsLayersDisplay: {
|
|
3783
|
+
...prev.etcsLayersDisplay,
|
|
3784
|
+
etcsBrakingTypes: {
|
|
3785
|
+
...prev.etcsLayersDisplay.etcsBrakingTypes,
|
|
3786
|
+
[etcsBrakingType]: newValue !== undefined
|
|
3787
|
+
? newValue
|
|
3788
|
+
: !prev.etcsLayersDisplay.etcsBrakingTypes[etcsBrakingType],
|
|
3789
|
+
},
|
|
3790
|
+
},
|
|
3791
|
+
}));
|
|
3792
|
+
};
|
|
3575
3793
|
return (React.createElement("div", { id: "settings-panel", style: {
|
|
3576
3794
|
background: `rgba(${color.substring(4, color.length - 1)}, 0.4)`,
|
|
3577
3795
|
height: `${SETTINGS_PANEL_BASE_HEIGHT}px`,
|
|
@@ -3579,34 +3797,84 @@ const SettingsPanel = ({ color, store, setStore, setIsMouseHoveringSettingsPanel
|
|
|
3579
3797
|
React.createElement("div", { className: "settings-panel-section" },
|
|
3580
3798
|
React.createElement("div", { className: "settings-panel-section-title" },
|
|
3581
3799
|
React.createElement("span", null, translations?.layersDisplay.context || 'Context')),
|
|
3582
|
-
LAYERS_SELECTION.map((selection) => (React.createElement(
|
|
3583
|
-
|
|
3584
|
-
|
|
3585
|
-
|
|
3586
|
-
layersDisplay
|
|
3587
|
-
|
|
3588
|
-
|
|
3800
|
+
LAYERS_SELECTION.map((selection) => (React.createElement(Checkbox, { key: selection, "data-testid": testIdPrefix ? `${testIdPrefix}-layer-${selection}` : undefined, label: translations?.layersDisplay[selection] || selection, checked: store.layersDisplay[selection], disabled: !isLayerActive(store, selection), onChange: () => {
|
|
3801
|
+
setStore((prev) => ({
|
|
3802
|
+
...prev,
|
|
3803
|
+
layersDisplay: {
|
|
3804
|
+
...prev.layersDisplay,
|
|
3805
|
+
[selection]: !prev.layersDisplay[selection],
|
|
3806
|
+
},
|
|
3807
|
+
}));
|
|
3808
|
+
if (selection === 'electricalProfiles' ||
|
|
3809
|
+
selection === 'powerRestrictions' ||
|
|
3810
|
+
selection === 'speedLimitTags') {
|
|
3811
|
+
adjustHeightOnLayerChange(selection, store.layersDisplay[selection]);
|
|
3812
|
+
}
|
|
3813
|
+
} })))),
|
|
3814
|
+
displayEtcs && (React.createElement("div", { className: "settings-panel-section" },
|
|
3815
|
+
React.createElement("div", { className: "settings-panel-section-title" },
|
|
3816
|
+
React.createElement("span", null, translations?.etcsLayersDisplay.title || 'ETCS')),
|
|
3817
|
+
React.createElement(CheckboxesTree, { id: 'etcs-braking-types', items: [
|
|
3818
|
+
{
|
|
3819
|
+
id: 0,
|
|
3820
|
+
props: {
|
|
3821
|
+
label: translations?.etcsLayersDisplay.etcsBrakingTypes[stopsAndTransitions] ||
|
|
3822
|
+
stopsAndTransitions,
|
|
3823
|
+
checked: store.etcsLayersDisplay.etcsBrakingTypes[stopsAndTransitions],
|
|
3824
|
+
onChange: () => {
|
|
3825
|
+
updateEtcsBrakingType(stopsAndTransitions);
|
|
3589
3826
|
},
|
|
3590
|
-
}
|
|
3591
|
-
|
|
3592
|
-
|
|
3593
|
-
|
|
3594
|
-
|
|
3595
|
-
|
|
3596
|
-
|
|
3597
|
-
|
|
3827
|
+
},
|
|
3828
|
+
},
|
|
3829
|
+
{
|
|
3830
|
+
id: 1,
|
|
3831
|
+
props: {
|
|
3832
|
+
label: translations?.etcsLayersDisplay.etcsBrakingTypes.signals,
|
|
3833
|
+
checked: otherEtcsBrakingTypes.every((key) => store.etcsLayersDisplay.etcsBrakingTypes[key]),
|
|
3834
|
+
isIndeterminate: otherEtcsBrakingTypes.some((key) => store.etcsLayersDisplay.etcsBrakingTypes[key]) &&
|
|
3835
|
+
!otherEtcsBrakingTypes.every((key) => store.etcsLayersDisplay.etcsBrakingTypes[key]),
|
|
3836
|
+
onChange: () => {
|
|
3837
|
+
const newValue = !otherEtcsBrakingTypes.every((key) => store.etcsLayersDisplay.etcsBrakingTypes[key]);
|
|
3838
|
+
otherEtcsBrakingTypes.map((etcsBrakingType) => updateEtcsBrakingType(etcsBrakingType, newValue));
|
|
3839
|
+
},
|
|
3840
|
+
},
|
|
3841
|
+
items: otherEtcsBrakingTypes.map((etcsBrakingType, index) => ({
|
|
3842
|
+
id: index + 2,
|
|
3843
|
+
props: {
|
|
3844
|
+
label: translations?.etcsLayersDisplay.etcsBrakingTypes[etcsBrakingType] ||
|
|
3845
|
+
etcsBrakingType,
|
|
3846
|
+
checked: store.etcsLayersDisplay.etcsBrakingTypes[etcsBrakingType],
|
|
3847
|
+
onChange: () => {
|
|
3848
|
+
updateEtcsBrakingType(etcsBrakingType);
|
|
3849
|
+
},
|
|
3850
|
+
},
|
|
3851
|
+
})),
|
|
3852
|
+
},
|
|
3853
|
+
] }),
|
|
3854
|
+
getActiveEtcsBrakingTypes(store.etcsLayersDisplay).length > 0 && (React.createElement("div", { className: "bottom-panel" }, Object.keys(ETCS_CURVE_SELECTION).map((selection) => (React.createElement(Checkbox, { key: selection, label: translations?.etcsLayersDisplay.etcsBrakingCurveTypes[selection] || selection, checked: store.etcsLayersDisplay.etcsBrakingCurveTypes[selection], onChange: () => {
|
|
3855
|
+
setStore((prev) => ({
|
|
3856
|
+
...prev,
|
|
3857
|
+
etcsLayersDisplay: {
|
|
3858
|
+
...prev.etcsLayersDisplay,
|
|
3859
|
+
etcsBrakingCurveTypes: {
|
|
3860
|
+
...prev.etcsLayersDisplay.etcsBrakingCurveTypes,
|
|
3861
|
+
[selection]: !prev.etcsLayersDisplay.etcsBrakingCurveTypes[selection],
|
|
3862
|
+
},
|
|
3863
|
+
},
|
|
3864
|
+
}));
|
|
3865
|
+
} }))))))),
|
|
3866
|
+
React.createElement("div", { className: "settings-panel-section" },
|
|
3598
3867
|
React.createElement("div", { className: "settings-panel-section-title" },
|
|
3599
3868
|
React.createElement("span", null, translations?.detailsBoxDisplay.reticleInfos || 'Reticle infos')),
|
|
3600
|
-
DETAILS_BOX_SELECTION.map((selection) => (React.createElement(
|
|
3601
|
-
|
|
3602
|
-
|
|
3603
|
-
|
|
3604
|
-
detailsBoxDisplay
|
|
3605
|
-
|
|
3606
|
-
|
|
3607
|
-
|
|
3608
|
-
|
|
3609
|
-
} }))))),
|
|
3869
|
+
DETAILS_BOX_SELECTION.filter((detail_box) => detail_box !== 'etcs' || displayEtcs).map((selection) => (React.createElement(Checkbox, { key: selection, label: translations?.detailsBoxDisplay[selection] || selection, checked: store.detailsBoxDisplay[selection], onChange: () => {
|
|
3870
|
+
setStore((prev) => ({
|
|
3871
|
+
...prev,
|
|
3872
|
+
detailsBoxDisplay: {
|
|
3873
|
+
...prev.detailsBoxDisplay,
|
|
3874
|
+
[selection]: !prev.detailsBoxDisplay[selection],
|
|
3875
|
+
},
|
|
3876
|
+
}));
|
|
3877
|
+
} })))),
|
|
3610
3878
|
React.createElement("button", { id: "close-settings-panel", "data-testid": testIdPrefix ? `${testIdPrefix}-close` : undefined, onClick: () => closeSettingsPanel() },
|
|
3611
3879
|
React.createElement("span", null,
|
|
3612
3880
|
React.createElement(X, null)))));
|
|
@@ -3849,9 +4117,7 @@ const computeCurvePoints = (canvasConfig, curveConfig, specificSpeeds) => {
|
|
|
3849
4117
|
return points;
|
|
3850
4118
|
};
|
|
3851
4119
|
const drawCurve = ({ ctx, width, height, store }) => {
|
|
3852
|
-
const { speeds, ecoSpeeds, ratioX, leftOffset } = store;
|
|
3853
|
-
const baseSpeedColor = chroma(17, 101, 180);
|
|
3854
|
-
const ecoSpeedColor = chroma(7, 69, 128);
|
|
4120
|
+
const { speeds, ecoSpeeds, etcsBrakingCurves, ratioX, leftOffset } = store;
|
|
3855
4121
|
clearCanvas(ctx, width, height);
|
|
3856
4122
|
ctx.save();
|
|
3857
4123
|
ctx.translate(leftOffset, 0);
|
|
@@ -3859,35 +4125,64 @@ const drawCurve = ({ ctx, width, height, store }) => {
|
|
|
3859
4125
|
const maxPosition = maxPositionValue(store.speeds);
|
|
3860
4126
|
const curvePoints = computeCurvePoints({ width, height }, { maxSpeed, maxPosition, ratioX }, speeds);
|
|
3861
4127
|
const ecoCurvePoints = computeCurvePoints({ width, height }, { maxSpeed, maxPosition, ratioX }, ecoSpeeds);
|
|
3862
|
-
// Curves must be drawn twice
|
|
4128
|
+
// Curves must be drawn twice, once for the fill and once for the stroke.
|
|
3863
4129
|
// The stroke must not draw the last two points. They're only present to close the shape but are not part of the curve.
|
|
4130
|
+
ctx.lineWidth = SPEEDS_LINEWIDTH;
|
|
4131
|
+
// Fill speed curve.
|
|
3864
4132
|
ctx.beginPath();
|
|
3865
|
-
ctx.
|
|
3866
|
-
ctx.strokeStyle = baseSpeedColor.hex();
|
|
3867
|
-
ctx.fillStyle = baseSpeedColor.alpha(0.15).hex();
|
|
4133
|
+
ctx.fillStyle = BASE_SPEED_COLOR.alpha(BASE_SPEED_FILL_ALPHA).hex();
|
|
3868
4134
|
curvePoints.forEach(({ x, y }) => {
|
|
3869
4135
|
ctx.lineTo(x, y);
|
|
3870
4136
|
});
|
|
3871
4137
|
ctx.fill();
|
|
4138
|
+
// Stroke speed curve.
|
|
3872
4139
|
ctx.beginPath();
|
|
4140
|
+
ctx.strokeStyle = BASE_SPEED_COLOR.hex();
|
|
3873
4141
|
curvePoints.slice(0, curvePoints.length - 2).forEach(({ x, y }) => {
|
|
3874
4142
|
ctx.lineTo(x, y);
|
|
3875
4143
|
});
|
|
3876
4144
|
ctx.stroke();
|
|
4145
|
+
// Fill eco speed curve.
|
|
3877
4146
|
ctx.beginPath();
|
|
3878
|
-
ctx.fillStyle =
|
|
3879
|
-
ctx.strokeStyle = ecoSpeedColor.hex();
|
|
4147
|
+
ctx.fillStyle = WHITE.hex();
|
|
3880
4148
|
ctx.globalCompositeOperation = 'destination-out';
|
|
3881
4149
|
ecoCurvePoints.forEach(({ x, y }) => {
|
|
3882
4150
|
ctx.lineTo(x, y);
|
|
3883
4151
|
});
|
|
3884
4152
|
ctx.fill();
|
|
4153
|
+
// Stroke eco speed curve.
|
|
3885
4154
|
ctx.beginPath();
|
|
4155
|
+
ctx.strokeStyle = ECO_SPEED_COLOR.hex();
|
|
3886
4156
|
ctx.globalCompositeOperation = 'source-over';
|
|
3887
4157
|
ecoCurvePoints.slice(0, ecoCurvePoints.length - 2).forEach(({ x, y }) => {
|
|
3888
4158
|
ctx.lineTo(x, y);
|
|
3889
4159
|
});
|
|
3890
4160
|
ctx.stroke();
|
|
4161
|
+
// Stroke etcs curves.
|
|
4162
|
+
if (etcsBrakingCurves) {
|
|
4163
|
+
const strokeEtcsCurve = (etcsBrakingCurveType, etcsCurve) => {
|
|
4164
|
+
const etcsCurvePoints = computeCurvePoints({ width, height }, { maxSpeed, maxPosition, ratioX }, etcsCurve);
|
|
4165
|
+
ctx.beginPath();
|
|
4166
|
+
ctx.lineWidth = ETCS_LINEWIDTH;
|
|
4167
|
+
ctx.strokeStyle = ETCS_COLOR_DICTIONARY[etcsBrakingCurveType]
|
|
4168
|
+
.alpha(ETCS_ALPHA_DICTIONARY[etcsBrakingCurveType])
|
|
4169
|
+
.hex();
|
|
4170
|
+
etcsCurvePoints.slice(0, etcsCurvePoints.length - 2).forEach(({ x, y }) => {
|
|
4171
|
+
ctx.lineTo(x, y);
|
|
4172
|
+
});
|
|
4173
|
+
ctx.stroke();
|
|
4174
|
+
};
|
|
4175
|
+
const activeEtcsBrakingTypes = getActiveEtcsBrakingTypes(store.etcsLayersDisplay);
|
|
4176
|
+
const activeEtcsBrakingCurveTypes = getActiveEtcsBrakingCurveTypes(store.etcsLayersDisplay);
|
|
4177
|
+
activeEtcsBrakingTypes.forEach((brakingType) => {
|
|
4178
|
+
const etcsCurves = etcsBrakingCurves[brakingType];
|
|
4179
|
+
etcsCurves.forEach((etcsCurve) => {
|
|
4180
|
+
activeEtcsBrakingCurveTypes.forEach((curveType) => {
|
|
4181
|
+
strokeEtcsCurve(curveType, etcsCurve[curveType]);
|
|
4182
|
+
});
|
|
4183
|
+
});
|
|
4184
|
+
});
|
|
4185
|
+
}
|
|
3891
4186
|
ctx.restore();
|
|
3892
4187
|
};
|
|
3893
4188
|
|
|
@@ -4179,7 +4474,7 @@ const PowerRestrictionsLayer = ({ width, marginTop, store }) => {
|
|
|
4179
4474
|
const DETAILBOX_MARGIN = 6;
|
|
4180
4475
|
const DETAILBOX_OFFSET = 12;
|
|
4181
4476
|
const DETAILBOX_LIMIT = 10;
|
|
4182
|
-
const DetailsBox = ({ width, height, store, curveX, curveY, speedText, ecoSpeedText, effortText, electricalModeText, electricalProfileText, powerRestrictionText, previousGradientText, modeText, }) => {
|
|
4477
|
+
const DetailsBox = ({ width, height, store, curveX, curveY, speedText, ecoSpeedText, etcsSpeedValues, translations, effortText, electricalModeText, electricalProfileText, powerRestrictionText, previousGradientText, modeText, }) => {
|
|
4183
4478
|
const { MARGIN_BOTTOM, MARGIN_RIGHT } = MARGINS;
|
|
4184
4479
|
const { energySource, tractionStatus, declivities, electricalProfiles, powerRestrictions } = store.detailsBoxDisplay;
|
|
4185
4480
|
let rightOffset = 0;
|
|
@@ -4196,24 +4491,59 @@ const DetailsBox = ({ width, height, store, curveX, curveY, speedText, ecoSpeedT
|
|
|
4196
4491
|
const boxY = curveY + DETAILBOX_MARGIN - bottomOffset;
|
|
4197
4492
|
const speedDifference = Number(speedText) - Number(ecoSpeedText);
|
|
4198
4493
|
const speedDifferenceText = speedDifference !== 0 ? `-${speedDifference.toFixed(1)}` : null;
|
|
4199
|
-
|
|
4494
|
+
const activeEtcsBrakingTypes = getActiveEtcsBrakingTypes(store.etcsLayersDisplay);
|
|
4495
|
+
const activeEtcsBrakingCurveTypes = getActiveEtcsBrakingCurveTypes(store.etcsLayersDisplay);
|
|
4496
|
+
const etcsEndOfCurves = etcsSpeedValues
|
|
4497
|
+
? [
|
|
4498
|
+
...new Set(activeEtcsBrakingTypes.flatMap((etcsBrakingType) => activeEtcsBrakingCurveTypes.flatMap((etcsBrakingCurveType) => etcsSpeedValues[etcsBrakingType][etcsBrakingCurveType].map((etcsValue) => etcsValue.etcsEndOfCurvePos)))),
|
|
4499
|
+
]
|
|
4500
|
+
.sort((a, b) => a - b)
|
|
4501
|
+
.map((n) => n.toFixed(1))
|
|
4502
|
+
: [];
|
|
4503
|
+
return (React.createElement("div", { id: "details-box", className: cx('absolute flex flex-row', { block: curveY, hidden: !curveY }), style: {
|
|
4200
4504
|
marginTop: boxY,
|
|
4201
4505
|
marginLeft: boxX,
|
|
4202
4506
|
} },
|
|
4203
|
-
React.createElement("
|
|
4204
|
-
|
|
4205
|
-
React.createElement("
|
|
4206
|
-
|
|
4207
|
-
|
|
4208
|
-
|
|
4209
|
-
|
|
4210
|
-
|
|
4211
|
-
React.createElement("
|
|
4212
|
-
|
|
4213
|
-
|
|
4214
|
-
|
|
4215
|
-
React.createElement(
|
|
4216
|
-
|
|
4507
|
+
React.createElement("div", { className: "default-details-box" },
|
|
4508
|
+
React.createElement("span", { id: "base-speed-text" }, speedText || '--'),
|
|
4509
|
+
React.createElement("div", null,
|
|
4510
|
+
React.createElement("span", { id: "mareco-speed-text" }, ecoSpeedText || '--'),
|
|
4511
|
+
speedDifferenceText && React.createElement("span", { id: "speed-difference-text" }, speedDifferenceText)),
|
|
4512
|
+
(energySource || tractionStatus || modeText || effortText) && React.createElement("hr", null),
|
|
4513
|
+
energySource && React.createElement("span", { id: "mode-text" }, modeText || '--'),
|
|
4514
|
+
tractionStatus && React.createElement("span", { id: "effort-text" }, effortText || '--'),
|
|
4515
|
+
electricalModeText && (React.createElement("div", { id: "electrical-mode-text" },
|
|
4516
|
+
React.createElement("span", null, electricalModeText || '--'),
|
|
4517
|
+
electricalProfiles && React.createElement("span", { className: "ml-2" }, electricalProfileText || '--'))),
|
|
4518
|
+
powerRestrictions && (React.createElement("span", { id: "power-restriction-text" }, powerRestrictionText || '--')),
|
|
4519
|
+
declivities && (React.createElement(React.Fragment, null,
|
|
4520
|
+
React.createElement("hr", null),
|
|
4521
|
+
React.createElement("span", { id: "previous-gradient-text" }, `${previousGradientText} ‰`)))),
|
|
4522
|
+
store.detailsBoxDisplay.etcs &&
|
|
4523
|
+
etcsSpeedValues &&
|
|
4524
|
+
activeEtcsBrakingTypes.length > 0 &&
|
|
4525
|
+
activeEtcsBrakingCurveTypes.length > 0 && (React.createElement("div", { className: "etcs-details-box", style: { minHeight: detailsBoxHeight } },
|
|
4526
|
+
React.createElement("div", { className: "etcs-grid", style: { gridTemplateColumns: `repeat(${etcsEndOfCurves.length + 2}, auto)` } },
|
|
4527
|
+
React.createElement("div", { className: "etcs-row-group", style: { gridColumnStart: 3 } }, etcsEndOfCurves.map((endOfCurve) => (React.createElement("div", { key: endOfCurve, className: "number-text" }, endOfCurve)))),
|
|
4528
|
+
activeEtcsBrakingTypes.map((brakingType) => {
|
|
4529
|
+
const specificEtcsSpeeds = etcsSpeedValues[brakingType];
|
|
4530
|
+
return (React.createElement("div", { key: brakingType, className: "etcs-row-group" }, activeEtcsBrakingCurveTypes.map((brakingCurveType, index) => {
|
|
4531
|
+
const speedValues = specificEtcsSpeeds[brakingCurveType];
|
|
4532
|
+
return (React.createElement(React.Fragment, null,
|
|
4533
|
+
index === 0 && (React.createElement("div", { style: {
|
|
4534
|
+
gridRow: `span ${activeEtcsBrakingCurveTypes.length}`,
|
|
4535
|
+
} }, translations?.etcsLayersDisplay.etcsBrakingTypes[brakingType] ||
|
|
4536
|
+
brakingType)),
|
|
4537
|
+
React.createElement("div", { style: {
|
|
4538
|
+
color: ETCS_COLOR_DICTIONARY[brakingCurveType].toString(),
|
|
4539
|
+
} }, brakingCurveType),
|
|
4540
|
+
etcsEndOfCurves.map((endOfCurve) => (React.createElement("div", { key: endOfCurve, className: "number-text", style: {
|
|
4541
|
+
color: ETCS_COLOR_DICTIONARY[brakingCurveType].toString(),
|
|
4542
|
+
} }, speedValues
|
|
4543
|
+
.find((speedValue) => speedValue.etcsEndOfCurvePos.toFixed(1) === endOfCurve)
|
|
4544
|
+
?.etcsSpeed.toFixed(2) ?? '--')))));
|
|
4545
|
+
})));
|
|
4546
|
+
}))))));
|
|
4217
4547
|
};
|
|
4218
4548
|
|
|
4219
4549
|
const { MARGIN_LEFT: MARGIN_LEFT$3, MARGIN_RIGHT: MARGIN_RIGHT$2, MARGIN_TOP: MARGIN_TOP$2, MARGIN_BOTTOM: MARGIN_BOTTOM$3, CURVE_MARGIN_TOP: CURVE_MARGIN_TOP$1, CURVE_MARGIN_SIDES: CURVE_MARGIN_SIDES$1, } = MARGINS;
|
|
@@ -4221,9 +4551,71 @@ const RETICLE_LINE = 12.75;
|
|
|
4221
4551
|
const SNAPPED_STOP_TEXT_OFFSET = 36;
|
|
4222
4552
|
const CURSOR_HEIGHT = 6;
|
|
4223
4553
|
const SNAPPED_CURSOR_HEIGHT = 24;
|
|
4554
|
+
const getEtcsSpeedsAtPosition = (curves, position) => {
|
|
4555
|
+
const result = {
|
|
4556
|
+
[EtcsBrakingType.STOP]: {
|
|
4557
|
+
[EtcsBrakingCurveType.IND]: [],
|
|
4558
|
+
[EtcsBrakingCurveType.PS]: [],
|
|
4559
|
+
[EtcsBrakingCurveType.GUI]: [],
|
|
4560
|
+
},
|
|
4561
|
+
[EtcsBrakingType.SLOWDOWN]: {
|
|
4562
|
+
[EtcsBrakingCurveType.IND]: [],
|
|
4563
|
+
[EtcsBrakingCurveType.PS]: [],
|
|
4564
|
+
[EtcsBrakingCurveType.GUI]: [],
|
|
4565
|
+
},
|
|
4566
|
+
[EtcsBrakingType.SPACING]: {
|
|
4567
|
+
[EtcsBrakingCurveType.IND]: [],
|
|
4568
|
+
[EtcsBrakingCurveType.PS]: [],
|
|
4569
|
+
[EtcsBrakingCurveType.GUI]: [],
|
|
4570
|
+
},
|
|
4571
|
+
[EtcsBrakingType.ROUTING]: {
|
|
4572
|
+
[EtcsBrakingCurveType.IND]: [],
|
|
4573
|
+
[EtcsBrakingCurveType.PS]: [],
|
|
4574
|
+
[EtcsBrakingCurveType.GUI]: [],
|
|
4575
|
+
},
|
|
4576
|
+
};
|
|
4577
|
+
const getEtcsSpeedAndEndOfCurveAtPosition = (speeds, pos) => {
|
|
4578
|
+
if (speeds.length === 0)
|
|
4579
|
+
return undefined;
|
|
4580
|
+
const first = speeds[0];
|
|
4581
|
+
const last = speeds[speeds.length - 1];
|
|
4582
|
+
const firstPosition = first.position.start;
|
|
4583
|
+
const lastPosition = last.position.end ? last.position.end : last.position.start;
|
|
4584
|
+
// Reject if position is outside the curve range
|
|
4585
|
+
if (pos < firstPosition || pos > lastPosition) {
|
|
4586
|
+
return undefined;
|
|
4587
|
+
}
|
|
4588
|
+
const index = binarySearch(speeds, pos, (p) => p.position.start);
|
|
4589
|
+
// If there is an exact match, return speed
|
|
4590
|
+
if (speeds[index].position.start === pos ||
|
|
4591
|
+
(speeds[index].position.end && speeds[index].position.end === pos)) {
|
|
4592
|
+
return { etcsSpeed: speeds[index].value, etcsEndOfCurvePos: lastPosition };
|
|
4593
|
+
}
|
|
4594
|
+
// Else, interpolate speed.
|
|
4595
|
+
const p1 = speeds[index];
|
|
4596
|
+
const p2 = speeds[index + 1];
|
|
4597
|
+
return {
|
|
4598
|
+
etcsSpeed: interpolate(p1.position.start, p1.value, p2.position.start, p2.value, pos),
|
|
4599
|
+
etcsEndOfCurvePos: lastPosition,
|
|
4600
|
+
};
|
|
4601
|
+
};
|
|
4602
|
+
for (const brakingType of Object.values(EtcsBrakingType)) {
|
|
4603
|
+
const curveList = curves[brakingType] ?? [];
|
|
4604
|
+
curveList.forEach((curve) => {
|
|
4605
|
+
for (const curveType of Object.values(EtcsBrakingCurveType)) {
|
|
4606
|
+
const etcsSpeeds = curve[curveType] ?? [];
|
|
4607
|
+
const value = getEtcsSpeedAndEndOfCurveAtPosition(etcsSpeeds, position);
|
|
4608
|
+
if (value !== undefined) {
|
|
4609
|
+
result[brakingType][curveType].push(value);
|
|
4610
|
+
}
|
|
4611
|
+
}
|
|
4612
|
+
});
|
|
4613
|
+
}
|
|
4614
|
+
return result;
|
|
4615
|
+
};
|
|
4224
4616
|
const drawCursor = ({ ctx, width, height, store }) => {
|
|
4225
4617
|
clearCanvas(ctx, width, height);
|
|
4226
|
-
const { cursor, layersDisplay, ratioX, leftOffset, speeds, ecoSpeeds, electrifications, slopes, electricalProfiles, powerRestrictions, } = store;
|
|
4618
|
+
const { cursor, layersDisplay, ratioX, leftOffset, speeds, ecoSpeeds, etcsBrakingCurves, electrifications, slopes, electricalProfiles, powerRestrictions, } = store;
|
|
4227
4619
|
let speedText = '';
|
|
4228
4620
|
let ecoSpeedText = '';
|
|
4229
4621
|
let stopText = '';
|
|
@@ -4303,6 +4695,9 @@ const drawCursor = ({ ctx, width, height, store }) => {
|
|
|
4303
4695
|
else if (prevEcoSpeed.value > nextEcoSpeed.value) {
|
|
4304
4696
|
effortText = 'decelerating';
|
|
4305
4697
|
}
|
|
4698
|
+
const etcsSpeedValues = etcsBrakingCurves
|
|
4699
|
+
? getEtcsSpeedsAtPosition(etcsBrakingCurves, cursorPosition)
|
|
4700
|
+
: undefined;
|
|
4306
4701
|
reticleY =
|
|
4307
4702
|
cursorBoxHeight -
|
|
4308
4703
|
(ecoSpeedValue / maxSpeed) * (cursorBoxHeight - CURVE_MARGIN_TOP$1) +
|
|
@@ -4385,6 +4780,7 @@ const drawCursor = ({ ctx, width, height, store }) => {
|
|
|
4385
4780
|
curveY: reticleY,
|
|
4386
4781
|
speedText,
|
|
4387
4782
|
ecoSpeedText,
|
|
4783
|
+
etcsSpeedValues,
|
|
4388
4784
|
effortText,
|
|
4389
4785
|
electricalModeText,
|
|
4390
4786
|
electricalProfileText,
|
|
@@ -4394,7 +4790,7 @@ const drawCursor = ({ ctx, width, height, store }) => {
|
|
|
4394
4790
|
};
|
|
4395
4791
|
};
|
|
4396
4792
|
|
|
4397
|
-
const ReticleLayer = ({ width, internalHeight: height, store }) => {
|
|
4793
|
+
const ReticleLayer = ({ width, internalHeight: height, store, translations, }) => {
|
|
4398
4794
|
const canvas = useRef(null);
|
|
4399
4795
|
const [trainDetails, setTrainDetails] = useState(null);
|
|
4400
4796
|
useEffect(() => {
|
|
@@ -4403,6 +4799,8 @@ const ReticleLayer = ({ width, internalHeight: height, store }) => {
|
|
|
4403
4799
|
// The tooltip shouldn't be displayed when hovering on the linear layers
|
|
4404
4800
|
if (store.cursor.y && store.cursor.y < height - MARGINS.MARGIN_TOP - MARGINS.MARGIN_BOTTOM) {
|
|
4405
4801
|
const detailsBox = drawCursor({ ctx, width, height, store });
|
|
4802
|
+
// TODO: fix this lint
|
|
4803
|
+
// eslint-disable-next-line react-hooks/set-state-in-effect
|
|
4406
4804
|
setTrainDetails(detailsBox || null);
|
|
4407
4805
|
}
|
|
4408
4806
|
else {
|
|
@@ -4412,7 +4810,7 @@ const ReticleLayer = ({ width, internalHeight: height, store }) => {
|
|
|
4412
4810
|
}, [width, height, store]);
|
|
4413
4811
|
return (React.createElement(React.Fragment, null,
|
|
4414
4812
|
React.createElement("canvas", { id: "cursor-layer", className: "absolute", ref: canvas, width: width, height: height }),
|
|
4415
|
-
trainDetails && React.createElement(DetailsBox, { width: width, height: height, store: store, ...trainDetails })));
|
|
4813
|
+
trainDetails && (React.createElement(DetailsBox, { width: width, height: height, store: store, translations: translations, ...trainDetails }))));
|
|
4416
4814
|
};
|
|
4417
4815
|
|
|
4418
4816
|
const TOOLTIP_WIDTH = 246;
|
|
@@ -4825,7 +5223,7 @@ const TickLayerYRight = ({ width, height, store }) => {
|
|
|
4825
5223
|
return (React.createElement("canvas", { id: "tick-layer-y-right", className: "absolute rounded-t-xl", ref: canvas, width: width, height: height }));
|
|
4826
5224
|
};
|
|
4827
5225
|
|
|
4828
|
-
const SpeedSpaceChart = ({ width, height, backgroundColor, data, setHeight, translations, }) => {
|
|
5226
|
+
const SpeedSpaceChart = ({ width, height, backgroundColor, data, setHeight, translations, fetchEtcsBrakingCurves, etcsBrakingCurves, }) => {
|
|
4829
5227
|
const [store, setStore] = useState({
|
|
4830
5228
|
speeds: [],
|
|
4831
5229
|
ecoSpeeds: [],
|
|
@@ -4847,6 +5245,7 @@ const SpeedSpaceChart = ({ width, height, backgroundColor, data, setHeight, tran
|
|
|
4847
5245
|
energySource: true,
|
|
4848
5246
|
tractionStatus: true,
|
|
4849
5247
|
declivities: true,
|
|
5248
|
+
etcs: false,
|
|
4850
5249
|
electricalProfiles: true,
|
|
4851
5250
|
powerRestrictions: true,
|
|
4852
5251
|
},
|
|
@@ -4858,6 +5257,7 @@ const SpeedSpaceChart = ({ width, height, backgroundColor, data, setHeight, tran
|
|
|
4858
5257
|
powerRestrictions: false,
|
|
4859
5258
|
speedLimitTags: false,
|
|
4860
5259
|
},
|
|
5260
|
+
etcsLayersDisplay: DEFAULT_ETCS_LAYERS_DISPLAY,
|
|
4861
5261
|
isSettingsPanelOpened: false,
|
|
4862
5262
|
});
|
|
4863
5263
|
const { mainChartHeight, powerRestrictionsTop, speedLimitTagsTop, electricalProfileLayerHeight, interactivityLayerHeight, } = useMemo(() => {
|
|
@@ -4924,6 +5324,22 @@ const SpeedSpaceChart = ({ width, height, backgroundColor, data, setHeight, tran
|
|
|
4924
5324
|
setHeight((prevHeight) => prevHeight + (isCurrentlyActive ? -adjustment : adjustment));
|
|
4925
5325
|
}, [setHeight]);
|
|
4926
5326
|
useEffect(() => {
|
|
5327
|
+
const shouldFetchEtcsCurves = getActiveEtcsBrakingTypes(store.etcsLayersDisplay).length > 0;
|
|
5328
|
+
if (fetchEtcsBrakingCurves && shouldFetchEtcsCurves) {
|
|
5329
|
+
fetchEtcsBrakingCurves();
|
|
5330
|
+
}
|
|
5331
|
+
}, [fetchEtcsBrakingCurves, store.etcsLayersDisplay]);
|
|
5332
|
+
useEffect(() => {
|
|
5333
|
+
// TODO: fix this lint
|
|
5334
|
+
// eslint-disable-next-line react-hooks/set-state-in-effect
|
|
5335
|
+
setStore((prev) => ({
|
|
5336
|
+
...prev,
|
|
5337
|
+
etcsBrakingCurves: etcsBrakingCurves,
|
|
5338
|
+
}));
|
|
5339
|
+
}, [etcsBrakingCurves]);
|
|
5340
|
+
useEffect(() => {
|
|
5341
|
+
// TODO: fix this lint
|
|
5342
|
+
// eslint-disable-next-line react-hooks/set-state-in-effect
|
|
4927
5343
|
setStore((prev) => ({
|
|
4928
5344
|
...prev,
|
|
4929
5345
|
...data,
|
|
@@ -4947,7 +5363,7 @@ const SpeedSpaceChart = ({ width, height, backgroundColor, data, setHeight, tran
|
|
|
4947
5363
|
React.createElement("div", { className: "flex justify-end absolute base-margin-top", style: { width: adjustedWidthRightAxis } },
|
|
4948
5364
|
React.createElement(InteractionButtons, { reset: reset, openSettingsPanel: openSettingsPanel, store: store, testIdPrefix: "interaction" })),
|
|
4949
5365
|
store.isSettingsPanelOpened && (React.createElement("div", { className: "flex justify-end absolute ml-2 base-margin-top", style: { width: adjustedWidthRightAxis } },
|
|
4950
|
-
React.createElement(SettingsPanel, { color: backgroundColor, store: store, setStore: setStore, setIsMouseHoveringSettingsPanel: setIsMouseHoveringSettingsPanel, translations: translations, testIdPrefix: "settings-panel", adjustHeightOnLayerChange: adjustHeightOnLayerChange }))),
|
|
5366
|
+
React.createElement(SettingsPanel, { color: backgroundColor, store: store, setStore: setStore, setIsMouseHoveringSettingsPanel: setIsMouseHoveringSettingsPanel, translations: translations, testIdPrefix: "settings-panel", adjustHeightOnLayerChange: adjustHeightOnLayerChange, displayEtcs: fetchEtcsBrakingCurves !== undefined }))),
|
|
4951
5367
|
store.layersDisplay.declivities && (React.createElement(DeclivityLayer, { width: WIDTH_OFFSET, height: HEIGHT_OFFSET, store: store })),
|
|
4952
5368
|
React.createElement(CurveLayer, { width: WIDTH_OFFSET, height: HEIGHT_OFFSET, store: store }),
|
|
4953
5369
|
store.layersDisplay.speedLimits && (React.createElement(SpeedLimitsLayer, { width: adjustedWidthRightAxis, height: mainChartHeight, store: store })),
|
|
@@ -4958,7 +5374,7 @@ const SpeedSpaceChart = ({ width, height, backgroundColor, data, setHeight, tran
|
|
|
4958
5374
|
store.layersDisplay.speedLimitTags && (React.createElement(SpeedLimitTagsLayer, { width: adjustedWidthRightAxis, marginTop: speedLimitTagsTop, store: store })),
|
|
4959
5375
|
React.createElement(TickLayerX, { width: adjustedWidthRightAxis, height: height, store: store }),
|
|
4960
5376
|
store.layersDisplay.declivities && (React.createElement(TickLayerYRight, { width: width, height: mainChartHeight, store: store })),
|
|
4961
|
-
!isMouseHoveringSettingsPanel && (React.createElement(ReticleLayer, { width: adjustedWidthRightAxis, internalHeight: mainChartHeight, store: store })),
|
|
5377
|
+
!isMouseHoveringSettingsPanel && (React.createElement(ReticleLayer, { width: adjustedWidthRightAxis, internalHeight: mainChartHeight, store: store, translations: translations })),
|
|
4962
5378
|
React.createElement(FrontInteractivityLayer, { width: WIDTH_OFFSET, height: interactivityLayerHeight, store: store, setStore: setStore })));
|
|
4963
5379
|
};
|
|
4964
5380
|
|
|
@@ -5191,6 +5607,8 @@ const X_TROUGHTRAIN_BACKGROUND_PADDING = 8;
|
|
|
5191
5607
|
const BACKGROUND_HEIGHT = 40;
|
|
5192
5608
|
const SELECTED_TRAIN_ID_GRADIANT = 2;
|
|
5193
5609
|
const PATH_SIZE_DEFAULT = 1;
|
|
5610
|
+
const LABEL_OFFSET_X = 10;
|
|
5611
|
+
const LABEL_OFFSET_Y = 50;
|
|
5194
5612
|
const drawDefaultZone = (ctx, { x, y, width }) => {
|
|
5195
5613
|
ctx.beginPath();
|
|
5196
5614
|
ctx.rect(x, y, width, OCCUPANCY_ZONE_HEIGHT);
|
|
@@ -5306,6 +5724,29 @@ const drawOccupationZone = (ctx, stcContext, { zone, yOffset, position, isSelect
|
|
|
5306
5724
|
isSelected,
|
|
5307
5725
|
});
|
|
5308
5726
|
};
|
|
5727
|
+
const drawZoneTrailingText = (ctx, { getTimePixel, getSpacePixel }, { zone, position, yOffset, trailingText, }) => {
|
|
5728
|
+
const xEnd = getTimePixel(zone.endTime) + LABEL_OFFSET_X;
|
|
5729
|
+
const yCenter = getSpacePixel(position) + yOffset + OCCUPANCY_ZONE_Y_START - LABEL_OFFSET_Y;
|
|
5730
|
+
ctx.save();
|
|
5731
|
+
ctx.font = '600 12px IBM Plex Mono';
|
|
5732
|
+
ctx.textAlign = 'left';
|
|
5733
|
+
ctx.textBaseline = 'middle';
|
|
5734
|
+
const text = trailingText;
|
|
5735
|
+
const paddingX = 6;
|
|
5736
|
+
const textW = ctx.measureText(text).width;
|
|
5737
|
+
const boxW = textW + paddingX * 2;
|
|
5738
|
+
const boxH = 18;
|
|
5739
|
+
const radius = 3;
|
|
5740
|
+
const x = xEnd;
|
|
5741
|
+
const yTop = yCenter - boxH / 2;
|
|
5742
|
+
ctx.fillStyle = 'rgba(0,0,0,0.55)';
|
|
5743
|
+
ctx.beginPath();
|
|
5744
|
+
ctx.roundRect(x, yTop, boxW, boxH, radius);
|
|
5745
|
+
ctx.fill();
|
|
5746
|
+
ctx.fillStyle = '#FFFFFF';
|
|
5747
|
+
ctx.fillText(trailingText, x + paddingX, yCenter + 0.5);
|
|
5748
|
+
ctx.restore();
|
|
5749
|
+
};
|
|
5309
5750
|
|
|
5310
5751
|
const Y_OFFSET_INCREMENT = 4;
|
|
5311
5752
|
const MAX_ZONES = 9;
|
|
@@ -5351,6 +5792,7 @@ const OccupancyZonesLayer = ({ tracks, occupancyZones, position, topPadding, sel
|
|
|
5351
5792
|
zone,
|
|
5352
5793
|
offsetY: trackY + yPosition,
|
|
5353
5794
|
isSelected: zone.trainId === selectedTrainId,
|
|
5795
|
+
trailingText: zone.trailingText,
|
|
5354
5796
|
});
|
|
5355
5797
|
zoneIndex++;
|
|
5356
5798
|
}
|
|
@@ -5396,14 +5838,23 @@ const OccupancyZonesLayer = ({ tracks, occupancyZones, position, topPadding, sel
|
|
|
5396
5838
|
const drawingFunction = useCallback((ctx, stcContext) => {
|
|
5397
5839
|
instructionsToDraw.forEach((instruction) => {
|
|
5398
5840
|
switch (instruction.type) {
|
|
5399
|
-
case 'occupancyZone':
|
|
5841
|
+
case 'occupancyZone': {
|
|
5400
5842
|
drawOccupationZone(ctx, stcContext, {
|
|
5401
5843
|
zone: instruction.zone,
|
|
5402
5844
|
position,
|
|
5403
5845
|
yOffset: instruction.offsetY,
|
|
5404
5846
|
isSelected: instruction.isSelected,
|
|
5405
5847
|
});
|
|
5848
|
+
if (instruction.trailingText) {
|
|
5849
|
+
drawZoneTrailingText(ctx, stcContext, {
|
|
5850
|
+
zone: instruction.zone,
|
|
5851
|
+
position,
|
|
5852
|
+
yOffset: instruction.offsetY,
|
|
5853
|
+
trailingText: instruction.trailingText,
|
|
5854
|
+
});
|
|
5855
|
+
}
|
|
5406
5856
|
break;
|
|
5857
|
+
}
|
|
5407
5858
|
case 'remainingTrains':
|
|
5408
5859
|
drawRemainingTrainsBox(ctx, stcContext, {
|
|
5409
5860
|
position,
|
|
@@ -5445,8 +5896,8 @@ const OccupancyZonesLayer = ({ tracks, occupancyZones, position, topPadding, sel
|
|
|
5445
5896
|
}
|
|
5446
5897
|
});
|
|
5447
5898
|
}, [position, instructionsToDraw]);
|
|
5448
|
-
usePicking('overlay', pickingFunction);
|
|
5449
|
-
useDraw('overlay', drawingFunction);
|
|
5899
|
+
usePicking(SpaceTimeChartCanvasContext, 'overlay', pickingFunction);
|
|
5900
|
+
useDraw(SpaceTimeChartCanvasContext, 'overlay', drawingFunction);
|
|
5450
5901
|
return null;
|
|
5451
5902
|
};
|
|
5452
5903
|
|
|
@@ -5548,7 +5999,7 @@ const TracksLayer = ({ tracks, position, topPadding, drawBorders, }) => {
|
|
|
5548
5999
|
const drawingFunction = useCallback((ctx, stcContext) => {
|
|
5549
6000
|
drawTracks(ctx, stcContext, { position, topPadding, tracks, drawBorders });
|
|
5550
6001
|
}, [drawBorders, position, topPadding, tracks]);
|
|
5551
|
-
useDraw('overlay', drawingFunction);
|
|
6002
|
+
useDraw(SpaceTimeChartCanvasContext, 'overlay', drawingFunction);
|
|
5552
6003
|
return null;
|
|
5553
6004
|
};
|
|
5554
6005
|
|
|
@@ -5637,5 +6088,5 @@ const TrackOccupancyStandalone = ({ tracks, occupancyZones, selectedTrainId, onS
|
|
|
5637
6088
|
}) }))))));
|
|
5638
6089
|
};
|
|
5639
6090
|
|
|
5640
|
-
export { BASE_WAYPOINT_HEIGHT, CANVAS_PADDING, COLORS,
|
|
6091
|
+
export { BASE_WAYPOINT_HEIGHT, CANVAS_PADDING, COLORS, Chronogram, ChronogramContext, ConflictLayer, ConflictTooltip, DEFAULT_LEVEL, DEFAULT_THEME, DEFAULT_ZOOM_MS_PER_PX, EtcsBrakingCurveType, EtcsBrakingType, FONTS, MINUTES_TEXT_OFFSET, Manchette, ManchetteWithSpaceTimeChart, MouseContext, OCCUPANCY_ZONE_HEIGHT, OCCUPANCY_ZONE_Y_START, OccupancyBlockLayer, PathLayer, PatternRect, Quadrilateral, STATION_TEXT_OFFSET, SpaceTimeChart, SpaceTimeChartCanvasContext, SpaceTimeChartContext, SpeedSpaceChart, TICKS_PATTERN, TICKS_PRIORITIES, TRACK_HEIGHT_CONTAINER, TimeCaptions, TimeGraduations, Tooltip$1 as Tooltip, TrackOccupancyCanvas, TrackOccupancyManchette, TrackOccupancyStandalone, Waypoint as WaypointComponent, WorkScheduleLayer, ZoomRect, computeRectZoomOffsets, getCrispLineCoordinate, getSpaceAtTime, isConflictPickingElement, isInteractiveWaypoint, isOccupancyPickingElement, isPathOnScreen, isPointPickingElement, isQuadrilaterPickingElement, isSegmentPickingElement, positionKmToMm, positionMmToKm, timeScaleToZoomValue, useDraw, useManchetteWithSpaceTimeChart, usePicking };
|
|
5641
6092
|
//# sourceMappingURL=index.esm.js.map
|