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.
package/package.json CHANGED
@@ -1,19 +1,27 @@
1
1
  {
2
2
  "name": "gifted-charts-core",
3
- "version": "0.0.21",
3
+ "version": "0.0.23",
4
4
  "description": "Mathematical and logical utilities used by react-gifted-charts and react-native-gifted-charts",
5
5
  "main": "index.ts",
6
6
  "files": [
7
7
  "src"
8
8
  ],
9
9
  "scripts": {
10
- "test": "echo \"Error: no test specified\" && exit 1"
10
+ "test": "echo \"Error: no test specified\" && exit 1",
11
+ "lint": "eslint src/**/*.ts"
11
12
  },
12
- "dependencies": {},
13
13
  "devDependencies": {
14
14
  "@testing-library/jest-dom": "^5.17.0",
15
15
  "@testing-library/react": "^13.4.0",
16
- "@testing-library/user-event": "^13.5.0"
16
+ "@testing-library/user-event": "^13.5.0",
17
+ "@typescript-eslint/eslint-plugin": "^6.21.0",
18
+ "eslint": "^8.56.0",
19
+ "eslint-config-standard-with-typescript": "^43.0.1",
20
+ "eslint-plugin-import": "^2.29.1",
21
+ "eslint-plugin-n": "^16.6.2",
22
+ "eslint-plugin-promise": "^6.1.1",
23
+ "eslint-plugin-react": "^7.33.2",
24
+ "typescript": "^5.3.3"
17
25
  },
18
26
  "peerDependencies": {
19
27
  "react": "*",
@@ -1,8 +1,41 @@
1
- import { ViewStyle } from "react-native";
2
- import { getBarFrontColor, getBarWidth } from "../utils";
3
- import { CommonPropsFor2Dand3DbarsType } from "./types";
1
+ import { type ColorValue, type ViewStyle } from 'react-native'
2
+ import { getBarFrontColor, getBarWidth } from '../utils'
3
+ import {
4
+ type FocusedBarConfig,
5
+ type BarChartPropsType,
6
+ type CommonPropsFor2Dand3DbarsType,
7
+ type barDataItem,
8
+ type stackDataItem
9
+ } from './types'
10
+ import { type ReactNode } from 'react'
4
11
 
5
- export const getPropsForAnimated2DWithGradient = (props) => {
12
+ interface Animated2dWithFradientPropsType extends BarChartPropsType {
13
+ item: barDataItem
14
+ index: number
15
+ barHeight: number
16
+ selectedIndex: number
17
+ barBackgroundPattern: () => ReactNode
18
+ barInnerComponent: (
19
+ item?: stackDataItem | barDataItem,
20
+ index?: number
21
+ ) => ReactNode
22
+ patternId: string
23
+ barStyle: object
24
+ intactTopLabel: boolean
25
+ }
26
+
27
+ interface IgetPropsForAnimated2DWithGradientReturnType {
28
+ commonStyleForBar: ViewStyle[]
29
+ barStyleWithBackground: ViewStyle[]
30
+ commonPropsFor2Dand3Dbars: CommonPropsFor2Dand3DbarsType
31
+ isFocused?: boolean
32
+ focusedBarConfig?: FocusedBarConfig
33
+ localFrontColor: ColorValue
34
+ }
35
+
36
+ export const getPropsForAnimated2DWithGradient = (
37
+ props: Animated2dWithFradientPropsType
38
+ ): IgetPropsForAnimated2DWithGradientReturnType => {
6
39
  const {
7
40
  barBorderWidth,
8
41
  barBorderColor,
@@ -34,36 +67,37 @@ export const getPropsForAnimated2DWithGradient = (props) => {
34
67
  selectedIndex,
35
68
  focusBarOnPress,
36
69
  focusedBarConfig,
37
- isThreeD,
38
- } = props;
70
+ isThreeD
71
+ } = props
39
72
 
40
- const isFocused = focusBarOnPress && selectedIndex === index;
73
+ const isFocused = (focusBarOnPress ?? false) && selectedIndex === index
41
74
  const itemOrPropsBarBorderRadius =
42
- item.barBorderRadius ?? barBorderRadius ?? 0;
43
- const localBarBorderRadius = isFocused
44
- ? focusedBarConfig?.borderRadius ?? itemOrPropsBarBorderRadius
45
- : itemOrPropsBarBorderRadius;
75
+ item.barBorderRadius ?? barBorderRadius ?? 0
76
+ const localBarBorderRadius =
77
+ isFocused ?? false
78
+ ? focusedBarConfig?.borderRadius ?? itemOrPropsBarBorderRadius
79
+ : itemOrPropsBarBorderRadius
46
80
  const localBarWidth = getBarWidth(
47
81
  isFocused,
48
82
  focusedBarConfig,
49
83
  item.barWidth,
50
84
  barWidth
51
- );
85
+ )
52
86
  const localFrontColor = getBarFrontColor(
53
87
  isFocused,
54
88
  focusedBarConfig,
55
89
  item.frontColor,
56
90
  frontColor,
57
91
  isThreeD
58
- );
59
- const localGradientColor = item.gradientColor || gradientColor;
60
- const localOpacity = opacity || 1;
92
+ )
93
+ const localGradientColor = item.gradientColor ?? gradientColor
94
+ const localOpacity = opacity ?? 1
61
95
 
62
96
  const commonStyleForBar: ViewStyle[] = [
63
97
  {
64
- position: "absolute",
65
- width: "100%",
66
- height: "100%",
98
+ position: 'absolute',
99
+ width: '100%',
100
+ height: '100%',
67
101
  borderWidth: barBorderWidth ?? 0,
68
102
  borderColor: barBorderColor,
69
103
  borderRadius: localBarBorderRadius,
@@ -82,52 +116,52 @@ export const getPropsForAnimated2DWithGradient = (props) => {
82
116
  borderBottomRightRadius:
83
117
  item.barBorderBottomRightRadius ??
84
118
  barBorderBottomRightRadius ??
85
- localBarBorderRadius,
86
- },
87
- ];
119
+ localBarBorderRadius
120
+ }
121
+ ]
88
122
 
89
- if (roundedBottom || (isFocused && focusedBarConfig?.roundedBottom)) {
123
+ if (roundedBottom ?? (isFocused && focusedBarConfig?.roundedBottom) ?? false) {
90
124
  commonStyleForBar.push({
91
125
  borderBottomLeftRadius: localBarWidth / 2,
92
- borderBottomRightRadius: localBarWidth / 2,
93
- });
126
+ borderBottomRightRadius: localBarWidth / 2
127
+ })
94
128
  }
