gifted-charts-core 0.0.20 → 0.0.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.
@@ -1,104 +1,135 @@
1
- import { useEffect, useState } from "react";
2
- import { PieChartPropsType } from "./types";
3
- import { getTextSizeForPieLabels } from "../utils";
1
+ import { useEffect, useState } from 'react'
2
+ import { type pieDataItem, type PieChartPropsType } from './types'
3
+ import { getTextSizeForPieLabels } from '../utils'
4
+ import { type ColorValue } from 'react-native'
4
5
 
5
- export const usePieChart = (props: PieChartPropsType) => {
6
- const radius = props.radius || 120;
6
+ interface IusePieChart {
7
+ radius: number
8
+ extraRadiusForFocused: number
9
+ pi: number
10
+ selectedIndex: number
11
+ setSelectedIndex: (index: number) => void
12
+ startAngle: number
13
+ setStartAngle: (angle: number) => void
14
+ total: number
15
+ setTotal: (total: number) => void
16
+ data: pieDataItem[]
17
+ donut?: boolean
18
+ isThreeD?: boolean
19
+ semiCircle?: boolean
20
+ inwardExtraLengthForFocused: number
21
+ canvasWidth: number
22
+ canvasHeight: number
23
+ strokeWidth: number
24
+ innerRadius: number
25
+ innerCircleColor: ColorValue
26
+ innerCircleBorderWidth: number
27
+ innerCircleBorderColor: ColorValue
28
+ shiftInnerCenterX: number
29
+ shiftInnerCenterY: number
30
+ tiltAngle: string
31
+ isDataShifted: boolean
32
+ paddingHorizontal: number
33
+ paddingVertical: number
34
+ }
35
+
36
+ export const usePieChart = (props: PieChartPropsType): IusePieChart => {
37
+ const radius = props.radius ?? 120
7
38
  const extraRadiusForFocused =
8
39
  props.extraRadiusForFocused ??
9
- (props.focusOnPress || props.sectionAutoFocus ? radius / 10 : 0);
10
- const pi = props.semiCircle ? Math.PI / 2 : Math.PI;
11
- const [selectedIndex, setSelectedIndex] = useState(-1); // at the start, nothing is selected
40
+ (props.focusOnPress ?? props.sectionAutoFocus ? radius / 10 : 0)
41
+ const pi = props.semiCircle ? Math.PI / 2 : Math.PI
42
+ const [selectedIndex, setSelectedIndex] = useState(-1) // at the start, nothing is selected
12
43
  // because we're going to use a useEffect, we need startAngle and total to be state variables
13
44
  const [startAngle, setStartAngle] = useState(
14
- props.initialAngle || (props.semiCircle ? -pi : 0)
15
- );
16
- const [total, setTotal] = useState(0);
45
+ props.initialAngle ?? (props.semiCircle ? -pi : 0)
46
+ )
47
+ const [total, setTotal] = useState(0)
17
48
 
18
49
  useEffect(() => {
19
50
  // Update the total, this could be use to replace the forEach : const newTotal = props.data.reduce((acc, item) => acc + item.value, 0);
20
- let newTotal = 0;
51
+ let newTotal = 0
21
52
  props.data.forEach((item) => {
22
- newTotal += item.value;
23
- });
24
- setTotal(newTotal);
53
+ newTotal += item.value
54
+ })
55
+ setTotal(newTotal)
25
56
 
26
57
  // Update selectedIndex based on focused item
27
58
  const newSelectedIndex = props.data.findIndex(
28
59
  (item) => item.focused === true
29
- );
30
- setSelectedIndex(newSelectedIndex);
60
+ )
61
+ setSelectedIndex(newSelectedIndex)
31
62
 
32
63
  // Calculate the new start angle
