@wonderflow/charts 12.11.0 → 14.0.0
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/components/bar-chart/bar-chart.d.ts +35 -7
- package/dist/components/bar-chart/bar-chart.d.ts.map +1 -1
- package/dist/components/bar-chart/bar-chart.js +21 -46
- package/dist/components/bar-chart/bar-chart.test.js +7 -1
- package/dist/components/cartesian-base/cartesian-base-axis.d.ts.map +1 -1
- package/dist/components/cartesian-base/cartesian-base-axis.js +10 -11
- package/dist/components/cartesian-base/cartesian-base-legend.d.ts.map +1 -1
- package/dist/components/cartesian-base/cartesian-base-legend.js +3 -3
- package/dist/components/cartesian-base/cartesian-base.d.ts +32 -0
- package/dist/components/cartesian-base/cartesian-base.d.ts.map +1 -1
- package/dist/components/cartesian-base/cartesian-base.js +8 -3
- package/dist/components/cartesian-base/cartesian-base.module.css +1 -1
- package/dist/components/headings/headings.d.ts +19 -2
- package/dist/components/headings/headings.d.ts.map +1 -1
- package/dist/components/headings/headings.js +72 -6
- package/dist/components/headings/headings.module.css +1 -0
- package/dist/components/headings/headings.test.d.ts +1 -1
- package/dist/components/headings/headings.test.d.ts.map +1 -1
- package/dist/components/headings/headings.test.js +37 -3
- package/dist/components/line-chart/line-chart.d.ts +31 -15
- package/dist/components/line-chart/line-chart.d.ts.map +1 -1
- package/dist/components/line-chart/line-chart.js +5 -3
- package/dist/components/line-chart/line-chart.test.js +2 -2
- package/dist/components/loader/bar-chart-loader.d.ts.map +1 -1
- package/dist/components/loader/bar-chart-loader.js +1 -1
- package/dist/components/loader/loader.js +2 -2
- package/dist/components/loader/loader.test.js +4 -2
- package/dist/components/shapes/bars/bars-average.d.ts +3 -0
- package/dist/components/shapes/bars/bars-average.d.ts.map +1 -0
- package/dist/components/shapes/bars/bars-average.js +30 -0
- package/dist/components/shapes/bars/bars-label.d.ts +22 -0
- package/dist/components/shapes/bars/bars-label.d.ts.map +1 -0
- package/dist/components/shapes/bars/bars-label.js +35 -0
- package/dist/components/shapes/bars/bars-layout.d.ts +3 -0
- package/dist/components/shapes/bars/bars-layout.d.ts.map +1 -0
- package/dist/components/shapes/bars/bars-layout.js +36 -0
- package/dist/components/shapes/bars/bars-overlay-labels.d.ts +6 -0
- package/dist/components/shapes/bars/bars-overlay-labels.d.ts.map +1 -0
- package/dist/components/shapes/bars/bars-overlay-labels.js +62 -0
- package/dist/components/shapes/bars/bars-overlay.d.ts +6 -0
- package/dist/components/shapes/bars/bars-overlay.d.ts.map +1 -0
- package/dist/components/shapes/bars/bars-overlay.js +59 -0
- package/dist/components/shapes/bars/bars-series-labels.d.ts +6 -0
- package/dist/components/shapes/bars/bars-series-labels.d.ts.map +1 -0
- package/dist/components/shapes/bars/bars-series-labels.js +62 -0
- package/dist/components/shapes/bars/bars-series.d.ts +6 -0
- package/dist/components/shapes/bars/bars-series.d.ts.map +1 -0
- package/dist/components/shapes/bars/bars-series.js +56 -0
- package/dist/components/shapes/bars/bars-stack-overlay-labels.d.ts +6 -0
- package/dist/components/shapes/bars/bars-stack-overlay-labels.d.ts.map +1 -0
- package/dist/components/shapes/bars/bars-stack-overlay-labels.js +50 -0
- package/dist/components/shapes/bars/bars-stack-overlay.d.ts +6 -0
- package/dist/components/shapes/bars/bars-stack-overlay.d.ts.map +1 -0
- package/dist/components/shapes/bars/bars-stack-overlay.js +50 -0
- package/dist/components/shapes/bars/bars-stack-series-labels.d.ts +6 -0
- package/dist/components/shapes/bars/bars-stack-series-labels.d.ts.map +1 -0
- package/dist/components/shapes/bars/bars-stack-series-labels.js +50 -0
- package/dist/components/shapes/bars/bars-stack-series.d.ts +6 -0
- package/dist/components/shapes/bars/bars-stack-series.d.ts.map +1 -0
- package/dist/components/shapes/bars/bars-stack-series.js +48 -0
- package/dist/components/shapes/bars/bars-tooltip.d.ts +3 -0
- package/dist/components/shapes/bars/bars-tooltip.d.ts.map +1 -0
- package/dist/components/shapes/bars/bars-tooltip.js +96 -0
- package/dist/components/shapes/bars/bars-trendline.d.ts +3 -0
- package/dist/components/shapes/bars/bars-trendline.d.ts.map +1 -0
- package/dist/components/shapes/bars/bars-trendline.js +43 -0
- package/dist/components/shapes/bars/bars.d.ts.map +1 -1
- package/dist/components/shapes/bars/bars.js +27 -18
- package/dist/components/shapes/bars/bars.module.css +1 -1
- package/dist/components/shapes/bars/bars.test.js +132 -8
- package/dist/components/shapes/bars/index.d.ts +13 -0
- package/dist/components/shapes/bars/index.d.ts.map +1 -1
- package/dist/components/shapes/bars/index.js +14 -1
- package/dist/components/shapes/lines/lines-average.d.ts.map +1 -1
- package/dist/components/shapes/lines/lines-average.js +8 -46
- package/dist/components/shapes/lines/lines-marker-labels.d.ts.map +1 -1
- package/dist/components/shapes/lines/lines-marker-labels.js +16 -12
- package/dist/components/shapes/lines/lines-overlay.d.ts.map +1 -1
- package/dist/components/shapes/lines/lines-overlay.js +21 -26
- package/dist/components/shapes/lines/lines-series.d.ts.map +1 -1
- package/dist/components/shapes/lines/lines-series.js +6 -19
- package/dist/components/shapes/lines/lines-tooltip.d.ts.map +1 -1
- package/dist/components/shapes/lines/lines-tooltip.js +21 -28
- package/dist/components/shapes/lines/lines-trendline.d.ts +3 -0
- package/dist/components/shapes/lines/lines-trendline.d.ts.map +1 -0
- package/dist/components/shapes/lines/lines-trendline.js +43 -0
- package/dist/components/shapes/lines/lines.d.ts.map +1 -1
- package/dist/components/shapes/lines/lines.js +2 -1
- package/dist/components/shapes/lines/lines.test.js +29 -8
- package/dist/hooks/hooks.test.d.ts.map +1 -1
- package/dist/hooks/hooks.test.js +162 -5
- package/dist/hooks/index.d.ts +4 -0
- package/dist/hooks/index.d.ts.map +1 -1
- package/dist/hooks/index.js +4 -0
- package/dist/hooks/useAverage.d.ts +82 -0
- package/dist/hooks/useAverage.d.ts.map +1 -0
- package/dist/hooks/useAverage.js +83 -0
- package/dist/hooks/useBarChart.d.ts +17 -0
- package/dist/hooks/useBarChart.d.ts.map +1 -0
- package/dist/hooks/useBarChart.js +110 -0
- package/dist/hooks/useBars.d.ts +66 -0
- package/dist/hooks/useBars.d.ts.map +1 -0
- package/dist/hooks/useBars.js +121 -0
- package/dist/hooks/useCartesian.d.ts +12 -2
- package/dist/hooks/useCartesian.d.ts.map +1 -1
- package/dist/hooks/useCartesian.js +19 -7
- package/dist/hooks/useLineChart.d.ts +3 -27
- package/dist/hooks/useLineChart.d.ts.map +1 -1
- package/dist/hooks/useLineChart.js +27 -16
- package/dist/hooks/useLines.d.ts +51 -0
- package/dist/hooks/useLines.d.ts.map +1 -0
- package/dist/hooks/useLines.js +70 -0
- package/dist/hooks/useTrendline.d.ts +60 -0
- package/dist/hooks/useTrendline.d.ts.map +1 -0
- package/dist/hooks/useTrendline.js +84 -0
- package/dist/providers/cartesian.d.ts +5 -1
- package/dist/providers/cartesian.d.ts.map +1 -1
- package/dist/providers/cartesian.js +19 -3
- package/dist/providers/data.d.ts +6 -7
- package/dist/providers/data.d.ts.map +1 -1
- package/dist/providers/data.js +4 -3
- package/dist/style-config/axis.js +3 -3
- package/dist/style-config/bars.d.ts +3 -0
- package/dist/style-config/bars.d.ts.map +1 -0
- package/dist/style-config/bars.js +43 -0
- package/dist/style-config/cartesian.d.ts.map +1 -1
- package/dist/style-config/cartesian.js +2 -0
- package/dist/style-config/colors.d.ts +1 -4
- package/dist/style-config/colors.d.ts.map +1 -1
- package/dist/style-config/colors.js +30 -36
- package/dist/style-config/headings.d.ts.map +1 -1
- package/dist/style-config/headings.js +16 -1
- package/dist/style-config/lines.d.ts.map +1 -1
- package/dist/style-config/lines.js +6 -0
- package/dist/style-config/themes.d.ts.map +1 -1
- package/dist/style-config/themes.js +18 -0
- package/dist/types/bar-chart.d.ts +26 -22
- package/dist/types/bar-chart.d.ts.map +1 -1
- package/dist/types/bar-chart.js +1 -39
- package/dist/types/bars.d.ts +68 -0
- package/dist/types/bars.d.ts.map +1 -0
- package/dist/types/bars.js +16 -0
- package/dist/types/cartesian.d.ts +7 -0
- package/dist/types/cartesian.d.ts.map +1 -1
- package/dist/types/headings.d.ts +15 -0
- package/dist/types/headings.d.ts.map +1 -1
- package/dist/types/headings.js +1 -1
- package/dist/types/index.d.ts +3 -0
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/index.js +4 -1
- package/dist/types/line-chart.d.ts +17 -19
- package/dist/types/line-chart.d.ts.map +1 -1
- package/dist/types/lines.d.ts +6 -0
- package/dist/types/lines.d.ts.map +1 -1
- package/dist/types/main.d.ts +15 -2
- package/dist/types/main.d.ts.map +1 -1
- package/dist/types/themes.d.ts +9 -0
- package/dist/types/themes.d.ts.map +1 -1
- package/dist/utils/axis.d.ts +12 -2
- package/dist/utils/axis.d.ts.map +1 -1
- package/dist/utils/axis.js +52 -15
- package/dist/utils/axis.test.js +72 -8
- package/dist/utils/bars.d.ts +63 -0
- package/dist/utils/bars.d.ts.map +1 -0
- package/dist/utils/bars.js +316 -0
- package/dist/utils/bars.test.d.ts +2 -0
- package/dist/utils/bars.test.d.ts.map +1 -0
- package/dist/utils/bars.test.js +388 -0
- package/dist/utils/data.d.ts +14 -6
- package/dist/utils/data.d.ts.map +1 -1
- package/dist/utils/data.js +128 -21
- package/dist/utils/data.test.js +195 -86
- package/dist/utils/format.d.ts +1 -0
- package/dist/utils/format.d.ts.map +1 -1
- package/dist/utils/format.js +2 -1
- package/dist/utils/index.d.ts +1 -0
- package/dist/utils/index.d.ts.map +1 -1
- package/dist/utils/index.js +2 -1
- package/dist/utils/lines.d.ts +3 -3
- package/dist/utils/lines.d.ts.map +1 -1
- package/dist/utils/lines.js +3 -1
- package/dist/utils/lines.test.js +26 -1
- package/dist/utils/metadata.d.ts +5 -8
- package/dist/utils/metadata.d.ts.map +1 -1
- package/dist/utils/metadata.js +8 -20
- package/dist/utils/metadata.test.js +5 -58
- package/dist/utils/viewport.test.js +8 -0
- package/package.json +13 -11
|
@@ -0,0 +1,388 @@
|
|
|
1
|
+
import { getBarChartLabels, getBarChartLabelsMaxLength, getBarChartLabelsMaxSize, getBarIndexPositionSeries, getBarLabelContent, getBarSize, getHeightForVerticalChartWithFixedBarSize, getLinearDomainStackSeries, getStackBarIndexPositionOverlay, getStackBarIndexPositionSeries, getStackBarLabelContent, getStackBarLabelMorphology, getStackBarThickness, } from './bars';
|
|
2
|
+
describe('getBarSize()', () => {
|
|
3
|
+
const bar = {
|
|
4
|
+
color: 'red',
|
|
5
|
+
height: 60,
|
|
6
|
+
index: 1,
|
|
7
|
+
key: 'name',
|
|
8
|
+
value: 15,
|
|
9
|
+
width: 24,
|
|
10
|
+
x: 20,
|
|
11
|
+
y: 35,
|
|
12
|
+
};
|
|
13
|
+
const axis = {
|
|
14
|
+
top: 0,
|
|
15
|
+
left: 0,
|
|
16
|
+
domain: [-100, 100],
|
|
17
|
+
scaleType: 'linear',
|
|
18
|
+
orientation: 'left',
|
|
19
|
+
scale: () => 10,
|
|
20
|
+
};
|
|
21
|
+
it('should return for positive values', () => {
|
|
22
|
+
const res = getBarSize(bar, axis, true);
|
|
23
|
+
const exp = 60;
|
|
24
|
+
expect(res).toStrictEqual(exp);
|
|
25
|
+
});
|
|
26
|
+
it('should return for vertical layout', () => {
|
|
27
|
+
const res = getBarSize(bar, { ...axis, scale: (x) => -x }, false);
|
|
28
|
+
const exp = 124;
|
|
29
|
+
expect(res).toStrictEqual(exp);
|
|
30
|
+
});
|
|
31
|
+
it('should return for negative value', () => {
|
|
32
|
+
const res = getBarSize({ ...bar, value: -60 }, { ...axis, scale: (x) => -x }, false);
|
|
33
|
+
const exp = 124;
|
|
34
|
+
expect(res).toStrictEqual(exp);
|
|
35
|
+
});
|
|
36
|
+
});
|
|
37
|
+
describe('getBarSize()', () => {
|
|
38
|
+
const bar = {
|
|
39
|
+
color: 'red',
|
|
40
|
+
height: 60,
|
|
41
|
+
index: 1,
|
|
42
|
+
key: 'name',
|
|
43
|
+
value: 15,
|
|
44
|
+
width: 24,
|
|
45
|
+
x: 20,
|
|
46
|
+
y: 35,
|
|
47
|
+
};
|
|
48
|
+
const axis = {
|
|
49
|
+
top: 0,
|
|
50
|
+
left: 0,
|
|
51
|
+
domain: [-100, 100],
|
|
52
|
+
scaleType: 'linear',
|
|
53
|
+
orientation: 'left',
|
|
54
|
+
scale: () => 50,
|
|
55
|
+
};
|
|
56
|
+
it('should return position, positive true', () => {
|
|
57
|
+
const res = getBarSize(bar, axis, true);
|
|
58
|
+
const exp = 60;
|
|
59
|
+
expect(res).toStrictEqual(exp);
|
|
60
|
+
});
|
|
61
|
+
it('should return position, positive false', () => {
|
|
62
|
+
const res = getBarSize(bar, axis, false);
|
|
63
|
+
const exp = 24;
|
|
64
|
+
expect(res).toStrictEqual(exp);
|
|
65
|
+
});
|
|
66
|
+
it('should return position, negative true', () => {
|
|
67
|
+
const res = getBarSize({ ...bar, value: -50 }, axis, false);
|
|
68
|
+
const exp = 24;
|
|
69
|
+
expect(res).toStrictEqual(exp);
|
|
70
|
+
});
|
|
71
|
+
it('should return position, negative false', () => {
|
|
72
|
+
const res = getBarSize({ ...bar, value: -50 }, axis, true);
|
|
73
|
+
const exp = 0;
|
|
74
|
+
expect(res).toStrictEqual(exp);
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
describe('getHeightForVerticalChartWithFixedBarSize()', () => {
|
|
78
|
+
const config = {
|
|
79
|
+
maxSize: 24,
|
|
80
|
+
paddingInnerGroup: 0.1,
|
|
81
|
+
paddingOuterGroup: 0.1,
|
|
82
|
+
paddingInner: 0.2,
|
|
83
|
+
paddingOuter: 1,
|
|
84
|
+
};
|
|
85
|
+
const data = [
|
|
86
|
+
{ date: '2024', value: 100 },
|
|
87
|
+
{ date: '2023', value: 100 },
|
|
88
|
+
];
|
|
89
|
+
const metadata = {
|
|
90
|
+
series: { dataKey: ['value'] },
|
|
91
|
+
overlay: { dataKey: undefined },
|
|
92
|
+
};
|
|
93
|
+
const res = getHeightForVerticalChartWithFixedBarSize(config, data, metadata);
|
|
94
|
+
expect(res).toStrictEqual(227);
|
|
95
|
+
});
|
|
96
|
+
describe('getBarIndexPositionSeries()', () => {
|
|
97
|
+
const bar = {
|
|
98
|
+
color: 'red',
|
|
99
|
+
height: 10,
|
|
100
|
+
index: 1,
|
|
101
|
+
key: 'test',
|
|
102
|
+
value: 123,
|
|
103
|
+
width: 20,
|
|
104
|
+
x: 3,
|
|
105
|
+
y: 5,
|
|
106
|
+
};
|
|
107
|
+
it('should return for custom thickness - horizontal', () => {
|
|
108
|
+
const res = getBarIndexPositionSeries(bar, 24, true);
|
|
109
|
+
const exp = 1;
|
|
110
|
+
expect(res).toStrictEqual(exp);
|
|
111
|
+
});
|
|
112
|
+
it('should return for custom thickness - vertical', () => {
|
|
113
|
+
const res = getBarIndexPositionSeries(bar, 24, false);
|
|
114
|
+
const exp = -2;
|
|
115
|
+
expect(res).toStrictEqual(exp);
|
|
116
|
+
});
|
|
117
|
+
it('should return for no custom thickness', () => {
|
|
118
|
+
const res = getBarIndexPositionSeries(bar, 20, false);
|
|
119
|
+
const exp = 0;
|
|
120
|
+
expect(res).toStrictEqual(exp);
|
|
121
|
+
});
|
|
122
|
+
});
|
|
123
|
+
describe('getStackBarIndexPositionSeries()', () => {
|
|
124
|
+
const bar = {
|
|
125
|
+
color: 'red',
|
|
126
|
+
height: 10,
|
|
127
|
+
index: 1,
|
|
128
|
+
key: 'test',
|
|
129
|
+
value: 123,
|
|
130
|
+
width: 20,
|
|
131
|
+
x: 3,
|
|
132
|
+
y: 5,
|
|
133
|
+
};
|
|
134
|
+
it('should return w custom thickness, overlay, horizontal', () => {
|
|
135
|
+
const res = getStackBarIndexPositionSeries(bar, 24, true, true);
|
|
136
|
+
const exp = -12;
|
|
137
|
+
expect(res).toStrictEqual(exp);
|
|
138
|
+
});
|
|
139
|
+
it('should return w custom thickness, no overlay, horizontal', () => {
|
|
140
|
+
const res = getStackBarIndexPositionSeries(bar, 24, false, true);
|
|
141
|
+
const exp = 1;
|
|
142
|
+
expect(res).toStrictEqual(exp);
|
|
143
|
+
});
|
|
144
|
+
it('should return w custom thickness, no overlay, vertical', () => {
|
|
145
|
+
const res = getStackBarIndexPositionSeries(bar, 20, false, false);
|
|
146
|
+
const exp = 0;
|
|
147
|
+
expect(res).toStrictEqual(exp);
|
|
148
|
+
});
|
|
149
|
+
it('should return w/o custom thickness, overlay, horizontal', () => {
|
|
150
|
+
const res = getStackBarIndexPositionSeries(bar, 20, true, true);
|
|
151
|
+
const exp = -8;
|
|
152
|
+
expect(res).toStrictEqual(exp);
|
|
153
|
+
});
|
|
154
|
+
it('should return w/o custom thickness, overlay, vertical', () => {
|
|
155
|
+
const res = getStackBarIndexPositionSeries(bar, 10, true, false);
|
|
156
|
+
const exp = -1;
|
|
157
|
+
expect(res).toStrictEqual(exp);
|
|
158
|
+
});
|
|
159
|
+
it('should return w custom thickness, overlay, vertical', () => {
|
|
160
|
+
const res = getStackBarIndexPositionSeries(bar, 12, true, false);
|
|
161
|
+
const exp = -3;
|
|
162
|
+
expect(res).toStrictEqual(exp);
|
|
163
|
+
});
|
|
164
|
+
it('should return w/o custom thickness, no overlay, vertical', () => {
|
|
165
|
+
const res = getStackBarIndexPositionSeries(bar, 10, false, false);
|
|
166
|
+
const exp = 5;
|
|
167
|
+
expect(res).toStrictEqual(exp);
|
|
168
|
+
});
|
|
169
|
+
});
|
|
170
|
+
describe('getStackBarIndexPositionOverlay()', () => {
|
|
171
|
+
const bar = {
|
|
172
|
+
color: 'red',
|
|
173
|
+
height: 10,
|
|
174
|
+
index: 1,
|
|
175
|
+
key: 'test',
|
|
176
|
+
value: 123,
|
|
177
|
+
width: 20,
|
|
178
|
+
x: 3,
|
|
179
|
+
y: 5,
|
|
180
|
+
};
|
|
181
|
+
it('should return - no custom width, horizontal', () => {
|
|
182
|
+
const res = getStackBarIndexPositionOverlay(bar, 20, true);
|
|
183
|
+
const exp = 14;
|
|
184
|
+
expect(res).toStrictEqual(exp);
|
|
185
|
+
});
|
|
186
|
+
it('should return - custom width, horizontal', () => {
|
|
187
|
+
const res = getStackBarIndexPositionOverlay(bar, 24, true);
|
|
188
|
+
const exp = 14;
|
|
189
|
+
expect(res).toStrictEqual(exp);
|
|
190
|
+
});
|
|
191
|
+
it('should return - custom width, vertical', () => {
|
|
192
|
+
const res = getStackBarIndexPositionOverlay(bar, 14, false);
|
|
193
|
+
const exp = 11;
|
|
194
|
+
expect(res).toStrictEqual(exp);
|
|
195
|
+
});
|
|
196
|
+
it('should return - no custom width, vertical', () => {
|
|
197
|
+
const res = getStackBarIndexPositionOverlay(bar, 10, false);
|
|
198
|
+
const exp = 11;
|
|
199
|
+
expect(res).toStrictEqual(exp);
|
|
200
|
+
});
|
|
201
|
+
});
|
|
202
|
+
describe('getStackBarThickness()', () => {
|
|
203
|
+
it('should return w fixbarsize and no overlay', () => {
|
|
204
|
+
const res = getStackBarThickness(24, 100, true, false);
|
|
205
|
+
const exp = 24;
|
|
206
|
+
expect(res).toStrictEqual(exp);
|
|
207
|
+
});
|
|
208
|
+
it('should return w/o fixbarsize and no overlay', () => {
|
|
209
|
+
const res = getStackBarThickness(24, 20, false, false);
|
|
210
|
+
const exp = 24;
|
|
211
|
+
expect(res).toStrictEqual(exp);
|
|
212
|
+
});
|
|
213
|
+
it('should return w fixbarsize and overlay', () => {
|
|
214
|
+
const res = getStackBarThickness(24, 100, true, true);
|
|
215
|
+
const exp = 11.5;
|
|
216
|
+
expect(res).toStrictEqual(exp);
|
|
217
|
+
});
|
|
218
|
+
it('should return w/o fixbarsize and overlay', () => {
|
|
219
|
+
const res = getStackBarThickness(24, 20, false, true);
|
|
220
|
+
const exp = 11.5;
|
|
221
|
+
expect(res).toStrictEqual(exp);
|
|
222
|
+
});
|
|
223
|
+
});
|
|
224
|
+
describe('getLinearDomainStackSeries()', () => {
|
|
225
|
+
it('should return for multiple nested keys', () => {
|
|
226
|
+
const data = [
|
|
227
|
+
{ test: { idy: 10, idx: 20 } },
|
|
228
|
+
{ test: { idy: 30, idx: 18 } }
|
|
229
|
+
];
|
|
230
|
+
const datakey = ['test.idy', 'test.idx'];
|
|
231
|
+
const res = getLinearDomainStackSeries(data, datakey, [0, 10]);
|
|
232
|
+
const exp = [0, 48];
|
|
233
|
+
expect(res).toStrictEqual(exp);
|
|
234
|
+
});
|
|
235
|
+
it('should return for multiple nested keys w negative values', () => {
|
|
236
|
+
const data = [
|
|
237
|
+
{ test: { idy: -10, idx: 20 } },
|
|
238
|
+
{ test: { idy: 30, idx: -18 } }
|
|
239
|
+
];
|
|
240
|
+
const datakey = ['test.idy', 'test.idx'];
|
|
241
|
+
const res = getLinearDomainStackSeries(data, datakey, [0, 10]);
|
|
242
|
+
const exp = [0, 12];
|
|
243
|
+
expect(res).toStrictEqual(exp);
|
|
244
|
+
});
|
|
245
|
+
});
|
|
246
|
+
describe('getBarChartLabels()', () => {
|
|
247
|
+
const data = [
|
|
248
|
+
{ test: { idy: 10, idx: 20 } },
|
|
249
|
+
{ test: { idy: 30, idx: 18 } }
|
|
250
|
+
];
|
|
251
|
+
it('should return for multiple series', () => {
|
|
252
|
+
const series = {
|
|
253
|
+
dataKey: ['test.idy', 'test.idx'],
|
|
254
|
+
};
|
|
255
|
+
const res = getBarChartLabels(data, series);
|
|
256
|
+
const { series: s } = res;
|
|
257
|
+
const exp = [
|
|
258
|
+
{ extraData: '', length: 3, value: 10 },
|
|
259
|
+
{ extraData: '', length: 3, value: 20 },
|
|
260
|
+
];
|
|
261
|
+
expect(s[0]).toStrictEqual(exp);
|
|
262
|
+
});
|
|
263
|
+
it('should return for multiple series', () => {
|
|
264
|
+
const series = {
|
|
265
|
+
dataKey: ['test.idx'],
|
|
266
|
+
};
|
|
267
|
+
const overlay = {
|
|
268
|
+
dataKey: ['test.idy'],
|
|
269
|
+
};
|
|
270
|
+
const res = getBarChartLabels(data, series, overlay);
|
|
271
|
+
const { series: s, overlay: o } = res;
|
|
272
|
+
const expSeries = [{ extraData: '', length: 3, value: 20 }];
|
|
273
|
+
const expOverlay = [{ extraData: '', length: 3, value: 10 }];
|
|
274
|
+
expect(s[0]).toStrictEqual(expSeries);
|
|
275
|
+
expect(o[0]).toStrictEqual(expOverlay);
|
|
276
|
+
});
|
|
277
|
+
});
|
|
278
|
+
describe('getBarChartLabelsMaxLength()', () => {
|
|
279
|
+
const data = [
|
|
280
|
+
{ test: { idy: 10, idx: 20 } },
|
|
281
|
+
{ test: { idy: 30, idx: 18 } }
|
|
282
|
+
];
|
|
283
|
+
it('should return for series', () => {
|
|
284
|
+
const series = {
|
|
285
|
+
dataKey: ['test.idy', 'test.idx'],
|
|
286
|
+
};
|
|
287
|
+
const labels = getBarChartLabels(data, series);
|
|
288
|
+
const res = getBarChartLabelsMaxLength(labels);
|
|
289
|
+
const exp = 3;
|
|
290
|
+
expect(res).toStrictEqual(exp);
|
|
291
|
+
});
|
|
292
|
+
it('should return for series and overlay', () => {
|
|
293
|
+
const series = {
|
|
294
|
+
dataKey: ['test.idx'],
|
|
295
|
+
};
|
|
296
|
+
const overlay = {
|
|
297
|
+
dataKey: ['test.idy'],
|
|
298
|
+
};
|
|
299
|
+
const labels = getBarChartLabels(data, series, overlay);
|
|
300
|
+
const res = getBarChartLabelsMaxLength(labels);
|
|
301
|
+
const exp = 3;
|
|
302
|
+
expect(res).toStrictEqual(exp);
|
|
303
|
+
});
|
|
304
|
+
});
|
|
305
|
+
describe('getBarChartLabelsMaxSize()', () => {
|
|
306
|
+
it('should return', () => {
|
|
307
|
+
const res = getBarChartLabelsMaxSize(10);
|
|
308
|
+
const exp = 96;
|
|
309
|
+
expect(res).toStrictEqual(exp);
|
|
310
|
+
});
|
|
311
|
+
});
|
|
312
|
+
describe('getBarLabelContent()', () => {
|
|
313
|
+
const bar = {
|
|
314
|
+
color: 'red',
|
|
315
|
+
height: 10,
|
|
316
|
+
index: 1,
|
|
317
|
+
key: 'test',
|
|
318
|
+
value: 123,
|
|
319
|
+
width: 20,
|
|
320
|
+
x: 3,
|
|
321
|
+
y: 5,
|
|
322
|
+
};
|
|
323
|
+
it('should return for plain object', () => {
|
|
324
|
+
const datum = { test: 101, extra: 321 };
|
|
325
|
+
const res = getBarLabelContent(datum, bar, 'test', d => d.extra);
|
|
326
|
+
const exp = { extra: 321, separator: ' - ', value: 123 };
|
|
327
|
+
expect(res).toStrictEqual(exp);
|
|
328
|
+
});
|
|
329
|
+
it('should return for nested object', () => {
|
|
330
|
+
const datum = { nested: { test: 101, extra: 321 } };
|
|
331
|
+
const res = getBarLabelContent(datum, bar, 'nested.test', d => d.extra);
|
|
332
|
+
const exp = { extra: 321, separator: ' - ', value: 123 };
|
|
333
|
+
expect(res).toStrictEqual(exp);
|
|
334
|
+
});
|
|
335
|
+
});
|
|
336
|
+
describe('getStackBarLabelContent()', () => {
|
|
337
|
+
it('should return for plain object', () => {
|
|
338
|
+
const datum = { test: 101, extra: 321 };
|
|
339
|
+
const res = getStackBarLabelContent(datum, ['test'], d => d.extra, 0);
|
|
340
|
+
const exp = { extra: 321, separator: ' - ', value: 101 };
|
|
341
|
+
expect(res).toStrictEqual(exp);
|
|
342
|
+
});
|
|
343
|
+
it('should return for nested object', () => {
|
|
344
|
+
const datum = { nested: { test: 101, extra: 321 } };
|
|
345
|
+
const res = getStackBarLabelContent(datum, ['nested.test'], d => d.extra, 0);
|
|
346
|
+
const exp = { extra: 321, separator: ' - ', value: 101 };
|
|
347
|
+
expect(res).toStrictEqual(exp);
|
|
348
|
+
});
|
|
349
|
+
});
|
|
350
|
+
describe('getStackBarLabelMorphology()', () => {
|
|
351
|
+
const bar = {
|
|
352
|
+
color: 'red',
|
|
353
|
+
height: 60,
|
|
354
|
+
index: 1,
|
|
355
|
+
key: 'name',
|
|
356
|
+
value: 15,
|
|
357
|
+
width: 24,
|
|
358
|
+
x: 20,
|
|
359
|
+
y: 35,
|
|
360
|
+
};
|
|
361
|
+
const content = { value: '10', separator: ' - ', extra: '23' };
|
|
362
|
+
const position = { x: 0, y: 0 };
|
|
363
|
+
const dimension = { xMax: 800, yMax: 600 };
|
|
364
|
+
it('should return morphology', () => {
|
|
365
|
+
const res = getStackBarLabelMorphology(bar, undefined, content, 24, position, dimension, true);
|
|
366
|
+
const exp = {
|
|
367
|
+
rect: { width: 19, x: 2.5, y: 56 },
|
|
368
|
+
text: { x: 12, y: 69 },
|
|
369
|
+
};
|
|
370
|
+
expect(res).toStrictEqual(exp);
|
|
371
|
+
});
|
|
372
|
+
it('should return morphology w extra data', () => {
|
|
373
|
+
const res = getStackBarLabelMorphology(bar, () => 'true', content, 24, position, dimension, true);
|
|
374
|
+
const exp = {
|
|
375
|
+
rect: { width: 67, x: -21.5, y: 56 },
|
|
376
|
+
text: { x: 12, y: 69 },
|
|
377
|
+
};
|
|
378
|
+
expect(res).toStrictEqual(exp);
|
|
379
|
+
});
|
|
380
|
+
it('should return morphology w extra data - vertical', () => {
|
|
381
|
+
const res = getStackBarLabelMorphology(bar, () => 'true', content, 24, position, dimension, false);
|
|
382
|
+
const exp = {
|
|
383
|
+
rect: { width: 67, x: 0, y: 3 },
|
|
384
|
+
text: { x: 33.5, y: 16 },
|
|
385
|
+
};
|
|
386
|
+
expect(res).toStrictEqual(exp);
|
|
387
|
+
});
|
|
388
|
+
});
|
package/dist/utils/data.d.ts
CHANGED
|
@@ -1,17 +1,25 @@
|
|
|
1
1
|
import { Except } from 'type-fest';
|
|
2
|
-
import { AverageType, AxisProps, Data, LineChartIndex,
|
|
3
|
-
import { BarChartIndex, BarChartOverlay, BarChartSeries } from '../types/bar-chart';
|
|
2
|
+
import { AverageType, AxisProps, Bar, BarChartIndex, BarChartSeries, ChartIndex, ChartSeries, Data, LineChartIndex, LineChartSeries, SortingType, TrendlineType } from '../types';
|
|
4
3
|
export declare const getValueFromObjectByPath: (object: Record<string, unknown>, path: string) => unknown;
|
|
5
4
|
export declare const getPrimitiveFromObjectByPath: (object: Record<string, unknown>, path: string) => string | number | undefined;
|
|
6
5
|
export declare const getLabelFromPath: (path: string) => string;
|
|
7
6
|
export declare const getPrimitivesFromObjectArrayByPath: (arr: Array<Record<string, unknown>>, path: string) => (string | number | undefined)[];
|
|
8
7
|
export declare const removeKeysFromObject: (obj: Record<string, unknown>, keys: string[]) => Record<string, unknown>;
|
|
9
|
-
export declare const
|
|
10
|
-
export declare const
|
|
8
|
+
export declare const mirrorDomain: (domain: Array<NonNullable<string | number>>) => NonNullable<string | number>[] | (number | undefined)[];
|
|
9
|
+
export declare const handleAxisDomainAndScaleType: <T extends ChartSeries | ChartIndex>(data: Data, axis: T, isStacked?: boolean) => Except<AxisProps, 'orientation'>;
|
|
10
|
+
export declare const handleLineChartDomainAndScaleType: <T extends LineChartIndex, U extends LineChartSeries>(data: Data, index: T, series: U, overlay?: U | undefined) => {
|
|
11
11
|
index: Except<AxisProps, "orientation">;
|
|
12
12
|
series: Except<AxisProps, "orientation">;
|
|
13
13
|
overlay: Except<AxisProps, "orientation"> | undefined;
|
|
14
14
|
};
|
|
15
|
-
export declare const
|
|
16
|
-
|
|
15
|
+
export declare const handleBarChartDomainAndScaleType: <T extends BarChartIndex, U extends BarChartSeries>(data: Data, index: T, series: U, overlay?: U | undefined, isStacked?: boolean) => {
|
|
16
|
+
index: Except<AxisProps, "orientation">;
|
|
17
|
+
series: Except<AxisProps, "orientation">;
|
|
18
|
+
overlay: Except<AxisProps, "orientation"> | undefined;
|
|
19
|
+
};
|
|
20
|
+
export declare const extractBarValueFromNestedKey: (barGroup: any, index: number, data: Data, scale: (value: any) => number | undefined, maxSize: number, isHorizontal: boolean) => any;
|
|
21
|
+
export declare const sortBarsBy: (bars: Bar[], sorting: SortingType, isHorizontal: boolean) => Bar[];
|
|
22
|
+
export declare const computeAverage: (data: Data, dataKeys: string[]) => AverageType | undefined;
|
|
23
|
+
export declare const computeTrendline: (data: Data, dataKeys: string[]) => TrendlineType[];
|
|
24
|
+
export declare const getDataKeyParentObject: (datum: Record<string, any>, dataKey: string) => any;
|
|
17
25
|
//# sourceMappingURL=data.d.ts.map
|
package/dist/utils/data.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"data.d.ts","sourceRoot":"","sources":["../../src/utils/data.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"data.d.ts","sourceRoot":"","sources":["../../src/utils/data.ts"],"names":[],"mappings":"AAkBA,OAAO,EAAE,MAAM,EAAE,MAAM,WAAW,CAAC;AAGnC,OAAO,EACL,WAAW,EACX,SAAS,EACT,GAAG,EACH,aAAa,EACb,cAAc,EACd,UAAU,EACV,WAAW,EACX,IAAI,EACJ,cAAc,EACd,eAAe,EACf,WAAW,EAAE,aAAa,EAC3B,MAAM,UAAU,CAAC;AASlB,eAAO,MAAM,wBAAwB,WAAY,OAAO,MAAM,EAAE,OAAO,CAAC,QAAQ,MAAM,YAA0B,CAAC;AAEjH,eAAO,MAAM,4BAA4B,WAAY,OAAO,MAAM,EAAE,OAAO,CAAC,QAAQ,MAAM,gCAGzF,CAAC;AAEF,eAAO,MAAM,gBAAgB,SAAU,MAAM,WAc5C,CAAC;AAEF,eAAO,MAAM,kCAAkC,QACxC,MAAM,OAAO,MAAM,EAAE,OAAO,CAAC,CAAC,QAC7B,MAAM,oCAC0C,CAAC;AAEzD,eAAO,MAAM,oBAAoB,QAAS,OAAO,MAAM,EAAE,OAAO,CAAC,QAAQ,MAAM,EAAE,4BAQhF,CAAC;AAEF,eAAO,MAAM,YAAY,WAAY,MAAM,YAAY,MAAM,GAAG,MAAM,CAAC,CAAC,4DAcvE,CAAC;AAEF,eAAO,MAAM,4BAA4B,wEAIzB,OAAO,KAClB,OAAO,SAAS,EAAE,aAAa,CA6EnC,CAAC;AAEF,eAAO,MAAM,iCAAiC;;;;CAmB7C,CAAC;AAEF,eAAO,MAAM,gCAAgC,4HAO7B,OAAO;;;;CAqBtB,CAAC;AAEF,eAAO,MAAM,4BAA4B,aAC7B,GAAG,SACN,MAAM,6BAEE,GAAG,KAAK,MAAM,GAAG,SAAS,WAChC,MAAM,gBACD,OAAO,QAsBrB,CAAC;AAEH,eAAO,MAAM,UAAU,SACf,GAAG,EAAE,WACF,WAAW,gBACN,OAAO,UAgCtB,CAAC;AAEF,eAAO,MAAM,cAAc,yBAEf,MAAM,EAAE,KACjB,WAAW,GAAG,SAWhB,CAAC;AAEF,eAAO,MAAM,gBAAgB,yBAEjB,MAAM,EAAE,KACjB,aAAa,EAoCd,CAAC;AAEH,eAAO,MAAM,sBAAsB,UAC1B,OAAO,MAAM,EAAE,GAAG,CAAC,WACjB,MAAM,QAKhB,CAAC"}
|
package/dist/utils/data.js
CHANGED
|
@@ -14,8 +14,11 @@
|
|
|
14
14
|
* limitations under the License.
|
|
15
15
|
*/
|
|
16
16
|
import _ from 'lodash';
|
|
17
|
-
import {
|
|
18
|
-
import {
|
|
17
|
+
import { SimpleLinearRegression } from 'ml-regression-simple-linear';
|
|
18
|
+
import { barsStyleConfig } from '../style-config/bars';
|
|
19
|
+
import { clampLinearDomain, inferScaleTypeFromDomain } from './axis';
|
|
20
|
+
import { getLinearDomainStackSeries } from './bars';
|
|
21
|
+
import { formatDate, maxPrecision } from './format';
|
|
19
22
|
import { getMinMaxDate, getMinMaxNumber, removeNilsFromDomain, } from './math';
|
|
20
23
|
export const getValueFromObjectByPath = (object, path) => _.at(object, path)[0];
|
|
21
24
|
export const getPrimitiveFromObjectByPath = (object, path) => {
|
|
@@ -43,8 +46,19 @@ export const removeKeysFromObject = (obj, keys) => {
|
|
|
43
46
|
});
|
|
44
47
|
return copy;
|
|
45
48
|
};
|
|
46
|
-
export const
|
|
47
|
-
const
|
|
49
|
+
export const mirrorDomain = (domain) => {
|
|
50
|
+
const max = _.max(domain) ?? 0;
|
|
51
|
+
const min = _.min(domain) ?? 0;
|
|
52
|
+
if (typeof max === 'number' && typeof min === 'number') {
|
|
53
|
+
const absMax = Math.abs(max);
|
|
54
|
+
const absMin = Math.abs(min);
|
|
55
|
+
const absMinMax = _.max([absMax, absMin]);
|
|
56
|
+
return [-absMinMax, absMinMax];
|
|
57
|
+
}
|
|
58
|
+
return domain;
|
|
59
|
+
};
|
|
60
|
+
export const handleAxisDomainAndScaleType = (data, axis, isStacked) => {
|
|
61
|
+
const hasData = Boolean(data.length);
|
|
48
62
|
let res = {
|
|
49
63
|
...axis,
|
|
50
64
|
hideTickLabel: true,
|
|
@@ -56,9 +70,11 @@ export const handleAxisDomainAndScaleType = (data, axis) => {
|
|
|
56
70
|
const keys = typeof dataKey === 'string' ? [dataKey] : dataKey;
|
|
57
71
|
const primitivesFromArray = keys.map((k) => getPrimitivesFromObjectArrayByPath(data, k));
|
|
58
72
|
const domainData = _.flattenDeep(primitivesFromArray);
|
|
59
|
-
let ownDomain =
|
|
73
|
+
let ownDomain = isStacked
|
|
74
|
+
? removeNilsFromDomain(getLinearDomainStackSeries(data, keys, domainData))
|
|
75
|
+
: removeNilsFromDomain(domainData);
|
|
60
76
|
const st = inferScaleTypeFromDomain(domainData, scaleType);
|
|
61
|
-
const hasCustomDomain = !!domain?.length;
|
|
77
|
+
const hasCustomDomain = !!(domain?.length);
|
|
62
78
|
if (hasCustomDomain) {
|
|
63
79
|
if (st === 'time') {
|
|
64
80
|
const minMaxDate = getMinMaxDate(ownDomain);
|
|
@@ -90,8 +106,14 @@ export const handleAxisDomainAndScaleType = (data, axis) => {
|
|
|
90
106
|
else if (st === 'linear') {
|
|
91
107
|
const minMaxNum = getMinMaxNumber(ownDomain);
|
|
92
108
|
if (minMaxNum) {
|
|
93
|
-
const
|
|
94
|
-
|
|
109
|
+
const clamped = clampLinearDomain(minMaxNum);
|
|
110
|
+
const [min, max] = clamped;
|
|
111
|
+
const extra = 1.05;
|
|
112
|
+
if (min === 0 && max > 0)
|
|
113
|
+
ownDomain = [min, _.ceil(max * extra)];
|
|
114
|
+
if (min < 0 && max === 0)
|
|
115
|
+
ownDomain = [_.floor(min * extra), max];
|
|
116
|
+
ownDomain = [_.floor(min * extra), _.ceil(max * extra)];
|
|
95
117
|
}
|
|
96
118
|
}
|
|
97
119
|
res = {
|
|
@@ -102,7 +124,7 @@ export const handleAxisDomainAndScaleType = (data, axis) => {
|
|
|
102
124
|
}
|
|
103
125
|
return removeKeysFromObject(res, ['dataKey', 'style', 'rename']);
|
|
104
126
|
};
|
|
105
|
-
export const
|
|
127
|
+
export const handleLineChartDomainAndScaleType = (data, index, series, overlay) => {
|
|
106
128
|
const i = handleAxisDomainAndScaleType(data, index);
|
|
107
129
|
const s = handleAxisDomainAndScaleType(data, series);
|
|
108
130
|
const o = overlay
|
|
@@ -114,20 +136,69 @@ export const handleChartDomainAndScaleType = (data, index, series, overlay) => {
|
|
|
114
136
|
overlay: o,
|
|
115
137
|
};
|
|
116
138
|
};
|
|
117
|
-
export const
|
|
139
|
+
export const handleBarChartDomainAndScaleType = (data, index, series, overlay, isStacked) => {
|
|
140
|
+
const indexOverride = {
|
|
141
|
+
...index,
|
|
142
|
+
scaleType: 'label',
|
|
143
|
+
paddingInner: barsStyleConfig.paddingInner,
|
|
144
|
+
paddingOuter: barsStyleConfig.paddingOuter,
|
|
145
|
+
round: true,
|
|
146
|
+
};
|
|
147
|
+
const i = handleAxisDomainAndScaleType(data, indexOverride);
|
|
148
|
+
const s = handleAxisDomainAndScaleType(data, series, isStacked);
|
|
149
|
+
const o = overlay
|
|
150
|
+
? handleAxisDomainAndScaleType(data, overlay, isStacked)
|
|
151
|
+
: undefined;
|
|
152
|
+
return {
|
|
153
|
+
index: i,
|
|
154
|
+
series: s,
|
|
155
|
+
overlay: o,
|
|
156
|
+
};
|
|
157
|
+
};
|
|
158
|
+
export const extractBarValueFromNestedKey = (barGroup, index, data, scale, maxSize, isHorizontal) => barGroup.bars.map((bar) => {
|
|
159
|
+
if (!bar.value) {
|
|
160
|
+
const newValue = getPrimitiveFromObjectByPath(data[index], bar.key);
|
|
161
|
+
const scaled = scale(newValue) ?? 0;
|
|
162
|
+
if (isHorizontal) {
|
|
163
|
+
return {
|
|
164
|
+
...bar,
|
|
165
|
+
value: newValue,
|
|
166
|
+
height: maxSize - scaled,
|
|
167
|
+
y: scaled,
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
return {
|
|
171
|
+
...bar,
|
|
172
|
+
value: newValue,
|
|
173
|
+
width: scaled,
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
return bar;
|
|
177
|
+
});
|
|
178
|
+
export const sortBarsBy = (bars, sorting, isHorizontal) => {
|
|
179
|
+
let res = [];
|
|
180
|
+
let isReverse = false;
|
|
118
181
|
if (sorting === 'as-is')
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
if (sorting
|
|
123
|
-
|
|
182
|
+
res = bars;
|
|
183
|
+
if (sorting === 'ascending-key' || sorting === 'ascending-value')
|
|
184
|
+
isReverse = true;
|
|
185
|
+
if (sorting.includes('key')) {
|
|
186
|
+
const withSort = _.sortBy(bars, 'key');
|
|
187
|
+
const withReverse = isReverse ? _.reverse(withSort) : withSort;
|
|
188
|
+
res = withReverse.map((el, i) => {
|
|
189
|
+
const { x, y } = bars[i];
|
|
190
|
+
return isHorizontal ? ({ ...el, x }) : ({ ...el, x, y });
|
|
191
|
+
});
|
|
124
192
|
}
|
|
125
|
-
if (sorting
|
|
126
|
-
|
|
193
|
+
if (sorting.includes('value')) {
|
|
194
|
+
const withSort = _.sortBy(bars, 'value');
|
|
195
|
+
const withReverse = isReverse ? _.reverse(withSort) : withSort;
|
|
196
|
+
res = withReverse.map((el, i) => {
|
|
197
|
+
const { x, y } = bars[i];
|
|
198
|
+
return isHorizontal ? ({ ...el, x }) : ({ ...el, x, y });
|
|
199
|
+
});
|
|
127
200
|
}
|
|
128
|
-
|
|
129
|
-
const orderedKeys = _.orderBy(entries, order).map(e => e.key);
|
|
130
|
-
return isAscending ? orderedKeys.reverse() : orderedKeys;
|
|
201
|
+
return res;
|
|
131
202
|
};
|
|
132
203
|
export const computeAverage = (data, dataKeys) => {
|
|
133
204
|
const dataKey = dataKeys.map((k) => ({
|
|
@@ -137,5 +208,41 @@ export const computeAverage = (data, dataKeys) => {
|
|
|
137
208
|
.filter((d) => typeof d === 'number')),
|
|
138
209
|
}));
|
|
139
210
|
const all = _.mean(dataKey.map(k => k.average));
|
|
140
|
-
return _.isNaN(all) ? undefined : { average: all, dataKey };
|
|
211
|
+
return _.isNaN(all) ? undefined : { average: maxPrecision(all, 2), dataKey };
|
|
212
|
+
};
|
|
213
|
+
export const computeTrendline = (data, dataKeys) => dataKeys.map((k) => {
|
|
214
|
+
const dataPoints = data
|
|
215
|
+
.map(d => getPrimitiveFromObjectByPath(d, k))
|
|
216
|
+
.filter((d) => typeof d === 'number');
|
|
217
|
+
const index = Array(dataPoints.length).fill('').map((_, i) => i);
|
|
218
|
+
const regression = new SimpleLinearRegression(index, dataPoints);
|
|
219
|
+
const { slope, intercept, coefficients } = regression;
|
|
220
|
+
const score = regression.score(index, dataPoints);
|
|
221
|
+
const trendline = data.map((_, i) => {
|
|
222
|
+
const val = slope * i + intercept;
|
|
223
|
+
return maxPrecision(val, 2);
|
|
224
|
+
});
|
|
225
|
+
const from = trendline[0];
|
|
226
|
+
const to = trendline[trendline.length - 1];
|
|
227
|
+
return {
|
|
228
|
+
name: k,
|
|
229
|
+
slope: maxPrecision(slope, 2),
|
|
230
|
+
intercept: maxPrecision(intercept, 2),
|
|
231
|
+
coefficients: coefficients.map(c => maxPrecision(c, 2)),
|
|
232
|
+
score: {
|
|
233
|
+
r: maxPrecision(score.r, 2),
|
|
234
|
+
r2: maxPrecision(score.r2, 2),
|
|
235
|
+
chi2: maxPrecision(score.chi2, 2),
|
|
236
|
+
rmsd: maxPrecision(score.rmsd, 2),
|
|
237
|
+
},
|
|
238
|
+
trendline,
|
|
239
|
+
from,
|
|
240
|
+
to,
|
|
241
|
+
};
|
|
242
|
+
});
|
|
243
|
+
export const getDataKeyParentObject = (datum, dataKey) => {
|
|
244
|
+
const labelFromPath = getLabelFromPath(dataKey);
|
|
245
|
+
if (labelFromPath === dataKey)
|
|
246
|
+
return datum;
|
|
247
|
+
return _.at(datum, labelFromPath)[0];
|
|
141
248
|
};
|