95
129
 
96
- if (cappedBars) {
130
+ if (cappedBars ?? false) {
97
131
  commonStyleForBar.push({
98
132
  borderTopLeftRadius:
99
- item.capRadius === 0 ? 0 : item.capRadius || capRadius || 0,
133
+ item.capRadius === 0 ? 0 : item.capRadius ?? capRadius ?? 0,
100
134
  borderTopRightRadius:
101
- item.capRadius === 0 ? 0 : item.capRadius || capRadius || 0,
102
- });
135
+ item.capRadius === 0 ? 0 : item.capRadius ?? capRadius ?? 0
136
+ })
103
137
  }
104
138
 
105
- if (roundedTop || (isFocused && focusedBarConfig?.roundedTop)) {
139
+ if (roundedTop ?? (isFocused && focusedBarConfig?.roundedTop) ?? false) {
106
140
  commonStyleForBar.push({
107
141
  borderTopLeftRadius: localBarWidth / 2,
108
- borderTopRightRadius: localBarWidth / 2,
109
- });
142
+ borderTopRightRadius: localBarWidth / 2
143
+ })
110
144
  }
111
145
  const barStyleWithBackground: ViewStyle[] = [
112
146
  ...commonStyleForBar,
113
147
  {
114
- backgroundColor: localFrontColor,
115
- },
116
- ];
148
+ backgroundColor: localFrontColor
149
+ }
150
+ ]
117
151
 