33
- let newStartAngle = props.initialAngle || (props.semiCircle ? -pi : 0);
64
+ const newStartAngle = props.initialAngle ?? (props.semiCircle ? -pi : 0)
34
65
  if (newSelectedIndex !== -1) {
35
66
  // it was !== 0 here before, which would not work, it's either !==-1 or >=0
36
67
  // This could be used to replace the for loop that was used before
37
68
  const sumBeforeSelectedIndex = props.data
38
69
  .slice(0, newSelectedIndex)
39
- .reduce((acc, item) => acc + item.value, 0);
70
+ .reduce((acc, item) => acc + item.value, 0)
40
71
  setStartAngle(
41
72
  newStartAngle + (2 * pi * sumBeforeSelectedIndex) / (newTotal || 1)
42
- );
73
+ )
43
74
  } else {
44
- setStartAngle(newStartAngle);
75
+ setStartAngle(newStartAngle)
45
76
  }
46
- }, [props.data, props.initialAngle, props.semiCircle]);
77
+ }, [props.data, props.initialAngle, props.semiCircle])
47
78
 
48
79
  useEffect(() => {
49
80
  if (selectedIndex !== -1) {
50
- const newStartAngle = props.initialAngle || (props.semiCircle ? -pi : 0);
51
- let start = 0;
81
+ const newStartAngle = props.initialAngle ?? (props.semiCircle ? -pi : 0)
82
+ let start = 0
52
83
  for (let i = 0; i < selectedIndex; i++) {
53
- start += props.data[i].value;
84
+ start += props.data[i].value
54
85
  }
55
86
  if (total) {
56
- setStartAngle(newStartAngle + (2 * pi * start) / (total || 1));
87
+ setStartAngle(newStartAngle + (2 * pi * start) / (total || 1))
57
88
  }
58
89
  }
59
- }, [selectedIndex]);
90
+ }, [selectedIndex])
60
91
 
61
92
  const {
62
93
  data,
63
94
  donut,
64
95
  isThreeD,
65
96
  semiCircle,
66
- inwardExtraLengthForFocused = 0,
67
- } = props;
97
+ inwardExtraLengthForFocused = 0
98
+ } = props
68
99
 
69
- const canvasWidth = radius * 2;
70
- const canvasHeight = isThreeD ? radius * 2.3 : radius * 2;
100
+ const canvasWidth = radius * 2
101
+ const canvasHeight = isThreeD ? radius * 2.3 : radius * 2
71
102
 
72
- const strokeWidth = props.strokeWidth || 0;
73
- const innerRadius = props.innerRadius || radius / 2.5;
103
+ const strokeWidth = props.strokeWidth ?? 0
104
+ const innerRadius = props.innerRadius ?? radius / 2.5
74
105
  const innerCircleColor =
75
- props.innerCircleColor || props.backgroundColor || "white";
106
+ props.innerCircleColor ?? props.backgroundColor ?? 'white'
76
107
  const innerCircleBorderWidth =
77
- props.innerCircleBorderWidth ||
78
- (props.innerCircleBorderColor ? strokeWidth || 2 : 0);
79
- const innerCircleBorderColor = props.innerCircleBorderColor || "lightgray";
80
- const shiftInnerCenterX = props.shiftInnerCenterX || 0;
81
- const shiftInnerCenterY = props.shiftInnerCenterY || 0;
108
+ props.innerCircleBorderWidth ??
109
+ (props.innerCircleBorderColor ? strokeWidth || 2 : 0)
110
+ const innerCircleBorderColor = props.innerCircleBorderColor ?? 'lightgray'
111
+ const shiftInnerCenterX = props.shiftInnerCenterX ?? 0
112
+ const shiftInnerCenterY = props.shiftInnerCenterY ?? 0
82
113
 
83
- let tiltAngle = props.tiltAngle || "55deg";
114
+ const tiltAngle = props.tiltAngle ?? '55deg'
84
115
 
85
- let isDataShifted = false;
116
+ let isDataShifted = false
86
117
 
87
118
  data.forEach((item: any) => {
88
119
  if (item.shiftX || item.shiftY) {
89
- isDataShifted = true;
120
+ isDataShifted = true
90
121
  }
91
- });
92
- const textSize = getTextSizeForPieLabels(props.textSize ?? 0, radius);
122
+ })
123
+ const textSize = getTextSizeForPieLabels(props.textSize ?? 0, radius)
93
124
 
94
125
  const paddingHorizontal =
