gifted-charts-core 0.0.21 → 0.0.23

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.
@@ -1,5 +1,10 @@
1
- import { useEffect, useMemo, useState } from "react";
2
- import { BarChartPropsType, barDataItem } from "./types";
1
+ import { useEffect, useMemo, useState } from 'react'
2
+ import {
3
+ type lineConfigType,
4
+ type BarChartPropsType,
5
+ type barDataItem,
6
+ type stackDataItem
7
+ } from './types'
3
8
  import {
4
9
  getArrowPoints,
5
10
  getAxesAndRulesProps,
@@ -11,164 +16,171 @@ import {
11
16
  getXForLineInBar,
12
17
  getYForLineInBar,
13
18
  maxAndMinUtil,
14
- svgPath,
15
- } from "../utils";
19
+ svgPath
20
+ } from '../utils'
16
21
  import {
17
22
  AxesAndRulesDefaults,
18
23
  BarDefaults,
19
24
  chartTypes,
20
25
  defaultLineConfig,
21
- defaultPointerConfig,
22
- } from "../utils/constants";
23
- import { BarAndLineChartsWrapperTypes } from "../utils/types";
24
-
25
- interface extendedBarChartPropsType extends BarChartPropsType {
26
- heightValue;
27
- widthValue;
28
- opacValue;
26
+ defaultPointerConfig
27
+ } from '../utils/constants'
28
+ import {
29
+ type BarAndLineChartsWrapperTypes,
30
+ type secondaryYAxisType
31
+ } from '../utils/types'
32
+ import { type Animated } from 'react-native'
33
+
34
+ export interface extendedBarChartPropsType extends BarChartPropsType {
35
+ heightValue: Animated.Value
36
+ widthValue: Animated.Value
37
+ opacValue: Animated.Value
38
+ verticalLinesUptoDataPoint?: boolean
39
+ secondaryYAxis?: secondaryYAxisType | boolean
29
40
  }
30
41
 
31
42
  export const useBarChart = (props: extendedBarChartPropsType) => {
32
- const { heightValue, widthValue, opacValue } = props;
33
- const [points, setPoints] = useState("");
34
- const [points2, setPoints2] = useState("");
35
- const [arrowPoints, setArrowPoints] = useState("");
36
- const [selectedIndex, setSelectedIndex] = useState(-1);
37
- const showLine = props.showLine || BarDefaults.showLine;
38
- const spacing = props.spacing ?? BarDefaults.spacing;
39
- const initialSpacing = props.initialSpacing ?? spacing;
40
- const endSpacing = props.endSpacing ?? spacing;
43
+ const { heightValue, widthValue, opacValue } = props
44
+ const [points, setPoints] = useState('')
45
+ const [points2, setPoints2] = useState('')
46
+ const [arrowPoints, setArrowPoints] = useState('')
47
+ const [selectedIndex, setSelectedIndex] = useState(-1)
48
+ const showLine = props.showLine ?? BarDefaults.showLine
49
+ const spacing = props.spacing ?? BarDefaults.spacing
50
+ const initialSpacing = props.initialSpacing ?? spacing
51
+ const endSpacing = props.endSpacing ?? spacing
41
52
  const showFractionalValues =
42
- props.showFractionalValues || AxesAndRulesDefaults.showFractionalValues;
53
+ props.showFractionalValues ?? AxesAndRulesDefaults.showFractionalValues
43
54
 
44
- const horizontal = props.horizontal ?? BarDefaults.horizontal;
45
- const rtl = props.rtl ?? BarDefaults.rtl;
46
- const yAxisAtTop = props.yAxisAtTop ?? BarDefaults.yAxisAtTop;
47
- const intactTopLabel = props.intactTopLabel ?? BarDefaults.intactTopLabel;
55
+ const horizontal = props.horizontal ?? BarDefaults.horizontal
56
+ const rtl = props.rtl ?? BarDefaults.rtl
57
+ const yAxisAtTop = props.yAxisAtTop ?? BarDefaults.yAxisAtTop
58
+ const intactTopLabel = props.intactTopLabel ?? BarDefaults.intactTopLabel
48
59
 
49
- const heightFromProps = horizontal ? props.width : props.height;
50
- const widthFromProps = horizontal ? props.height : props.width;
60
+ const heightFromProps = horizontal ? props.width : props.height
61
+ const widthFromProps = horizontal ? props.height : props.width
51
62
 
52
- const isAnimated = props.isAnimated ?? BarDefaults.isAnimated;
63
+ const isAnimated = props.isAnimated ?? BarDefaults.isAnimated
53
64
  const animationDuration =
54
- props.animationDuration ?? BarDefaults.animationDuration;
65
+ props.animationDuration ?? BarDefaults.animationDuration
55
66
 
56
67
  const data = useMemo(() => {
57
68
  if (!props.data) {
58
- return [];
69
+ return []
59
70
  }
60
71
  if (props.yAxisOffset) {
61
72
  return props.data.map((item) => {
62
- item.value = item.value - (props.yAxisOffset ?? 0);
63
- return item;
64
- });
73
+ item.value = (item.value ?? 0) - (props.yAxisOffset ?? 0)
74
+ return item
75
+ })
65
76
  }
66
- return props.data;
67
- }, [props.yAxisOffset, props.data]);
77
+ return props.data
78
+ }, [props.yAxisOffset, props.data])
68
79
 
69
80
  const secondaryData = getSecondaryDataWithOffsetIncluded(
70
81
  props.secondaryData,
71
82
  props.secondaryYAxis
72
- );
83
+ )
73
84
 
