-
Notifications
You must be signed in to change notification settings - Fork 157
/
Copy pathget-data.ts
412 lines (397 loc) · 18.9 KB
/
get-data.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
import { Chart } from '../chart';
import { withInBounds, PointData, getValueXByPoint, getValueYByPoint, AccPointData, sort, Point3D, valueToCoefficient, valueToPolarCoefficient } from '../../common/utils/helper';
import { Rect } from '@syncfusion/ej2-svg-base';
import { Series, Points } from '../series/chart-series';
import { Tooltip } from '../user-interaction/tooltip';
import { Crosshair } from '../user-interaction/crosshair';
/**
* The `ChartData` class is used to retrieve data on mouse move events in a chart.
*
* @private
*/
export class ChartData {
/** @private */
public chart: Chart;
public lierIndex: number;
/** @private */
public currentPoints: PointData[] | AccPointData[] | Point3D[] = [];
/** @private */
public previousPoints: PointData[] | AccPointData[] | Point3D[] = [];
public insideRegion: boolean = false;
public commonXvalues: number[] = [];
/**
* Constructor for the data.
*
* @private
*/
constructor(chart: Chart) {
this.chart = chart;
this.lierIndex = 0;
}
/**
* Method to get the Data.
*
* @private
*/
public getData(): PointData {
const chart: Chart = this.chart;
let point: Points = null;
let series: Series = null;
let width: number; let height: number;
let mouseX: number; let mouseY: number;
for (let len: number = chart.visibleSeries.length, i: number = len - 1; i >= 0; i--) {
series = chart.visibleSeries[i as number];
width = (series.type === 'Scatter' || series.drawType === 'Scatter' || (series.marker.visible))
? (series.marker.height + 5) / 2 : 0;
height = (series.type === 'Scatter' || series.drawType === 'Scatter' || (series.marker.visible))
? (series.marker.width + 5) / 2 : 0;
mouseX = chart.mouseX; mouseY = chart.mouseY;
if (series.dragSettings.enable && series.isRectSeries) {
if (!(series.type === 'Bar' && chart.isTransposed) && (chart.isTransposed || series.type === 'Bar')) {
const markerWidth: number = series.marker.width / 2;
mouseX = series.yAxis.isAxisInverse ? mouseX + markerWidth : mouseX - markerWidth;
} else {
const markerHeight: number = series.marker.height / 2;
mouseY = series.yAxis.isAxisInverse ? mouseY - markerHeight : mouseY + markerHeight;
}
}
if (series.visible && withInBounds(mouseX, mouseY, series.clipRect, width, height)) {
point = this.getRectPoint(series, series.clipRect, mouseX, mouseY);
}
if (point) {
return new PointData(point, series);
}
}
return new PointData(point, series);
}
public isSelected(chart : Chart) : boolean {
return ((chart.selectionMode.indexOf('Drag') > -1 || chart.selectionMode.indexOf('Lasso') > -1 ) && chart.selectionModule &&
chart.selectionModule.rectPoints !== null);
}
private getRectPoint(series: Series, rect: Rect, x: number, y: number): Points {
const chart: Chart = this.chart;
let fromCenterX: number; let fromCenterY: number;
let clickAngle: number; let arcAngle: number = 0;
let startAngle: number; let endAngle: number;
let distanceFromCenter: number;
if (chart.isScrolling) {
return null;
}
for (const point of series.points) {
if (!point.regionData) {
if (!point.regions || !point.regions.length) {
continue;
}
}
if (point.regionData && this.chart.chartAreaType === 'PolarRadar' && series.drawType.indexOf('Column') > -1) {
fromCenterX = x - (series.clipRect.width / 2 + series.clipRect.x);
fromCenterY = y - (series.clipRect.height / 2 + series.clipRect.y);
arcAngle = 2 * Math.PI * (point.regionData.currentXPosition < 0 ? 1 + point.regionData.currentXPosition
: point.regionData.currentXPosition);
clickAngle = (Math.atan2(fromCenterY, fromCenterX) + 0.5 * Math.PI - arcAngle) % (2 * Math.PI);
clickAngle = clickAngle < 0 ? 2 * Math.PI + clickAngle : clickAngle;
clickAngle = clickAngle + 2 * Math.PI * series.chart.primaryXAxis.startAngle;
startAngle = point.regionData.startAngle;
startAngle -= arcAngle;
startAngle = startAngle < 0 ? 2 * Math.PI + startAngle : startAngle;
endAngle = point.regionData.endAngle;
endAngle -= arcAngle;
endAngle = endAngle < 0 ? 2 * Math.PI + endAngle : endAngle;
distanceFromCenter = Math.sqrt(Math.pow(Math.abs(fromCenterX), 2) + Math.pow(Math.abs(fromCenterY), 2));
if (clickAngle >= startAngle && clickAngle <= endAngle &&
(((distanceFromCenter >= point.regionData.innerRadius && distanceFromCenter <= point.regionData.radius) ||
(distanceFromCenter <= point.regionData.innerRadius && distanceFromCenter >= point.regionData.radius))
&& distanceFromCenter <= series.chart.radius)) {
return point;
}
}
if ((series.dragSettings.enable && series.isRectSeries) || (series.isRectSeries && series.marker.visible && this.chart.chartAreaType !== 'PolarRadar')) {
if (this.isPointInThresholdRegion(x, y, point, rect, series)) {
this.insideRegion = true;
return point;
}
}
if (!this.insideRegion && this.checkRegionContainsPoint(point.regions, rect, x, y)) {
return point;
} else if (this.insideRegion && this.checkRegionContainsPoint(point.regions, rect, x, y)) {
return point;
}
}
return null;
}
/**
* Checks if the given point is contained within any of the regions defined by the array of rectangles.
*
* @param {Rect[]} regionRect - The array of rectangles defining the regions.
* @param {Rect} rect - The rectangle to check against the regions.
* @param {number} x - The x-coordinate of the point.
* @param {number} y - The y-coordinate of the point.
* @returns {boolean} - True if the point is contained within any of the regions, otherwise false.
*/
private checkRegionContainsPoint(regionRect: Rect[], rect: Rect, x: number, y: number): boolean {
return regionRect.some((region: Rect, index: number) => {
this.lierIndex = index;
return withInBounds(
x, y,
new Rect(
(this.chart.chartAreaType === 'Cartesian' ? rect.x : 0) + region.x,
(this.chart.chartAreaType === 'Cartesian' ? rect.y : 0) + region.y,
region.width, region.height
)
);
});
}
/**
* Checks if the given point is within the threshold region of a data point.
*
* @param {number} x - The x-coordinate of the point to check.
* @param {number} y - The y-coordinate of the point to check.
* @param {Points} point - The data point.
* @param {Rect} rect - The rectangle representing the threshold region.
* @param {Series} series - The series to which the data point belongs.
* @returns {boolean} - True if the point is within the threshold region, otherwise false.
*/
private isPointInThresholdRegion(x: number, y: number, point: Points, rect: Rect, series: Series): boolean {
const isBar: boolean = series.type === 'Bar';
const isInversed: boolean = series.yAxis.isAxisInverse;
const isTransposed: boolean = series.chart.isTransposed;
const heightValue: number = 10; let yValue: number = 0;
let xValue: number = 0;
let width: number;
let height: number = width = 2 * heightValue;
if (isInversed && isTransposed) {
if (isBar) {
yValue = point.regions[0].height - heightValue;
width = point.regions[0].width;
} else {
xValue = -heightValue;
height = point.regions[0].height;
}
} else if (isInversed || point.yValue < 0) {
if (isBar) {
xValue = -heightValue;
height = point.regions[0].height;
} else {
yValue = point.regions[0].height - heightValue;
width = point.regions[0].width;
}
} else if (isTransposed) {
if (isBar) {
yValue = -heightValue;
width = point.regions[0].width;
} else {
xValue = point.regions[0].width - heightValue;
height = point.regions[0].height;
}
} else {
if (isBar) {
xValue = point.regions[0].width - heightValue;
height = point.regions[0].height;
} else {
yValue = -heightValue;
width = point.regions[0].width;
}
}
return point.regions.some((region: Rect) => {
return withInBounds(
x, y,
new Rect(
(this.chart.chartAreaType === 'Cartesian' ? rect.x : 0) + region.x + xValue,
(this.chart.chartAreaType === 'Cartesian' ? rect.y : 0) + region.y + yValue,
width, height
)
);
});
}
/**
* Gets the index of the closest data point to the given value in the series.
*
* @param {Series} series - The series.
* @param {number} value - The value to which the closest data point is sought.
* @param {number[]} [xvalues] - The x-values of the data points.
* @returns {number} - The index of the closest data point.
* @private
*/
public getClosest(series: Series, value: number, xvalues?: number[]): number {
let closest: number; let data: number;
const xData: number[] = xvalues ? xvalues : series.xData;
const xLength: number = xData.length;
let leftSideNearest: number = 0.5;
let rightSideNearest: number = 0.5;
if (series.xAxis.valueType === 'DateTime' && series.points.length === 1) {
leftSideNearest = series.xAxis.visibleRange.min;
rightSideNearest = series.xAxis.visibleRange.max;
for (let index: number = 0; index < series.chart.visibleSeries.length; index++) {
const visibleSeries: Series = series.chart.visibleSeries[index as number];
if (visibleSeries.xMin >= leftSideNearest && visibleSeries.xMin < series.xMin) {
leftSideNearest = visibleSeries.xMin + 0.1;
}
if (visibleSeries.xMax <= rightSideNearest && visibleSeries.xMax > series.xMax) {
rightSideNearest = visibleSeries.xMax - 0.1;
}
if (visibleSeries.visible && visibleSeries.points.length > 1) {
if (visibleSeries.xMax >= leftSideNearest && visibleSeries.xMax < series.xMin) {
leftSideNearest = visibleSeries.xMax + 0.1;
}
if (visibleSeries.xMin <= rightSideNearest && visibleSeries.xMin > series.xMax) {
rightSideNearest = visibleSeries.xMin - 0.1;
}
}
}
if (leftSideNearest !== series.xAxis.visibleRange.min) {
leftSideNearest = Math.abs(series.xMin - leftSideNearest) / 2;
}
if (rightSideNearest !== series.xAxis.visibleRange.max) {
rightSideNearest = Math.abs(series.xMax - rightSideNearest) / 2;
}
}
if (value >= <number>series.xMin - leftSideNearest && value <= <number>series.xMax + rightSideNearest) {
for (let i: number = 0; i < xLength; i++) {
data = xData[i as number];
if (closest == null || Math.abs(data - value) < Math.abs(closest - value)) {
closest = data;
}
}
}
const isDataExist: boolean = series.xData.indexOf(closest) !== -1;
if (isDataExist) {
return closest;
} else {
return null;
}
}
private binarySearch(target: number, list: Points[]): Points {
let first: number = 0;
let last: number = list.length;
let position: number = -1;
let found: boolean = false;
let middle: number;
while (found === false && first <= last) {
middle = Math.floor((first + last) / 2);
if (list[middle as number].xValue === target){
found = true;
position = middle;
} else if (list[middle as number].xValue > target) {
last = middle - 1;
} else {
first = middle + 1;
}
}
return position !== -1 ? list[position as number] : null;
}
public getClosestX(chart: Chart, series: Series, xvalues?: number[]): PointData {
let value: number;
const rect: Rect = series.clipRect;
if (chart.mouseX <= rect.x + rect.width && chart.mouseX >= rect.x) {
if (!chart.requireInvertedAxis) {
value = getValueXByPoint(chart.mouseX - rect.x, rect.width, series.xAxis);
} else {
value = getValueYByPoint(chart.mouseY - rect.y, rect.height, series.xAxis);
}
}
const closest: number = this.getClosest(series, value, xvalues);
const point: Points = ((closest || closest === 0) && series.points.length > 0) ? this.binarySearch(closest, sort(series.points, ['xValue']) as Points[]) : null;
if (point && point.visible) {
if (!(this.chart.chartAreaType === 'Cartesian' && ((series.category === 'Indicator' && series.name === 'Histogram') ||
(point.symbolLocations && point.symbolLocations.length > 0 && point.symbolLocations[0].x >= 0 &&
point.symbolLocations[0].x <= rect.width)))) {
return null;
}
return new PointData(point, series);
}
return null;
}
/**
* Merges the x-values of the data points from multiple series into a single array.
*
* @param {Series[]} visibleSeries - The array of visible series.
* @returns {number[]} - The merged array of x-values.
* @private
*/
public mergeXvalues(visibleSeries: Series[]): number[] {
if (visibleSeries.length && (!this.commonXvalues.length || (this.commonXvalues.length !== visibleSeries[0].xData.length))) {
this.commonXvalues = visibleSeries[0].xData;
for (let index: number = 1; index < visibleSeries.length; index++) {
this.commonXvalues = this.getDistinctValues(this.commonXvalues, visibleSeries[index as number].xData);
}
}
return this.commonXvalues;
}
public commonXValue(visibleSeries: Series[]): number[] {
const commonXValues: number[] = [];
for (let j: number = 0; j < visibleSeries.length; j++) {
for (let i: number = 0; (visibleSeries[j as number].points && i < visibleSeries[j as number].points.length); i++) {
const point: Points = visibleSeries[j as number].points[i as number];
if (point && (point.index === 0 || point.index === visibleSeries[j as number].points.length - 1 ||
(point.symbolLocations && point.symbolLocations.length > 0))) {
commonXValues.push(point.xValue);
}
}
}
return commonXValues;
}
private getDistinctValues(first: number[] = [], second: number[] = []): number[] {
const intial: object = {};
const result: number[] = [];
let index: number;
for (index = 0; index < first.length; index++) {
const temp: number = first[index as number];
if (!intial[temp as number]) {
intial[temp as number] = true;
result.push(temp);
}
}
for (index = 0; index < second.length; index++) {
const temp: number = second[index as number];
if (!intial[temp as number]) {
intial[temp as number] = true;
result.push(temp);
}
}
return result;
}
/**
* Calculates and sets the X and Y values for mouse interactions based on the data point and chart configuration.
*
* @param {PointData} data - The data point information containing the x and y values.
* @param {Chart} chart - The chart instance for which the mouse values are being calculated.
* @param {Tooltip | Crosshair} interactionElement - The interaction element instance to set the calculated mouse values.
* @returns {void}
* @private
*/
public findMouseValues(data: PointData, chart: Chart, interactionElement: Tooltip | Crosshair): void {
if (!chart.requireInvertedAxis) {
if (chart.chartAreaType === 'PolarRadar') {
interactionElement.valueX = valueToPolarCoefficient(data.point.xValue, data.series.xAxis) * data.series.xAxis.rect.width
+ data.series.xAxis.rect.x;
} else {
interactionElement.valueX = (data.series.category === 'TrendLine' && chart.tooltip.shared) ? interactionElement.valueX :
valueToCoefficient(data.point.xValue, data.series.xAxis) * data.series.xAxis.rect.width
+ data.series.xAxis.rect.x;
}
interactionElement.valueY = chart.mouseY;
if (chart.crosshair.snapToData) {
if (data.series.type === 'BoxAndWhisker' && data.point.regions.length) {
interactionElement.valueY = data.point.regions[0].y + data.series.clipRect.y;
} else {
interactionElement.valueY = data.point.symbolLocations[0].y + data.series.clipRect.y;
}
}
} else {
interactionElement.valueY = (1 - valueToCoefficient(data.point.xValue, data.series.xAxis)) * data.series.xAxis.rect.height
+ data.series.xAxis.rect.y;
interactionElement.valueX = chart.mouseX;
if (chart.crosshair.snapToData) {
if (data.series.type === 'BoxAndWhisker' && data.point.regions.length) {
interactionElement.valueX = data.point.regions[0].x + data.series.clipRect.x;
} else {
if (!chart.tooltip.shared) {
interactionElement.valueX = data.point.symbolLocations[0].x + data.series.clipRect.x;
} else {
chart.crosshairModule.valueX = data.point.symbolLocations[0].x + data.series.clipRect.x;
}
}
}
}
}
}