95
- props.paddingHorizontal ?? props.labelsPosition === "onBorder"
126
+ props.paddingHorizontal ?? props.labelsPosition === 'onBorder'
96
127
  ? (props.textBackgroundRadius ?? textSize) * 2 + 6
97
- : 0;
128
+ : 0
98
129
  const paddingVertical =
99
- props.paddingVertical ?? props.labelsPosition === "onBorder"
130
+ props.paddingVertical ?? props.labelsPosition === 'onBorder'
100
131
  ? (props.textBackgroundRadius ?? textSize) * 2 + 6
101
- : 0;
132
+ : 0
102
133
 
103
134
  return {
104
135
  radius,
@@ -127,6 +158,6 @@ export const usePieChart = (props: PieChartPropsType) => {
127
158
  tiltAngle,
128
159
  isDataShifted,
129
160
  paddingHorizontal,
130
- paddingVertical,
131
- };
132
- };
161
+ paddingVertical
162
+ }
163
+ }
@@ -1,5 +1,5 @@
1
- import { getTextSizeForPieLabels } from "../utils";
2
- import { PieChartMainProps, pieDataItem } from "./types";
1
+ import { getTextSizeForPieLabels } from '../utils'
2
+ import { type PieChartMainProps, type pieDataItem } from './types'
3
3
 
4
4
  export const getPieChartMainProps = (props: PieChartMainProps) => {
5
5
  const {
@@ -7,120 +7,120 @@ export const getPieChartMainProps = (props: PieChartMainProps) => {
7
7
  isBiggerPie,
8
8
  paddingHorizontal,
9
9
  paddingVertical,
10
- extraRadiusForFocused,
11
- } = props;
12
- const propData = props.data;
13
- const data: Array<pieDataItem> = [];
14
- let itemHasInnerComponent = false;
10
+ extraRadiusForFocused
11
+ } = props
12
+ const propData = props.data
13
+ const data: pieDataItem[] = []
14
+ let itemHasInnerComponent = false
15
15
  if (propData) {
16
16
  for (let i = 0; i < propData.length; i++) {
17
- if (propData[i].pieInnerComponent) itemHasInnerComponent = true;
17
+ if (propData[i].pieInnerComponent) itemHasInnerComponent = true
18
18
  if (propData[i].value !== 0) {
19
- data.push(propData[i]);
19
+ data.push(propData[i])
20
20
  } else {
21
21
  data.push({
22
22
  ...propData[i],
23
23
  value:
24
24
  props.data.map((item) => item.value).reduce((v, a) => v + a) /
25
- 160000,
26
- });
25
+ 160000
26
+ })
27
27
  }
28
28
  }
29
29
  }
30
- const showInnerComponent = !!props.pieInnerComponent || itemHasInnerComponent;
30
+ const showInnerComponent = !!props.pieInnerComponent || itemHasInnerComponent
31
31
 
32
- const radius = props.radius || 120;
33
- const canvasWidth = radius * 2;
34
- const canvasHeight = isThreeD ? radius * 2.3 : radius * 2;
35
- const shadowWidth = props.shadowWidth || radius / 5;
36
- const backgroundColor = props.backgroundColor || "transparent";
37
- const shadowColor = props.shadowColor || "lightgray";
38
- const semiCircle = props.semiCircle || false;
32
+ const radius = props.radius ?? 120
33
+ const canvasWidth = radius * 2
34
+ const canvasHeight = isThreeD ? radius * 2.3 : radius * 2
35
+ const shadowWidth = props.shadowWidth ?? radius / 5
36
+ const backgroundColor = props.backgroundColor ?? 'transparent'
37
+ const shadowColor = props.shadowColor ?? 'lightgray'
38
+ const semiCircle = props.semiCircle ?? false
39
39
 
40
- let pi = Math.PI;
41
- const initialAngle = props.initialAngle || (semiCircle ? pi / -2 : 0);
42
- const shadow = props.shadow || false;
43
- const donut = props.donut || false;
44
- const strokeWidth = props.strokeWidth || 0;
40
+ let pi = Math.PI
41
+ const initialAngle = props.initialAngle ?? (semiCircle ? pi / -2 : 0)
42
+ const shadow = props.shadow ?? false
43
+ const donut = props.donut ?? false
44
+ const strokeWidth = props.strokeWidth ?? 0
45
45
  const strokeColor =
