@tradingaction/core 2.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/LICENSE +24 -0
- package/README.md +5 -0
- package/lib/CanvasContainer.d.ts +19 -0
- package/lib/CanvasContainer.js +28 -0
- package/lib/CanvasContainer.js.map +1 -0
- package/lib/Chart.d.ts +32 -0
- package/lib/Chart.js +57 -0
- package/lib/Chart.js.map +1 -0
- package/lib/ChartCanvas.d.ts +235 -0
- package/lib/ChartCanvas.js +810 -0
- package/lib/ChartCanvas.js.map +1 -0
- package/lib/EventCapture.d.ts +131 -0
- package/lib/EventCapture.js +489 -0
- package/lib/EventCapture.js.map +1 -0
- package/lib/GenericChartComponent.d.ts +21 -0
- package/lib/GenericChartComponent.js +75 -0
- package/lib/GenericChartComponent.js.map +1 -0
- package/lib/GenericComponent.d.ts +81 -0
- package/lib/GenericComponent.js +355 -0
- package/lib/GenericComponent.js.map +1 -0
- package/lib/MoreProps.d.ts +16 -0
- package/lib/MoreProps.js +2 -0
- package/lib/MoreProps.js.map +1 -0
- package/lib/index.d.ts +7 -0
- package/lib/index.js +8 -0
- package/lib/index.js.map +1 -0
- package/lib/useEvent.d.ts +1 -0
- package/lib/useEvent.js +13 -0
- package/lib/useEvent.js.map +1 -0
- package/lib/utils/ChartDataUtil.d.ts +49 -0
- package/lib/utils/ChartDataUtil.js +205 -0
- package/lib/utils/ChartDataUtil.js.map +1 -0
- package/lib/utils/PureComponent.d.ts +4 -0
- package/lib/utils/PureComponent.js +10 -0
- package/lib/utils/PureComponent.js.map +1 -0
- package/lib/utils/accumulatingWindow.d.ts +15 -0
- package/lib/utils/accumulatingWindow.js +98 -0
- package/lib/utils/accumulatingWindow.js.map +1 -0
- package/lib/utils/barWidth.d.ts +15 -0
- package/lib/utils/barWidth.js +27 -0
- package/lib/utils/barWidth.js.map +1 -0
- package/lib/utils/closestItem.d.ts +5 -0
- package/lib/utils/closestItem.js +45 -0
- package/lib/utils/closestItem.js.map +1 -0
- package/lib/utils/evaluator.d.ts +7 -0
- package/lib/utils/evaluator.js +94 -0
- package/lib/utils/evaluator.js.map +1 -0
- package/lib/utils/identity.d.ts +1 -0
- package/lib/utils/identity.js +2 -0
- package/lib/utils/identity.js.map +1 -0
- package/lib/utils/index.d.ts +46 -0
- package/lib/utils/index.js +126 -0
- package/lib/utils/index.js.map +1 -0
- package/lib/utils/noop.d.ts +1 -0
- package/lib/utils/noop.js +3 -0
- package/lib/utils/noop.js.map +1 -0
- package/lib/utils/shallowEqual.d.ts +1 -0
- package/lib/utils/shallowEqual.js +22 -0
- package/lib/utils/shallowEqual.js.map +1 -0
- package/lib/utils/slidingWindow.d.ts +19 -0
- package/lib/utils/slidingWindow.js +109 -0
- package/lib/utils/slidingWindow.js.map +1 -0
- package/lib/utils/strokeDasharray.d.ts +3 -0
- package/lib/utils/strokeDasharray.js +37 -0
- package/lib/utils/strokeDasharray.js.map +1 -0
- package/lib/utils/zipper.d.ts +7 -0
- package/lib/utils/zipper.js +36 -0
- package/lib/utils/zipper.js.map +1 -0
- package/lib/zoom/index.d.ts +1 -0
- package/lib/zoom/index.js +2 -0
- package/lib/zoom/index.js.map +1 -0
- package/lib/zoom/zoomBehavior.d.ts +10 -0
- package/lib/zoom/zoomBehavior.js +18 -0
- package/lib/zoom/zoomBehavior.js.map +1 -0
- package/package.json +52 -0
- package/src/CanvasContainer.tsx +44 -0
- package/src/Chart.tsx +114 -0
- package/src/ChartCanvas.tsx +1336 -0
- package/src/EventCapture.tsx +709 -0
- package/src/GenericChartComponent.tsx +98 -0
- package/src/GenericComponent.tsx +454 -0
- package/src/MoreProps.ts +17 -0
- package/src/index.ts +7 -0
- package/src/useEvent.ts +14 -0
- package/src/utils/ChartDataUtil.ts +297 -0
- package/src/utils/PureComponent.tsx +12 -0
- package/src/utils/accumulatingWindow.ts +118 -0
- package/src/utils/barWidth.ts +44 -0
- package/src/utils/closestItem.ts +60 -0
- package/src/utils/evaluator.ts +163 -0
- package/src/utils/identity.ts +1 -0
- package/src/utils/index.ts +153 -0
- package/src/utils/noop.ts +2 -0
- package/src/utils/shallowEqual.ts +25 -0
- package/src/utils/slidingWindow.ts +140 -0
- package/src/utils/strokeDasharray.ts +52 -0
- package/src/utils/zipper.ts +45 -0
- package/src/zoom/index.ts +1 -0
- package/src/zoom/zoomBehavior.ts +34 -0
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
import { extent } from "d3-array";
|
|
2
|
+
import { ScaleContinuousNumeric, scaleLinear, ScaleTime } from "d3-scale";
|
|
3
|
+
import flattenDeep from "lodash.flattendeep";
|
|
4
|
+
import * as React from "react";
|
|
5
|
+
import type { ChartProps } from "../Chart";
|
|
6
|
+
|
|
7
|
+
import { functor, getClosestItem, isNotDefined, isObject, last, mapObject, shallowEqual, zipper } from "./index";
|
|
8
|
+
|
|
9
|
+
export interface ChartConfig {
|
|
10
|
+
id: number | string;
|
|
11
|
+
// readonly origin: number[] | ((width: number, height: number) => number[]);
|
|
12
|
+
readonly origin: number[];
|
|
13
|
+
readonly padding: number | { top: number; bottom: number };
|
|
14
|
+
readonly originalYExtentsProp?: number[] | ((data: any) => number) | ((data: any) => number[]);
|
|
15
|
+
readonly yExtents?: number[] | ((data: any) => number) | ((data: any) => number[]);
|
|
16
|
+
readonly yExtentsCalculator?: (options: {
|
|
17
|
+
plotData: any[];
|
|
18
|
+
xDomain: any;
|
|
19
|
+
xAccessor: any;
|
|
20
|
+
displayXAccessor: any;
|
|
21
|
+
fullData: any[];
|
|
22
|
+
}) => number[];
|
|
23
|
+
readonly flipYScale?: boolean;
|
|
24
|
+
readonly yScale: ScaleContinuousNumeric<number, number>;
|
|
25
|
+
readonly yPan: boolean;
|
|
26
|
+
readonly yPanEnabled: boolean;
|
|
27
|
+
readonly realYDomain?: number[];
|
|
28
|
+
readonly width: number;
|
|
29
|
+
readonly height: number;
|
|
30
|
+
mouseCoordinates?: { at: string; format: () => unknown };
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export const ChartDefaultConfig = {
|
|
34
|
+
flipYScale: false,
|
|
35
|
+
id: 0,
|
|
36
|
+
origin: [0, 0],
|
|
37
|
+
padding: 0,
|
|
38
|
+
yPan: true,
|
|
39
|
+
yPanEnabled: false,
|
|
40
|
+
yScale: scaleLinear(),
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
export function getChartOrigin(origin: any, contextWidth: number, contextHeight: number) {
|
|
44
|
+
const originCoordinates = typeof origin === "function" ? origin(contextWidth, contextHeight) : origin;
|
|
45
|
+
|
|
46
|
+
return originCoordinates;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export function getDimensions({ width, height }: any, chartProps: any) {
|
|
50
|
+
const chartHeight = chartProps.height || height;
|
|
51
|
+
|
|
52
|
+
return {
|
|
53
|
+
availableHeight: height,
|
|
54
|
+
width,
|
|
55
|
+
height: chartHeight,
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function values(func: any) {
|
|
60
|
+
return (d: any) => {
|
|
61
|
+
const obj = func(d);
|
|
62
|
+
if (isObject(obj)) {
|
|
63
|
+
return mapObject(obj);
|
|
64
|
+
}
|
|
65
|
+
return obj;
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function isArraySize2AndNumber(yExtentsProp: any): yExtentsProp is [number, number] {
|
|
70
|
+
if (Array.isArray(yExtentsProp) && yExtentsProp.length === 2) {
|
|
71
|
+
const [a, b] = yExtentsProp;
|
|
72
|
+
return typeof a === "number" && typeof b === "number";
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
return false;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const isChartProps = (props: ChartProps | any | undefined): props is ChartProps => {
|
|
79
|
+
if (props === undefined) {
|
|
80
|
+
return false;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const chartProps = props as ChartProps;
|
|
84
|
+
if (chartProps.id === undefined) {
|
|
85
|
+
return false;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
return true;
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
export function getNewChartConfig(innerDimension: any, children: any, existingChartConfig: ChartConfig[] = []) {
|
|
92
|
+
return React.Children.map(children, (each): ChartConfig | undefined => {
|
|
93
|
+
if (!each || !isChartProps(each.props)) {
|
|
94
|
+
return undefined;
|
|
95
|
+
}
|
|
96
|
+
const chartProps = {
|
|
97
|
+
...ChartDefaultConfig,
|
|
98
|
+
...(each.props as ChartProps),
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
const {
|
|
102
|
+
id,
|
|
103
|
+
origin,
|
|
104
|
+
padding,
|
|
105
|
+
yExtents: yExtentsProp,
|
|
106
|
+
yScale: yScaleProp = ChartDefaultConfig.yScale,
|
|
107
|
+
flipYScale,
|
|
108
|
+
yExtentsCalculator,
|
|
109
|
+
} = chartProps;
|
|
110
|
+
|
|
111
|
+
const yScale = yScaleProp.copy();
|
|
112
|
+
const { width, height, availableHeight } = getDimensions(innerDimension, chartProps);
|
|
113
|
+
|
|
114
|
+
const { yPan } = chartProps;
|
|
115
|
+
let { yPanEnabled } = chartProps;
|
|
116
|
+
const yExtents = yExtentsProp
|
|
117
|
+
? (Array.isArray(yExtentsProp) ? yExtentsProp : [yExtentsProp]).map(functor)
|
|
118
|
+
: undefined;
|
|
119
|
+
|
|
120
|
+
const prevChartConfig = existingChartConfig.find((d) => d.id === id);
|
|
121
|
+
|
|
122
|
+
if (isArraySize2AndNumber(yExtentsProp)) {
|
|
123
|
+
if (
|
|
124
|
+
!!prevChartConfig &&
|
|
125
|
+
prevChartConfig.yPan &&
|
|
126
|
+
prevChartConfig.yPanEnabled &&
|
|
127
|
+
yPan &&
|
|
128
|
+
yPanEnabled &&
|
|
129
|
+
shallowEqual(prevChartConfig.originalYExtentsProp, yExtentsProp)
|
|
130
|
+
) {
|
|
131
|
+
yScale.domain(prevChartConfig.yScale.domain());
|
|
132
|
+
} else {
|
|
133
|
+
const [a, b] = yExtentsProp;
|
|
134
|
+
yScale.domain([a, b]);
|
|
135
|
+
}
|
|
136
|
+
} else if (!!prevChartConfig && prevChartConfig.yPanEnabled) {
|
|
137
|
+
if (isArraySize2AndNumber(prevChartConfig.originalYExtentsProp)) {
|
|
138
|
+
// do nothing
|
|
139
|
+
} else {
|
|
140
|
+
yScale.domain(prevChartConfig.yScale.domain());
|
|
141
|
+
yPanEnabled = true;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
return {
|
|
146
|
+
id,
|
|
147
|
+
origin: functor(origin)(width, availableHeight),
|
|
148
|
+
padding,
|
|
149
|
+
originalYExtentsProp: yExtentsProp,
|
|
150
|
+
yExtents,
|
|
151
|
+
yExtentsCalculator,
|
|
152
|
+
flipYScale,
|
|
153
|
+
yScale,
|
|
154
|
+
yPan,
|
|
155
|
+
yPanEnabled,
|
|
156
|
+
width,
|
|
157
|
+
height,
|
|
158
|
+
};
|
|
159
|
+
}).filter((each: any) => each !== undefined);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
export function getCurrentCharts(chartConfig: ChartConfig[], mouseXY: number[]) {
|
|
163
|
+
const currentCharts = chartConfig
|
|
164
|
+
.filter((eachConfig) => {
|
|
165
|
+
const top = eachConfig.origin[1];
|
|
166
|
+
const bottom = top + eachConfig.height;
|
|
167
|
+
return mouseXY[1] > top && mouseXY[1] < bottom;
|
|
168
|
+
})
|
|
169
|
+
.map((config: any) => config.id);
|
|
170
|
+
|
|
171
|
+
return currentCharts;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
function setRange(scale: any, height: number, padding: any, flipYScale: any) {
|
|
175
|
+
if (scale.rangeRoundPoints || isNotDefined(scale.invert)) {
|
|
176
|
+
if (isNaN(padding)) {
|
|
177
|
+
throw new Error("padding has to be a number for ordinal scale");
|
|
178
|
+
}
|
|
179
|
+
if (scale.rangeRoundPoints) {
|
|
180
|
+
scale.rangeRoundPoints(flipYScale ? [0, height] : [height, 0], padding);
|
|
181
|
+
}
|
|
182
|
+
if (scale.rangeRound) {
|
|
183
|
+
scale.range(flipYScale ? [0, height] : [height, 0]).padding(padding);
|
|
184
|
+
}
|
|
185
|
+
} else {
|
|
186
|
+
const { top, bottom } = isNaN(padding) ? padding : { top: padding, bottom: padding };
|
|
187
|
+
|
|
188
|
+
scale.range(flipYScale ? [top, height - bottom] : [height - bottom, top]);
|
|
189
|
+
}
|
|
190
|
+
return scale;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
function yDomainFromYExtents(yExtents: any, yScale: any, plotData: any[]) {
|
|
194
|
+
const yValues = yExtents.map((eachExtent: any) => plotData.map(values(eachExtent)));
|
|
195
|
+
|
|
196
|
+
const allYValues: number[] = flattenDeep(yValues);
|
|
197
|
+
|
|
198
|
+
const realYDomain = yScale.invert ? (extent(allYValues) as [number, number]) : [...new Set(allYValues).values()];
|
|
199
|
+
|
|
200
|
+
return realYDomain;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
export function getChartConfigWithUpdatedYScales(
|
|
204
|
+
chartConfig: ChartConfig[],
|
|
205
|
+
{ plotData, xAccessor, displayXAccessor, fullData }: any,
|
|
206
|
+
xDomain: any,
|
|
207
|
+
dy?: number,
|
|
208
|
+
chartsToPan?: (string | number)[],
|
|
209
|
+
): ChartConfig[] {
|
|
210
|
+
const yDomains = chartConfig.map(({ yExtentsCalculator, yExtents, yScale }) => {
|
|
211
|
+
const realYDomain = yExtentsCalculator
|
|
212
|
+
? yExtentsCalculator({ plotData, xDomain, xAccessor, displayXAccessor, fullData })
|
|
213
|
+
: yDomainFromYExtents(yExtents, yScale, plotData);
|
|
214
|
+
|
|
215
|
+
const yDomainDY =
|
|
216
|
+
dy !== undefined
|
|
217
|
+
? yScale
|
|
218
|
+
.range()
|
|
219
|
+
.map((each: any) => each - dy)
|
|
220
|
+
.map(yScale.invert)
|
|
221
|
+
: yScale.domain();
|
|
222
|
+
return {
|
|
223
|
+
realYDomain,
|
|
224
|
+
yDomainDY,
|
|
225
|
+
prevYDomain: yScale.domain(),
|
|
226
|
+
};
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
const combine = zipper().combine(
|
|
230
|
+
(config: ChartConfig, { realYDomain, yDomainDY, prevYDomain }: (typeof yDomains)[number]): ChartConfig => {
|
|
231
|
+
const { id, padding, height, yScale, yPan, flipYScale, yPanEnabled = false } = config;
|
|
232
|
+
|
|
233
|
+
const another = chartsToPan !== undefined ? chartsToPan.indexOf(id) > -1 : true;
|
|
234
|
+
const domain = yPan && yPanEnabled ? (another ? yDomainDY : prevYDomain) : realYDomain;
|
|
235
|
+
|
|
236
|
+
const newYScale = setRange(yScale.copy().domain(domain), height, padding, flipYScale);
|
|
237
|
+
|
|
238
|
+
return {
|
|
239
|
+
...config,
|
|
240
|
+
yScale: newYScale,
|
|
241
|
+
realYDomain,
|
|
242
|
+
};
|
|
243
|
+
},
|
|
244
|
+
);
|
|
245
|
+
|
|
246
|
+
const updatedChartConfig = combine(chartConfig, yDomains) as ChartConfig[];
|
|
247
|
+
|
|
248
|
+
return updatedChartConfig;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
export function getCurrentItem(
|
|
252
|
+
xScale: ScaleContinuousNumeric<number, number> | ScaleTime<number, number>,
|
|
253
|
+
xAccessor: any,
|
|
254
|
+
mouseXY: number[],
|
|
255
|
+
plotData: any[],
|
|
256
|
+
) {
|
|
257
|
+
let xValue: number | Date;
|
|
258
|
+
let item: any;
|
|
259
|
+
if (xScale.invert) {
|
|
260
|
+
xValue = xScale.invert(mouseXY[0]);
|
|
261
|
+
item = getClosestItem(plotData, xValue, xAccessor);
|
|
262
|
+
} else {
|
|
263
|
+
const dr = xScale
|
|
264
|
+
.range()
|
|
265
|
+
.map((d, idx) => ({ x: Math.abs(d - mouseXY[0]), idx }))
|
|
266
|
+
.reduce((a, b) => (a.x < b.x ? a : b));
|
|
267
|
+
|
|
268
|
+
item = dr !== undefined ? plotData[dr.idx] : plotData[0];
|
|
269
|
+
}
|
|
270
|
+
return item;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
export function getXValue(
|
|
274
|
+
xScale: ScaleContinuousNumeric<number, number> | ScaleTime<number, number>,
|
|
275
|
+
xAccessor: any,
|
|
276
|
+
mouseXY: number[],
|
|
277
|
+
plotData: any[],
|
|
278
|
+
) {
|
|
279
|
+
let xValue: number | Date;
|
|
280
|
+
let item: any;
|
|
281
|
+
if (xScale.invert) {
|
|
282
|
+
xValue = xScale.invert(mouseXY[0]);
|
|
283
|
+
if (xValue > xAccessor(last(plotData))) {
|
|
284
|
+
return Math.round(xValue.valueOf());
|
|
285
|
+
} else {
|
|
286
|
+
item = getClosestItem(plotData, xValue, xAccessor);
|
|
287
|
+
}
|
|
288
|
+
} else {
|
|
289
|
+
const dr = xScale
|
|
290
|
+
.range()
|
|
291
|
+
.map((d, idx) => ({ x: Math.abs(d - mouseXY[0]), idx }))
|
|
292
|
+
.reduce((a, b) => (a.x < b.x ? a : b));
|
|
293
|
+
|
|
294
|
+
item = dr !== undefined ? plotData[dr.idx] : plotData[0];
|
|
295
|
+
}
|
|
296
|
+
return xAccessor(item);
|
|
297
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import { shallowEqual } from "./shallowEqual";
|
|
3
|
+
|
|
4
|
+
export class PureComponent<T, S = {}, SS = any> extends React.Component<T, S, SS> {
|
|
5
|
+
public shouldComponentUpdate(nextProps: T, nextState: S, nextContext: SS) {
|
|
6
|
+
return (
|
|
7
|
+
!shallowEqual(this.props, nextProps) ||
|
|
8
|
+
!shallowEqual(this.state, nextState) ||
|
|
9
|
+
!shallowEqual(this.context, nextContext)
|
|
10
|
+
);
|
|
11
|
+
}
|
|
12
|
+
}
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
/*
|
|
2
|
+
|
|
3
|
+
Taken from https://github.com/ScottLogic/d3fc/blob/master/src/indicator/algorithm/calculator/slidingWindow.js
|
|
4
|
+
|
|
5
|
+
The MIT License (MIT)
|
|
6
|
+
|
|
7
|
+
Copyright (c) 2014-2015 Scott Logic Ltd.
|
|
8
|
+
|
|
9
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
10
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
11
|
+
in the Software without restriction, including without limitation the rights
|
|
12
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
13
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
14
|
+
furnished to do so, subject to the following conditions:
|
|
15
|
+
|
|
16
|
+
The above copyright notice and this permission notice shall be included in
|
|
17
|
+
all copies or substantial portions of the Software.
|
|
18
|
+
|
|
19
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
20
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
21
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
22
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
23
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
24
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
25
|
+
THE SOFTWARE.
|
|
26
|
+
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
import { identity } from "./identity";
|
|
30
|
+
import { functor } from "./index";
|
|
31
|
+
import { noop } from "./noop";
|
|
32
|
+
|
|
33
|
+
interface AccumulatingWindow {
|
|
34
|
+
(data: any[]): any[];
|
|
35
|
+
accumulateTill(): any;
|
|
36
|
+
accumulateTill(x: any): AccumulatingWindow;
|
|
37
|
+
accumulator(): any;
|
|
38
|
+
accumulator(x: any): AccumulatingWindow;
|
|
39
|
+
value(): any;
|
|
40
|
+
value(x: any): AccumulatingWindow;
|
|
41
|
+
discardTillStart(): boolean;
|
|
42
|
+
discardTillStart(x: boolean): AccumulatingWindow;
|
|
43
|
+
discardTillEnd(): boolean;
|
|
44
|
+
discardTillEnd(x: boolean): AccumulatingWindow;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export default function () {
|
|
48
|
+
let accumulateTill = functor(false);
|
|
49
|
+
let accumulator = noop;
|
|
50
|
+
let value = identity;
|
|
51
|
+
let discardTillStart = false;
|
|
52
|
+
let discardTillEnd = false;
|
|
53
|
+
|
|
54
|
+
const accumulatingWindow = function (data: any[]) {
|
|
55
|
+
let accumulatedWindow: any[] | undefined = discardTillStart ? undefined : [];
|
|
56
|
+
const response: any[] = [];
|
|
57
|
+
let accumulatorIdx = 0;
|
|
58
|
+
let i = 0;
|
|
59
|
+
for (i = 0; i < data.length; i++) {
|
|
60
|
+
const d = data[i];
|
|
61
|
+
if (accumulateTill(d, i, accumulatedWindow || [])) {
|
|
62
|
+
if (accumulatedWindow && accumulatedWindow.length > 0) {
|
|
63
|
+
// @ts-ignore
|
|
64
|
+
response.push(accumulator(accumulatedWindow, i, accumulatorIdx++));
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
accumulatedWindow = [value(d)];
|
|
68
|
+
} else if (accumulatedWindow) {
|
|
69
|
+
accumulatedWindow.push(value(d));
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
if (!discardTillEnd) {
|
|
74
|
+
// @ts-ignore
|
|
75
|
+
response.push(accumulator(accumulatedWindow, i, accumulatorIdx));
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
return response;
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
accumulatingWindow.accumulateTill = function (x: any) {
|
|
82
|
+
if (!arguments.length) {
|
|
83
|
+
return accumulateTill;
|
|
84
|
+
}
|
|
85
|
+
accumulateTill = functor(x);
|
|
86
|
+
return accumulatingWindow;
|
|
87
|
+
};
|
|
88
|
+
accumulatingWindow.accumulator = function (x: any) {
|
|
89
|
+
if (!arguments.length) {
|
|
90
|
+
return accumulator;
|
|
91
|
+
}
|
|
92
|
+
accumulator = x;
|
|
93
|
+
return accumulatingWindow;
|
|
94
|
+
};
|
|
95
|
+
accumulatingWindow.value = function (x: any) {
|
|
96
|
+
if (!arguments.length) {
|
|
97
|
+
return value;
|
|
98
|
+
}
|
|
99
|
+
value = x;
|
|
100
|
+
return accumulatingWindow;
|
|
101
|
+
};
|
|
102
|
+
accumulatingWindow.discardTillStart = function (x: any) {
|
|
103
|
+
if (!arguments.length) {
|
|
104
|
+
return discardTillStart;
|
|
105
|
+
}
|
|
106
|
+
discardTillStart = x;
|
|
107
|
+
return accumulatingWindow;
|
|
108
|
+
};
|
|
109
|
+
accumulatingWindow.discardTillEnd = function (x: any) {
|
|
110
|
+
if (!arguments.length) {
|
|
111
|
+
return discardTillEnd;
|
|
112
|
+
}
|
|
113
|
+
discardTillEnd = x;
|
|
114
|
+
return accumulatingWindow;
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
return accumulatingWindow as AccumulatingWindow;
|
|
118
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { ScaleContinuousNumeric, ScaleTime } from "d3-scale";
|
|
2
|
+
import { first, last } from ".";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Bar width is based on the amount of items in the plot data and the distance between the first and last of those
|
|
6
|
+
* items.
|
|
7
|
+
* @param props the props passed to the series.
|
|
8
|
+
* @param moreProps an object holding the xScale, xAccessor and plotData.
|
|
9
|
+
* @return {number} the bar width.
|
|
10
|
+
*/
|
|
11
|
+
export const plotDataLengthBarWidth = <T>(
|
|
12
|
+
props: { widthRatio: number },
|
|
13
|
+
moreProps: {
|
|
14
|
+
xAccessor: (datum: T) => number | Date;
|
|
15
|
+
xScale: ScaleContinuousNumeric<number, number> | ScaleTime<number, number>;
|
|
16
|
+
plotData: T[];
|
|
17
|
+
},
|
|
18
|
+
): number => {
|
|
19
|
+
const { widthRatio } = props;
|
|
20
|
+
const { xAccessor, xScale, plotData } = moreProps;
|
|
21
|
+
|
|
22
|
+
const [l, r] = xScale.range();
|
|
23
|
+
|
|
24
|
+
if (xScale.invert != null) {
|
|
25
|
+
const [dl, dr] = xScale.domain();
|
|
26
|
+
if (typeof dl === "number" && typeof dr === "number") {
|
|
27
|
+
const totalWidth = Math.abs(r - l);
|
|
28
|
+
|
|
29
|
+
const width = totalWidth / Math.abs(dl - dr);
|
|
30
|
+
|
|
31
|
+
return width * widthRatio;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const width = xScale(xAccessor(last(plotData))) - xScale(xAccessor(first(plotData)));
|
|
35
|
+
|
|
36
|
+
return (width / plotData.length) * widthRatio * 0.7;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const totalWidth = Math.abs(r - l);
|
|
40
|
+
|
|
41
|
+
const width = totalWidth / xScale.domain().length;
|
|
42
|
+
|
|
43
|
+
return width * widthRatio;
|
|
44
|
+
};
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
export const getClosestItemIndexes = <T, TAccessor extends number | Date>(
|
|
2
|
+
array: T[],
|
|
3
|
+
value: TAccessor,
|
|
4
|
+
accessor: (item: T) => TAccessor,
|
|
5
|
+
) => {
|
|
6
|
+
let lo = 0;
|
|
7
|
+
let hi = array.length - 1;
|
|
8
|
+
while (hi - lo > 1) {
|
|
9
|
+
const mid = Math.round((lo + hi) / 2);
|
|
10
|
+
const itemAtMid = array[mid];
|
|
11
|
+
const valueAtMid = accessor(itemAtMid);
|
|
12
|
+
if (valueAtMid <= value) {
|
|
13
|
+
lo = mid;
|
|
14
|
+
} else {
|
|
15
|
+
hi = mid;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const lowItemValue = accessor(array[lo]);
|
|
20
|
+
const highItemValue = accessor(array[hi]);
|
|
21
|
+
|
|
22
|
+
// for Date object === does not work, so using the <= in combination with >=
|
|
23
|
+
// the same code works for both dates and numbers
|
|
24
|
+
if (lowItemValue?.valueOf() === value?.valueOf()) {
|
|
25
|
+
hi = lo;
|
|
26
|
+
}
|
|
27
|
+
if (highItemValue?.valueOf() === value?.valueOf()) {
|
|
28
|
+
lo = hi;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
if (lowItemValue < value && highItemValue < value) {
|
|
32
|
+
lo = hi;
|
|
33
|
+
}
|
|
34
|
+
if (lowItemValue > value && highItemValue > value) {
|
|
35
|
+
hi = lo;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return { left: lo, right: hi };
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
export const getClosestItem = <T, TAccessor extends number | Date>(
|
|
42
|
+
array: T[],
|
|
43
|
+
value: TAccessor,
|
|
44
|
+
accessor: (item: T) => TAccessor,
|
|
45
|
+
) => {
|
|
46
|
+
const { left, right } = getClosestItemIndexes(array, value, accessor);
|
|
47
|
+
if (left === right) {
|
|
48
|
+
return array[left];
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const leftItem = accessor(array[left]);
|
|
52
|
+
const rightItem = accessor(array[right]);
|
|
53
|
+
|
|
54
|
+
const closest =
|
|
55
|
+
Math.abs(leftItem.valueOf() - value.valueOf()) < Math.abs(rightItem.valueOf() - value.valueOf())
|
|
56
|
+
? array[left]
|
|
57
|
+
: array[right];
|
|
58
|
+
|
|
59
|
+
return closest;
|
|
60
|
+
};
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
import { max, min } from "d3-array";
|
|
2
|
+
import { ScaleContinuousNumeric, ScaleTime } from "d3-scale";
|
|
3
|
+
import { getClosestItemIndexes, head, isDefined, isNotDefined, last } from "../utils";
|
|
4
|
+
|
|
5
|
+
function getNewEnd<T, TAccessor extends number | Date>(
|
|
6
|
+
fallbackEnd: { lastItem: T; lastItemX: TAccessor },
|
|
7
|
+
xAccessor: (item: T) => TAccessor,
|
|
8
|
+
initialXScale: ScaleContinuousNumeric<number, number> | ScaleTime<number, number>,
|
|
9
|
+
start: any,
|
|
10
|
+
) {
|
|
11
|
+
const { lastItem, lastItemX } = fallbackEnd;
|
|
12
|
+
|
|
13
|
+
const lastItemXValue = xAccessor(lastItem);
|
|
14
|
+
|
|
15
|
+
const [rangeStart, rangeEnd] = initialXScale.range();
|
|
16
|
+
|
|
17
|
+
const newEnd =
|
|
18
|
+
((rangeEnd - rangeStart) / (lastItemX.valueOf() - rangeStart)) * (lastItemXValue.valueOf() - start.valueOf()) +
|
|
19
|
+
start.valueOf();
|
|
20
|
+
|
|
21
|
+
return newEnd;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function extentsWrapper<TDomain extends number | Date>(
|
|
25
|
+
useWholeData: boolean,
|
|
26
|
+
clamp:
|
|
27
|
+
| boolean
|
|
28
|
+
| "left"
|
|
29
|
+
| "right"
|
|
30
|
+
| "both"
|
|
31
|
+
| ((domain: [TDomain, TDomain], headTail: [TDomain, TDomain]) => [TDomain, TDomain]),
|
|
32
|
+
pointsPerPxThreshold: number,
|
|
33
|
+
minPointsPerPxThreshold: number,
|
|
34
|
+
flipXScale: boolean,
|
|
35
|
+
) {
|
|
36
|
+
function filterData<T>(
|
|
37
|
+
data: T[],
|
|
38
|
+
inputDomain: [TDomain, TDomain],
|
|
39
|
+
xAccessor: (item: T) => TDomain,
|
|
40
|
+
initialXScale: ScaleContinuousNumeric<number, number> | ScaleTime<number, number>,
|
|
41
|
+
{ currentPlotData, currentDomain, fallbackStart, fallbackEnd, ignoreThresholds = false }: any = {},
|
|
42
|
+
) {
|
|
43
|
+
if (useWholeData) {
|
|
44
|
+
return { plotData: data, domain: inputDomain };
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
let left: TDomain = head(inputDomain);
|
|
48
|
+
let right: TDomain = last(inputDomain);
|
|
49
|
+
let clampedDomain = inputDomain;
|
|
50
|
+
|
|
51
|
+
let filteredData = getFilteredResponse(data, left, right, xAccessor);
|
|
52
|
+
if (filteredData.length === 1 && fallbackStart !== undefined) {
|
|
53
|
+
left = fallbackStart;
|
|
54
|
+
right = getNewEnd(fallbackEnd, xAccessor, initialXScale, left);
|
|
55
|
+
|
|
56
|
+
clampedDomain = [left, right];
|
|
57
|
+
filteredData = getFilteredResponse(data, left, right, xAccessor);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
if (typeof clamp === "function") {
|
|
61
|
+
clampedDomain = clamp(clampedDomain, [xAccessor(head(data)), xAccessor(last(data))]);
|
|
62
|
+
} else {
|
|
63
|
+
if (clamp === "left" || clamp === "both" || clamp === true) {
|
|
64
|
+
clampedDomain = [max([left, xAccessor(head(data))])!, clampedDomain[1]];
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
if (clamp === "right" || clamp === "both" || clamp === true) {
|
|
68
|
+
clampedDomain = [clampedDomain[0], min([right, xAccessor(last(data))])!];
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
if (clampedDomain !== inputDomain) {
|
|
73
|
+
filteredData = getFilteredResponse(data, clampedDomain[0], clampedDomain[1], xAccessor);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const realInputDomain = clampedDomain;
|
|
77
|
+
|
|
78
|
+
const xScale = initialXScale.copy().domain(realInputDomain) as
|
|
79
|
+
| ScaleContinuousNumeric<number, number>
|
|
80
|
+
| ScaleTime<number, number>;
|
|
81
|
+
|
|
82
|
+
let width = Math.floor(xScale(xAccessor(last(filteredData))) - xScale(xAccessor(head(filteredData))));
|
|
83
|
+
|
|
84
|
+
// prevent negative width when flipXScale
|
|
85
|
+
if (flipXScale && width < 0) {
|
|
86
|
+
width = width * -1;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
let plotData: T[];
|
|
90
|
+
let domain: [number | Date, number | Date];
|
|
91
|
+
|
|
92
|
+
const chartWidth = last(xScale.range()) - head(xScale.range());
|
|
93
|
+
|
|
94
|
+
if (
|
|
95
|
+
(ignoreThresholds && filteredData.length > 1) ||
|
|
96
|
+
canShowTheseManyPeriods(width, filteredData.length, pointsPerPxThreshold, minPointsPerPxThreshold)
|
|
97
|
+
) {
|
|
98
|
+
plotData = filteredData;
|
|
99
|
+
domain = realInputDomain;
|
|
100
|
+
} else {
|
|
101
|
+
if (chartWidth > showMaxThreshold(width, pointsPerPxThreshold) && isDefined(fallbackEnd)) {
|
|
102
|
+
plotData = filteredData;
|
|
103
|
+
const newEnd = getNewEnd(fallbackEnd, xAccessor, initialXScale, head(realInputDomain));
|
|
104
|
+
domain = [head(realInputDomain), newEnd];
|
|
105
|
+
} else {
|
|
106
|
+
plotData =
|
|
107
|
+
currentPlotData ?? filteredData.slice(filteredData.length - showMax(width, pointsPerPxThreshold));
|
|
108
|
+
domain = currentDomain ?? [xAccessor(head(plotData)), xAccessor(last(plotData))];
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
return { plotData, domain };
|
|
112
|
+
}
|
|
113
|
+
return { filterData };
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function canShowTheseManyPeriods(width: number, arrayLength: number, maxThreshold: number, minThreshold: number) {
|
|
117
|
+
const widthAdjustedMinThreshold = showMinThreshold(width, minThreshold);
|
|
118
|
+
const widthAdjustedMaxTheshold = showMaxThreshold(width, maxThreshold);
|
|
119
|
+
return arrayLength >= widthAdjustedMinThreshold && arrayLength < widthAdjustedMaxTheshold;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function showMinThreshold(width: number, threshold: number) {
|
|
123
|
+
return Math.max(1, Math.ceil(width * threshold));
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function showMaxThreshold(width: number, threshold: number) {
|
|
127
|
+
return Math.floor(width * threshold);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function showMax(width: number, threshold: number) {
|
|
131
|
+
return Math.floor(showMaxThreshold(width, threshold) * 0.97);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function getFilteredResponse<T, TAccessor extends number | Date>(
|
|
135
|
+
data: T[],
|
|
136
|
+
left: TAccessor,
|
|
137
|
+
right: TAccessor,
|
|
138
|
+
xAccessor: (item: T) => TAccessor,
|
|
139
|
+
) {
|
|
140
|
+
const newLeftIndex = getClosestItemIndexes(data, left, xAccessor).left;
|
|
141
|
+
const newRightIndex = getClosestItemIndexes(data, right, xAccessor).right;
|
|
142
|
+
|
|
143
|
+
const filteredData = data.slice(newLeftIndex, newRightIndex + 1);
|
|
144
|
+
|
|
145
|
+
return filteredData;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
export default function ({
|
|
149
|
+
xScale,
|
|
150
|
+
useWholeData,
|
|
151
|
+
clamp,
|
|
152
|
+
pointsPerPxThreshold,
|
|
153
|
+
minPointsPerPxThreshold,
|
|
154
|
+
flipXScale,
|
|
155
|
+
}: any) {
|
|
156
|
+
return extentsWrapper(
|
|
157
|
+
useWholeData || isNotDefined(xScale.invert),
|
|
158
|
+
clamp,
|
|
159
|
+
pointsPerPxThreshold,
|
|
160
|
+
minPointsPerPxThreshold,
|
|
161
|
+
flipXScale,
|
|
162
|
+
);
|
|
163
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const identity = (d: any) => d;
|