react-native-gifted-charts 1.3.33 → 1.4.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.
Files changed (31) hide show
  1. package/README.md +9 -2
  2. package/package.json +2 -1
  3. package/src/BarChart/Animated2DWithGradient.tsx +13 -154
  4. package/src/BarChart/RenderBars.tsx +29 -179
  5. package/src/BarChart/RenderStackBars.tsx +22 -104
  6. package/src/BarChart/index.tsx +87 -686
  7. package/src/Components/AnimatedThreeDBar/index.tsx +16 -48
  8. package/src/Components/BarAndLineChartsWrapper/index.tsx +38 -283
  9. package/src/Components/BarAndLineChartsWrapper/renderHorizSections.tsx +17 -339
  10. package/src/Components/BarAndLineChartsWrapper/renderLineInBarChart/index.tsx +147 -0
  11. package/src/Components/BarAndLineChartsWrapper/renderLineInBarChart/renderDataPoints.tsx +157 -0
  12. package/src/Components/BarAndLineChartsWrapper/renderLineInBarChart/renderSpecificDataPoints.tsx +86 -0
  13. package/src/Components/BarAndLineChartsWrapper/renderLineInBarChart/renderSpecificVerticalLines.tsx +42 -0
  14. package/src/Components/BarAndLineChartsWrapper/renderVerticalLines.tsx +1 -1
  15. package/src/Components/BarSpecificComponents/cap.tsx +1 -1
  16. package/src/Components/common/StripAndLabel.tsx +3 -56
  17. package/src/Components/lineSvg.tsx +1 -1
  18. package/src/LineChart/LineChartBicolor.tsx +80 -516
  19. package/src/LineChart/index.tsx +266 -1778
  20. package/src/PieChart/index.tsx +20 -84
  21. package/src/PieChart/main.tsx +47 -119
  22. package/src/PopulationPyramid/index.tsx +90 -203
  23. package/src/index.tsx +2 -14
  24. package/src/BarChart/types.ts +0 -394
  25. package/src/Components/BarAndLineChartsWrapper/renderLineInBarChart.tsx +0 -402
  26. package/src/LineChart/types.ts +0 -575
  27. package/src/PieChart/types.ts +0 -77
  28. package/src/PopulationPyramid/types.ts +0 -200
  29. package/src/utils/constants.ts +0 -333
  30. package/src/utils/index.tsx +0 -1137
  31. package/src/utils/types.ts +0 -360