46
- props.strokeColor || (strokeWidth ? "gray" : "transparent");
47
- const innerRadius = props.innerRadius || radius / 2.5;
46
+ props.strokeColor ?? (strokeWidth ? 'gray' : 'transparent')
47
+ const innerRadius = props.innerRadius ?? radius / 2.5
48
48
 
49
- const showText = props.showText || false;
50
- const textColor = props.textColor || "";
51
- const textSize = getTextSizeForPieLabels(props.textSize ?? 0, radius);
52
- let tiltAngle = props.tiltAngle || "55deg";
49
+ const showText = props.showText ?? false
50
+ const textColor = props.textColor ?? ''
51
+ const textSize = getTextSizeForPieLabels(props.textSize ?? 0, radius)
52
+ let tiltAngle = props.tiltAngle ?? '55deg'
53
53
  if (
54
54
  tiltAngle &&
55
55
  !isNaN(Number(tiltAngle)) &&
56
- !(tiltAngle + "").endsWith("deg")
56
+ !(tiltAngle + '').endsWith('deg')
57
57
  ) {
58
- tiltAngle += "deg";
58
+ tiltAngle += 'deg'
59
59
  }
60
60
 
61
61
  // const tilt = props.tilt ? Math.min(props.tilt, 1) : props.isThreeD ? 0.5 : 1;
62
62
  const labelsPosition = props.labelsPosition
63
63
  ? props.labelsPosition
64
64
  : donut || props.centerLabelComponent
65
- ? "outward"
66
- : "mid";
65
+ ? 'outward'
66
+ : 'mid'
67
67
 
68
- const showTextBackground = props.showTextBackground || false;
69
- const textBackgroundColor = props.textBackgroundColor || "white";
70
- const showValuesAsLabels = props.showValuesAsLabels || false;
71
- const showGradient = props.showGradient || false;
72
- const gradientCenterColor = props.gradientCenterColor || "white";
73
- const toggleFocusOnPress = props.toggleFocusOnPress ?? true;
68
+ const showTextBackground = props.showTextBackground ?? false
69
+ const textBackgroundColor = props.textBackgroundColor ?? 'white'
70
+ const showValuesAsLabels = props.showValuesAsLabels ?? false
71
+ const showGradient = props.showGradient ?? false
72
+ const gradientCenterColor = props.gradientCenterColor ?? 'white'
73
+ const toggleFocusOnPress = props.toggleFocusOnPress ?? true
74
74
 
75
- let minShiftX = 0,
76
- maxShiftX = 0,
77
- minShiftY = 0,
78
- maxShiftY = 0,
79
- total = 0;
75
+ let minShiftX = 0
76
+ let maxShiftX = 0
77
+ let minShiftY = 0
78
+ let maxShiftY = 0
79
+ let total = 0
80
80
 
81
81
  data.forEach((item: any) => {
82
82
  if (item.shiftX || item.shiftY) {
83
83
  if (minShiftX > item.shiftX) {
84
- minShiftX = item.shiftX;
84
+ minShiftX = item.shiftX
85
85
  }
86
86
  if (minShiftY > item.shiftY) {
87
- minShiftY = item.shiftY;
87
+ minShiftY = item.shiftY
88
88
  }
89
89
  if (maxShiftX < item.shiftX) {
90
- maxShiftX = item.shiftX;
90
+ maxShiftX = item.shiftX
91
91
  }
92
92
  if (maxShiftY < item.shiftY) {
93
- maxShiftY = item.shiftY;
93
+ maxShiftY = item.shiftY
94
94
  }
95
95
  }
96
- });
96
+ })
97
97
 
98
- const horizAdjustment = maxShiftX - minShiftX;
99
- const vertAdjustment = maxShiftY - minShiftY;
98
+ const horizAdjustment = maxShiftX - minShiftX
99
+ const vertAdjustment = maxShiftY - minShiftY
100
100
 