74
85
  const lineData = useMemo(() => {
75
86
  if (!props.lineData) {
76
- return props.stackData ?? data;
87
+ return props.stackData ?? data
77
88
  }
78
89
  if (props.yAxisOffset) {
79
- return props.lineData.map((item) => {
80
- item.value = item.value - (props.yAxisOffset ?? 0);
81
- return item;
82
- });
90
+ return props.lineData.map((item) => ({
91
+ ...item,
92
+ value: (item.value ?? 0) - (props.yAxisOffset ?? 0)
93
+ }))
83
94
  }
84
- return props.lineData;
85
- }, [props.yAxisOffset, props.lineData, data, props.stackData]);
95
+ return props.lineData
96
+ }, [props.yAxisOffset, props.lineData, data, props.stackData])
86
97
 
87
- const lineData2 = props.lineData2;
98
+ const lineData2 = props.lineData2
88
99
 
89
- const lineBehindBars = props.lineBehindBars || BarDefaults.lineBehindBars;
100
+ const lineBehindBars = props.lineBehindBars ?? BarDefaults.lineBehindBars
90
101
 
91
- defaultLineConfig.initialSpacing = initialSpacing;
92
- defaultLineConfig.endIndex = lineData.length - 1;
93
- defaultLineConfig.animationDuration = animationDuration;
102
+ defaultLineConfig.initialSpacing = initialSpacing
103
+ defaultLineConfig.endIndex = lineData.length - 1
104
+ defaultLineConfig.animationDuration = animationDuration
94
105
 
95
- const lineConfig = props.lineConfig
106
+ const lineConfig: lineConfigType = props.lineConfig
96
107
  ? getLineConfigForBarChart(props.lineConfig, initialSpacing)
97
- : defaultLineConfig;
98
- const lineConfig2 = props.lineConfig2
108
+ : defaultLineConfig
109
+ const lineConfig2: lineConfigType = props.lineConfig2
99
110
  ? getLineConfigForBarChart(props.lineConfig2, initialSpacing)
100
- : defaultLineConfig;
111
+ : defaultLineConfig
101
112
  const noOfSections = getNoOfSections(
102
113
  props.noOfSections,
103
114
  props.maxValue,
104
115
  props.stepValue
105
- );
116
+ )
106
117
  const containerHeight =
107
118
  heightFromProps ??
108
- ((props.stepHeight ?? 0) * noOfSections ||
109
- AxesAndRulesDefaults.containerHeight);
110
- const horizSections = [{ value: "0" }];
111
- const stepHeight = props.stepHeight ?? containerHeight / noOfSections;
112
- const labelWidth = props.labelWidth ?? AxesAndRulesDefaults.labelWidth;
113
- const scrollToEnd = props.scrollToEnd ?? BarDefaults.scrollToEnd;
114
- const scrollAnimation = props.scrollAnimation ?? BarDefaults.scrollAnimation;
119
+ (props.stepHeight
120
+ ? props.stepHeight * noOfSections
121
+ : AxesAndRulesDefaults.containerHeight)
122
+ const horizSections = [{ value: '0' }]
123
+ const stepHeight = props.stepHeight ?? containerHeight / noOfSections
124
+ const labelWidth = props.labelWidth ?? AxesAndRulesDefaults.labelWidth
125
+ const scrollToEnd = props.scrollToEnd ?? BarDefaults.scrollToEnd
126
+ const scrollAnimation = props.scrollAnimation ?? BarDefaults.scrollAnimation
115
127
  const scrollEventThrottle =
116
- props.scrollEventThrottle ?? BarDefaults.scrollEventThrottle;
128
+ props.scrollEventThrottle ?? BarDefaults.scrollEventThrottle
117
129
  const labelsExtraHeight =
118
- props.labelsExtraHeight ?? AxesAndRulesDefaults.labelsExtraHeight;
130
+ props.labelsExtraHeight ?? AxesAndRulesDefaults.labelsExtraHeight
119
131
 
120
- let totalWidth = initialSpacing;
121
- let maxItem = 0,
122
- minItem = 0;
132
+ let totalWidth = initialSpacing
133
+ let maxItem = 0
134
+ let minItem = 0
123
135
  if (props.stackData) {
124
136
  props.stackData.forEach((stackItem) => {
125
137
  const stackSumMax = stackItem.stacks.reduce(
126
138
  (acc, stack) => acc + (stack.value >= 0 ? stack.value : 0),
127
139
  0
128
- );
140
+ )
129
141
  const stackSumMin = stackItem.stacks.reduce(
130
142
  (acc, stack) => acc + (stack.value < 0 ? stack.value : 0),
131
143
  0
132
- );
144
+ )
133
145
 
134
146
  if (stackSumMax > maxItem) {
135
- maxItem = stackSumMax;
147
+ maxItem = stackSumMax
136
148
  }
137
149
 
138
150
  if (stackSumMin < minItem) {
139
- minItem = stackSumMin;
151
+ minItem = stackSumMin
140
152
  }
141
153
  totalWidth +=
142
154
  (stackItem.stacks[0].barWidth ??
143
155
  props.barWidth ??
144
- BarDefaults.barWidth) + spacing;
145
- });
156
+ BarDefaults.barWidth) + spacing
157
+ })
146
158
  } else {
147
159
  data.forEach((item: barDataItem) => {
148
160
  if (item.value > maxItem) {
149
- maxItem = item.value;
161
+ maxItem = item.value
150
162
  }
151
163
  if (item.value < minItem) {
152
- minItem = item.value;
164
+ minItem = item.value
153
165
  }
154
166
  totalWidth +=
155
167
  (item.barWidth ?? props.barWidth ?? BarDefaults.barWidth) +
156
- (item.spacing ?? spacing);
157
- });
168
+ (item.spacing ?? spacing)
169
+ })
158
170
  }
