gifted-charts-core 0.1.20 → 0.1.22

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 (86) hide show
  1. package/{src → dist}/BarChart/types.d.ts +2 -1
  2. package/{src → dist}/LineChart/index.d.ts +0 -3
  3. package/{src → dist}/LineChart/index.js +5 -54
  4. package/{src → dist}/LineChart/types.d.ts +2 -1
  5. package/{src → dist}/PopulationPyramid/index.js +4 -3
  6. package/{src → dist}/PopulationPyramid/types.d.ts +3 -1
  7. package/{src → dist}/components/BarAndLineChartsWrapper/index.d.ts +1 -0
  8. package/{src → dist}/components/BarAndLineChartsWrapper/index.js +6 -4
  9. package/{src → dist}/components/common/types.d.ts +9 -8
  10. package/{src → dist}/index.d.ts +2 -1
  11. package/{src → dist}/utils/constants.d.ts +2 -0
  12. package/{src → dist}/utils/constants.js +2 -0
  13. package/{src → dist}/utils/index.js +1 -0
  14. package/{src → dist}/utils/types.d.ts +1 -0
  15. package/package.json +4 -4
  16. package/src/BarChart/Animated2DWithGradient.js.map +0 -1
  17. package/src/BarChart/Animated2DWithGradient.ts +0 -192
  18. package/src/BarChart/RenderStackBars.js.map +0 -1
  19. package/src/BarChart/RenderStackBars.ts +0 -160
  20. package/src/BarChart/index.js.map +0 -1
  21. package/src/BarChart/index.ts +0 -909
  22. package/src/BarChart/types.js.map +0 -1
  23. package/src/BarChart/types.ts +0 -666
  24. package/src/LineChart/LineChartBiColor.js.map +0 -1
  25. package/src/LineChart/LineChartBiColor.ts +0 -613
  26. package/src/LineChart/index.js.map +0 -1
  27. package/src/LineChart/index.ts +0 -2205
  28. package/src/LineChart/types.js.map +0 -1
  29. package/src/LineChart/types.ts +0 -614
  30. package/src/PieChart/index.js.map +0 -1
  31. package/src/PieChart/index.ts +0 -180
  32. package/src/PieChart/main.js.map +0 -1
  33. package/src/PieChart/main.ts +0 -173
  34. package/src/PieChart/pro.ts +0 -309
  35. package/src/PieChart/types.js.map +0 -1
  36. package/src/PieChart/types.ts +0 -105
  37. package/src/PopulationPyramid/index.js.map +0 -1
  38. package/src/PopulationPyramid/index.ts +0 -358
  39. package/src/PopulationPyramid/types.js.map +0 -1
  40. package/src/PopulationPyramid/types.ts +0 -256
  41. package/src/components/AnimatedThreeDBar/index.js.map +0 -1
  42. package/src/components/AnimatedThreeDBar/index.ts +0 -60
  43. package/src/components/BarAndLineChartsWrapper/getHorizSectionsVals.js.map +0 -1
  44. package/src/components/BarAndLineChartsWrapper/getHorizSectionsVals.ts +0 -408
  45. package/src/components/BarAndLineChartsWrapper/index.js.map +0 -1
  46. package/src/components/BarAndLineChartsWrapper/index.ts +0 -404
  47. package/src/components/common/StripAndLabel.js.map +0 -1
  48. package/src/components/common/StripAndLabel.ts +0 -80
  49. package/src/components/common/types.ts +0 -32
  50. package/src/index.ts +0 -152
  51. package/src/utils/constants.js.map +0 -1
  52. package/src/utils/constants.ts +0 -347
  53. package/src/utils/index.js.map +0 -1
  54. package/src/utils/index.ts +0 -1562
  55. package/src/utils/types.js.map +0 -1
  56. package/src/utils/types.ts +0 -436
  57. /package/{src → dist}/BarChart/Animated2DWithGradient.d.ts +0 -0
  58. /package/{src → dist}/BarChart/Animated2DWithGradient.js +0 -0
  59. /package/{src → dist}/BarChart/RenderStackBars.d.ts +0 -0
  60. /package/{src → dist}/BarChart/RenderStackBars.js +0 -0
  61. /package/{src → dist}/BarChart/index.d.ts +0 -0
  62. /package/{src → dist}/BarChart/index.js +0 -0
  63. /package/{src → dist}/BarChart/types.js +0 -0
  64. /package/{src → dist}/LineChart/LineChartBiColor.d.ts +0 -0
  65. /package/{src → dist}/LineChart/LineChartBiColor.js +0 -0
  66. /package/{src → dist}/LineChart/types.js +0 -0
  67. /package/{src → dist}/PieChart/index.d.ts +0 -0
  68. /package/{src → dist}/PieChart/index.js +0 -0
  69. /package/{src → dist}/PieChart/main.d.ts +0 -0
  70. /package/{src → dist}/PieChart/main.js +0 -0
  71. /package/{src → dist}/PieChart/pro.d.ts +0 -0
  72. /package/{src → dist}/PieChart/pro.js +0 -0
  73. /package/{src → dist}/PieChart/types.d.ts +0 -0
  74. /package/{src → dist}/PieChart/types.js +0 -0
  75. /package/{src → dist}/PopulationPyramid/index.d.ts +0 -0
  76. /package/{src → dist}/PopulationPyramid/types.js +0 -0
  77. /package/{src → dist}/components/AnimatedThreeDBar/index.d.ts +0 -0
  78. /package/{src → dist}/components/AnimatedThreeDBar/index.js +0 -0
  79. /package/{src → dist}/components/BarAndLineChartsWrapper/getHorizSectionsVals.d.ts +0 -0
  80. /package/{src → dist}/components/BarAndLineChartsWrapper/getHorizSectionsVals.js +0 -0
  81. /package/{src → dist}/components/common/StripAndLabel.d.ts +0 -0
  82. /package/{src → dist}/components/common/StripAndLabel.js +0 -0
  83. /package/{src → dist}/components/common/types.js +0 -0
  84. /package/{src → dist}/index.js +0 -0
  85. /package/{src → dist}/utils/index.d.ts +0 -0
  86. /package/{src → dist}/utils/types.js +0 -0