101
101
  if (semiCircle) {
102
- pi = Math.PI / 2;
102
+ pi = Math.PI / 2
103
103
  }
104
104
 
105
- let cx = radius,
106
- cy = radius;
105
+ const cx = radius
106
+ const cy = radius
107
107
 
108
108
  total =
109
- data && data.length
109
+ data && data.length > 0
110
110
  ? data.map((item) => item.value).reduce((v, a) => v + a)
111
- : 0;
112
- let acc = 0;
111
+ : 0
112
+ let acc = 0
113
113
  let pData = data.map((item) => {
114
- acc += item.value / total;
115
- return acc;
116
- });
117
- acc = 0;
118
- let mData = data.map((item) => {
119
- let pAcc = acc;
120
- acc += item.value / total;
121
- return pAcc + (acc - pAcc) / 2;
122
- });
123
- pData = [0, ...pData];
114
+ acc += item.value / total
115
+ return acc
116
+ })
117
+ acc = 0
118
+ const mData = data.map((item) => {
119
+ const pAcc = acc
120
+ acc += item.value / total
121
+ return pAcc + (acc - pAcc) / 2
122
+ })
123
+ pData = [0, ...pData]
124
124
 
125
125
  return {
126
126
  isThreeD,
@@ -168,6 +168,6 @@ export const getPieChartMainProps = (props: PieChartMainProps) => {
168
168
  acc,
169
169
  paddingHorizontal,
170
170
  paddingVertical,
171
- extraRadiusForFocused,
172
- };
173
- };
171
+ extraRadiusForFocused
172
+ }
173
+ }
@@ -1,89 +1,89 @@
1
- import { ColorValue } from "react-native";
2
- import { FontStyle } from "react-native-svg";
1
+ import { type ColorValue } from 'react-native'
2
+ import { type FontStyle } from 'react-native-svg'
3
3
 