159
171
 
160
- let secondaryMaxItem = 0,
161
- secondaryMinItem = 0;
172
+ let secondaryMaxItem = 0
173
+ let secondaryMinItem = 0
162
174
 
163
175
  if (lineConfig.isSecondary) {
164
- lineData.forEach((item: barDataItem) => {
165
- if (item.value > secondaryMaxItem) {
166
- secondaryMaxItem = item.value;
176
+ lineData.forEach((item) => {
177
+ if ((item.value ?? 0) > secondaryMaxItem) {
178
+ secondaryMaxItem = item.value ?? 0
167
179
  }
168
- if (item.value < secondaryMinItem) {
169
- secondaryMinItem = item.value;
180
+ if ((item.value ?? 0) < secondaryMinItem) {
181
+ secondaryMinItem = item.value ?? 0
170
182
  }
171
- });
183
+ })
172
184
  }
173
185
 
174
186
  const maxAndMin = maxAndMinUtil(
@@ -176,174 +188,169 @@ export const useBarChart = (props: extendedBarChartPropsType) => {
176
188
  minItem,
177
189
  props.roundToDigits,
178
190
  props.showFractionalValues
179
- );
191
+ )
180
192
 
181
193
  const secondaryMaxAndMin = maxAndMinUtil(
182
194
  secondaryMaxItem,
183
195
  secondaryMinItem,
184
196
  props.roundToDigits,
185
197
  props.showFractionalValues
186
- );
198
+ )
187
199
 
188
200
  const maxValue = getMaxValue(
189
201
  props.maxValue,
190
202
  props.stepValue,
191
203
  noOfSections,
192
204
  maxAndMin.maxItem
193
- );
205
+ )
194
206
  const secondaryMaxValue = lineConfig.isSecondary
195
207
  ? secondaryMaxAndMin.maxItem
196
- : maxValue;
197
- const mostNegativeValue = props.mostNegativeValue ?? maxAndMin.minItem;
208
+ : maxValue
209
+ const mostNegativeValue = props.mostNegativeValue ?? maxAndMin.minItem
198
210
 
199
- const stepValue = props.stepValue ?? maxValue / noOfSections;
211
+ const stepValue = props.stepValue ?? maxValue / noOfSections
200
212
  const noOfSectionsBelowXAxis =
201
- props.noOfSectionsBelowXAxis ?? -mostNegativeValue / stepValue;
213
+ props.noOfSectionsBelowXAxis ?? -mostNegativeValue / stepValue
202
214
  const showScrollIndicator =
203
- props.showScrollIndicator ?? BarDefaults.showScrollIndicator;
204
- const side = props.side ?? BarDefaults.side;
205
- const rotateLabel = props.rotateLabel ?? AxesAndRulesDefaults.rotateLabel;
206
- const opacity = props.opacity ?? BarDefaults.opacity;
207
- const isThreeD = props.isThreeD ?? BarDefaults.isThreeD;
215
+ props.showScrollIndicator ?? BarDefaults.showScrollIndicator
216
+ const side = props.side ?? BarDefaults.side
217
+ const rotateLabel = props.rotateLabel ?? AxesAndRulesDefaults.rotateLabel
218
+ const opacity = props.opacity ?? BarDefaults.opacity
219
+ const isThreeD = props.isThreeD ?? BarDefaults.isThreeD
208
220
 
209
221
  const showXAxisIndices =
210
- props.showXAxisIndices ?? AxesAndRulesDefaults.showXAxisIndices;
222
+ props.showXAxisIndices ?? AxesAndRulesDefaults.showXAxisIndices
211
223
  const xAxisIndicesHeight =
212
- props.xAxisIndicesHeight ?? AxesAndRulesDefaults.xAxisIndicesHeight;
224
+ props.xAxisIndicesHeight ?? AxesAndRulesDefaults.xAxisIndicesHeight
213
225
  const xAxisIndicesWidth =
214
- props.xAxisIndicesWidth ?? AxesAndRulesDefaults.xAxisIndicesWidth;
226
+ props.xAxisIndicesWidth ?? AxesAndRulesDefaults.xAxisIndicesWidth
215
227
  const xAxisIndicesColor =
216
- props.xAxisIndicesColor ?? AxesAndRulesDefaults.xAxisIndicesColor;
228
+ props.xAxisIndicesColor ?? AxesAndRulesDefaults.xAxisIndicesColor
217
229
 
218
230
  const xAxisThickness =
219
- props.xAxisThickness ?? AxesAndRulesDefaults.xAxisThickness;
231
+ props.xAxisThickness ?? AxesAndRulesDefaults.xAxisThickness
220
232
 
221
233
  const xAxisTextNumberOfLines =
222
- props.xAxisTextNumberOfLines ?? AxesAndRulesDefaults.xAxisTextNumberOfLines;
234
+ props.xAxisTextNumberOfLines ?? AxesAndRulesDefaults.xAxisTextNumberOfLines
223
235
  const xAxisLabelsVerticalShift =
224
236
  props.xAxisLabelsVerticalShift ??
225
- AxesAndRulesDefaults.xAxisLabelsVerticalShift;
226
- const horizontalRulesStyle = props.horizontalRulesStyle;
237
+ AxesAndRulesDefaults.xAxisLabelsVerticalShift
238
+ const horizontalRulesStyle = props.horizontalRulesStyle
227
239
  const yAxisLabelWidth =
