-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
extremums.ts
60 lines (49 loc) · 1.9 KB
/
extremums.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import { ExtremumGetter, ExtremumGetterResult } from '../models/seriesType/config';
export const getExtremumX: ExtremumGetter<'line'> = (params) => {
const { axis } = params;
const minX = Math.min(...(axis.data ?? []));
const maxX = Math.max(...(axis.data ?? []));
return [minX, maxX];
};
type GetValuesTypes = (d: [number, number]) => [number, number];
function getSeriesExtremums(
getValues: GetValuesTypes,
stackedData: [number, number][],
): ExtremumGetterResult {
if (stackedData.length === 0) {
return [null, null];
}
return stackedData.reduce((seriesAcc, stackedValue) => {
const [base, value] = getValues(stackedValue);
if (seriesAcc[0] === null) {
return [Math.min(base, value), Math.max(base, value)] as [number, number];
}
return [Math.min(base, value, seriesAcc[0]), Math.max(base, value, seriesAcc[1])];
}, getValues(stackedData[0]));
}
export const getExtremumY: ExtremumGetter<'line'> = (params) => {
const { series, axis, isDefaultAxis } = params;
return Object.keys(series)
.filter(
(seriesId) =>
series[seriesId].yAxisKey === axis.id ||
(isDefaultAxis && series[seriesId].yAxisKey === undefined),
)
.reduce(
(acc: ExtremumGetterResult, seriesId) => {
const { area, stackedData } = series[seriesId];
const isArea = area !== undefined;
const getValues: GetValuesTypes = isArea ? (d) => d : (d) => [d[1], d[1]]; // Since this series is not used to display an area, we do not consider the base (the d[0]).
const seriesExtremums = getSeriesExtremums(getValues, stackedData);
if (acc[0] === null) {
return seriesExtremums;
}
if (seriesExtremums[0] === null) {
return acc;
}
const [seriesMin, seriesMax] = seriesExtremums;
return [Math.min(seriesMin, acc[0]), Math.max(seriesMax, acc[1])];
},
[null, null],
);
};