@@ -1,1137 +0,0 @@
1
- import {
2
- RANGE_ENTER,
3
- RANGE_EXIT,
4
- STOP,
5
- defaultLineConfig,
6
- loc,
7
- } from './constants';
8
- import {arrowConfigType, CurveType, LineProperties, LineSegment} from './types';
9
-
10
- const versionString = require('react-native/package.json').version;
11
-
12
- const versionAr = versionString?.split?.('.') ?? '';
13
- const msb = Number(versionAr[0]);
14
- const mid = Number(versionAr[1]);
15
- const lsb = Number(versionAr[2]);
16
-
17
- export const rnVersion =
18
- (!isNaN(msb) ? msb : 0) * 1000000 +
19
- (!isNaN(mid) ? mid : 0) * 10000 +
20
- (!isNaN(lsb) ? lsb : 0);
21
-
22
- export const getCumulativeWidth = (
23
- data: any,
24
- index: number,
25
- spacing: number,
26
- ) => {
27
- let cumWidth = 0;
28
- for (let i = 0; i < index; i++) {
29
- let {barWidth} = data[i];
30
- barWidth = barWidth || 30;
31
- cumWidth += barWidth + (spacing ?? 20);
32
- }
33
- return cumWidth;
34
- };
35
-
36
- export const getLighterColor = (color: String) => {
37
- let r,
38
- g,
39
- b,
40
- lighter = '#';
41
- if (color.startsWith('#')) {
42
- if (color.length < 7) {
43
- r = parseInt(color[1], 16);
44
- g = parseInt(color[2], 16);
45
- b = parseInt(color[3], 16);
46
- if (r < 14) {
47
- r += 2;
48
- lighter += r.toString(16);
49
- }
50
- if (g < 14) {
51
- g += 2;
52
- lighter += g.toString(16);
53
- }
54
- if (b < 14) {
55
- b += 2;
56
- lighter += b.toString(16);
57
- }
58
- } else {
59
- r = parseInt(color[1] + color[2], 16);
60
- g = parseInt(color[3] + color[4], 16);
61
- b = parseInt(color[5] + color[6], 16);
62
-
63
- if (r < 224) {
64
- r += 32;
65
- lighter += r.toString(16);
66
- }
67
- if (g < 224) {
68
- g += 32;
69
- lighter += g.toString(16);
70
- }
71
- if (b < 224) {
72
- b += 32;
73
- lighter += b.toString(16);
74
- }
75
- }
76
- }
77
- return lighter;
78
- };
79
-
80
- export const svgQuadraticCurvePath = points => {
81
- let path = 'M' + points[0][0] + ',' + points[0][1];
82
-
83
- for (let i = 0; i < points.length - 1; i++) {
84
- const xMid = (points[i][0] + points[i + 1][0]) / 2;
85
- const yMid = (points[i][1] + points[i + 1][1]) / 2;
86
- const cpX1 = (xMid + points[i][0]) / 2;
87
- const cpX2 = (xMid + points[i + 1][0]) / 2;
88
- path +=
89
- 'Q ' +
90
- cpX1 +
91
- ', ' +
92
- points[i][1] +
93
- ', ' +
94
- xMid +
95
- ', ' +
96
- yMid +
97
- (' Q ' +
98
- cpX2 +
99
- ', ' +
100
- points[i + 1][1] +
101
- ', ' +
102
- points[i + 1][0] +
103
- ', ' +
104
- points[i + 1][1]);
105
- }
106
-
107
- return path;
108
- };
109
-
110
- export const svgPath = (
111
- points: Array<Array<number>>,
112
- curveType: CurveType,
113
- curvature: number,
114
- ) => {
115
- if (!points?.length) return '';
116
- if (curveType === CurveType.QUADRATIC) {
117
- return svgQuadraticCurvePath(points);
118
- }
119
- // build the d attributes by looping over the points
120
- const d = points.reduce(
121
- (acc, point, i, a) =>
122
- i === 0
123
- ? // if first point
124
- `M${point[0]},${point[1]}`
125
- : // else
126
- `${acc} ${bezierCommand(point, i, a, curvature)}`,
127
- '',
128
- );
129
- return d;
130
- };
131
-
132
- const line = (pointA: Array<number>, pointB: Array<number>) => {
133
- const lengthX = pointB[0] - pointA[0];
134
- const lengthY = pointB[1] - pointA[1];
135
- return {
136
- length: Math.sqrt(Math.pow(lengthX, 2) + Math.pow(lengthY, 2)),
137
- angle: Math.atan2(lengthY, lengthX),
138
- };
139
- };
140
-
141
- const controlPoint = (
142
- curvature: number,
143
- current: Array<number>,
144
- previous: Array<number>,
145
- next: Array<number>,
146
- reverse?: any,
147
- ) => {
148
- // When 'current' is the first or last point of the array
149
- // 'previous' or 'next' don't exist.
150
- // Replace with 'current'
151
- const p = previous || current;
152
- const n = next || current;
153
- // The smoothing ratio
154
- const smoothing = curvature;
155
- // Properties of the opposed-line
156
- const o = line(p, n);
157
- // If is end-control-point, add PI to the angle to go backward
158
- const angle = o.angle + (reverse ? Math.PI : 0);
159
- const length = o.length * smoothing;
160
- // The control point position is relative to the current point
161
- const x = current[0] + Math.cos(angle) * length;
162
- const y = current[1] + Math.sin(angle) * length;
163
- return [x, y];
164
- };
165
-
166
- export const bezierCommand = (
167
- point: Array<number>,
168
- i: number,
169
- a: Array<Array<number>>,
170
- curvature: number,
171
- ) => {
172
- // start control point
173
- const [cpsX, cpsY] = controlPoint(curvature, a[i - 1], a[i - 2], point);
174
- // end control point
175
- const [cpeX, cpeY] = controlPoint(curvature, point, a[i - 1], a[i + 1], true);
176
- return `C${cpsX},${cpsY} ${cpeX},${cpeY} ${point[0]},${point[1]}`;
177
- };
178
-
179
- export const getSegmentString = (
180
- lineSegment,
181
- index,
182
- startDelimeter,
183
- endDelimeter,
184
- ) => {
185
- const segment = lineSegment?.find(segment => segment.startIndex === index);
186
- return segment ? startDelimeter + JSON.stringify(segment) + endDelimeter : '';
187
- };
188
-
189
- export const getCurvePathWithSegments = (
190
- path: string,
191
- lineSegment: LineSegment[] | undefined,
192
- startDelimeter,
193
- endDelimeter,
194
- ) => {
195
- if (!lineSegment?.length) return path;
196
- let newPath = '';
197
- const pathArray = path.split('C');
198
- for (let i = 0; i < pathArray.length; i++) {
199
- const segment = lineSegment?.find(segment => segment.startIndex === i);
200
- newPath +=
201
- (pathArray[i].startsWith('M') ? '' : 'C') +
202
- pathArray[i] +
203
- (segment ? startDelimeter + JSON.stringify(segment) + endDelimeter : '');
204
- }
205
- return newPath;
206
- };
207
-
208
- export const getPreviousSegmentsLastPoint = (isCurved, previousSegment) => {
209
- const prevSegmentLastPoint = isCurved
210
- ? previousSegment.substring(previousSegment.trim().lastIndexOf(' '))
211
- : previousSegment
212
- .substring(previousSegment.lastIndexOf('L'))
213
- .replace('L', 'M');
214
-
215
- return (
216
- (prevSegmentLastPoint.trim()[0] === 'M' ? '' : 'M') + prevSegmentLastPoint
217
- );
218
- };
219
-
220
- export const getPathWithHighlight = (
221
- data,
222
- i,
223
- highlightedRange,
224
- startIndex,
225
- endIndex,
226
- getX,
227
- getY,
228
- ) => {
229
- let path = '';
230
- const {from, to} = highlightedRange;
231
- const currentPointRegion =
232
- data[i].value < from ? loc.DOWN : data[i].value > to ? loc.UP : loc.IN;
233
-
234
- if (i !== endIndex) {
235
- const nextPointRegion =
236
- data[i + 1].value < from
237
- ? loc.DOWN
238
- : data[i + 1].value > to
239
- ? loc.UP
240
- : loc.IN;
241
- if (
242
- currentPointRegion !== nextPointRegion ||
243
- (i === startIndex && currentPointRegion === loc.IN)
244
- ) {
245
- const x1 = getX(i),
246
- y1 = getY(data[i].value),
247
- x2 = getX(i + 1),
248
- y2 = getY(data[i + 1].value);
249
-
250
- let m = (y2 - y1) / (x2 - x1),
251
- x,
252
- y;
253
- if (i === startIndex && currentPointRegion === loc.IN) {
254
- // If the 1st point lies IN
255
- y = y1;
256
- x = x1;
257
-
258
- path +=
259
- 'L' +
260
- x +
261
- ' ' +
262
- y +
263
- ' ' +
264
- RANGE_ENTER +
265
- JSON.stringify(highlightedRange) +
266
- STOP;
267
-
268
- if (nextPointRegion === loc.UP) {
269
- y = getY(to);
270
- x = (y - y1) / m + x1;
271
-
272
- path += 'L' + x + ' ' + y + ' ' + RANGE_EXIT;
273
- } else if (nextPointRegion === loc.DOWN) {
274
- y = getY(from);
275
- x = (y - y1) / m + x1;
276
-
277
- path += 'L' + x + ' ' + y + ' ' + RANGE_EXIT;
278
- }
279
- } else if (currentPointRegion !== nextPointRegion) {
280
- if (currentPointRegion === loc.DOWN && nextPointRegion === loc.UP) {
281
- // if current point is in DOWN and next point is in UP, then we will add 2 points to the the path
282
- y = getY(from);
283
- x = (y - y1) / m + x1;
284
-
285
- path +=
286
- 'L' +
287
- x +
288
- ' ' +
289
- y +
290
- ' ' +
291
- RANGE_ENTER +
292
- JSON.stringify(highlightedRange) +
293
- STOP;
294
- y = getY(to);
295
- x = (y - y1) / m + x1;
296
-
297
- path += 'L' + x + ' ' + y + ' ' + RANGE_EXIT;
298
- } else if (
299
- currentPointRegion === loc.UP &&
300
- nextPointRegion === loc.DOWN
301
- ) {
302
- // if current point is in UP and next point is in DOWN, then we will add 2 points to the the path
303
- y = getY(to);
304
- x = (y - y1) / m + x1;
305
-
306
- path +=
307
- 'L' +
308
- x +
309
- ' ' +
310
- y +
311
- ' ' +
312
- RANGE_ENTER +
313
- JSON.stringify(highlightedRange) +
314
- STOP;
315
- y = getY(from);
316
- x = (y - y1) / m + x1;
317
-
318
- path += 'L' + x + ' ' + y + ' ' + RANGE_EXIT;
319
- } else {
320
- if (
321
- (currentPointRegion === loc.UP && nextPointRegion === loc.IN) ||
322
- (currentPointRegion === loc.IN && nextPointRegion === loc.UP)
323
- ) {
324
- y = getY(to);
325
- } else if (
326
- (currentPointRegion === loc.IN && nextPointRegion === loc.DOWN) ||
327
- (currentPointRegion === loc.DOWN && nextPointRegion === loc.IN)
328
- ) {
329
- y = getY(from);
330
- }
331
- m = (y2 - y1) / (x2 - x1);
332
- x = (y - y1) / m + x1;
333
-
334
- const prefix =
335
- nextPointRegion === loc.IN
336
- ? RANGE_ENTER + JSON.stringify(highlightedRange) + STOP
337
- : RANGE_EXIT;
338
-
339
- path += 'L' + x + ' ' + y + ' ' + prefix;
340
- }
341
- }
342
- }
343
- } else if (currentPointRegion === loc.IN) {
344
- // If the last point lies IN, add RANGE_EXIT
345
- path += RANGE_EXIT;
346
- }
347
-
348
- return path;
349
- };
350
-
351
- export const getRegionPathObjects = (
352
- points,
353
- color,
354
- currentLineThickness,
355
- thickness,
356
- strokeDashArray,
357
- isCurved,
358
- startDelimeter,
359
- stop,
360
- endDelimeter,
361
- ) => {
362
- const ar: [any] = [{}];
363
- let tempStr = points;
364
-
365
- if (!points.startsWith(startDelimeter)) {
366
- /********************** line upto first segment *****************/
367
-
368
- const lineSvgProps: LineProperties = {
369
- d: points.substring(0, points.indexOf(startDelimeter)),
370
- color,
371
- strokeWidth: currentLineThickness || thickness,
372
- };
373
- if (strokeDashArray) {
374
- lineSvgProps.strokeDashArray = strokeDashArray;
375
- }
376
- ar.push(lineSvgProps);
377
- }
378
-
379
- while (tempStr.includes(startDelimeter)) {
380
- const startDelimeterIndex = tempStr.indexOf(startDelimeter);
381
- const stopIndex = tempStr.indexOf(stop);
382
- const endDelimeterIndex = tempStr.indexOf(endDelimeter);
383
-
384
- const segmentConfigString = tempStr.substring(
385
- startDelimeterIndex + startDelimeter.length,
386
- stopIndex,
387
- );
388
-
389
- const segmentConfig = JSON.parse(segmentConfigString);
390
-
391
- let segment = tempStr.substring(stopIndex + stop.length, endDelimeterIndex);
392
-
393
- const previousSegment = ar[ar.length - 1].d;
394
- const moveToLastPointOfPreviousSegment = getPreviousSegmentsLastPoint(
395
- isCurved,
396
- previousSegment,
397
- );
398
-
399
- /********************** segment line *****************/
400
-
401
- const lineSvgProps: LineProperties = {
402
- d: moveToLastPointOfPreviousSegment + segment,
403
- color: segmentConfig.color ?? color,
404
- strokeWidth:
405
- segmentConfig.thickness ?? (currentLineThickness || thickness),
406
- };
407
- if (segmentConfig.strokeDashArray) {
408
- lineSvgProps.strokeDashArray = segmentConfig.strokeDashArray;
409
- }
410
- ar.push(lineSvgProps);
411
-
412
- tempStr = tempStr.substring(endDelimeterIndex + endDelimeter.length);
413
-
414
- const nextDelimiterIndex = tempStr.indexOf(startDelimeter);
415
- const stringUptoNextSegment = tempStr.substring(0, nextDelimiterIndex);
416
-
417
- /********************** line upto the next segment *****************/
418
-
419
- if (
420
- nextDelimiterIndex !== -1 &&
421
- stringUptoNextSegment.indexOf(isCurved ? 'C' : 'L') !== -1
422
- ) {
423
- const previousSegment = ar[ar.length - 1].d;
424
- const moveToLastPointOfPreviousSegment = getPreviousSegmentsLastPoint(
425
- isCurved,
426
- previousSegment,
427
- );
428
- const lineSvgProps: LineProperties = {
429
- d: moveToLastPointOfPreviousSegment + ' ' + stringUptoNextSegment,
430
- color,
431
- strokeWidth: currentLineThickness || thickness,
432
- };
433
- if (strokeDashArray) {
434
- lineSvgProps.strokeDashArray = strokeDashArray;
435
- }
436
- ar.push(lineSvgProps);
437
- }
438
- }
439
-
440
- /********************** line after the last segment *****************/
441
-
442
- if (tempStr.length) {
443
- const previousSegment = ar[ar.length - 1].d;
444
- const moveToLastPointOfPreviousSegment = getPreviousSegmentsLastPoint(
445
- isCurved,
446
- previousSegment,
447
- );
448
- const lineSvgProps: LineProperties = {
449
- d: moveToLastPointOfPreviousSegment + tempStr,
450
- color,
451
- strokeWidth: currentLineThickness || thickness,
452
- };
453
- if (strokeDashArray) {
454
- lineSvgProps.strokeDashArray = strokeDashArray;
455
- }
456
- ar.push(lineSvgProps);
457
- }
458
-
459
- ar.shift();
460
- return ar;
461
- };
462
-
463
- export const getSegmentedPathObjects = (
464
- points,
465
- color,
466
- currentLineThickness,
467
- thickness,
468
- strokeDashArray,
469
- isCurved,
470
- startDelimeter,
471
- endDelimeter,
472
- ) => {
473
- const ar: [any] = [{}];
474
- let tempStr = points;
475
-
476
- if (!points.startsWith(startDelimeter)) {
477
- /********************** line upto first segment *****************/
478
-
479
- const lineSvgProps: LineProperties = {
480
- d: points.substring(0, points.indexOf(startDelimeter)),
481
- color,
482
- strokeWidth: currentLineThickness || thickness,
483
- };
484
- if (strokeDashArray) {
485
- lineSvgProps.strokeDashArray = strokeDashArray;
486
- }
487
- ar.push(lineSvgProps);
488
- }
489
-
490
- while (tempStr.includes(startDelimeter)) {
491
- const startDelimeterIndex = tempStr.indexOf(startDelimeter);
492
- const endDelimeterIndex = tempStr.indexOf(endDelimeter);
493
-
494
- const segmentConfigString = tempStr.substring(
495
- startDelimeterIndex + startDelimeter.length,
496
- endDelimeterIndex,
497
- );
498
-
499
- const segmentConfig = JSON.parse(segmentConfigString);
500
-
501
- const {startIndex, endIndex} = segmentConfig;
502
- const segmentLength = endIndex - startIndex;
503
- let segment = tempStr.substring(endDelimeterIndex + endDelimeter.length);
504
- let c = 0,
505
- s = 0,
506
- i;
507
- for (i = 0; i < segment.length; i++) {
508
- if (segment[i] === (isCurved ? 'C' : 'L')) c++;
509
- if (c === segmentLength) {
510
- if (segment[i] === ' ') s++;
511
- if (s === (isCurved ? 3 : 2)) break;
512
- }
513
- }
514
- segment = segment.substring(0, i);
515
-
516
- const previousSegment = ar[ar.length - 1].d;
517
- const moveToLastPointOfPreviousSegment = getPreviousSegmentsLastPoint(
518
- isCurved,
519
- previousSegment,
520
- );
521
-
522
- /********************** segment line *****************/
523
-
524
- const lineSvgProps: LineProperties = {
525
- d: moveToLastPointOfPreviousSegment + segment,
526
- color: segmentConfig.color ?? color,
527
- strokeWidth:
528
- segmentConfig.thickness ?? (currentLineThickness || thickness),
529
- };
530
- if (segmentConfig.strokeDashArray) {
531
- lineSvgProps.strokeDashArray = segmentConfig.strokeDashArray;
532
- }
533
- ar.push(lineSvgProps);
534
-
535
- tempStr = tempStr.substring(endDelimeterIndex + endDelimeter.length + i);
536
-
537
- const nextDelimiterIndex = tempStr.indexOf(startDelimeter);
538
- const stringUptoNextSegment = tempStr.substring(0, nextDelimiterIndex);
539
-
540
- /********************** line upto the next segment *****************/
541
-
542
- if (
543
- nextDelimiterIndex !== -1 &&
544
- stringUptoNextSegment.indexOf(isCurved ? 'C' : 'L') !== -1
545
- ) {
546
- const previousSegment = ar[ar.length - 1].d;
547
- const moveToLastPointOfPreviousSegment = getPreviousSegmentsLastPoint(
548
- isCurved,
549
- previousSegment,
550
- );
551
- const lineSvgProps: LineProperties = {
552
- d: moveToLastPointOfPreviousSegment + ' ' + stringUptoNextSegment,
553
- color,
554
- strokeWidth: currentLineThickness || thickness,
555
- };
556
- if (strokeDashArray) {
557
- lineSvgProps.strokeDashArray = strokeDashArray;
558
- }
559
- ar.push(lineSvgProps);
560
- }
561
- }
562
-
563
- /********************** line after the last segment *****************/
564
-
565
- if (tempStr.length) {
566
- const previousSegment = ar[ar.length - 1].d;
567
- const moveToLastPointOfPreviousSegment = getPreviousSegmentsLastPoint(
568
- isCurved,
569
- previousSegment,
570
- );
571
- const lineSvgProps: LineProperties = {
572
- d: moveToLastPointOfPreviousSegment + tempStr,
573
- color,
574
- strokeWidth: currentLineThickness || thickness,
575
- };
576
- if (strokeDashArray) {
577
- lineSvgProps.strokeDashArray = strokeDashArray;
578
- }
579
- ar.push(lineSvgProps);
580
- }
581
-
582
- ar.shift();
583
- return ar;
584
- };
585
-
586
- export const getArrowPoints = (
587
- arrowTipX: number,
588
- arrowTipY: number,
589
- x1: number,
590
- y1: number,
591
- arrowLength?: number,
592
- arrowWidth?: number,
593
- showArrowBase?: boolean,
594
- ) => {
595
- let dataLineSlope = (arrowTipY - y1) / (arrowTipX - x1);
596
- let d = arrowLength ?? 0;
597
- let d2 = (arrowWidth ?? 0) / 2;
598
- let interSectionX =
599
- arrowTipX - Math.sqrt((d * d) / (dataLineSlope * dataLineSlope + 1));
600
- let interSectionY = arrowTipY - dataLineSlope * (arrowTipX - interSectionX);
601
-
602
- let arrowBasex1, arrowBaseY1, arrowBaseX2, arrowBaseY2;
603
- if (dataLineSlope === 0) {
604
- arrowBasex1 = interSectionX;
605
- arrowBaseY1 = interSectionY - d2;
606
- arrowBaseX2 = interSectionX;
607
- arrowBaseY2 = interSectionY + d2;
608
- } else {
609
- let arrowBaseSlope = -1 / dataLineSlope;
610
- arrowBasex1 =
611
- interSectionX -
612
- Math.sqrt((d2 * d2) / (arrowBaseSlope * arrowBaseSlope + 1));
613
- arrowBaseY1 =
614
- interSectionY - arrowBaseSlope * (interSectionX - arrowBasex1);
615
-
616
- arrowBaseX2 =
617
- interSectionX +
618
- Math.sqrt((d2 * d2) / (arrowBaseSlope * arrowBaseSlope + 1));
619
- arrowBaseY2 =
620
- interSectionY + arrowBaseSlope * (interSectionX - arrowBasex1);
621
- }
622
- let arrowPoints = ` M${interSectionX} ${interSectionY}`;
623
- arrowPoints += ` ${showArrowBase ? 'L' : 'M'}${arrowBasex1} ${arrowBaseY1}`;
624
- arrowPoints += ` L${arrowTipX} ${arrowTipY}`;
625
- arrowPoints += ` M${interSectionX} ${interSectionY}`;
626
- arrowPoints += ` ${showArrowBase ? 'L' : 'M'}${arrowBaseX2} ${arrowBaseY2}`;
627
- arrowPoints += ` L${arrowTipX} ${arrowTipY}`;
628
-
629
- return arrowPoints;
630
- };
631
-
632
- export const getAxesAndRulesProps = (
633
- props: any,
634
- stepValue: number,
635
- maxValue?: number,
636
- ) => {
637
- const axesAndRulesProps = {
638
- yAxisSide: props.yAxisSide,
639
- yAxisLabelContainerStyle: props.yAxisLabelContainerStyle,
640
- yAxisColor: props.yAxisColor,
641
- yAxisExtraHeight: props.yAxisExtraHeight,
642
- trimYAxisAtTop: props.trimYAxisAtTop,
643
- overflowTop: props.overflowTop,
644
- yAxisThickness: props.yAxisThickness,
645
- xAxisColor: props.xAxisColor,
646
- xAxisLength: props.xAxisLength,
647
- xAxisType: props.xAxisType,
648
- xAxisTextNumberOfLines: props.xAxisTextNumberOfLines ?? 1,
649
- xAxisLabelsHeight: props.xAxisLabelsHeight,
650
- xAxisLabelsVerticalShift: props.xAxisLabelsVerticalShift,
651
- dashWidth: props.dashWidth,
652
- dashGap: props.dashGap,
653
- backgroundColor: props.backgroundColor,
654
- hideRules: props.hideRules,
655
- rulesLength: props.rulesLength,
656
- rulesType: props.rulesType,
657
- rulesThickness: props.rulesThickness,
658
- rulesColor: props.rulesColor,
659
- rulesConfigArray: props.rulesConfigArray,
660
- showYAxisIndices: props.showYAxisIndices,
661
- yAxisIndicesHeight: props.yAxisIndicesHeight,
662
- yAxisIndicesWidth: props.yAxisIndicesWidth,
663
- yAxisIndicesColor: props.yAxisIndicesColor,
664
- hideOrigin: props.hideOrigin,
665
- hideYAxisText: props.hideYAxisText,
666
- yAxisTextNumberOfLines: props.yAxisTextNumberOfLines,
667
- yAxisLabelPrefix: props.yAxisLabelPrefix,
668
- yAxisLabelSuffix: props.yAxisLabelSuffix,
669
- yAxisTextStyle: props.yAxisTextStyle,
670
-
671
- referenceLinesConfig: {
672
- showReferenceLine1: props.showReferenceLine1,
673
- referenceLine1Position: props.referenceLine1Position,
674
- referenceLine1Config: props.referenceLine1Config,
675
- showReferenceLine2: props.showReferenceLine2,
676
- referenceLine2Position: props.referenceLine2Position,
677
- referenceLine2Config: props.referenceLine2Config,
678
- showReferenceLine3: props.showReferenceLine3,
679
- referenceLine3Position: props.referenceLine3Position,
680
- referenceLine3Config: props.referenceLine3Config,
681
- referenceLinesOverChartContent: props.referenceLinesOverChartContent,
682
- },
683
-
684
- showVerticalLines: props.showVerticalLines,
685
- verticalLinesThickness: props.verticalLinesThickness,
686
- verticalLinesHeight: props.verticalLinesHeight,
687
- verticalLinesColor: props.verticalLinesColor,
688
- verticalLinesShift: props.verticalLinesShift,
689
- verticalLinesZIndex: props.verticalLinesZIndex,
690
- verticalLinesSpacing: props.verticalLinesSpacing,
691
- noOfVerticalLines: props.noOfVerticalLines,
692
-
693
- //specific to Line charts-
694
- verticalLinesUptoDataPoint: props.verticalLinesUptoDataPoint,
695
-
696
- roundToDigits: props.roundToDigits,
697
- stepValue,
698
-
699
- secondaryYAxis: props.secondaryYAxis,
700
- formatYLabel: props.formatYLabel,
701
- };
702
- if (props.secondaryYAxis && maxValue !== undefined) {
703
- axesAndRulesProps.secondaryYAxis = {...props.secondaryYAxis, maxValue};
704
- }
705
-
706
- return axesAndRulesProps;
707
- };
708
-
709
- export const getExtendedContainerHeightWithPadding = (
710
- containerHeight: number,
711
- overflowTop?: number,
712
- ) => containerHeight + (overflowTop ?? 0) + 10;
713
-
714
- export const getSecondaryDataWithOffsetIncluded = (
715
- secondaryData?: any,
716
- secondaryYAxis?: any,
717
- ) => {
718
- if (secondaryData && secondaryYAxis?.yAxisOffset) {
719
- return secondaryData?.map(item => {
720
- item.value = item.value - (secondaryYAxis?.yAxisOffset ?? 0);
721
- return item;
722
- });
723
- }
724
- return secondaryData;
725
- };
726
-
727
- export const getArrowProperty = (
728
- property: string,
729
- count: number,
730
- props: any,
731
- defaultArrowConfig: arrowConfigType,
732
- ) => {
733
- return (
734
- props[`arrowConfig${count}`]?.[`${property}`] ??
735
- props[`arrowConfig`]?.[`${property}`] ??
736
- defaultArrowConfig[`${property}`]
737
- );
738
- };
739
-
740
- export const getAllArrowProperties = (
741
- props: any,
742
- defaultArrowConfig: arrowConfigType,
743
- ) => {
744
- const arrowLength1 = getArrowProperty('length', 1, props, defaultArrowConfig);
745
- const arrowWidth1 = getArrowProperty('width', 1, props, defaultArrowConfig);
746
- const arrowStrokeWidth1 = getArrowProperty(
747
- 'strokeWidth',
748
- 1,
749
- props,
750
- defaultArrowConfig,
751
- );
752
- const arrowStrokeColor1 = getArrowProperty(
753
- 'strokeColor',
754
- 1,
755
- props,
756
- defaultArrowConfig,
757
- );
758
- const arrowFillColor1 = getArrowProperty(
759
- 'fillColor',
760
- 1,
761
- props,
762
- defaultArrowConfig,
763
- );
764
- const showArrowBase1 = getArrowProperty(
765
- 'showArrowBase',
766
- 1,
767
- props,
768
- defaultArrowConfig,
769
- );
770
-
771
- const arrowLength2 = getArrowProperty('length', 2, props, defaultArrowConfig);
772
- const arrowWidth2 = getArrowProperty('width', 2, props, defaultArrowConfig);
773
- const arrowStrokeWidth2 = getArrowProperty(
774
- 'strokeWidth',
775
- 2,
776
- props,
777
- defaultArrowConfig,
778
- );
779
- const arrowStrokeColor2 = getArrowProperty(
780
- 'strokeColor',
781
- 2,
782
- props,
783
- defaultArrowConfig,
784
- );
785
- const arrowFillColor2 = getArrowProperty(
786
- 'fillColor',
787
- 2,
788
- props,
789
- defaultArrowConfig,
790
- );
791
- const showArrowBase2 = getArrowProperty(
792
- 'showArrowBase',
793
- 2,
794
- props,
795
- defaultArrowConfig,
796
- );
797
-
798
- const arrowLength3 = getArrowProperty('length', 3, props, defaultArrowConfig);
799
- const arrowWidth3 = getArrowProperty('width', 3, props, defaultArrowConfig);
800
- const arrowStrokeWidth3 = getArrowProperty(
801
- 'strokeWidth',
802
- 3,
803
- props,
804
- defaultArrowConfig,
805
- );
806
- const arrowStrokeColor3 = getArrowProperty(
807
- 'strokeColor',
808
- 3,
809
- props,
810
- defaultArrowConfig,
811
- );
812
- const arrowFillColor3 = getArrowProperty(
813
- 'fillColor',
814
- 3,
815
- props,
816
- defaultArrowConfig,
817
- );
818
- const showArrowBase3 = getArrowProperty(
819
- 'showArrowBase',
820
- 3,
821
- props,
822
- defaultArrowConfig,
823
- );
824
-
825
- const arrowLength4 = getArrowProperty('length', 4, props, defaultArrowConfig);
826
- const arrowWidth4 = getArrowProperty('width', 4, props, defaultArrowConfig);
827
- const arrowStrokeWidth4 = getArrowProperty(
828
- 'strokeWidth',
829
- 4,
830
- props,
831
- defaultArrowConfig,
832
- );
833
- const arrowStrokeColor4 = getArrowProperty(
834
- 'strokeColor',
835
- 4,
836
- props,
837
- defaultArrowConfig,
838
- );
839
- const arrowFillColor4 = getArrowProperty(
840
- 'fillColor',
841
- 4,
842
- props,
843
- defaultArrowConfig,
844
- );
845
- const showArrowBase4 = getArrowProperty(
846
- 'showArrowBase',
847
- 4,
848
- props,
849
- defaultArrowConfig,
850
- );
851
-
852
- const arrowLength5 = getArrowProperty('length', 5, props, defaultArrowConfig);
853
- const arrowWidth5 = getArrowProperty('width', 5, props, defaultArrowConfig);
854
- const arrowStrokeWidth5 = getArrowProperty(
855
- 'strokeWidth',
856
- 5,
857
- props,
858
- defaultArrowConfig,
859
- );
860
- const arrowStrokeColor5 = getArrowProperty(
861
- 'strokeColor',
862
- 5,
863
- props,
864
- defaultArrowConfig,
865
- );
866
- const arrowFillColor5 = getArrowProperty(
867
- 'fillColor',
868
- 5,
869
- props,
870
- defaultArrowConfig,
871
- );
872
- const showArrowBase5 = getArrowProperty(
873
- 'showArrowBase',
874
- 5,
875
- props,
876
- defaultArrowConfig,
877
- );
878
-
879
- const arrowLengthsFromSet = props.dataSet?.map(
880
- item => item?.arrowConfig?.length ?? arrowLength1,
881
- );
882
- const arrowWidthsFromSet = props.dataSet?.map(
883
- item => item?.arrowConfig?.arrowWidth ?? arrowWidth1,
884
- );
885
- const arrowStrokeWidthsFromSet = props.dataSet?.map(
886
- item => item?.arrowConfig?.arrowStrokeWidth ?? arrowStrokeWidth1,
887
- );
888
- const arrowStrokeColorsFromSet = props.dataSet?.map(
889
- item => item?.arrowConfig?.arrowStrokeColor ?? arrowStrokeColor1,
890
- );
891
- const arrowFillColorsFromSet = props.dataSet?.map(
892
- item => item?.arrowConfig?.arrowFillColor ?? arrowFillColor1,
893
- );
894
- const showArrowBasesFromSet = props.dataSet?.map(
895
- item => item?.arrowConfig?.showArrowBase ?? showArrowBase1,
896
- );
897
-
898
- return {
899
- arrowLength1,
900
- arrowWidth1,
901
- arrowStrokeWidth1,
902
- arrowStrokeColor1,
903
- arrowFillColor1,
904
- showArrowBase1,
905
- arrowLength2,
906
- arrowWidth2,
907
- arrowStrokeWidth2,
908
- arrowStrokeColor2,
909
- arrowFillColor2,
910
- showArrowBase2,
911
- arrowLength3,
912
- arrowWidth3,
913
- arrowStrokeWidth3,
914
- arrowStrokeColor3,
915
- arrowFillColor3,
916
- showArrowBase3,
917
- arrowLength4,
918
- arrowWidth4,
919
- arrowStrokeWidth4,
920
- arrowStrokeColor4,
921
- arrowFillColor4,
922
- showArrowBase4,
923
- arrowLength5,
924
- arrowWidth5,
925
- arrowStrokeWidth5,
926
- arrowStrokeColor5,
927
- arrowFillColor5,
928
- showArrowBase5,
929
- arrowLengthsFromSet,
930
- arrowWidthsFromSet,
931
- arrowStrokeWidthsFromSet,
932
- arrowStrokeColorsFromSet,
933
- arrowFillColorsFromSet,
934
- showArrowBasesFromSet,
935
- };
936
- };
937
-
938
- type MaxAndMin = {
939
- maxItem: number;
940
- minItem: number;
941
- };
942
-
943
- export const maxAndMinUtil = (
944
- maxItem: number,
945
- minItem: number,
946
- roundToDigits?: number,
947
- showFractionalValues?: boolean,
948
- ): MaxAndMin => {
949
- if (showFractionalValues || roundToDigits) {
950
- maxItem *= 10 * (roundToDigits || 1);
951
- maxItem = maxItem + (10 - (maxItem % 10));
952
- maxItem /= 10 * (roundToDigits || 1);
953
- maxItem = parseFloat(maxItem.toFixed(roundToDigits || 1));
954
-
955
- if (minItem !== 0) {
956
- minItem *= 10 * (roundToDigits || 1);
957
- minItem = minItem - (10 + (minItem % 10));
958
- minItem /= 10 * (roundToDigits || 1);
959
- minItem = parseFloat(minItem.toFixed(roundToDigits || 1));
960
- }
961
- } else {
962
- maxItem = maxItem + (10 - (maxItem % 10));
963
- if (minItem !== 0) {
964
- minItem = minItem - (10 + (minItem % 10));
965
- }
966
- }
967
-
968
- return {maxItem, minItem};
969
- };
970
-
971
- export const computeMaxAndMinItems = (
972
- data: any,
973
- roundToDigits?: number,
974
- showFractionalValues?: boolean,
975
- ): MaxAndMin => {
976
- if (!data?.length) {
977
- return {maxItem: 0, minItem: 0};
978
- }
979
- let maxItem = 0,
980
- minItem = 0;
981
-
982
- data.forEach((item: any) => {
983
- if (item.value > maxItem) {
984
- maxItem = item.value;
985
- }
986
- if (item.value < minItem) {
987
- minItem = item.value;
988
- }
989
- });
990
-
991
- return maxAndMinUtil(maxItem, minItem, roundToDigits, showFractionalValues);
992
- };
993
-
994
- export const getLabelTextUtil = (
995
- val: any,
996
- index: number,
997
- showFractionalValues?: boolean,
998
- yAxisLabelTexts?: Array<string>,
999
- yAxisOffset?: number,
1000
- yAxisLabelPrefix?: string,
1001
- yAxisLabelSuffix?: string,
1002
- roundToDigits?: number,
1003
- formatYLabel?: (label: string) => string,
1004
- ) => {
1005
- let label = '';
1006
- if (
1007
- showFractionalValues ||
1008
- (yAxisLabelTexts && yAxisLabelTexts[index] !== undefined)
1009
- ) {
1010
- if (yAxisLabelTexts?.[index]) return val;
1011
- if (val) {
1012
- label = isNaN(Number(val))
1013
- ? val
1014
- : (Number(val) + (yAxisOffset ?? 0)).toFixed(roundToDigits);
1015
- } else {
1016
- label = yAxisOffset?.toString() ?? '0';
1017
- }
1018
- } else {
1019
- if (val) {
1020
- label = val.toString().split('.')[0];
1021
- label = (Number(label) + (yAxisOffset ?? 0)).toString();
1022
- } else {
1023
- label = yAxisOffset?.toString() ?? '0';
1024
- }
1025
- }
1026
-
1027
- return (
1028
- yAxisLabelPrefix +
1029
- (formatYLabel ? formatYLabel(label) : label) +
1030
- yAxisLabelSuffix
1031
- );
1032
- };
1033
-
1034
- export const getXForLineInBar = (
1035
- index: number,
1036
- firstBarWidth: number,
1037
- currentBarWidth: number,
1038
- yAxisLabelWidth: number,
1039
- lineConfig: any,
1040
- spacing: number,
1041
- ) =>
1042
- yAxisLabelWidth +
1043
- firstBarWidth / 2 +
1044
- lineConfig.initialSpacing +
1045
- (currentBarWidth + (lineConfig.spacing ?? spacing)) * index +
1046
- lineConfig.shiftX -
1047
- lineConfig.dataPointsWidth / 2 -
1048
- 4;
1049
-
1050
- export const getYForLineInBar = (value, shiftY, containerHeight, maxValue) =>
1051
- containerHeight - shiftY - (value * containerHeight) / maxValue;
1052
-
1053
- export const clone = obj => {
1054
- if (obj === null || typeof obj !== 'object' || 'isActiveClone' in obj)
1055
- return obj;
1056
-
1057
- let temp;
1058
- if (obj instanceof Date) temp = new Date(obj);
1059
- else temp = obj.constructor();
1060
-
1061
- for (let key in obj) {
1062
- if (Object.prototype.hasOwnProperty.call(obj, key)) {
1063
- obj['isActiveClone'] = null;
1064
- temp[key] = clone(obj[key]);
1065
- delete obj['isActiveClone'];
1066
- }
1067
- }
1068
- return temp;
1069
- };
1070
-
1071
- export const getLineConfigForBarChart = (lineConfig, barInitialSpacing) => {
1072
- return {
1073
- initialSpacing:
1074
- lineConfig.initialSpacing ??
1075
- barInitialSpacing ??
1076
- defaultLineConfig.initialSpacing,
1077
- spacing: lineConfig.spacing,
1078
- curved: lineConfig.curved || defaultLineConfig.curved,
1079
- curvature: lineConfig.curvature ?? defaultLineConfig.curvature,
1080
- curveType: lineConfig.curveType ?? defaultLineConfig.curveType,
1081
- isAnimated: lineConfig.isAnimated || defaultLineConfig.isAnimated,
1082
- animationDuration:
1083
- lineConfig.animationDuration || defaultLineConfig.animationDuration,
1084
- thickness: lineConfig.thickness || defaultLineConfig.thickness,
1085
- color: lineConfig.color || defaultLineConfig.color,
1086
- hideDataPoints:
1087
- lineConfig.hideDataPoints || defaultLineConfig.hideDataPoints,
1088
- dataPointsShape:
1089
- lineConfig.dataPointsShape || defaultLineConfig.dataPointsShape,
1090
- dataPointsHeight:
1091
- lineConfig.dataPointsHeight || defaultLineConfig.dataPointsHeight,
1092
- dataPointsWidth:
1093
- lineConfig.dataPointsWidth || defaultLineConfig.dataPointsWidth,
1094
- dataPointsColor:
1095
- lineConfig.dataPointsColor || defaultLineConfig.dataPointsColor,
1096
- dataPointsRadius:
1097
- lineConfig.dataPointsRadius || defaultLineConfig.dataPointsRadius,
1098
- textColor: lineConfig.textColor || defaultLineConfig.textColor,
1099
- textFontSize: lineConfig.textFontSize || defaultLineConfig.textFontSize,
1100
- textShiftX: lineConfig.textShiftX || defaultLineConfig.textShiftX,
1101
- textShiftY: lineConfig.textShiftY || defaultLineConfig.textShiftY,
1102
- shiftX: lineConfig.shiftX || defaultLineConfig.shiftX,
1103
- shiftY: lineConfig.shiftY || defaultLineConfig.shiftY,
1104
- delay: lineConfig.delay || defaultLineConfig.delay,
1105
- startIndex: lineConfig.startIndex || defaultLineConfig.startIndex,
1106
- endIndex:
1107
- lineConfig.endIndex === 0
1108
- ? 0
1109
- : lineConfig.endIndex || defaultLineConfig.endIndex,
1110
-
1111
- showArrow: lineConfig.showArrow ?? defaultLineConfig.showArrow,
1112
- arrowConfig: {
1113
- length:
1114
- lineConfig.arrowConfig?.length ?? defaultLineConfig.arrowConfig?.length,
1115
- width:
1116
- lineConfig.arrowConfig?.width ?? defaultLineConfig.arrowConfig?.width,
1117
-
1118
- strokeWidth:
1119
- lineConfig.arrowConfig?.strokeWidth ??
1120
- defaultLineConfig.arrowConfig?.strokeWidth,
1121
-
1122
- strokeColor:
1123
- lineConfig.arrowConfig?.strokeColor ??
1124
- defaultLineConfig.arrowConfig?.strokeColor,
1125
-
1126
- fillColor:
1127
- lineConfig.arrowConfig?.fillColor ??
1128
- defaultLineConfig.arrowConfig?.fillColor,
1129
-
1130
- showArrowBase:
1131
- lineConfig.arrowConfig?.showArrowBase ??
1132
- defaultLineConfig.arrowConfig?.showArrowBase,
1133
- },
1134
- customDataPoint: lineConfig.customDataPoint,
1135
- isSecondary: lineConfig.isSecondary ?? defaultLineConfig.isSecondary,
1136
- };
1137
- };