228
240
  props.yAxisLabelWidth ??
229
241
  (props.hideYAxisText
230
242
  ? AxesAndRulesDefaults.yAxisEmptyLabelWidth
231
- : AxesAndRulesDefaults.yAxisLabelWidth);
243
+ : AxesAndRulesDefaults.yAxisLabelWidth)
232
244
 
233
- const autoShiftLabels = props.autoShiftLabels ?? false;
245
+ const autoShiftLabels = props.autoShiftLabels ?? false
234
246
 
235
- const barWidth = props.barWidth || BarDefaults.barWidth;
236
- const barBorderColor = props.barBorderColor ?? BarDefaults.barBorderColor;
247
+ const barWidth = props.barWidth ?? BarDefaults.barWidth
248
+ const barBorderColor = props.barBorderColor ?? BarDefaults.barBorderColor
237
249
 
238
250
  const extendedContainerHeight = getExtendedContainerHeightWithPadding(
239
251
  containerHeight,
240
252
  0
241
- );
253
+ )
242
254
 
243
255
  const containerHeightIncludingBelowXAxis =
244
- extendedContainerHeight + noOfSectionsBelowXAxis * stepHeight;
245
-
246
- const [pointerIndex, setPointerIndex] = useState(-1);
247
- const [pointerX, setPointerX] = useState(0);
248
- const [pointerY, setPointerY] = useState(0);
249
- const [pointerItem, setPointerItem] = useState({
250
- pointerShiftX: 0,
251
- pointerShiftY: 0,
252
- });
253
- const [responderStartTime, setResponderStartTime] = useState(0);
254
- const [responderActive, setResponderActive] = useState(false);
255
-
256
- const pointerConfig = props.pointerConfig;
257
- const getPointerProps = props.getPointerProps || null;
258
- const pointerHeight = pointerConfig?.height ?? defaultPointerConfig.height;
259
- const pointerWidth = pointerConfig?.width ?? defaultPointerConfig.width;
260
- const pointerRadius = pointerConfig?.radius ?? defaultPointerConfig.radius;
256
+ extendedContainerHeight + noOfSectionsBelowXAxis * stepHeight
257
+
258
+ const [pointerIndex, setPointerIndex] = useState(-1)
259
+ const [pointerX, setPointerX] = useState(0)
260
+ const [pointerY, setPointerY] = useState(0)
261
+ const [pointerItem, setPointerItem] = useState<barDataItem | stackDataItem>()
262
+ const [responderStartTime, setResponderStartTime] = useState(0)
263
+ const [responderActive, setResponderActive] = useState(false)
264
+
265
+ const pointerConfig = props.pointerConfig
266
+ const getPointerProps = props.getPointerProps ?? null
267
+ const pointerHeight = pointerConfig?.height ?? defaultPointerConfig.height
268
+ const pointerWidth = pointerConfig?.width ?? defaultPointerConfig.width
269
+ const pointerRadius = pointerConfig?.radius ?? defaultPointerConfig.radius
261
270
  const pointerColor =
262
- pointerConfig?.pointerColor ?? defaultPointerConfig.pointerColor;
271
+ pointerConfig?.pointerColor ?? defaultPointerConfig.pointerColor
263
272
  const pointerComponent =
264
- pointerConfig?.pointerComponent ?? defaultPointerConfig.pointerComponent;
273
+ pointerConfig?.pointerComponent ?? defaultPointerConfig.pointerComponent
265
274
 
266
275
  const showPointerStrip =
267
276
  pointerConfig?.showPointerStrip === false
268
277
  ? false
269
- : defaultPointerConfig.showPointerStrip;
278
+ : defaultPointerConfig.showPointerStrip
270
279
  const pointerStripHeight =
271
- pointerConfig?.pointerStripHeight ??
272
- defaultPointerConfig.pointerStripHeight;
280
+ pointerConfig?.pointerStripHeight ?? defaultPointerConfig.pointerStripHeight
273
281
  const pointerStripWidth =
274
- pointerConfig?.pointerStripWidth ?? defaultPointerConfig.pointerStripWidth;
282
+ pointerConfig?.pointerStripWidth ?? defaultPointerConfig.pointerStripWidth
275
283
  const pointerStripColor =
276
- pointerConfig?.pointerStripColor ?? defaultPointerConfig.pointerStripColor;
284
+ pointerConfig?.pointerStripColor ?? defaultPointerConfig.pointerStripColor
277
285
  const pointerStripUptoDataPoint =
278
286
  pointerConfig?.pointerStripUptoDataPoint ??
279
- defaultPointerConfig.pointerStripUptoDataPoint;
287
+ defaultPointerConfig.pointerStripUptoDataPoint
280
288
  const pointerLabelComponent =
281
289
  pointerConfig?.pointerLabelComponent ??
282
- defaultPointerConfig.pointerLabelComponent;
290
+ defaultPointerConfig.pointerLabelComponent
283
291
  const stripOverPointer =
284
- pointerConfig?.stripOverPointer ?? defaultPointerConfig.stripOverPointer;
292
+ pointerConfig?.stripOverPointer ?? defaultPointerConfig.stripOverPointer
285
293
  const shiftPointerLabelX =
286
- pointerConfig?.shiftPointerLabelX ??
287
- defaultPointerConfig.shiftPointerLabelX;
294
+ pointerConfig?.shiftPointerLabelX ?? defaultPointerConfig.shiftPointerLabelX
288
295
  const shiftPointerLabelY =