118
152
  const commonPropsFor2Dand3Dbars: CommonPropsFor2Dand3DbarsType = {
119
- barBackgroundPattern: item.barBackgroundPattern || barBackgroundPattern,
153
+ barBackgroundPattern: item.barBackgroundPattern ?? barBackgroundPattern,
120
154
  barInnerComponent: isFocused
121
155
  ? focusedBarConfig?.barInnerComponent ?? barInnerComponent
122
156
  : barInnerComponent,
123
- patternId: item.patternId || patternId,
157
+ patternId: item.patternId ?? patternId,
124
158
  barWidth: localBarWidth,
125
- barStyle: barStyle,
126
- item: item,
127
- index: index,
159
+ barStyle,
160
+ item,
161
+ index,
128
162
 
129
163
  frontColor: localFrontColor,
130
- showGradient: item.showGradient || showGradient || false,
164
+ showGradient: item.showGradient ?? showGradient ?? false,
131
165
  gradientColor: isFocused
132
166
  ? focusedBarConfig?.gradientColor ?? localGradientColor
133
167
  : localGradientColor,
@@ -136,10 +170,10 @@ export const getPropsForAnimated2DWithGradient = (props) => {
136
170
  : localOpacity,
137
171
  height: barHeight,
138
172
  intactTopLabel,
139
- showValuesAsTopLabel: !!showValuesAsTopLabel,
173
+ showValuesAsTopLabel: (showValuesAsTopLabel ?? false),
140
174
  topLabelContainerStyle,
141
- topLabelTextStyle,
142
- };
175
+ topLabelTextStyle
176
+ }
143
177
 
144
178
  return {
145
179
  commonStyleForBar,
@@ -147,6 +181,6 @@ export const getPropsForAnimated2DWithGradient = (props) => {
147
181
  commonPropsFor2Dand3Dbars,
148
182
  isFocused,
149
183
  focusedBarConfig,
150
- localFrontColor,
151
- };
152
- };
184
+ localFrontColor
185
+ }
186
+ }
@@ -1,5 +1,5 @@
1
- import { useState } from "react";
2
- import { StackedBarChartPropsType, stackDataItem } from "./types";
1
+ import { useState } from 'react'
2
+ import { type StackedBarChartPropsType, type stackDataItem } from './types'
3
3
 
4
4
  export const useRenderStackBars = (props: StackedBarChartPropsType) => {
5
5
  const {
@@ -10,45 +10,45 @@ export const useRenderStackBars = (props: StackedBarChartPropsType) => {
10
10
  propSpacing,
11
11
  initialSpacing,
12
12
  stackData,
13
- isAnimated,
14
- } = props;
15
- const cotainsNegative = item.stacks.some((item) => item.value < 0);
16
- const noAnimation = cotainsNegative || !isAnimated;
13
+ isAnimated
14
+ } = props
15
+ const cotainsNegative = item.stacks.some((item) => item.value < 0)
16
+ const noAnimation = cotainsNegative || !isAnimated
17
17
 
18
18
  const localBarInnerComponent =
19
- item.barInnerComponent ?? props.barInnerComponent;
19
+ item.barInnerComponent ?? props.barInnerComponent
20
20
 
21
21
  const {
22
22
  borderRadius,
23
23
  borderTopLeftRadius,
24
24
  borderTopRightRadius,
25
25
  borderBottomLeftRadius,
26
- borderBottomRightRadius,
27
- } = item;
26
+ borderBottomRightRadius
27
+ } = item
28
28
 
29
- let leftSpacing = initialSpacing;
29
+ let leftSpacing = initialSpacing
30
30
  for (let i = 0; i < index; i++) {
31
31
  leftSpacing +=
32
32
  (stackData[i].spacing ?? propSpacing ?? 0) +
33
- (stackData[i].stacks[0].barWidth ?? props.barWidth ?? 30);
33
+ (stackData[i].stacks[0].barWidth ?? props.barWidth ?? 30)
34
34
  }
35
- const disablePress = props.disablePress || false;
35
+ const disablePress = props.disablePress ?? false
36
36
 