@@ -1,1562 +0,0 @@
1
- import { type ColorValue } from 'react-native'
2
- import {
3
- type IDataSanitisationProps,
4
- type lineDataItem
5
- } from '../LineChart/types'
6
- import {
7
- AxesAndRulesDefaults,
8
- BarDefaults,
9
- RANGE_ENTER,
10
- RANGE_EXIT,
11
- STOP,
12
- defaultCurvature,
13
- defaultLineConfig,
14
- loc
15
- } from './constants'
16
- import {
17
- type arrowConfigType,
18
- CurveType,
19
- type HighlightedRange,
20
- type LineProperties,
21
- type LineSegment,
22
- Framework,
23
- referenceConfigType
24
- } from './types'
25
- import {
26
- type lineConfigType,
27
- type BarChartPropsType,
28
- type FocusedBarConfig,
29
- type barDataItem
30
- } from '../BarChart/types'
31
- import { type extendedLineChartPropsType } from '../LineChart'
32
- import { type extendedBarChartPropsType } from '../BarChart'
33
-
34
- export const getCumulativeWidth = (
35
- data: any,
36
- index: number,
37
- spacing: number
38
- ): number => {
39
- let cumWidth = 0
40
- for (let i = 0; i < index; i++) {
41
- let { barWidth } = data[i]
42
- barWidth = barWidth ?? 30
43
- cumWidth += barWidth + (spacing ?? 20)
44
- }
45
- return cumWidth
46
- }
47
-
48
- export const getLighterColor = (color: string): string => {
49
- let r
50
- let g
51
- let b
52
- let lighter = '#'
53
- if (color.startsWith('#')) {
54
- if (color.length < 7) {
55
- r = parseInt(color[1], 16)
56
- g = parseInt(color[2], 16)
57
- b = parseInt(color[3], 16)
58
- if (r < 14) {
59
- r += 2
60
- lighter += r.toString(16)
61
- }
62
- if (g < 14) {
63
- g += 2
64
- lighter += g.toString(16)
65
- }
66
- if (b < 14) {
67
- b += 2
68
- lighter += b.toString(16)
69
- }
70
- } else {
71
- r = parseInt(color[1] + color[2], 16)
72
- g = parseInt(color[3] + color[4], 16)
73
- b = parseInt(color[5] + color[6], 16)
74
-
75
- if (r < 224) {
76
- r += 32
77
- lighter += r.toString(16)
78
- }
79
- if (g < 224) {
80
- g += 32
81
- lighter += g.toString(16)
82
- }
83
- if (b < 224) {
84
- b += 32
85
- lighter += b.toString(16)
86
- }
87
- }
88
- }
89
- return lighter
90
- }
91
-
92
- export const svgQuadraticCurvePath = (points: number[][]): string => {
93
- let path = 'M' + points[0][0] + ',' + points[0][1]
94
-
95
- for (let i = 0; i < points.length - 1; i++) {
96
- const xMid = (points[i][0] + points[i + 1][0]) / 2
97
- const yMid = (points[i][1] + points[i + 1][1]) / 2
98
- const cpX1 = (xMid + points[i][0]) / 2
99
- const cpX2 = (xMid + points[i + 1][0]) / 2
100
- path +=
101
- 'Q ' +
102
- cpX1 +
103
- ', ' +
104
- points[i][1] +
105
- ', ' +
106
- xMid +
107
- ', ' +
108
- yMid +
109
- (' Q ' +
110
- cpX2 +
111
- ', ' +
112
- points[i + 1][1] +
113
- ', ' +
114
- points[i + 1][0] +
115
- ', ' +
116
- points[i + 1][1])
117
- }
118
-
119
- return path
120
- }
121
-
122
- export const svgPath = (
123
- points: number[][],
124
- curveType?: CurveType,
125
- curvature?: number
126
- ): string => {
127
- if (!points?.length) return ''
128
- if (curveType === CurveType.QUADRATIC) {
129
- return svgQuadraticCurvePath(points)
130
- }
131
- // build the d attributes by looping over the points
132
- const d = points.reduce(
133
- (acc, point, i, a) =>
134
- i === 0
135
- ? `M${point[0]},${point[1]}`
136
- : `${acc} ${bezierCommand(point, i, a, curvature ?? defaultCurvature)}`,
137
- ''
138
- )
139
- return d
140
- }
141
- interface Iline {
142
- length: number
143
- angle: number
144
- }
145
- const line = (pointA: number[], pointB: number[]): Iline => {
146
- const lengthX = pointB[0] - pointA[0]
147
- const lengthY = pointB[1] - pointA[1]
148
- return {
149
- length: Math.sqrt(Math.pow(lengthX, 2) + Math.pow(lengthY, 2)),
150
- angle: Math.atan2(lengthY, lengthX)
151
- }
152
- }
153
-
154
- const controlPoint = (
155
- curvature: number,
156
- current: number[],
157
- previous: number[],
158
- next: number[],
159
- reverse?: any
160
- ): number[] => {
161
- // When 'current' is the first or last point of the array
162
- // 'previous' or 'next' don't exist.
163
- // Replace with 'current'
164
- const p = previous ?? current
165
- const n = next ?? current
166
- // The smoothing ratio
167
- const smoothing = curvature
168
- // Properties of the opposed-line
169
- const o = line(p, n)
170
- // If is end-control-point, add PI to the angle to go backward
171
- const angle = o.angle + (reverse ? Math.PI : 0)
172
- const length = o.length * smoothing
173
- // The control point position is relative to the current point
174
- const x = current[0] + Math.cos(angle) * length
175
- const y = current[1] + Math.sin(angle) * length
176
- return [x, y]
177
- }
178
-
179
- export const bezierCommand = (
180
- point: number[],
181
- i: number,
182
- a: number[][],
183
- curvature: number
184
- ): string => {
185
- // start control point
186
- const [cpsX, cpsY] = controlPoint(curvature, a[i - 1], a[i - 2], point)
187
- // end control point
188
- const [cpeX, cpeY] = controlPoint(curvature, point, a[i - 1], a[i + 1], true)
189
- return `C${cpsX},${cpsY} ${cpeX},${cpeY} ${point[0]},${point[1]}`
190
- }
191
-
192
- export const getSegmentString = (
193
- lineSegment: LineSegment[] | undefined,
194
- index: number,
195
- startDelimeter: string,
196
- endDelimeter: string
197
- ): string => {
198
- const segment = lineSegment?.find((segment) => segment.startIndex === index)
199
- return segment ? startDelimeter + JSON.stringify(segment) + endDelimeter : ''
200
- }
201
-
202
- export const getCurvePathWithSegments = (
203
- path: string,
204
- lineSegment: LineSegment[] | undefined,
205
- startDelimeter: string,
206
- endDelimeter: string
207
- ): string => {
208
- if (!lineSegment?.length) return path
209
- let newPath = ''
210
- const pathArray = path.split('C')
211
- for (let i = 0; i < pathArray.length; i++) {
212
- const segment = lineSegment?.find((segment) => segment.startIndex === i)
213
- newPath +=
214
- (pathArray[i].startsWith('M') ? '' : 'C') +
215
- pathArray[i] +
216
- (segment ? startDelimeter + JSON.stringify(segment) + endDelimeter : '')
217
- }
218
- return newPath
219
- }
220
-
221
- export const getPreviousSegmentsLastPoint = (
222
- isCurved: boolean,
223
- previousSegment: string
224
- ): string => {
225
- const prevSegmentLastPoint = isCurved
226
- ? previousSegment.substring(previousSegment.trim().lastIndexOf(' '))
227
- : previousSegment
228
- .substring(previousSegment.lastIndexOf('L'))
229
- .replace('L', 'M')
230
-
231
- return (
232
- (prevSegmentLastPoint.trim()[0] === 'M' ? '' : 'M') + prevSegmentLastPoint
233
- )
234
- }
235
-
236
- export const getPathWithHighlight = (
237
- data: lineDataItem[],
238
- i: number,
239
- highlightedRange: HighlightedRange,
240
- startIndex: number,
241
- endIndex: number,
242
- getX: (i: number) => number,
243
- getY: (value: number) => number
244
- ): string => {
245
- let path = ''
246
- const { from, to } = highlightedRange
247
- const currentPointRegion =
248
- data[i].value < from ? loc.DOWN : data[i].value > to ? loc.UP : loc.IN
249
-
250
- if (i !== endIndex) {
251
- const nextPointRegion =
252
- data[i + 1].value < from
253
- ? loc.DOWN
254
- : data[i + 1].value > to
255
- ? loc.UP
256
- : loc.IN
257
- if (
258
- currentPointRegion !== nextPointRegion ||
259
- (i === startIndex && currentPointRegion === loc.IN)
260
- ) {
261
- const x1 = getX(i)
262
- const y1 = getY(data[i].value)
263
- const x2 = getX(i + 1)
264
- const y2 = getY(data[i + 1].value)
265
-
266
- let m = (y2 - y1) / (x2 - x1)
267
- let x = 0
268
- let y = 0
269
- if (i === startIndex && currentPointRegion === loc.IN) {
270
- // If the 1st point lies IN
271
- y = y1
272
- x = x1
273
-
274
- path +=
275
- 'L' +
276
- x +
277
- ' ' +
278
- y +
279
- ' ' +
280
- RANGE_ENTER +
281
- JSON.stringify(highlightedRange) +
282
- STOP
283
-
284
- if (nextPointRegion === loc.UP) {
285
- y = getY(to)
286
- x = (y - y1) / m + x1
287
-
288
- path += 'L' + x + ' ' + y + ' ' + RANGE_EXIT
289
- } else if (nextPointRegion === loc.DOWN) {
290
- y = getY(from)
291
- x = (y - y1) / m + x1
292
-
293
- path += 'L' + x + ' ' + y + ' ' + RANGE_EXIT
294
- }
295
- } else if (currentPointRegion !== nextPointRegion) {
296
- if (currentPointRegion === loc.DOWN && nextPointRegion === loc.UP) {
297
- // if current point is in DOWN and next point is in UP, then we will add 2 points to the the path
298
- y = getY(from)
299
- x = (y - y1) / m + x1
300
-
301
- path +=
302
- 'L' +
303
- x +
304
- ' ' +
305
- y +
306
- ' ' +
307
- RANGE_ENTER +
308
- JSON.stringify(highlightedRange) +
309
- STOP
310
- y = getY(to)
311
- x = (y - y1) / m + x1
312
-
313
- path += 'L' + x + ' ' + y + ' ' + RANGE_EXIT
314
- } else if (
315
- currentPointRegion === loc.UP &&
316
- nextPointRegion === loc.DOWN
317
- ) {
318
- // if current point is in UP and next point is in DOWN, then we will add 2 points to the the path
319
- y = getY(to)
320
- x = (y - y1) / m + x1
321
-
322
- path +=
323
- 'L' +
324
- x +
325
- ' ' +
326
- y +
327
- ' ' +
328
- RANGE_ENTER +
329
- JSON.stringify(highlightedRange) +
330
- STOP
331
- y = getY(from)
332
- x = (y - y1) / m + x1
333
-
334
- path += 'L' + x + ' ' + y + ' ' + RANGE_EXIT
335
- } else {
336
- if (
337
- (currentPointRegion === loc.UP && nextPointRegion === loc.IN) ||
338
- (currentPointRegion === loc.IN && nextPointRegion === loc.UP)
339
- ) {
340
- y = getY(to)
341
- } else if (
342
- (currentPointRegion === loc.IN && nextPointRegion === loc.DOWN) ||
343
- (currentPointRegion === loc.DOWN && nextPointRegion === loc.IN)
344
- ) {
345
- y = getY(from)
346
- }
347
- m = (y2 - y1) / (x2 - x1)
348
- x = (y - y1) / m + x1
349
-
350
- const prefix =
351
- nextPointRegion === loc.IN
352
- ? RANGE_ENTER + JSON.stringify(highlightedRange) + STOP
353
- : RANGE_EXIT
354
-
355
- path += 'L' + x + ' ' + y + ' ' + prefix
356
- }
357
- }
358
- }
359
- } else if (currentPointRegion === loc.IN) {
360
- // If the last point lies IN, add RANGE_EXIT
361
- path += RANGE_EXIT
362
- }
363
-
364
- return path
365
- }
366
-
367
- export const getRegionPathObjects = (
368
- points: string,
369
- color: ColorValue,
370
- currentLineThickness: number,
371
- thickness: number,
372
- strokeDashArray: number[],
373
- isCurved: boolean,
374
- startDelimeter: string,
375
- stop: string,
376
- endDelimeter: string
377
- ): LineProperties[] => {
378
- const ar: [LineProperties] = [{ d: '', color: '', strokeWidth: 0 }]
379
- let tempStr = points
380
-
381
- if (!points.startsWith(startDelimeter)) {
382
- /** ******************** line upto first segment *****************/
383
-
384
- const lineSvgProps: LineProperties = {
385
- d: points.substring(0, points.indexOf(startDelimeter)),
386
- color,
387
- strokeWidth: currentLineThickness || thickness
388
- }
389
- if (strokeDashArray) {
390
- lineSvgProps.strokeDashArray = strokeDashArray
391
- }
392
- ar.push(lineSvgProps)
393
- }
394
-
395
- while (tempStr.includes(startDelimeter)) {
396
- const startDelimeterIndex = tempStr.indexOf(startDelimeter)
397
- const stopIndex = tempStr.indexOf(stop)
398
- const endDelimeterIndex = tempStr.indexOf(endDelimeter)
399
-
400
- const segmentConfigString = tempStr.substring(
401
- startDelimeterIndex + startDelimeter.length,
402
- stopIndex
403
- )
404
-
405
- const segmentConfig = JSON.parse(segmentConfigString)
406
-
407
- const segment = tempStr.substring(
408
- stopIndex + stop.length,
409
- endDelimeterIndex
410
- )
411
-
412
- const previousSegment = ar[ar.length - 1].d
413
- const moveToLastPointOfPreviousSegment = getPreviousSegmentsLastPoint(
414
- isCurved,
415
- previousSegment
416
- )
417
-
418
- /** ******************** segment line *****************/
419
-
420
- const lineSvgProps: LineProperties = {
421
- d: moveToLastPointOfPreviousSegment + segment,
422
- color: segmentConfig.color ?? color,
423
- strokeWidth:
424
- segmentConfig.thickness ?? (currentLineThickness || thickness)
425
- }
426
- if (segmentConfig.strokeDashArray) {
427
- lineSvgProps.strokeDashArray = segmentConfig.strokeDashArray
428
- }
429
- ar.push(lineSvgProps)
430
-
431
- tempStr = tempStr.substring(endDelimeterIndex + endDelimeter.length)
432
-
433
- const nextDelimiterIndex = tempStr.indexOf(startDelimeter)
434
- const stringUptoNextSegment = tempStr.substring(0, nextDelimiterIndex)
435
-
436
- /** ******************** line upto the next segment *****************/
437
-
438
- if (
439
- nextDelimiterIndex !== -1 &&
440
- stringUptoNextSegment.includes(isCurved ? 'C' : 'L')
441
- ) {
442
- const previousSegment = ar[ar.length - 1].d
443
- const moveToLastPointOfPreviousSegment = getPreviousSegmentsLastPoint(
444
- isCurved,
445
- previousSegment
446
- )
447
- const lineSvgProps: LineProperties = {
448
- d: moveToLastPointOfPreviousSegment + ' ' + stringUptoNextSegment,
449
- color,
450
- strokeWidth: currentLineThickness || thickness
451
- }
452
- if (strokeDashArray) {
453
- lineSvgProps.strokeDashArray = strokeDashArray
454
- }
455
- ar.push(lineSvgProps)
456
- }
457
- }
458
-
459
- /** ******************** line after the last segment *****************/
460
-
461
- if (tempStr.length) {
462
- const previousSegment = ar[ar.length - 1].d
463
- const moveToLastPointOfPreviousSegment = getPreviousSegmentsLastPoint(
464
- isCurved,
465
- previousSegment
466
- )
467
- const lineSvgProps: LineProperties = {
468
- d: moveToLastPointOfPreviousSegment + tempStr,
469
- color,
470
- strokeWidth: currentLineThickness || thickness
471
- }
472
- if (strokeDashArray) {
473
- lineSvgProps.strokeDashArray = strokeDashArray
474
- }
475
- ar.push(lineSvgProps)
476
- }
477
-
478
- ar.shift()
479
- return ar
480
- }
481
-
482
- export const getSegmentedPathObjects = (
483
- points: string,
484
- color: ColorValue,
485
- currentLineThickness: number,
486
- thickness: number,
487
- strokeDashArray: number[],
488
- isCurved: boolean,
489
- startDelimeter: string,
490
- endDelimeter: string
491
- ): LineProperties[] => {
492
- const ar: [LineProperties] = [{ d: '', color: '', strokeWidth: 0 }]
493
- let tempStr = points
494
-
495
- if (!points.startsWith(startDelimeter)) {
496
- /** ******************** line upto first segment *****************/
497
-
498
- const lineSvgProps: LineProperties = {
499
- d: points.substring(0, points.indexOf(startDelimeter)),
500
- color,
501
- strokeWidth: currentLineThickness || thickness
502
- }
503
- if (strokeDashArray) {
504
- lineSvgProps.strokeDashArray = strokeDashArray
505
- }
506
- ar.push(lineSvgProps)
507
- }
508
-
509
- while (tempStr.includes(startDelimeter)) {
510
- const startDelimeterIndex = tempStr.indexOf(startDelimeter)
511
- const endDelimeterIndex = tempStr.indexOf(endDelimeter)
512
-
513
- const segmentConfigString = tempStr.substring(
514
- startDelimeterIndex + startDelimeter.length,
515
- endDelimeterIndex
516
- )
517
-
518
- const segmentConfig = JSON.parse(segmentConfigString)
519
-
520
- const { startIndex, endIndex } = segmentConfig
521
- const segmentLength = endIndex - startIndex
522
- let segment = tempStr.substring(endDelimeterIndex + endDelimeter.length)
523
- let c = 0
524
- let s = 0
525
- let i
526
- for (i = 0; i < segment.length; i++) {
527
- if (segment[i] === (isCurved ? 'C' : 'L')) c++
528
- if (c === segmentLength) {
529
- if (segment[i] === ' ') s++
530
- if (s === (isCurved ? 3 : 2)) break
531
- }
532
- }
533
- segment = segment.substring(0, i)
534
-
535
- const previousSegment = ar[ar.length - 1].d
536
- const moveToLastPointOfPreviousSegment = getPreviousSegmentsLastPoint(
537
- isCurved,
538
- previousSegment
539
- )
540
-
541
- /** ******************** segment line *****************/
542
-
543
- const lineSvgProps: LineProperties = {
544
- d: moveToLastPointOfPreviousSegment + segment,
545
- color: segmentConfig.color ?? color,
546
- strokeWidth:
547
- segmentConfig.thickness ?? (currentLineThickness || thickness)
548
- }
549
- if (segmentConfig.strokeDashArray) {
550
- lineSvgProps.strokeDashArray = segmentConfig.strokeDashArray
551
- }
552
- ar.push(lineSvgProps)
553
-
554
- tempStr = tempStr.substring(endDelimeterIndex + endDelimeter.length + i)
555
-
556
- const nextDelimiterIndex = tempStr.indexOf(startDelimeter)
557
- const stringUptoNextSegment = tempStr.substring(0, nextDelimiterIndex)
558
-
559
- /** ******************** line upto the next segment *****************/
560
-
561
- if (
562
- nextDelimiterIndex !== -1 &&
563
- stringUptoNextSegment.includes(isCurved ? 'C' : 'L')
564
- ) {
565
- const previousSegment = ar[ar.length - 1].d
566
- const moveToLastPointOfPreviousSegment = getPreviousSegmentsLastPoint(
567
- isCurved,
568
- previousSegment
569
- )
570
- const lineSvgProps: LineProperties = {
571
- d: moveToLastPointOfPreviousSegment + ' ' + stringUptoNextSegment,
572
- color,
573
- strokeWidth: currentLineThickness || thickness
574
- }
575
- if (strokeDashArray) {
576
- lineSvgProps.strokeDashArray = strokeDashArray
577
- }
578
- ar.push(lineSvgProps)
579
- }
580
- }
581
-
582
- /** ******************** line after the last segment *****************/
583
-
584
- if (tempStr.length) {
585
- const previousSegment = ar[ar.length - 1].d
586
- const moveToLastPointOfPreviousSegment = getPreviousSegmentsLastPoint(
587
- isCurved,
588
- previousSegment
589
- )
590
- const lineSvgProps: LineProperties = {
591
- d: moveToLastPointOfPreviousSegment + tempStr,
592
- color,
593
- strokeWidth: currentLineThickness || thickness
594
- }
595
- if (strokeDashArray) {
596
- lineSvgProps.strokeDashArray = strokeDashArray
597
- }
598
- ar.push(lineSvgProps)
599
- }
600
-
601
- ar.shift()
602
- return ar
603
- }
604
-
605
- export const getArrowPoints = (
606
- arrowTipX: number,
607
- arrowTipY: number,
608
- x1: number,
609
- y1: number,
610
- arrowLength?: number,
611
- arrowWidth?: number,
612
- showArrowBase?: boolean
613
- ): string => {
614
- const dataLineSlope = (arrowTipY - y1) / (arrowTipX - x1)
615
- const d = arrowLength ?? 0
616
- const d2 = (arrowWidth ?? 0) / 2
617
- const interSectionX =
618
- arrowTipX - Math.sqrt((d * d) / (dataLineSlope * dataLineSlope + 1))
619
- const interSectionY = arrowTipY - dataLineSlope * (arrowTipX - interSectionX)
620
-
621
- let arrowBasex1, arrowBaseY1, arrowBaseX2, arrowBaseY2
622
- if (dataLineSlope === 0) {
623
- arrowBasex1 = interSectionX
624
- arrowBaseY1 = interSectionY - d2
625
- arrowBaseX2 = interSectionX
626
- arrowBaseY2 = interSectionY + d2
627
- } else {
628
- const arrowBaseSlope = -1 / dataLineSlope
629
- arrowBasex1 =
630
- interSectionX -
631
- Math.sqrt((d2 * d2) / (arrowBaseSlope * arrowBaseSlope + 1))
632
- arrowBaseY1 = interSectionY - arrowBaseSlope * (interSectionX - arrowBasex1)
633
-
634
- arrowBaseX2 =
635
- interSectionX +
636
- Math.sqrt((d2 * d2) / (arrowBaseSlope * arrowBaseSlope + 1))
637
- arrowBaseY2 = interSectionY + arrowBaseSlope * (interSectionX - arrowBasex1)
638
- }
639
- let arrowPoints = ` M${interSectionX} ${interSectionY}`
640
- arrowPoints += ` ${showArrowBase ? 'L' : 'M'}${arrowBasex1} ${arrowBaseY1}`
641
- arrowPoints += ` L${arrowTipX} ${arrowTipY}`
642
- arrowPoints += ` M${interSectionX} ${interSectionY}`
643
- arrowPoints += ` ${showArrowBase ? 'L' : 'M'}${arrowBaseX2} ${arrowBaseY2}`
644
- arrowPoints += ` L${arrowTipX} ${arrowTipY}`
645
-
646
- return arrowPoints
647
- }
648
-
649
- interface ReferenceLinesConfig {
650
- showReferenceLine1?: boolean
651
- referenceLine1Position?: number
652
- referenceLine1Config?: referenceConfigType
653
- showReferenceLine2?: boolean
654
- referenceLine2Position?: number
655
- referenceLine2Config?: referenceConfigType
656
- showReferenceLine3?: boolean
657
- referenceLine3Position?: number
658
- referenceLine3Config?: referenceConfigType
659
- referenceLinesOverChartContent?: boolean
660
- }
661
- interface IgetAxesAndRulesProps extends BarChartPropsType {
662
- verticalLinesUptoDataPoint?: boolean
663
- referenceLinesConfig: ReferenceLinesConfig
664
- }
665
-
666
- export const getAxesAndRulesProps = (
667
- props: extendedBarChartPropsType,
668
- stepValue: number,
669
- negativeStepValue?: number,
670
- maxValue?: number
671
- ): IgetAxesAndRulesProps => {
672
- const secondaryYAxis =
673
- !props.secondaryYAxis || props.secondaryYAxis === true
674
- ? {}
675
- : props.secondaryYAxis
676
- const axesAndRulesProps: IgetAxesAndRulesProps = {
677
- yAxisSide: props.yAxisSide,
678
- yAxisLabelContainerStyle: props.yAxisLabelContainerStyle,
679
- yAxisColor: props.yAxisColor,
680
- yAxisExtraHeight: props.yAxisExtraHeight,
681
- trimYAxisAtTop: props.trimYAxisAtTop,
682
- overflowTop:
683
- props.overflowTop ?? props.secondaryXAxis
684
- ? AxesAndRulesDefaults.overflowTopWithSecondaryXAxis
685
- : AxesAndRulesDefaults.overflowTop,
686
- yAxisThickness: props.yAxisThickness,
687
- xAxisColor: props.xAxisColor,
688
- xAxisLength: props.xAxisLength,
689
- xAxisType: props.xAxisType,
690
- xAxisTextNumberOfLines: props.xAxisTextNumberOfLines ?? 1,
691
- xAxisThickness: props.xAxisThickness ?? AxesAndRulesDefaults.xAxisThickness,
692
- xAxisLabelsHeight: props.xAxisLabelsHeight,
693
- xAxisLabelsVerticalShift: props.xAxisLabelsVerticalShift,
694
- dashWidth: props.dashWidth,
695
- dashGap: props.dashGap,
696
- backgroundColor: props.backgroundColor,
697
- hideRules: props.hideRules,
698
- rulesLength: props.rulesLength,
699
- rulesType: props.rulesType,
700
- rulesThickness: props.rulesThickness,
701
- rulesColor: props.rulesColor,
702
- rulesConfigArray: props.rulesConfigArray,
703
- showYAxisIndices: props.showYAxisIndices,
704
- yAxisIndicesHeight: props.yAxisIndicesHeight,
705
- yAxisIndicesWidth: props.yAxisIndicesWidth,
706
- yAxisIndicesColor: props.yAxisIndicesColor,
707
- hideOrigin: props.hideOrigin,
708
- hideYAxisText: props.hideYAxisText,
709
- yAxisTextNumberOfLines: props.yAxisTextNumberOfLines,
710
- yAxisLabelPrefix: props.yAxisLabelPrefix,
711
- yAxisLabelSuffix: props.yAxisLabelSuffix,
712
- yAxisTextStyle: props.yAxisTextStyle,
713
-
714
- referenceLinesConfig: {
715
- showReferenceLine1: props.showReferenceLine1,
716
- referenceLine1Position: props.referenceLine1Position,
717
- referenceLine1Config: props.referenceLine1Config,
718
- showReferenceLine2: props.showReferenceLine2,
719
- referenceLine2Position: props.referenceLine2Position,
720
- referenceLine2Config: props.referenceLine2Config,
721
- showReferenceLine3: props.showReferenceLine3,
722
- referenceLine3Position: props.referenceLine3Position,
723
- referenceLine3Config: props.referenceLine3Config,
724
- referenceLinesOverChartContent: props.referenceLinesOverChartContent
725
- },
726
-
727
- showVerticalLines: props.showVerticalLines,
728
- verticalLinesThickness: props.verticalLinesThickness,
729
- verticalLinesHeight: props.verticalLinesHeight,
730
- verticalLinesColor: props.verticalLinesColor,
731
- verticalLinesShift: props.verticalLinesShift,
732
- verticalLinesStrokeDashArray: props.verticalLinesStrokeDashArray,
733
- verticalLinesZIndex: props.verticalLinesZIndex,
734
- verticalLinesSpacing: props.verticalLinesSpacing,
735
- noOfVerticalLines: props.noOfVerticalLines,
736
-
737
- // specific to Line charts-
738
- verticalLinesUptoDataPoint: props.verticalLinesUptoDataPoint,
739
-
740
- roundToDigits: props.roundToDigits,
741
- stepValue,
742
- negativeStepValue: negativeStepValue ?? stepValue,
743
-
744
- secondaryYAxis: props.secondaryYAxis,
745
- formatYLabel: props.formatYLabel,
746
- secondaryXAxis: props.secondaryXAxis
747
- }
748
- if (
749
- (props.secondaryYAxis ?? props.lineConfig?.isSecondary) &&
750
- maxValue !== undefined &&
751
- secondaryYAxis &&
752
- secondaryYAxis.maxValue === undefined
753
- ) {
754
- axesAndRulesProps.secondaryYAxis = { ...secondaryYAxis, maxValue }
755
- }
756
-
757
- return axesAndRulesProps
758
- }
759
-
760
- export const getExtendedContainerHeightWithPadding = (
761
- containerHeight: number,
762
- overflowTop?: number
763
- ): number => containerHeight + (overflowTop ?? 0) + 10
764
-
765
- export const getSecondaryDataWithOffsetIncluded = (
766
- secondaryData?: barDataItem[] | lineDataItem[],
767
- secondaryYAxis?: any | undefined,
768
- showDataPointsForMissingValues?: boolean,
769
- interpolateMissingValues?: boolean,
770
- onlyPositive?: boolean
771
- ): barDataItem[] | lineDataItem[] | undefined => {
772
- if (!secondaryData) return secondaryData
773
- const nullishHandledData = getInterpolatedData(
774
- secondaryData,
775
- showDataPointsForMissingValues,
776
- interpolateMissingValues,
777
- onlyPositive
778
- )
779
- if (secondaryYAxis?.yAxisOffset) {
780
- return nullishHandledData.map((item) => {
781
- item.value = item.value - (secondaryYAxis?.yAxisOffset ?? 0)
782
- return item
783
- })
784
- }
785
- return nullishHandledData
786
- }
787
-
788
- export const getArrowProperty = (
789
- property: string,
790
- count: number,
791
- props: extendedLineChartPropsType,
792
- defaultArrowConfig: arrowConfigType
793
- ): any => {
794
- const arrowNumber = `arrowConfig${count}` as keyof extendedLineChartPropsType
795
- return (
796
- props[arrowNumber]?.[property] ??
797
- props['arrowConfig' as keyof extendedLineChartPropsType]?.[property] ??
798
- defaultArrowConfig[property as keyof arrowConfigType]
799
- )
800
- }
801
-
802
- interface IgetAllArrowProperties {
803
- arrowLength1: number
804
- arrowWidth1: number
805
- arrowStrokeWidth1: number
806
- arrowStrokeColor1: ColorValue
807
- arrowFillColor1: ColorValue
808
- showArrowBase1: boolean
809
- arrowLength2: number
810
- arrowWidth2: number
811
- arrowStrokeWidth2: number
812
- arrowStrokeColor2: ColorValue
813
- arrowFillColor2: ColorValue
814
- showArrowBase2: boolean
815
- arrowLength3: number
816
- arrowWidth3: number
817
- arrowStrokeWidth3: number
818
- arrowStrokeColor3: ColorValue
819
- arrowFillColor3: ColorValue
820
- showArrowBase3: boolean
821
- arrowLength4: number
822
- arrowWidth4: number
823
- arrowStrokeWidth4: number
824
- arrowStrokeColor4: ColorValue
825
- arrowFillColor4: ColorValue
826
- showArrowBase4: boolean
827
- arrowLength5: number
828
- arrowWidth5: number
829
- arrowStrokeWidth5: number
830
- arrowStrokeColor5: ColorValue
831
- arrowFillColor5: ColorValue
832
- showArrowBase5: boolean
833
- arrowLengthsFromSet?: number[]
834
- arrowWidthsFromSet?: number[]
835
- arrowStrokeWidthsFromSet?: number[]
836
- arrowStrokeColorsFromSet?: ColorValue[]
837
- arrowFillColorsFromSet?: ColorValue[]
838
- showArrowBasesFromSet?: boolean[]
839
- }
840
-
841
- export const getAllArrowProperties = (
842
- props: extendedLineChartPropsType,
843
- defaultArrowConfig: arrowConfigType
844
- ): IgetAllArrowProperties => {
845
- const arrowLength1 = getArrowProperty('length', 1, props, defaultArrowConfig)
846
- const arrowWidth1 = getArrowProperty('width', 1, props, defaultArrowConfig)
847
- const arrowStrokeWidth1 = getArrowProperty(
848
- 'strokeWidth',
849
- 1,
850
- props,
851
- defaultArrowConfig
852
- )
853
- const arrowStrokeColor1 = getArrowProperty(
854
- 'strokeColor',
855
- 1,
856
- props,
857
- defaultArrowConfig
858
- )
859
- const arrowFillColor1 = getArrowProperty(
860
- 'fillColor',
861
- 1,
862
- props,
863
- defaultArrowConfig
864
- )
865
- const showArrowBase1 = getArrowProperty(
866
- 'showArrowBase',
867
- 1,
868
- props,
869
- defaultArrowConfig
870
- )
871
-
872
- const arrowLength2 = getArrowProperty('length', 2, props, defaultArrowConfig)
873
- const arrowWidth2 = getArrowProperty('width', 2, props, defaultArrowConfig)
874
- const arrowStrokeWidth2 = getArrowProperty(
875
- 'strokeWidth',
876
- 2,
877
- props,
878
- defaultArrowConfig
879
- )
880
- const arrowStrokeColor2 = getArrowProperty(
881
- 'strokeColor',
882
- 2,
883
- props,
884
- defaultArrowConfig
885
- )
886
- const arrowFillColor2 = getArrowProperty(
887
- 'fillColor',
888
- 2,
889
- props,
890
- defaultArrowConfig
891
- )
892
- const showArrowBase2 = getArrowProperty(
893
- 'showArrowBase',
894
- 2,
895
- props,
896
- defaultArrowConfig
897
- )
898
-
899
- const arrowLength3 = getArrowProperty('length', 3, props, defaultArrowConfig)
900
- const arrowWidth3 = getArrowProperty('width', 3, props, defaultArrowConfig)
901
- const arrowStrokeWidth3 = getArrowProperty(
902
- 'strokeWidth',
903
- 3,
904
- props,
905
- defaultArrowConfig
906
- )
907
- const arrowStrokeColor3 = getArrowProperty(
908
- 'strokeColor',
909
- 3,
910
- props,
911
- defaultArrowConfig
912
- )
913
- const arrowFillColor3 = getArrowProperty(
914
- 'fillColor',
915
- 3,
916
- props,
917
- defaultArrowConfig
918
- )
919
- const showArrowBase3 = getArrowProperty(
920
- 'showArrowBase',
921
- 3,
922
- props,
923
- defaultArrowConfig
924
- )
925
-
926
- const arrowLength4 = getArrowProperty('length', 4, props, defaultArrowConfig)
927
- const arrowWidth4 = getArrowProperty('width', 4, props, defaultArrowConfig)
928
- const arrowStrokeWidth4 = getArrowProperty(
929
- 'strokeWidth',
930
- 4,
931
- props,
932
- defaultArrowConfig
933
- )
934
- const arrowStrokeColor4 = getArrowProperty(
935
- 'strokeColor',
936
- 4,
937
- props,
938
- defaultArrowConfig
939
- )
940
- const arrowFillColor4 = getArrowProperty(
941
- 'fillColor',
942
- 4,
943
- props,
944
- defaultArrowConfig
945
- )
946
- const showArrowBase4 = getArrowProperty(
947
- 'showArrowBase',
948
- 4,
949
- props,
950
- defaultArrowConfig
951
- )
952
-
953
- const arrowLength5 = getArrowProperty('length', 5, props, defaultArrowConfig)
954
- const arrowWidth5 = getArrowProperty('width', 5, props, defaultArrowConfig)
955
- const arrowStrokeWidth5 = getArrowProperty(
956
- 'strokeWidth',
957
- 5,
958
- props,
959
- defaultArrowConfig
960
- )
961
- const arrowStrokeColor5 = getArrowProperty(
962
- 'strokeColor',
963
- 5,
964
- props,
965
- defaultArrowConfig
966
- )
967
- const arrowFillColor5 = getArrowProperty(
968
- 'fillColor',
969
- 5,
970
- props,
971
- defaultArrowConfig
972
- )
973
- const showArrowBase5 = getArrowProperty(
974
- 'showArrowBase',
975
- 5,
976
- props,
977
- defaultArrowConfig
978
- )
979
-
980
- const arrowLengthsFromSet = props.dataSet?.map(
981
- (item) => item?.arrowConfig?.length ?? arrowLength1
982
- )
983
- const arrowWidthsFromSet = props.dataSet?.map(
984
- (item) => item?.arrowConfig?.width ?? arrowWidth1
985
- )
986
- const arrowStrokeWidthsFromSet = props.dataSet?.map(
987
- (item) => item?.arrowConfig?.strokeWidth ?? arrowStrokeWidth1
988
- )
989
- const arrowStrokeColorsFromSet = props.dataSet?.map(
990
- (item) => item?.arrowConfig?.strokeColor ?? arrowStrokeColor1
991
- )
992
- const arrowFillColorsFromSet = props.dataSet?.map(
993
- (item) => item?.arrowConfig?.fillColor ?? arrowFillColor1
994
- )
995
- const showArrowBasesFromSet = props.dataSet?.map(
996
- (item) => item?.arrowConfig?.showArrowBase ?? showArrowBase1
997
- )
998
-
999
- return {
1000
- arrowLength1,
1001
- arrowWidth1,
1002
- arrowStrokeWidth1,
1003
- arrowStrokeColor1,
1004
- arrowFillColor1,
1005
- showArrowBase1,
1006
- arrowLength2,
1007
- arrowWidth2,
1008
- arrowStrokeWidth2,
1009
- arrowStrokeColor2,
1010
- arrowFillColor2,
1011
- showArrowBase2,
1012
- arrowLength3,
1013
- arrowWidth3,
1014
- arrowStrokeWidth3,
1015
- arrowStrokeColor3,
1016
- arrowFillColor3,
1017
- showArrowBase3,
1018
- arrowLength4,
1019
- arrowWidth4,
1020
- arrowStrokeWidth4,
1021
- arrowStrokeColor4,
1022
- arrowFillColor4,
1023
- showArrowBase4,
1024
- arrowLength5,
1025
- arrowWidth5,
1026
- arrowStrokeWidth5,
1027
- arrowStrokeColor5,
1028
- arrowFillColor5,
1029
- showArrowBase5,
1030
- arrowLengthsFromSet,
1031
- arrowWidthsFromSet,
1032
- arrowStrokeWidthsFromSet,
1033
- arrowStrokeColorsFromSet,
1034
- arrowFillColorsFromSet,
1035
- showArrowBasesFromSet
1036
- }
1037
- }
1038
-
1039
- interface MaxAndMin {
1040
- maxItem: number
1041
- minItem: number
1042
- }
1043
-
1044
- export const maxAndMinUtil = (
1045
- maxItem: number,
1046
- minItem: number,
1047
- roundToDigits?: number,
1048
- showFractionalValues?: boolean
1049
- ): MaxAndMin => {
1050
- if (showFractionalValues ?? roundToDigits) {
1051
- maxItem *= 10 * (roundToDigits ?? 1)
1052
- maxItem = maxItem + (10 - (maxItem % 10))
1053
- maxItem /= 10 * (roundToDigits ?? 1)
1054
- maxItem = parseFloat(maxItem.toFixed(roundToDigits ?? 1))
1055
-
1056
- if (minItem !== 0) {
1057
- minItem *= 10 * (roundToDigits ?? 1)
1058
- minItem = minItem - (10 + (minItem % 10))
1059
- minItem /= 10 * (roundToDigits ?? 1)
1060
- minItem = parseFloat(minItem.toFixed(roundToDigits ?? 1))
1061
- }
1062
- } else {
1063
- maxItem = maxItem + (10 - (maxItem % 10))
1064
- if (minItem !== 0) {
1065
- minItem *= 10
1066
- minItem = minItem - (10 + (minItem % 10))
1067
- minItem /= 10
1068
- }
1069
- }
1070
-
1071
- return { maxItem, minItem }
1072
- }
1073
-
1074
- export const computeMaxAndMinItems = (
1075
- data: any[] | undefined,
1076
- roundToDigits?: number,
1077
- showFractionalValues?: boolean
1078
- ): MaxAndMin => {
1079
- if (!data?.length) {
1080
- return { maxItem: 0, minItem: 0 }
1081
- }
1082
- let maxItem = 0
1083
- let minItem = 0
1084
-
1085
- data.forEach((item: any) => {
1086
- if (item.value > maxItem) {
1087
- maxItem = item.value
1088
- }
1089
- if (item.value < minItem) {
1090
- minItem = item.value
1091
- }
1092
- })
1093
-
1094
- return maxAndMinUtil(maxItem, minItem, roundToDigits, showFractionalValues)
1095
- }
1096
-
1097
- export const getLabelTextUtil = (
1098
- val: string,
1099
- index: number,
1100
- showFractionalValues?: boolean,
1101
- yAxisLabelTexts?: string[],
1102
- yAxisOffset?: number,
1103
- yAxisLabelPrefix?: string,
1104
- yAxisLabelSuffix?: string,
1105
- roundToDigits?: number,
1106
- formatYLabel?: (label: string) => string
1107
- ): string => {
1108
- let label = ''
1109
- if (showFractionalValues || yAxisLabelTexts?.[index] !== undefined) {
1110
- if (yAxisLabelTexts?.[index]) return val
1111
- if (val) {
1112
- label = isNaN(Number(val))
1113
- ? val
1114
- : (Number(val) + (yAxisOffset ?? 0)).toFixed(roundToDigits)
1115
- } else {
1116
- label = yAxisOffset?.toString() ?? '0'
1117
- }
1118
- } else {
1119
- if (val) {
1120
- label = val.toString().split('.')[0]
1121
- label = (Number(label) + (yAxisOffset ?? 0)).toString()
1122
- } else {
1123
- label = yAxisOffset?.toString() ?? '0'
1124
- }
1125
- }
1126
-
1127
- return (
1128
- yAxisLabelPrefix +
1129
- (formatYLabel ? formatYLabel(label) : label) +
1130
- yAxisLabelSuffix
1131
- )
1132
- }
1133
-
1134
- export const getXForLineInBar = (
1135
- index: number,
1136
- firstBarWidth: number,
1137
- currentBarWidth: number,
1138
- yAxisLabelWidth: number,
1139
- lineConfig: any,
1140
- spacing: number
1141
- ): number =>
1142
- yAxisLabelWidth +
1143
- firstBarWidth / 2 +
1144
- lineConfig.initialSpacing +
1145
- (currentBarWidth + (lineConfig.spacing ?? spacing)) * index +
1146
- lineConfig.shiftX -
1147
- lineConfig.dataPointsWidth / 2 -
1148
- 4
1149
-
1150
- export const getYForLineInBar = (
1151
- value: number | undefined,
1152
- shiftY: number | undefined,
1153
- containerHeight: number,
1154
- maxValue: number
1155
- ): number =>
1156
- containerHeight - (shiftY ?? 0) - ((value ?? 0) * containerHeight) / maxValue
1157
-
1158
- export const clone = (obj: any): any => {
1159
- if (obj === null || typeof obj !== 'object' || 'isActiveClone' in obj) {
1160
- return obj
1161
- }
1162
-
1163
- let temp
1164
- if (obj instanceof Date) temp = new Date(obj)
1165
- else temp = obj.constructor()
1166
-
1167
- for (const key in obj) {
1168
- if (Object.prototype.hasOwnProperty.call(obj, key)) {
1169
- obj.isActiveClone = null
1170
- temp[key] = clone(obj[key])
1171
- delete obj.isActiveClone
1172
- }
1173
- }
1174
- return temp
1175
- }
1176
-
1177
- export const getLineConfigForBarChart = (
1178
- lineConfig: lineConfigType,
1179
- barInitialSpacing: number
1180
- ): lineConfigType => {
1181
- return {
1182
- initialSpacing:
1183
- lineConfig.initialSpacing ??
1184
- barInitialSpacing ??
1185
- defaultLineConfig.initialSpacing,
1186
- spacing: lineConfig.spacing,
1187
- curved: lineConfig.curved ?? defaultLineConfig.curved,
1188
- curvature: lineConfig.curvature ?? defaultLineConfig.curvature,
1189
- curveType: lineConfig.curveType ?? defaultLineConfig.curveType,
1190
- isAnimated: lineConfig.isAnimated ?? defaultLineConfig.isAnimated,
1191
- animationDuration:
1192
- lineConfig.animationDuration ?? defaultLineConfig.animationDuration,
1193
- thickness: lineConfig.thickness ?? defaultLineConfig.thickness,
1194
- color: lineConfig.color ?? defaultLineConfig.color,
1195
- hideDataPoints:
1196
- lineConfig.hideDataPoints ?? defaultLineConfig.hideDataPoints,
1197
- dataPointsShape:
1198
- lineConfig.dataPointsShape ?? defaultLineConfig.dataPointsShape,
1199
- dataPointsHeight:
1200
- lineConfig.dataPointsHeight ?? defaultLineConfig.dataPointsHeight,
1201
- dataPointsWidth:
1202
- lineConfig.dataPointsWidth ?? defaultLineConfig.dataPointsWidth,
1203
- dataPointsColor:
1204
- lineConfig.dataPointsColor ?? defaultLineConfig.dataPointsColor,
1205
- dataPointsRadius:
1206
- lineConfig.dataPointsRadius ?? defaultLineConfig.dataPointsRadius,
1207
- textColor: lineConfig.textColor ?? defaultLineConfig.textColor,
1208
- textFontSize: lineConfig.textFontSize ?? defaultLineConfig.textFontSize,
1209
- textShiftX: lineConfig.textShiftX ?? defaultLineConfig.textShiftX,
1210
- textShiftY: lineConfig.textShiftY ?? defaultLineConfig.textShiftY,
1211
- shiftX: lineConfig.shiftX ?? defaultLineConfig.shiftX,
1212
- shiftY: lineConfig.shiftY ?? defaultLineConfig.shiftY,
1213
- delay: lineConfig.delay ?? defaultLineConfig.delay,
1214
- startIndex: lineConfig.startIndex ?? defaultLineConfig.startIndex,
1215
- endIndex:
1216
- lineConfig.endIndex === 0
1217
- ? 0
1218
- : lineConfig.endIndex ?? defaultLineConfig.endIndex,
1219
-
1220
- showArrow: lineConfig.showArrow ?? defaultLineConfig.showArrow,
1221
- arrowConfig: {
1222
- length:
1223
- lineConfig.arrowConfig?.length ?? defaultLineConfig.arrowConfig?.length,
1224
- width:
1225
- lineConfig.arrowConfig?.width ?? defaultLineConfig.arrowConfig?.width,
1226
-
1227
- strokeWidth:
1228
- lineConfig.arrowConfig?.strokeWidth ??
1229
- defaultLineConfig.arrowConfig?.strokeWidth,
1230
-
1231
- strokeColor:
1232
- lineConfig.arrowConfig?.strokeColor ??
1233
- defaultLineConfig.arrowConfig?.strokeColor,
1234
-
1235
- fillColor:
1236
- lineConfig.arrowConfig?.fillColor ??
1237
- defaultLineConfig.arrowConfig?.fillColor,
1238
-
1239
- showArrowBase:
1240
- lineConfig.arrowConfig?.showArrowBase ??
1241
- defaultLineConfig.arrowConfig?.showArrowBase
1242
- },
1243
- customDataPoint: lineConfig.customDataPoint,
1244
- isSecondary: lineConfig.isSecondary ?? defaultLineConfig.isSecondary,
1245
- focusEnabled: lineConfig.focusEnabled ?? defaultLineConfig.focusEnabled,
1246
- focusedDataPointColor:
1247
- lineConfig.focusedDataPointColor ??
1248
- defaultLineConfig.focusedDataPointColor,
1249
- focusedDataPointRadius:
1250
- lineConfig.focusedDataPointRadius ??
1251
- defaultLineConfig.focusedDataPointRadius,
1252
- focusedDataPointIndex: lineConfig.focusedDataPointIndex
1253
- }
1254
- }
1255
-
1256
- export const getNoOfSections = (
1257
- noOfSections: number | undefined,
1258
- maxValue: number | undefined,
1259
- stepValue: number | undefined
1260
- ): number =>
1261
- maxValue && stepValue
1262
- ? maxValue / stepValue
1263
- : noOfSections ?? AxesAndRulesDefaults.noOfSections
1264
-
1265
- export const getMaxValue = (
1266
- maxValue: number | undefined,
1267
- stepValue: number | undefined,
1268
- noOfSections: number,
1269
- maxItem: number
1270
- ): number => maxValue ?? (stepValue ? stepValue * noOfSections : maxItem)
1271
-
1272
- export const getMostNegativeValue = (
1273
- minValue: number | undefined,
1274
- stepValue: number | undefined,
1275
- noOfSections: number | undefined,
1276
- minItem: number
1277
- ): number =>
1278
- minValue ?? (stepValue && noOfSections ? stepValue * noOfSections : minItem)
1279
-
1280
- export const getBarFrontColor = (
1281
- isFocused?: boolean,
1282
- focusedBarConfig?: FocusedBarConfig,
1283
- itemFrontColor?: ColorValue,
1284
- frontColor?: ColorValue,
1285
- isThreeD?: boolean
1286
- ): ColorValue => {
1287
- if (isFocused) {
1288
- return (
1289
- focusedBarConfig?.color ??
1290
- (isThreeD
1291
- ? BarDefaults.focusedThreeDBarFrontColor
1292
- : BarDefaults.focusedBarFrontColor)
1293
- )
1294
- }
1295
- return (
1296
- itemFrontColor ??
1297
- frontColor ??
1298
- (isThreeD ? BarDefaults.threeDBarFrontColor : BarDefaults.frontColor)
1299
- )
1300
- }
1301
-
1302
- export const getBarSideColor = (
1303
- isFocused?: boolean,
1304
- focusedBarConfig?: FocusedBarConfig,
1305
- itemSideColor?: ColorValue,
1306
- sideColor?: ColorValue
1307
- ): ColorValue | undefined => {
1308
- if (isFocused) {
1309
- return focusedBarConfig?.sideColor ?? BarDefaults.focusedBarSideColor
1310
- }
1311
- return itemSideColor ?? sideColor
1312
- }
1313
-
1314
- export const getBarTopColor = (
1315
- isFocused?: boolean,
1316
- focusedBarConfig?: FocusedBarConfig,
1317
- itemTopColor?: ColorValue,
1318
- topColor?: ColorValue
1319
- ): ColorValue | undefined => {
1320
- if (isFocused) {
1321
- return focusedBarConfig?.topColor ?? BarDefaults.focusedBarTopColor
1322
- }
1323
- return itemTopColor ?? topColor
1324
- }
1325
-
1326
- export const getBarWidth = (
1327
- isFocused?: boolean,
1328
- focusedBarConfig?: FocusedBarConfig,
1329
- itemBarWidth?: number,
1330
- barWidth?: number
1331
- ): number => {
1332
- const localBarWidth = itemBarWidth ?? barWidth ?? BarDefaults.barWidth
1333
- if (isFocused) {
1334
- return focusedBarConfig?.width ?? localBarWidth
1335
- }
1336
- return localBarWidth
1337
- }
1338
-
1339
- export const getInterpolatedData = (
1340
- dataParam: lineDataItem[],
1341
- showDataPointsForMissingValues?: boolean,
1342
- interpolateMissingValues?: boolean,
1343
- onlyPositive?: boolean
1344
- ): lineDataItem[] => {
1345
- if (!interpolateMissingValues) {
1346
- return dataParam.map((item) => {
1347
- if (typeof item.value !== 'number') {
1348
- if (showDataPointsForMissingValues) return { ...item, value: 0 }
1349
- return { ...item, value: 0, hideDataPoint: true }
1350
- }
1351
- return item
1352
- })
1353
- }
1354
- if (!interpolateMissingValues) return dataParam
1355
- const data: lineDataItem[] = clone(dataParam)
1356
- const n = data.length
1357
-
1358
- /** ************ PRE-PROCESSING **************/
1359
- let numericValue: number
1360
- const numericValuesLength = data.filter((item) => {
1361
- const isNum = typeof item.value === 'number'
1362
- if (isNum) {
1363
- numericValue = item.value
1364
- return true
1365
- }
1366
- return false
1367
- }).length
1368
-
1369
- if (!numericValuesLength) return []
1370
-
1371
- if (numericValuesLength === 1) {
1372
- data.forEach((item) => {
1373
- if (!showDataPointsForMissingValues && typeof item.value !== 'number') {
1374
- item.hideDataPoint = true
1375
- }
1376
- item.value = numericValue
1377
- })
1378
- return data
1379
- }
1380
- /**********************************************************************/
1381
-
1382
- data.forEach((item, index) => {
1383
- if (typeof item.value === 'number') return
1384
- // Cut the line in 2 halves-> pre and post
1385
- // Now there are 4 possibilities-
1386
- // 1. Both pre and post have valid values
1387
- // 2. Only pre has valid value
1388
- // 3. Only post has valid value
1389
- // 4. None has valid value -> this is already handled in preprocessing
1390
-
1391
- const pre: lineDataItem[] = data.slice(0, index)
1392
- const post: lineDataItem[] = data.slice(index + 1, n)
1393
-
1394
- const preValidIndex = pre.findLastIndex(
1395
- (item: lineDataItem) => typeof item.value === 'number'
1396
- )
1397
- const postValidInd = post.findIndex(
1398
- (item) => typeof item.value === 'number'
1399
- )
1400
- const postValidIndex = postValidInd + index + 1
1401
-
1402
- let count, step
1403
-
1404
- // 1. Both pre and post have valid values
1405
- if (preValidIndex !== -1 && postValidInd !== -1) {
1406
- count = postValidIndex - preValidIndex
1407
- step = (data[postValidIndex].value - data[preValidIndex].value) / count
1408
- data[index].value =
1409
- data[preValidIndex].value + step * (index - preValidIndex)
1410
- } else if (preValidIndex !== -1 && postValidInd === -1) {
1411
- // 2. Only pre has valid value
1412
- // Now there are 2 possibilities-
1413
- // 1. There's only 1 valid value in the pre -> this is already handled in preprocessing
1414
- // 2. There are more than valid values in pre
1415
- const secondPre: lineDataItem[] = data.slice(0, preValidIndex)
1416
- const secondPreIndex = secondPre.findLastIndex(
1417
- (item: lineDataItem) => typeof item.value === 'number'
1418
- )
1419
-
1420
- count = preValidIndex - secondPreIndex
1421
- step = (data[secondPreIndex].value - data[preValidIndex].value) / count
1422
- data[index].value =
1423
- data[preValidIndex].value - step * (index - preValidIndex)
1424
- } else if (preValidIndex === -1 && postValidInd !== -1) {
1425
- // 3. Only post has valid value
1426
- // Now there are 2 possibilities-
1427
- // 1. There's only 1 valid value in the post -> this is already handled in preprocessing
1428
- // 2. There are more than valid values in post
1429
-
1430
- const secondPost: lineDataItem[] = data.slice(postValidIndex + 1, n)
1431
- const secondPostInd = secondPost.findIndex(
1432
- (item) => typeof item.value === 'number'
1433
- )
1434
- const secondPostIndex = secondPostInd + postValidIndex + 1
1435
-
1436
- count = secondPostIndex - postValidIndex
1437
- step = (data[secondPostIndex].value - data[postValidIndex].value) / count
1438
- data[index].value =
1439
- data[postValidIndex].value - step * (postValidIndex - index)
1440
- }
1441
-
1442
- // hide data point (since it is interpolated)
1443
- if (!showDataPointsForMissingValues) {
1444
- item.hideDataPoint = true
1445
- }
1446
- })
1447
- return onlyPositive
1448
- ? data.map((item) => ({ ...item, value: Math.max(item.value, 0) }))
1449
- : data
1450
- }
1451
-
1452
- export const getLineSegmentsForMissingValues = (
1453
- data?: lineDataItem[]
1454
- ): LineSegment[] | undefined => {
1455
- if (!data?.length) return undefined
1456
- let i
1457
- const n = data.length
1458
- const numericValuesLength = data.filter(
1459
- (item) => typeof item.value === 'number'
1460
- ).length
1461
- if (!numericValuesLength) return undefined
1462
- const segments: LineSegment[] = []
1463
- for (i = 0; i < n; i++) {
1464
- if (typeof data[i].value !== 'number') {
1465
- const nextValidInd: number = data
1466
- .slice(i + 1, n)
1467
- .findIndex((item) => typeof item.value === 'number')
1468
- if (nextValidInd === -1) {
1469
- segments.push({
1470
- startIndex: Math.max(i - 1, 0),
1471
- endIndex: n,
1472
- color: 'transparent'
1473
- })
1474
- break
1475
- }
1476
- const nextValidIndex: number = nextValidInd + i + 1
1477
- segments.push({
1478
- startIndex: Math.max(i - 1, 0),
1479
- endIndex: nextValidIndex,
1480
- color: 'transparent'
1481
- })
1482
- i = nextValidIndex
1483
- }
1484
- }
1485
- return segments
1486
- }
1487
-
1488
- export const getTextSizeForPieLabels = (
1489
- textSize: number,
1490
- radius: number
1491
- ): number => (textSize ? Math.min(textSize, radius / 5) : 16)
1492
-
1493
- export const adjustToOffset = (
1494
- data: lineDataItem[],
1495
- yAxisOffset?: number
1496
- ): lineDataItem[] =>
1497
- data.map((item) => ({ ...item, value: item.value - (yAxisOffset ?? 0) }))
1498
-
1499
- export const getSanitisedData = (
1500
- data: lineDataItem[] | undefined,
1501
- dataSanitisationProps: IDataSanitisationProps
1502
- ): lineDataItem[] => {
1503
- if (!data) {
1504
- return []
1505
- }
1506
- const {
1507
- showDataPointsForMissingValues,
1508
- interpolateMissingValues,
1509
- onlyPositive,
1510
- yAxisOffset
1511
- } = dataSanitisationProps
1512
- const nullishHandledData = getInterpolatedData(
1513
- data,
1514
- showDataPointsForMissingValues,
1515
- interpolateMissingValues,
1516
- onlyPositive
1517
- )
1518
- if (yAxisOffset) {
1519
- return adjustToOffset(nullishHandledData, yAxisOffset)
1520
- }
1521
- return nullishHandledData
1522
- }
1523
-
1524
- export const getStrokeDashArray = (
1525
- strokeDash?: number[] | string,
1526
- framework?: Framework
1527
- ): number[] | string | undefined => {
1528
- let strokeDashArrayOrString: number[] | string | undefined
1529
- if (framework === Framework.reactJS) {
1530
- if (strokeDash instanceof Array) {
1531
- strokeDashArrayOrString = strokeDash.toString().replace(',', ' ')
1532
- } else if (typeof strokeDash === 'string') {
1533
- strokeDashArrayOrString = strokeDash
1534
- .replace(',', ' ')
1535
- .replace('[', '')
1536
- .replace(']', '')
1537
- .replace('{', '')
1538
- .replace('}', '')
1539
- }
1540
- } else {
1541
- if (strokeDash instanceof Array) {
1542
- strokeDashArrayOrString = strokeDash
1543
- } else if (typeof strokeDash === 'string') {
1544
- const ar = strokeDash
1545
- .replace(',', ' ')
1546
- .replace('[', '')
1547
- .replace(']', '')
1548
- .replace('{', '')
1549
- .replace('}', '')
1550
- .split(' ')
1551
- if (ar[0] && ar[1]) {
1552
- const n1 = Number(ar[0])
1553
- const n2 = Number(ar[1])
1554
- if (!isNaN(n1) && !isNaN(n2)) {
1555
- strokeDashArrayOrString = [n1, n2]
1556
- }
1557
- }
1558
- }
1559
- }
1560
-
1561
- return strokeDashArrayOrString
1562
- }