289
- pointerConfig?.shiftPointerLabelY ??
290
- defaultPointerConfig.shiftPointerLabelY;
296
+ pointerConfig?.shiftPointerLabelY ?? defaultPointerConfig.shiftPointerLabelY
291
297
  const pointerLabelWidth =
292
- pointerConfig?.pointerLabelWidth ?? defaultPointerConfig.pointerLabelWidth;
298
+ pointerConfig?.pointerLabelWidth ?? defaultPointerConfig.pointerLabelWidth
293
299
  const pointerLabelHeight =
294
- pointerConfig?.pointerLabelHeight ??
295
- defaultPointerConfig.pointerLabelHeight;
300
+ pointerConfig?.pointerLabelHeight ?? defaultPointerConfig.pointerLabelHeight
296
301
  const autoAdjustPointerLabelPosition =
297
302
  pointerConfig?.autoAdjustPointerLabelPosition ??
298
- defaultPointerConfig.autoAdjustPointerLabelPosition;
303
+ defaultPointerConfig.autoAdjustPointerLabelPosition
299
304
  const pointerVanishDelay =
300
- pointerConfig?.pointerVanishDelay ??
301
- defaultPointerConfig.pointerVanishDelay;
305
+ pointerConfig?.pointerVanishDelay ?? defaultPointerConfig.pointerVanishDelay
302
306
  const activatePointersOnLongPress =
303
307
  pointerConfig?.activatePointersOnLongPress ??
304
- defaultPointerConfig.activatePointersOnLongPress;
308
+ defaultPointerConfig.activatePointersOnLongPress
305
309
  const activatePointersDelay =
306
310
  pointerConfig?.activatePointersDelay ??
307
- defaultPointerConfig.activatePointersDelay;
311
+ defaultPointerConfig.activatePointersDelay
308
312
  const initialPointerIndex =
309
313
  pointerConfig?.initialPointerIndex ??
310
- defaultPointerConfig.initialPointerIndex;
314
+ defaultPointerConfig.initialPointerIndex
311
315
  const initialPointerAppearDelay =
312
316
  pointerConfig?.initialPointerAppearDelay ??
313
317
  (isAnimated
314
318
  ? animationDuration
315
- : defaultPointerConfig.initialPointerAppearDelay);
319
+ : defaultPointerConfig.initialPointerAppearDelay)
316
320
  const persistPointer =
317
- pointerConfig?.persistPointer ?? defaultPointerConfig.persistPointer;
321
+ pointerConfig?.persistPointer ?? defaultPointerConfig.persistPointer
318
322
  const hidePointer1 =
319
- pointerConfig?.hidePointer1 ?? defaultPointerConfig.hidePointer1;
320
- const pointerEvents = pointerConfig?.pointerEvents;
323
+ pointerConfig?.hidePointer1 ?? defaultPointerConfig.hidePointer1
324
+ const pointerEvents = pointerConfig?.pointerEvents
321
325
  const stripBehindBars =
322
- pointerConfig?.stripBehindBars ?? defaultPointerConfig.stripBehindBars;
326
+ pointerConfig?.stripBehindBars ?? defaultPointerConfig.stripBehindBars
323
327
 
324
328
  const disableScroll =
325
- props.disableScroll ||
329
+ props.disableScroll ??
326
330
  (pointerConfig
327
331
  ? activatePointersOnLongPress
328
- ? responderActive
329
- ? true
330
- : false
332
+ ? !!responderActive
331
333
  : true
332
- : false);
334
+ : false)
333
335
 
334
- const barInnerComponent = props.barInnerComponent;
336
+ const barInnerComponent = props.barInnerComponent
335
337
 
