-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
base.ts
100 lines (89 loc) · 2.4 KB
/
base.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import type { Scale } from 'chart.js';
export interface IErrorBarXDataPoint {
/**
* the actual value
*/
x: number;
/**
* the minimal absolute error bar value
*/
xMin: number | number[];
/**
* the maximal absolute error bar value
*/
xMax: number | number[];
}
export interface IErrorBarYDataPoint {
/**
* the actual value
*/
y: number;
/**
* the minimal absolute error bar value
*/
yMin: number | number[];
/**
* the maximal absolute error bar value
*/
yMax: number | number[];
}
export interface IErrorBarRDataPoint {
/**
* the actual value
*/
r: number;
/**
* the minimal absolute error bar value
*/
rMin: number | number[];
/**
* the maximal absolute error bar value
*/
rMax: number | number[];
}
export interface IErrorBarXYDataPoint extends IErrorBarXDataPoint, IErrorBarYDataPoint {}
export function getMinMax(
scale: Scale,
superMethod: (scale: Scale) => { min: number; max: number }
): { min: number; max: number } {
const { axis } = scale;
scale.axis = `${axis}MinMin`;
const { min } = superMethod(scale);
scale.axis = `${axis}MaxMax`;
const { max } = superMethod(scale);
scale.axis = axis;
return { min, max };
}
function computeExtrema(v: number, vm: number | number[], op: (...args: number[]) => number) {
if (Array.isArray(vm)) {
return op(v, ...vm);
}
if (typeof vm === 'number') {
return op(v, vm);
}
return v;
}
export function parseErrorNumberData(parsed: any[], scale: Scale, data: any[], start: number, count: number): void {
const axis = typeof scale === 'string' ? scale : scale.axis;
const vMin = `${axis}Min`;
const vMax = `${axis}Max`;
const vMinMin = `${axis}MinMin`;
const vMaxMax = `${axis}MaxMax`;
for (let i = 0; i < count; i += 1) {
const index = i + start;
const p = parsed[i];
p[vMin] = typeof data[index] === 'object' ? data[index][vMin] : null;
p[vMax] = typeof data[index] === 'object' ? data[index][vMax] : null;
p[vMinMin] = computeExtrema(p[axis], p[vMin], Math.min);
p[vMaxMax] = computeExtrema(p[axis], p[vMax], Math.max);
}
}
export function parseErrorLabelData(parsed: any[], scale: Scale, start: number, count: number): void {
const { axis } = scale;
const labels = scale.getLabels();
for (let i = 0; i < count; i += 1) {
const index = i + start;
const p = parsed[i];
p[axis] = scale.parse(labels[index], index);
}
}