4
- export type PieChartPropsType = {
5
- radius?: number;
6
- isThreeD?: boolean;
7
- donut?: boolean;
8
- innerRadius?: number;
9
- shadow?: boolean;
10
- innerCircleColor?: ColorValue;
11
- innerCircleBorderWidth?: number;
12
- innerCircleBorderColor?: ColorValue;
13
- shiftInnerCenterX?: number;
14
- shiftInnerCenterY?: number;
15
- shadowColor?: string;
16
- shadowWidth?: number;
17
- strokeWidth?: number;
18
- strokeColor?: string;
19
- backgroundColor?: string;
20
- data: Array<pieDataItem>;
21
- semiCircle?: boolean;
4
+ export interface PieChartPropsType {
5
+ radius?: number
6
+ isThreeD?: boolean
7
+ donut?: boolean
8
+ innerRadius?: number
9
+ shadow?: boolean
10
+ innerCircleColor?: ColorValue
11
+ innerCircleBorderWidth?: number
12
+ innerCircleBorderColor?: ColorValue
13
+ shiftInnerCenterX?: number
14
+ shiftInnerCenterY?: number
15
+ shadowColor?: string
16
+ shadowWidth?: number
17
+ strokeWidth?: number
18
+ strokeColor?: string
19
+ backgroundColor?: string
20
+ data: pieDataItem[]
21
+ semiCircle?: boolean
22
22
 
23
- showText?: boolean;
24
- textColor?: string;
25
- textSize?: number;
26
- fontStyle?: FontStyle;
27
- fontWeight?: string;
28
- font?: string;
29
- showTextBackground?: boolean;
30
- textBackgroundColor?: string;
31
- textBackgroundRadius?: number;
32
- showValuesAsLabels?: boolean;
23
+ showText?: boolean
24
+ textColor?: string
25
+ textSize?: number
26
+ fontStyle?: FontStyle
27
+ fontWeight?: string
28
+ font?: string
29
+ showTextBackground?: boolean
30
+ textBackgroundColor?: string
31
+ textBackgroundRadius?: number
32
+ showValuesAsLabels?: boolean
33
33
 
34
- centerLabelComponent?: Function;
35
- tiltAngle?: string;
36
- initialAngle?: number;
37
- labelsPosition?: "onBorder" | "outward" | "inward" | "mid";
38
- showGradient?: boolean;
39
- gradientCenterColor?: string;
40
- onPress?: Function;
41
- focusOnPress?: boolean;
42
- toggleFocusOnPress?: boolean;
43
- selectedIndex?: number;
44
- setSelectedIndex?: Function;
45
- sectionAutoFocus?: boolean;
46
- onLabelPress?: Function;
47
- extraRadiusForFocused?: number;
48
- inwardExtraLengthForFocused?: number;
49
- pieInnerComponent?: (item?: pieDataItem, index?: number) => any;
50
- pieInnerComponentHeight?: number;
51
- pieInnerComponentWidth?: number;
52
- paddingHorizontal?: number;
53
- paddingVertical?: number;
54
- };
55
- export type pieDataItem = {
56
- value: number;
57
- shiftX?: number;
58
- shiftY?: number;
59
- color?: string;
60
- gradientCenterColor?: string;
61
- text?: string;
62
- textColor?: string;
63
- textSize?: number;
64
- fontStyle?: FontStyle;
65
- fontWeight?: string;
66
- font?: string;
67
- textBackgroundColor?: string;
68
- textBackgroundRadius?: number;
69
- shiftTextX?: number;
70
- shiftTextY?: number;
71
- shiftTextBackgroundX?: number;
72
- shiftTextBackgroundY?: number;
73
- labelPosition?: "onBorder" | "outward" | "inward" | "mid";
74
- onPress?: Function;
75
- onLabelPress?: Function;
76
- strokeWidth?: number;
77
- strokeColor?: string;
78
- focused?: boolean;
79
- peripheral?: boolean;
80
- pieInnerComponent?: (item?: pieDataItem, index?: number) => any;
81
- };
34
+ centerLabelComponent?: Function
35
+ tiltAngle?: string
36
+ initialAngle?: number
37
+ labelsPosition?: 'onBorder' | 'outward' | 'inward' | 'mid'
38
+ showGradient?: boolean
39
+ gradientCenterColor?: string
40
+ onPress?: Function
41
+ focusOnPress?: boolean
42
+ toggleFocusOnPress?: boolean
43
+ selectedIndex?: number
44
+ setSelectedIndex?: Function
45
+ sectionAutoFocus?: boolean
46
+ onLabelPress?: Function
47
+ extraRadiusForFocused?: number
48
+ inwardExtraLengthForFocused?: number
49
+ pieInnerComponent?: (item?: pieDataItem, index?: number) => any
50
+ pieInnerComponentHeight?: number
51
+ pieInnerComponentWidth?: number
52
+ paddingHorizontal?: number
53
+ paddingVertical?: number
54
+ }
55
+ export interface pieDataItem {
56
+ value: number
57
+ shiftX?: number
58
+ shiftY?: number
59
+ color?: string
60
+ gradientCenterColor?: string
61
+ text?: string
62
+ textColor?: string
63
+ textSize?: number
64
+ fontStyle?: FontStyle
65
+ fontWeight?: string
66
+ font?: string
67
+ textBackgroundColor?: string
68
+ textBackgroundRadius?: number
69
+ shiftTextX?: number
70
+ shiftTextY?: number
71
+ shiftTextBackgroundX?: number
72
+ shiftTextBackgroundY?: number
73
+ labelPosition?: 'onBorder' | 'outward' | 'inward' | 'mid'
74
+ onPress?: Function
75
+ onLabelPress?: Function
76
+ strokeWidth?: number
77
+ strokeColor?: string
78
+ focused?: boolean
79
+ peripheral?: boolean
80
+ pieInnerComponent?: (item?: pieDataItem, index?: number) => any
81
+ }
82
82
 
83
83
  export interface PieChartMainProps extends PieChartPropsType {
84
- setSelectedIndex: Function;
85
- isBiggerPie?: boolean;
86
- paddingHorizontal: number;
87
- paddingVertical: number;
88
- extraRadiusForFocused: number;
84
+ setSelectedIndex: Function
85
+ isBiggerPie?: boolean
86
+ paddingHorizontal: number
87
+ paddingVertical: number
88
+ extraRadiusForFocused: number
89
89
  }