336
338
  useEffect(() => {
337
339
  if (showLine) {
338
- let pp = "",
339
- pp2 = "";
340
+ let pp = ''
341
+ let pp2 = ''
340
342
  const firstBarWidth =
341
- (props.stackData ?? data)?.[0].barWidth ?? props.barWidth ?? 30;
343
+ (props.stackData ?? data)?.[0].barWidth ?? props.barWidth ?? 30
342
344
  if (!lineConfig.curved) {
343
345
  for (let i = 0; i < lineData.length; i++) {
344
- if (i < lineConfig.startIndex || i > lineConfig.endIndex) continue;
346
+ if (
347
+ i < (lineConfig.startIndex ?? 0) ||
348
+ i > (lineConfig.endIndex ?? 0)
349
+ ) {
350
+ continue
351
+ }
345
352
  const currentBarWidth =
346
- data?.[i]?.barWidth ?? props.barWidth ?? BarDefaults.barWidth;
353
+ data?.[i]?.barWidth ?? props.barWidth ?? BarDefaults.barWidth
347
354
  const currentValue = props.lineData
348
355
  ? props.lineData[i].value
349
356
  : props.stackData
@@ -351,9 +358,9 @@ export const useBarChart = (props: extendedBarChartPropsType) => {
351
358
  (total, item) => total + item.value,
352
359
  0
353
360
  )
354
- : data[i].value;
361
+ : data[i].value
355
362
  pp +=
356
- "L" +
363
+ 'L' +
357
364
  getXForLineInBar(
358
365
  i,
359
366
  firstBarWidth,
@@ -362,43 +369,48 @@ export const useBarChart = (props: extendedBarChartPropsType) => {
362
369
  lineConfig,
363
370
  spacing
364
371
  ) +
365
- " " +
372
+ ' ' +
366
373
  getYForLineInBar(
367
374
  currentValue,
368
375
  lineConfig.shiftY,
369
376
  containerHeight,
370
377
  lineConfig.isSecondary ? secondaryMaxValue : maxValue
371
378
  ) +
372
- " ";
379
+ ' '
373
380
  }
374
- setPoints(pp.replace("L", "M"));
381
+ setPoints(pp.replace('L', 'M'))
375
382
  if (lineData.length > 1 && lineConfig.showArrow) {
376
- let ppArray = pp.trim().split(" ");
377
- let arrowTipY = parseInt(ppArray[ppArray.length - 1]);
378
- let arrowTipX = parseInt(
379
- ppArray[ppArray.length - 2].replace("L", "")
380
- );
381
- let y1 = parseInt(ppArray[ppArray.length - 3]);
382
- let x1 = parseInt(ppArray[ppArray.length - 4].replace("L", ""));
383
-
384
- let arrowPoints = getArrowPoints(
383
+ const ppArray = pp.trim().split(' ')
384
+ const arrowTipY = parseInt(ppArray[ppArray.length - 1])
385
+ const arrowTipX = parseInt(
386
+ ppArray[ppArray.length - 2].replace('L', '')
387
+ )
388
+ const y1 = parseInt(ppArray[ppArray.length - 3])
389
+ const x1 = parseInt(ppArray[ppArray.length - 4].replace('L', ''))
390
+
391
+ const arrowPoints = getArrowPoints(
385
392
  arrowTipX,
386
393
  arrowTipY,
387
394
  x1,
388
395
  y1,
389
- lineConfig.arrowConfig.length,
390
- lineConfig.arrowConfig.width,
391
- lineConfig.arrowConfig.showArrowBase
392
- );
396
+ lineConfig.arrowConfig?.length,
397
+ lineConfig.arrowConfig?.width,
398
+ lineConfig.arrowConfig?.showArrowBase
399
+ )
393
400
 
394
- setArrowPoints(arrowPoints);
401
+ setArrowPoints(arrowPoints)
395
402
  }
396
403
  } else {
397
- let p1Array: Array<Array<number>> = [];
404
+ const p1Array: number[][] = []
398
405
  for (let i = 0; i < lineData.length; i++) {
399
- if (i < lineConfig.startIndex || i > lineConfig.endIndex) continue;
406
+ if (
407
+ i < (lineConfig.startIndex ?? 0) ||
408
+ i > (lineConfig.endIndex ?? 0)
409
+ ) {
410
+ continue
411
+ }
400
412
  const currentBarWidth =
401
- data?.[i]?.barWidth ?? props.barWidth ?? BarDefaults.barWidth;
413
+ data?.[i]?.barWidth ?? props.barWidth ?? BarDefaults.barWidth
402
414
  const currentValue = props.lineData
403
415
  ? props.lineData[i].value
404
416
  : props.stackData
@@ -406,7 +418,7 @@ export const useBarChart = (props: extendedBarChartPropsType) => {
406
418
  (total, item) => total + item.value,
407
419
  0
408
420
  )
409
- : data[i].value;
421
+ : data[i].value
410
422
  p1Array.push([
411
423
  getXForLineInBar(
412
424
  i,
@@ -421,22 +433,30 @@ export const useBarChart = (props: extendedBarChartPropsType) => {
421
433
  lineConfig.shiftY,
422
434
  containerHeight,
423
435
  lineConfig.isSecondary ? secondaryMaxValue : maxValue
424
- ),
425
- ]);
426
- let xx = svgPath(p1Array, lineConfig.curveType, lineConfig.curvature);
427
- setPoints(xx);
436
+ )
437
+ ])
438
+ const xx = svgPath(
439
+ p1Array,
440
+ lineConfig.curveType,
441
+ lineConfig.curvature
442
+ )
443
+ setPoints(xx)
428
444
  }
429
445
  }
430
446
  if (lineData2?.length) {
431
447
  if (!lineConfig2?.curved) {
432
448
  for (let i = 0; i < lineData2.length; i++) {
433
- if (i < lineConfig2.startIndex || i > lineConfig2.endIndex)
434
- continue;
449
+ if (
450
+ i < (lineConfig2.startIndex ?? 0) ||
451
+ i > (lineConfig2.endIndex ?? 0)
452
+ ) {
453
+ continue
454
+ }
435
455
  const currentBarWidth =
436
- data?.[i]?.barWidth ?? props.barWidth ?? BarDefaults.barWidth;
437
- const currentValue = lineData2[i].value;
456
+ data?.[i]?.barWidth ?? props.barWidth ?? BarDefaults.barWidth
457
+ const currentValue = lineData2[i].value
438
458
  pp2 +=
439
- "L" +
459
+ 'L' +
440
460
  getXForLineInBar(
441
461
  i,
442
462
  firstBarWidth,
@@ -445,24 +465,28 @@ export const useBarChart = (props: extendedBarChartPropsType) => {
445
465
  lineConfig2,
446
466
  spacing
447
467
  ) +
448
- " " +
468
+ ' ' +
449
469
  getYForLineInBar(
450
470
  currentValue,
451
471
  lineConfig2.shiftY,
452
472
  containerHeight,
453
473
  lineConfig2.isSecondary ? secondaryMaxValue : maxValue
454
474
  ) +
455
- " ";
475
+ ' '
456
476
  }
457
- setPoints2(pp2.replace("L", "M"));
477
+ setPoints2(pp2.replace('L', 'M'))
458
478
  } else {
459
- let p2Array: Array<Array<number>> = [];
479
+ const p2Array: number[][] = []
460
480
  for (let i = 0; i < lineData2.length; i++) {
461
- if (i < lineConfig2.startIndex || i > lineConfig2.endIndex)
462
- continue;
481
+ if (
482
+ i < (lineConfig2.startIndex ?? 0) ||
483
+ i > (lineConfig2.endIndex ?? 0)
484
+ ) {
485
+ continue
486
+ }
463
487
  const currentBarWidth =
464
- data?.[i]?.barWidth ?? props.barWidth ?? BarDefaults.barWidth;
465
- const currentValue = lineData2[i].value;
488
+ data?.[i]?.barWidth ?? props.barWidth ?? BarDefaults.barWidth
489
+ const currentValue = lineData2[i].value
466
490
  p2Array.push([
467
491
  getXForLineInBar(
468
492
  i,
@@ -477,14 +501,14 @@ export const useBarChart = (props: extendedBarChartPropsType) => {
477
501
  lineConfig2.shiftY,
478
502
  containerHeight,
479
503
  lineConfig2.isSecondary ? secondaryMaxValue : maxValue
480
- ),
481
- ]);
482
- let xx = svgPath(
504
+ )
505
+ ])
506
+ const xx = svgPath(
483
507
  p2Array,
484
508
  lineConfig2.curveType,
485
509
  lineConfig2.curvature
486
- );
487
- setPoints2(xx);
510
+ )
511
+ setPoints2(xx)
488
512
  }
489
513
  }
490
514
  }
@@ -509,86 +533,94 @@ export const useBarChart = (props: extendedBarChartPropsType) => {
509
533
  spacing,
510
534
  yAxisLabelWidth,
511
535
  lineConfig.showArrow,
512
- lineConfig.arrowConfig.length,
513
- lineConfig.arrowConfig.width,
514
- lineConfig.arrowConfig.showArrowBase,
515
- ]);
536
+ lineConfig.arrowConfig?.length,
537
+ lineConfig.arrowConfig?.width,
538
+ lineConfig.arrowConfig?.showArrowBase
539
+ ])
516
540
  useEffect(() => {
517
541
  if (initialPointerIndex !== -1) {
518
- const item = (props.stackData ?? data)?.[initialPointerIndex];
519
- const stackItem = props.stackData?.[initialPointerIndex];
542
+ const item = (props.stackData ?? data)?.[initialPointerIndex]
543
+ const stackItem = props.stackData?.[initialPointerIndex]
520
544
  const stackSum = stackItem?.stacks?.reduce(
521
545
  (acc, stack) => acc + (stack.value ?? 0),
522
546
  0
523
- );
547
+ )
524
548
  const x =
525
549
  initialSpacing +
526
550
  (spacing + barWidth) * initialPointerIndex -
527
- (pointerRadius || pointerWidth / 2) +
528
- barWidth / 2;
551
+ (pointerRadius ?? pointerWidth / 2) +
552
+ barWidth / 2
529
553
  const y =
530
554
  containerHeight -
531
555
  ((stackSum ?? data[initialPointerIndex].value) * containerHeight) /
532
556
  maxValue -
533
- (pointerRadius || pointerHeight / 2) +
534
- 10;
557
+ (pointerRadius ?? pointerHeight / 2) +
558
+ 10
535
559
  if (initialPointerAppearDelay) {
536
560
  setTimeout(() => {
537
- setPointerConfig(initialPointerIndex, item, x, y);
538
- }, initialPointerAppearDelay);
561
+ setPointerConfig(initialPointerIndex, item, x, y)
562
+ }, initialPointerAppearDelay)
539
563
  } else {
540
- setPointerConfig(initialPointerIndex, item, x, y);
564
+ setPointerConfig(initialPointerIndex, item, x, y)
541
565
  }
