gifted-charts-core 0.0.1

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/README.md ADDED
@@ -0,0 +1 @@
1
+ This library contains the common code used by [react-native-gifted-charts](https://github.com/Abhinandan-Kushwaha/react-native-gifted-charts) and react-gifted-charts.
package/index.ts ADDED
@@ -0,0 +1,132 @@
1
+ /***********************************************************************************************************************/
2
+ /***************************************** Bar Chart *****************************************/
3
+ /***********************************************************************************************************************/
4
+
5
+ export { useBarChart } from "./src/BarChart";
6
+ export { getPropsForAnimated2DWithGradient } from "./src/BarChart/Animated2DWithGradient";
7
+ export { useRenderStackBars } from "./src/BarChart/RenderStackBars";
8
+ export {
9
+ stackDataItem,
10
+ StackedBarChartPropsType,
11
+ BarChartPropsType,
12
+ defaultLineConfigType,
13
+ barDataItem,
14
+ Animated2DWithGradientPropsType,
15
+ RenderBarsPropsType,
16
+ trianglePropTypes,
17
+ animatedBarPropTypes,
18
+ } from "./src/BarChart/types";
19
+
20
+ /************************************************************************************************************************/
21
+ /***************************************** Line Chart *****************************************/
22
+ /************************************************************************************************************************/
23
+
24
+ export { useLineChart } from "./src/LineChart";
25
+ export { useLineChartBiColor } from "./src/LineChart/LineChartBiColor";
26
+ export {
27
+ LineChartPropsType,
28
+ lineDataItem,
29
+ bicolorLineDataItem,
30
+ LineChartBicolorPropsType,
31
+ } from "./src/LineChart/types";
32
+
33
+ /***********************************************************************************************************************/
34
+ /***************************************** Pie Chart *****************************************/
35
+ /***********************************************************************************************************************/
36
+
37
+ export { usePieChart } from "./src/PieChart";
38
+ export { getPieChartMainProps } from "./src/PieChart/main";
39
+ export { PieChartPropsType, pieDataItem } from "./src/PieChart/types";
40
+
41
+ /***********************************************************************************************************************/
42
+ /************************************ Population Pyramid Chart ************************************/
43
+ /***********************************************************************************************************************/
44
+
45
+ export { usePopulationPyramid } from "./src/PopulationPyramid";
46
+ export {
47
+ popnPyramidDataItem,
48
+ RulesProps,
49
+ PopulationPyramidPropsType,
50
+ } from "./src/PopulationPyramid/types";
51
+
52
+ /***********************************************************************************************************************/
53
+ /************************************ Common Components ************************************/
54
+ /***********************************************************************************************************************/
55
+
56
+ export { useAnimatedThreeDBar } from "./src/components/AnimatedThreeDBar";
57
+ export { getHorizSectionVals } from "./src/components/BarAndLineChartsWrapper/getHorizSectionsVals";
58
+ export { useBarAndLineChartsWrapper } from "./src/components/BarAndLineChartsWrapper";
59
+ export { getTopAndLeftForStripAndLabel } from "./src/components/common/StripAndLabel";
60
+
61
+ /***********************************************************************************************************************/
62
+ /********************************* common utils, constants and types ********************************/
63
+ /***********************************************************************************************************************/
64
+
65
+ export {
66
+ rnVersion,
67
+ getCumulativeWidth,
68
+ getLighterColor,
69
+ svgQuadraticCurvePath,
70
+ svgPath,
71
+ bezierCommand,
72
+ getSegmentString,
73
+ getCurvePathWithSegments,
74
+ getPreviousSegmentsLastPoint,
75
+ getPathWithHighlight,
76
+ getRegionPathObjects,
77
+ getSegmentedPathObjects,
78
+ getArrowPoints,
79
+ getAxesAndRulesProps,
80
+ getExtendedContainerHeightWithPadding,
81
+ getSecondaryDataWithOffsetIncluded,
82
+ getArrowProperty,
83
+ getAllArrowProperties,
84
+ maxAndMinUtil,
85
+ computeMaxAndMinItems,
86
+ getLabelTextUtil,
87
+ getXForLineInBar,
88
+ getYForLineInBar,
89
+ clone,
90
+ getLineConfigForBarChart,
91
+ } from "./src/utils";
92
+
93
+ export {
94
+ chartTypes,
95
+ screenWidth,
96
+ yAxisSides,
97
+ loc,
98
+ SEGMENT_START,
99
+ SEGMENT_END,
100
+ RANGE_ENTER,
101
+ RANGE_EXIT,
102
+ STOP,
103
+ ruleTypes,
104
+ AxesAndRulesDefaults,
105
+ defaultArrowConfig,
106
+ BarDefaults,
107
+ defaultLineConfig,
108
+ LineDefaults,
109
+ defaultPointerConfig,
110
+ pieColors,
111
+ populationDefaults,
112
+ } from "./src/utils/constants";
113
+
114
+ export {
115
+ RuleType,
116
+ RuleTypes,
117
+ RulesConfig,
118
+ PointerEvents,
119
+ secondaryYAxisType,
120
+ secondaryLineConfigType,
121
+ referenceConfigType,
122
+ arrowConfigType,
123
+ horizSectionPropTypes,
124
+ HorizSectionsType,
125
+ BarAndLineChartsWrapperTypes,
126
+ Pointer,
127
+ HighlightedRange,
128
+ LineSegment,
129
+ LineSvgProps,
130
+ LineProperties,
131
+ DataSet,
132
+ } from "./src/utils/types";
package/package.json ADDED
@@ -0,0 +1,55 @@
1
+ {
2
+ "name": "gifted-charts-core",
3
+ "version": "0.0.1",
4
+ "description": "Mathematical and logical utilities used by react-gifted-charts and react-native-gifted-charts",
5
+ "main": "index.ts",
6
+ "files": [
7
+ "src"
8
+ ],
9
+ "scripts": {
10
+ "test": "echo \"Error: no test specified\" && exit 1"
11
+ },
12
+ "dependencies": {},
13
+ "devDependencies": {
14
+ "@testing-library/jest-dom": "^5.17.0",
15
+ "@testing-library/react": "^13.4.0",
16
+ "@testing-library/user-event": "^13.5.0"
17
+ },
18
+ "peerDependencies": {
19
+ "react": "*",
20
+ "react-native": "*"
21
+ },
22
+ "eslintConfig": {
23
+ "extends": [
24
+ "react-app",
25
+ "react-app/jest"
26
+ ]
27
+ },
28
+ "browserslist": {
29
+ "production": [
30
+ ">0.2%",
31
+ "not dead",
32
+ "not op_mini all"
33
+ ],
34
+ "development": [
35
+ "last 1 chrome version",
36
+ "last 1 firefox version",
37
+ "last 1 safari version"
38
+ ]
39
+ },
40
+ "repository": "https://github.com/Abhinandan-Kushwaha/gifted-charts-core",
41
+ "author": "Abhinandan Kushwaha <kushabhi5@gmail.com> (https://github.com/Abhinandan-Kushwaha)",
42
+ "license": "MIT",
43
+ "bugs": {
44
+ "url": "https://github.com/Abhinandan-Kushwaha/gifted-charts-core/issues"
45
+ },
46
+ "homepage": "https://github.com/Abhinandan-Kushwaha/gifted-charts-core#readme",
47
+ "publishConfig": {
48
+ "registry": "https://registry.npmjs.org/"
49
+ },
50
+ "keywords": [
51
+ "gifted-charts",
52
+ "react-gifted-charts",
53
+ "react-native-gifted-charts"
54
+ ]
55
+ }
@@ -0,0 +1,112 @@
1
+ import { ViewStyle } from "react-native";
2
+
3
+ export const getPropsForAnimated2DWithGradient = (props) => {
4
+ const {
5
+ barBorderWidth,
6
+ barBorderColor,
7
+ barBorderRadius,
8
+ item,
9
+ barBorderTopLeftRadius,
10
+ barBorderTopRightRadius,
11
+ barBorderBottomLeftRadius,
12
+ barBorderBottomRightRadius,
13
+ barWidth,
14
+ localBarInnerComponent,
15
+ barStyle,
16
+ index,
17
+ opacity,
18
+ barHeight,
19
+ intactTopLabel,
20
+ showValuesAsTopLabel,
21
+ topLabelContainerStyle,
22
+ topLabelTextStyle,
23
+ roundedBottom,
24
+ cappedBars,
25
+ capRadius,
26
+ roundedTop,
27
+ barBackgroundPattern,
28
+ patternId,
29
+ frontColor,
30
+ showGradient,
31
+ gradientColor,
32
+ } = props;
33
+
34
+ const commonStyleForBar: ViewStyle[] = [
35
+ {
36
+ position: "absolute",
37
+ width: "100%",
38
+ height: "100%",
39
+ borderWidth: barBorderWidth ?? 0,
40
+ borderColor: barBorderColor,
41
+ borderRadius: item.barBorderRadius ?? barBorderRadius ?? 0,
42
+ borderTopLeftRadius:
43
+ item.barBorderTopLeftRadius ??
44
+ barBorderTopLeftRadius ??
45
+ item.barBorderRadius ??
46
+ barBorderRadius,
47
+ borderTopRightRadius:
48
+ item.barBorderTopRightRadius ??
49
+ barBorderTopRightRadius ??
50
+ item.barBorderRadius ??
51
+ barBorderRadius,
52
+ borderBottomLeftRadius:
53
+ item.barBorderBottomLeftRadius ??
54
+ barBorderBottomLeftRadius ??
55
+ item.barBorderRadius ??
56
+ barBorderRadius,
57
+ borderBottomRightRadius:
58
+ item.barBorderBottomRightRadius ??
59
+ barBorderBottomRightRadius ??
60
+ item.barBorderRadius ??
61
+ barBorderRadius,
62
+ },
63
+ ];
64
+
65
+ if (roundedBottom) {
66
+ commonStyleForBar.push({
67
+ borderBottomLeftRadius: (item.barWidth || barWidth || 30) / 2,
68
+ borderBottomRightRadius: (item.barWidth || barWidth || 30) / 2,
69
+ });
70
+ }
71
+
72
+ if (cappedBars) {
73
+ commonStyleForBar.push({
74
+ borderTopLeftRadius:
75
+ item.capRadius === 0 ? 0 : item.capRadius || capRadius || 0,
76
+ borderTopRightRadius:
77
+ item.capRadius === 0 ? 0 : item.capRadius || capRadius || 0,
78
+ });
79
+ }
80
+
81
+ if (roundedTop) {
82
+ commonStyleForBar.push({
83
+ borderTopLeftRadius: (item.barWidth || barWidth || 30) / 2,
84
+ borderTopRightRadius: (item.barWidth || barWidth || 30) / 2,
85
+ });
86
+ }
87
+
88
+ const commonPropsFor2Dand3Dbars = {
89
+ barBackgroundPattern: item.barBackgroundPattern || barBackgroundPattern,
90
+ barInnerComponent: localBarInnerComponent,
91
+ patternId: item.patternId || patternId,
92
+ width: item.barWidth || barWidth || 30,
93
+ barStyle: barStyle,
94
+ item: item,
95
+ index: index,
96
+
97
+ frontColor: item.frontColor || frontColor || "",
98
+ showGradient: item.showGradient || showGradient || false,
99
+ gradientColor: item.gradientColor || gradientColor,
100
+ opacity: opacity || 1,
101
+ height: barHeight,
102
+ intactTopLabel,
103
+ showValuesAsTopLabel: !!showValuesAsTopLabel,
104
+ topLabelContainerStyle,
105
+ topLabelTextStyle,
106
+ };
107
+
108
+ return {
109
+ commonStyleForBar,
110
+ commonPropsFor2Dand3Dbars,
111
+ };
112
+ };
@@ -0,0 +1,139 @@
1
+ import { useState } from "react";
2
+ import { StackedBarChartPropsType, stackDataItem } from "./types";
3
+
4
+ export const useRenderStackBars = (props: StackedBarChartPropsType) => {
5
+ const {
6
+ item,
7
+ index,
8
+ containerHeight,
9
+ maxValue,
10
+ propSpacing,
11
+ initialSpacing,
12
+ stackData,
13
+ isAnimated,
14
+ } = props;
15
+ const cotainsNegative = item.stacks.some((item) => item.value < 0);
16
+ const noAnimation = cotainsNegative || !isAnimated;
17
+
18
+ const localBarInnerComponent =
19
+ item.barInnerComponent ?? props.barInnerComponent;
20
+
21
+ const {
22
+ borderRadius,
23
+ borderTopLeftRadius,
24
+ borderTopRightRadius,
25
+ borderBottomLeftRadius,
26
+ borderBottomRightRadius,
27
+ } = item;
28
+
29
+ let leftSpacing = initialSpacing;
30
+ for (let i = 0; i < index; i++) {
31
+ leftSpacing +=
32
+ (stackData[i].spacing ?? propSpacing ?? 0) +
33
+ (stackData[i].stacks[0].barWidth ?? props.barWidth ?? 30);
34
+ }
35
+ const disablePress = props.disablePress || false;
36
+
37
+ const totalHeight = props.item.stacks.reduce(
38
+ (acc, stack) =>
39
+ acc +
40
+ (Math.abs(stack.value) * (containerHeight || 200)) / (maxValue || 200),
41
+ 0
42
+ );
43
+
44
+ const [height, setHeight] = useState(noAnimation ? totalHeight : 1);
45
+
46
+ const getBarHeight = (value: number, marginBottom?: number) => {
47
+ return (
48
+ (Math.abs(value) * (containerHeight || 200)) / (maxValue || 200) -
49
+ (marginBottom || 0)
50
+ );
51
+ };
52
+
53
+ const getPosition = (index: number) => {
54
+ /* Returns bottom position for stack item
55
+ negative values are below origin (-> negative position) */
56
+ const height = getBarHeight(
57
+ item.stacks[index].value,
58
+ item.stacks[index].marginBottom
59
+ );
60
+
61
+ const itemValue = item.stacks[index].value;
62
+ const isNegative = itemValue <= 0;
63
+ let position = isNegative ? -(height || 0) : 0;
64
+
65
+ for (let i = 0; i < index; i++) {
66
+ const valueOnIndex = item.stacks[i].value;
67
+ if (isNegative && valueOnIndex <= 0) {
68
+ position +=
69
+ (valueOnIndex * (containerHeight || 200)) / (maxValue || 200);
70
+ } else if (!isNegative && valueOnIndex >= 0) {
71
+ position +=
72
+ (valueOnIndex * (containerHeight || 200)) / (maxValue || 200);
73
+ }
74
+ }
75
+ return position;
76
+ };
77
+
78
+ const getLowestPosition = () => {
79
+ return (
80
+ item.stacks
81
+ .map((_, index) => getPosition(index))
82
+ .sort((a, b) => a - b)?.[0] || 0
83
+ );
84
+ };
85
+
86
+ const lowestBarPosition = getLowestPosition();
87
+
88
+ const getStackBorderRadii = (item: stackDataItem, index: number) => {
89
+ const stackItem = item.stacks[index];
90
+ const borderRadii = {
91
+ borderTopLeftRadius:
92
+ stackItem.borderTopLeftRadius ??
93
+ stackItem.borderRadius ??
94
+ props.barBorderTopLeftRadius ??
95
+ props.barBorderRadius ??
96
+ 0,
97
+ borderTopRightRadius:
98
+ stackItem.borderTopRightRadius ??
99
+ stackItem.borderRadius ??
100
+ props.barBorderTopRightRadius ??
101
+ props.barBorderRadius ??
102
+ 0,
103
+ borderBottomLeftRadius:
104
+ stackItem.borderBottomLeftRadius ??
105
+ stackItem.borderRadius ??
106
+ props.barBorderBottomLeftRadius ??
107
+ props.barBorderRadius ??
108
+ 0,
109
+ borderBottomRightRadius:
110
+ stackItem.borderBottomRightRadius ??
111
+ stackItem.borderRadius ??
112
+ props.barBorderBottomRightRadius ??
113
+ props.barBorderRadius ??
114
+ 0,
115
+ };
116
+ return borderRadii;
117
+ };
118
+
119
+ return {
120
+ cotainsNegative,
121
+ noAnimation,
122
+ localBarInnerComponent,
123
+ borderRadius,
124
+ borderTopLeftRadius,
125
+ borderTopRightRadius,
126
+ borderBottomLeftRadius,
127
+ borderBottomRightRadius,
128
+ leftSpacing,
129
+ disablePress,
130
+ totalHeight,
131
+ height,
132
+ setHeight,
133
+ getBarHeight,
134
+ getPosition,
135
+ getLowestPosition,
136
+ lowestBarPosition,
137
+ getStackBorderRadii,
138
+ };
139
+ };