37
37
  const totalHeight = props.item.stacks.reduce(
38
38
  (acc, stack) =>
39
39
  acc +
40
- (Math.abs(stack.value) * (containerHeight || 200)) / (maxValue || 200),
40
+ (Math.abs(stack.value) * (containerHeight ?? 200)) / (maxValue || 200),
41
41
  0
42
- );
42
+ )
43
43
 
44
- const [height, setHeight] = useState(noAnimation ? totalHeight : 1);
44
+ const [height, setHeight] = useState(noAnimation ? totalHeight : 1)
45
45
 
46
- const getBarHeight = (value: number, marginBottom?: number) => {
46
+ const getBarHeight = (value: number, marginBottom?: number): number => {
47
47
  return (
48
- (Math.abs(value) * (containerHeight || 200)) / (maxValue || 200) -
49
- (marginBottom || 0)
50
- );
51
- };
48
+ (Math.abs(value) * (containerHeight ?? 200)) / (maxValue || 200) -
49
+ (marginBottom ?? 0)
50
+ )
51
+ }
52
52
 
53
53
  const getPosition = (index: number) => {
54
54
  /* Returns bottom position for stack item
@@ -56,37 +56,37 @@ export const useRenderStackBars = (props: StackedBarChartPropsType) => {
56
56
  const height = getBarHeight(
57
57
  item.stacks[index].value,
58
58
  item.stacks[index].marginBottom
59
- );
59
+ )
60
60
 
61
- const itemValue = item.stacks[index].value;
62
- const isNegative = itemValue <= 0;
63
- let position = isNegative ? -(height || 0) : 0;
61
+ const itemValue = item.stacks[index].value
62
+ const isNegative = itemValue <= 0
63
+ let position = isNegative ? -(height || 0) : 0
64
64
 
65
65
  for (let i = 0; i < index; i++) {
66
- const valueOnIndex = item.stacks[i].value;
66
+ const valueOnIndex = item.stacks[i].value
67
67
  if (isNegative && valueOnIndex <= 0) {
68
68
  position +=
69
- (valueOnIndex * (containerHeight || 200)) / (maxValue || 200);
69
+ (valueOnIndex * (containerHeight ?? 200)) / (maxValue || 200)
70
70
  } else if (!isNegative && valueOnIndex >= 0) {
71
71
  position +=
72
- (valueOnIndex * (containerHeight || 200)) / (maxValue || 200);
72
+ (valueOnIndex * (containerHeight ?? 200)) / (maxValue || 200)
73
73
  }
74
74
  }
75
- return position;
76
- };
75
+ return position
76
+ }
77
77
 
78
- const getLowestPosition = () => {
78
+ const getLowestPosition = (): number => {
79
79
  return (
80
80
  item.stacks
81
81
  .map((_, index) => getPosition(index))
82
82
  .sort((a, b) => a - b)?.[0] || 0
83
- );
84
- };
83
+ )
84
+ }
85
85
 
86
- const lowestBarPosition = getLowestPosition();
86
+ const lowestBarPosition = getLowestPosition()
87
87
 
88
88
  const getStackBorderRadii = (item: stackDataItem, index: number) => {
89
- const stackItem = item.stacks[index];
89
+ const stackItem = item.stacks[index]
90
90
  const borderRadii = {
91
91
  borderTopLeftRadius:
92
92
  stackItem.borderTopLeftRadius ??
@@ -111,10 +111,10 @@ export const useRenderStackBars = (props: StackedBarChartPropsType) => {
111
111
  stackItem.borderRadius ??
112
112
  props.barBorderBottomRightRadius ??
113
113
  props.barBorderRadius ??
114
- 0,
115
- };
116
- return borderRadii;
117
- };
114
+ 0
115
+ }
116
+ return borderRadii
117
+ }
118
118
 
119
119
  return {
120
120
  cotainsNegative,
@@ -134,6 +134,6 @@ export const useRenderStackBars = (props: StackedBarChartPropsType) => {
134
134
  getPosition,
135
135
  getLowestPosition,
136
136
  lowestBarPosition,
137
- getStackBorderRadii,
138
- };
139
- };
137
+ getStackBorderRadii
138
+ }
139
+ }