542
566
  }
543
- }, []);
544
-
545
- const setPointerConfig = (initialPointerIndex, item, x, y) => {
546
- setPointerIndex(initialPointerIndex);
547
- setPointerItem(item);
548
- setPointerX(x);
549
- setPointerY(y);
550
- };
567
+ }, [])
568
+
569
+ const setPointerConfig = (
570
+ initialPointerIndex: number,
571
+ item: barDataItem | stackDataItem,
572
+ x: number,
573
+ y: number
574
+ ): void => {
575
+ setPointerIndex(initialPointerIndex)
576
+ setPointerItem(item)
577
+ setPointerX(x)
578
+ setPointerY(y)
579
+ }
551
580
 
552
581
  const animatedHeight = heightValue.interpolate({
553
582
  inputRange: [0, 1],
554
- outputRange: ["0%", "100%"],
555
- });
583
+ outputRange: ['0%', '100%']
584
+ })
556
585
  const appearingOpacity = opacValue.interpolate({
557
586
  inputRange: [0, 1],
558
- outputRange: [0, 1],
559
- });
587
+ outputRange: [0, 1]
588
+ })
560
589
 
561
590
  const animatedWidth = widthValue.interpolate({
562
591
  inputRange: [0, 1],
563
- outputRange: [0, initialSpacing + totalWidth + endSpacing],
564
- });
592
+ outputRange: [0, initialSpacing + totalWidth + endSpacing]
593
+ })
565
594
 
