Skip to content

Commit 11a8ea7

Browse files
committed
speedspacechart: remove min speed management
Signed-off-by: Florian Amsallem <[email protected]>
1 parent 92dfb1e commit 11a8ea7

File tree

6 files changed

+18
-34
lines changed

6 files changed

+18
-34
lines changed

ui-speedspacechart/src/__tests__/utils.spec.ts

+4-6
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
getLinearLayersDisplayedHeight,
1010
maxPositionValue,
1111
positionOnGraphScale,
12-
speedRangeValues,
12+
maxSpeedValue,
1313
slopesValues,
1414
binarySearch,
1515
positionToPosX,
@@ -92,12 +92,10 @@ describe('getGraphOffsets', () => {
9292
});
9393
});
9494

95-
describe('speedRangeValues', () => {
96-
it('should return the correct minSpeed, maxSpeed and speedRange', () => {
97-
const { minSpeed, maxSpeed, speedRange } = speedRangeValues(store);
98-
expect(minSpeed).toBe(10);
95+
describe('maxSpeedValue', () => {
96+
it('should return the correct maxSpeed', () => {
97+
const maxSpeed = maxSpeedValue(store);
9998
expect(maxSpeed).toBe(30);
100-
expect(speedRange).toBe(20);
10199
});
102100
});
103101

ui-speedspacechart/src/components/helpers/drawElements/axisY.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import type { DrawFunctionParams } from '../../../types/chartTypes';
22
import { MARGINS, TICK_TITLE_MARGINS } from '../../const';
3-
import { clearCanvas, speedRangeValues } from '../../utils';
3+
import { clearCanvas, maxSpeedValue } from '../../utils';
44

55
const { MARGIN_LEFT, MARGIN_TOP, MARGIN_BOTTOM, CURVE_MARGIN_TOP, MARGIN_RIGHT } = MARGINS;
66
const { Y_LEFT_VERTICAL, Y_LEFT_HORIZONTAL } = TICK_TITLE_MARGINS;
77
const TICK_WIDTH = 6;
88
const TEXT_POSITION_X = 36;
99

1010
export const drawAxisY = ({ ctx, width, height, store }: DrawFunctionParams) => {
11-
const { maxSpeed } = speedRangeValues(store);
11+
const maxSpeed = maxSpeedValue(store);
1212

1313
clearCanvas(ctx, width, height);
1414

ui-speedspacechart/src/components/helpers/drawElements/curve.ts

+6-10
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import type {
55
LayerData,
66
} from '../../../types/chartTypes';
77
import { MARGINS } from '../../const';
8-
import { clearCanvas, maxPositionValue, speedRangeValues } from '../../utils';
8+
import { clearCanvas, maxPositionValue, maxSpeedValue } from '../../utils';
99

1010
const { CURVE_MARGIN_TOP, CURVE_MARGIN_SIDES } = MARGINS;
1111

@@ -14,7 +14,7 @@ const drawSpecificCurve = (
1414
canvasConfig: CanvasConfig,
1515
specificSpeeds: LayerData<number>[]
1616
) => {
17-
const { minSpeed, speedRange, maxPosition, ratioX } = curveConfig;
17+
const { maxSpeed, maxPosition, ratioX } = curveConfig;
1818
const { width, height, ctx } = canvasConfig;
1919

2020
const adjustedWidth = width - CURVE_MARGIN_SIDES;
@@ -24,7 +24,7 @@ const drawSpecificCurve = (
2424

2525
return specificSpeeds.forEach(({ position, value }) => {
2626
// normalize speed based on range of values
27-
const normalizedSpeed = (value - minSpeed) / speedRange;
27+
const normalizedSpeed = value / maxSpeed;
2828
const x = position.start * xcoef + halfCurveMarginSides;
2929
const y = height - normalizedSpeed * adjustedHeight;
3030
ctx.lineTo(x, y);
@@ -39,15 +39,15 @@ export const drawCurve = ({ ctx, width, height, store }: DrawFunctionParams) =>
3939
ctx.save();
4040
ctx.translate(leftOffset, 0);
4141

42-
const { minSpeed, speedRange } = speedRangeValues(store);
42+
const maxSpeed = maxSpeedValue(store);
4343
const maxPosition = maxPositionValue(store);
4444

4545
ctx.lineWidth = 0.5;
4646
ctx.fillStyle = 'rgba(17, 101, 180, 0.02)';
4747
ctx.strokeStyle = 'rgb(17, 101, 180, 0.5)';
4848

4949
ctx.beginPath();
50-
drawSpecificCurve({ minSpeed, speedRange, maxPosition, ratioX }, { width, height, ctx }, speeds);
50+
drawSpecificCurve({ maxSpeed, maxPosition, ratioX }, { width, height, ctx }, speeds);
5151
ctx.closePath();
5252
ctx.fill();
5353

@@ -58,11 +58,7 @@ export const drawCurve = ({ ctx, width, height, store }: DrawFunctionParams) =>
5858
ctx.globalCompositeOperation = 'destination-out';
5959

6060
ctx.beginPath();
61-
drawSpecificCurve(
62-
{ minSpeed, speedRange, maxPosition, ratioX },
63-
{ width, height, ctx },
64-
ecoSpeeds
65-
);
61+
drawSpecificCurve({ maxSpeed, maxPosition, ratioX }, { width, height, ctx }, ecoSpeeds);
6662
ctx.closePath();
6763
ctx.fill();
6864
ctx.globalCompositeOperation = 'source-over';

ui-speedspacechart/src/components/helpers/drawElements/reticle.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
binarySearch,
66
getAdaptiveHeight,
77
maxPositionValue,
8-
speedRangeValues,
8+
maxSpeedValue,
99
interpolate,
1010
positionToPosX,
1111
getCursorPosition,
@@ -60,7 +60,7 @@ export const drawCursor = ({ ctx, width, height, store }: DrawFunctionParams) =>
6060
let modeText = '';
6161
let reticleY = 0;
6262

63-
const { maxSpeed } = speedRangeValues(store);
63+
const maxSpeed = maxSpeedValue(store);
6464
const maxPosition = maxPositionValue(store);
6565

6666
const heightWithoutLayers = getAdaptiveHeight(height, layersDisplay, false);

ui-speedspacechart/src/components/utils.ts

+3-12
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,6 @@ import {
88
} from './const';
99
import type { LayerData, Store } from '../types/chartTypes';
1010

11-
type SpeedRangeValues = {
12-
minSpeed: number;
13-
maxSpeed: number;
14-
speedRange: number;
15-
};
16-
1711
type SlopesValues = {
1812
minGradient: number;
1913
maxGradient: number;
@@ -29,15 +23,12 @@ export const getGraphOffsets = (width: number, height: number, declivities?: boo
2923

3024
/**
3125
* /**
32-
* Given a store including a list of speed data, return the minSpeed, maxSpeed and speedRange
26+
* Given a store including a list of speed data, return the maxSpeed
3327
* @param store
3428
*/
35-
export const speedRangeValues = (store: Store): SpeedRangeValues => {
29+
export const maxSpeedValue = (store: Store) => {
3630
const speeds = store.speeds;
37-
const minSpeed = Math.min(...speeds.map(({ value }) => value));
38-
const maxSpeed = Math.max(...speeds.map(({ value }) => value));
39-
const speedRange = maxSpeed - minSpeed;
40-
return { minSpeed, maxSpeed, speedRange };
31+
return Math.max(...speeds.map(({ value }) => value));
4132
};
4233

4334
/**

ui-speedspacechart/src/types/chartTypes.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,7 @@ export type Store = Data & {
6969
};
7070

7171
export type CurveConfig = {
72-
minSpeed: number;
73-
speedRange: number;
72+
maxSpeed: number;
7473
maxPosition: number;
7574
ratioX: number;
7675
};

0 commit comments

Comments
 (0)