566
- const getPropsCommonForBarAndStack = (item, index) => {
595
+ const getPropsCommonForBarAndStack = (
596
+ item: stackDataItem | barDataItem,
597
+ index: number
598
+ ) => {
567
599
  return {
568
600
  key: index,
569
- item: item,
570
- index: index,
571
- containerHeight: containerHeight,
572
- maxValue: maxValue,
601
+ item,
602
+ index,
603
+ containerHeight,
604
+ maxValue,
573
605
  spacing: item.spacing ?? spacing,
574
606
  propSpacing: spacing,
575
- xAxisThickness: xAxisThickness,
607
+ xAxisThickness,
576
608
  barWidth: props.barWidth,
577
- opacity: opacity,
578
- disablePress: item.disablePress || props.disablePress,
579
- rotateLabel: rotateLabel,
580
- showXAxisIndices: showXAxisIndices,
581
- xAxisIndicesHeight: xAxisIndicesHeight,
582
- xAxisIndicesWidth: xAxisIndicesWidth,
583
- xAxisIndicesColor: xAxisIndicesColor,
584
- horizontal: horizontal,
585
- rtl: rtl,
586
- intactTopLabel: intactTopLabel,
609
+ opacity,
610
+ disablePress: item.disablePress ?? props.disablePress,
611
+ rotateLabel,
612
+ showXAxisIndices,
613
+ xAxisIndicesHeight,
614
+ xAxisIndicesWidth,
615
+ xAxisIndicesColor,
616
+ horizontal,
617
+ rtl,
618
+ intactTopLabel,
587
619
  showValuesAsTopLabel: props.showValuesAsTopLabel,
588
620
  topLabelContainerStyle: props.topLabelContainerStyle,
589
621
  topLabelTextStyle: props.topLabelTextStyle,
590
622
  barBorderWidth: props.barBorderWidth,
591
- barBorderColor: barBorderColor,
623
+ barBorderColor,
592
624
  barBorderRadius: props.barBorderRadius,
593
625
  barBorderTopLeftRadius: props.barBorderTopLeftRadius,
594
626
  barBorderTopRightRadius: props.barBorderTopRightRadius,
@@ -605,27 +637,25 @@ export const useBarChart = (props: extendedBarChartPropsType) => {
605
637
  onPressOut: props.onPressOut,
606
638
  focusBarOnPress: props.focusBarOnPress,
607
639
  focusedBarConfig: props.focusedBarConfig,
608
- xAxisTextNumberOfLines: xAxisTextNumberOfLines,
640
+ xAxisTextNumberOfLines,
609
641
  xAxisLabelsHeight: props.xAxisLabelsHeight,
610
642
  xAxisLabelsVerticalShift,
611
643
  renderTooltip: props.renderTooltip,
612
- leftShiftForTooltip: props.leftShiftForTooltip || 0,
613
- initialSpacing: initialSpacing,
614
- selectedIndex: selectedIndex,
615
- setSelectedIndex: setSelectedIndex,
616
- activeOpacity: props.activeOpacity || 0.2,
644
+ leftShiftForTooltip: props.leftShiftForTooltip ?? 0,
645
+ initialSpacing,
646
+ selectedIndex,
647
+ setSelectedIndex,
648
+ activeOpacity: props.activeOpacity ?? 0.2,
617
649
  noOfSectionsBelowXAxis,
618
650
 
619
- leftShiftForLastIndexTooltip: props.leftShiftForLastIndexTooltip || 0,
651
+ leftShiftForLastIndexTooltip: props.leftShiftForLastIndexTooltip ?? 0,
620
652
  label:
621
- item.label ||
622
- (props.xAxisLabelTexts && props.xAxisLabelTexts[index]
623
- ? props.xAxisLabelTexts[index]
624
- : ""),
625
- labelTextStyle: item.labelTextStyle || props.xAxisLabelTextStyle,
626
- pointerConfig,
627
- };
628
- };
653
+ item.label ??
654
+ (props.xAxisLabelTexts?.[index] ? props.xAxisLabelTexts[index] : ''),
655
+ labelTextStyle: item.labelTextStyle ?? props.xAxisLabelTextStyle,
656
+ pointerConfig
657
+ }
658
+ }
629
659
 
630
660
  const barAndLineChartsWrapperProps: BarAndLineChartsWrapperTypes = {
631
661
  chartType: chartTypes.BAR,
@@ -642,8 +672,8 @@ export const useBarChart = (props: extendedBarChartPropsType) => {
642
672
  initialSpacing,
643
673
  data,
644
674
  stackData: props.stackData,
645
- secondaryData: secondaryData,
646
- barWidth: props.barWidth || BarDefaults.barWidth,
675
+ secondaryData,
676
+ barWidth: props.barWidth ?? BarDefaults.barWidth,
647
677
  xAxisThickness,
648
678
  totalWidth,
649
679
  disableScroll,
@@ -667,7 +697,7 @@ export const useBarChart = (props: extendedBarChartPropsType) => {
667
697
  points2,
668
698
  arrowPoints,
669
699
 
670
- //horizSectionProps-
700
+ // horizSectionProps-
671
701
  width: widthFromProps,
672
702
  horizSections,
673
703
  endSpacing,
@@ -700,8 +730,8 @@ export const useBarChart = (props: extendedBarChartPropsType) => {
700
730
 
701
731
  onEndReached: props.onEndReached,
702
732
  onStartReached: props.onStartReached,
703
- endReachedOffset: props.endReachedOffset ?? BarDefaults.endReachedOffset,
704
- };
733
+ endReachedOffset: props.endReachedOffset ?? BarDefaults.endReachedOffset
734
+ }
705
735
 
706
736
  return {
707
737
  lineConfig,
@@ -808,6 +838,6 @@ export const useBarChart = (props: extendedBarChartPropsType) => {
808
838
  getPointerProps,
809
839
  pointerIndex,
810
840
  getPropsCommonForBarAndStack,
811
- barAndLineChartsWrapperProps,
812
- };
813
- };
841
+ barAndLineChartsWrapperProps
842
+ }
843
+ }