gifted-charts-core 0.0.50 → 0.1.0

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,6 +1,6 @@
1
1
  {
2
2
  "name": "gifted-charts-core",
3
- "version": "0.0.50",
3
+ "version": "0.1.0",
4
4
  "description": "Mathematical and logical utilities used by react-gifted-charts and react-native-gifted-charts",
5
5
  "main": "./src/index.js",
6
6
  "files": [
@@ -26,17 +26,18 @@ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
26
26
  import { defaultAnimationDuration } from '../utils/constants';
27
27
  export var usePiePro = function (props) {
28
28
  var _a, _b;
29
- var data = props.data, isAnimated = props.isAnimated, donut = props.donut, semiCircle = props.semiCircle, _c = props.radius, radius = _c === void 0 ? 120 : _c, _d = props.innerRadius, innerRadius = _d === void 0 ? radius / 2.5 : _d, _e = props.strokeWidth, strokeWidth = _e === void 0 ? 0 : _e, _f = props.edgesRadius, edgesRadius = _f === void 0 ? 0 : _f, _g = props.startAngle, startAngle = _g === void 0 ? 0 : _g;
29
+ var data = props.data, isAnimated = props.isAnimated, donut = props.donut, semiCircle = props.semiCircle, _c = props.radius, radius = _c === void 0 ? 120 : _c, _d = props.innerRadius, innerRadius = _d === void 0 ? donut ? radius / 2.5 : 0 : _d, _e = props.strokeWidth, strokeWidth = _e === void 0 ? 0 : _e, _f = props.edgesRadius, edgesRadius = _f === void 0 ? 0 : _f, _g = props.startAngle, startAngle = _g === void 0 ? 0 : _g;
30
30
  var endAngle = (_a = props.endAngle) !== null && _a !== void 0 ? _a : startAngle + Math.PI * (semiCircle ? 1 : 2);
31
- var total = data.reduce(function (acc, item) { return acc + item.value; }, 0);
31
+ var total = data.reduce(function (acc, item) { return acc + (item === null || item === void 0 ? void 0 : item.value); }, 0);
32
32
  var animationDuration = (_b = props.animationDuration) !== null && _b !== void 0 ? _b : defaultAnimationDuration;
33
33
  // let endAngleLocal = 0
34
34
  var addValues = function (index) {
35
+ var _a;
35
36
  if (index < 0)
36
37
  return 0;
37
38
  var sum = 0;
38
39
  for (var i = 0; i <= index; i++)
39
- sum += data[i].value;
40
+ sum += (_a = data[i]) === null || _a === void 0 ? void 0 : _a.value;
40
41
  return sum;
41
42
  };
42
43
  var labelsPosition = props.labelsPosition
@@ -44,17 +45,18 @@ export var usePiePro = function (props) {
44
45
  : donut || props.centerLabelComponent
45
46
  ? 'outward'
46
47
  : 'mid';
47
- var getCoordinates = function (index, additionalValue, addInOnlyStart, addInOnlyEnd) {
48
+ var getCoordinates = function (index, additionalValue, addInOnlyStart, addInOnlyEnd, totalParam) {
49
+ var _a;
48
50
  var addedValue = addValues(index - 1) + (addInOnlyEnd ? 0 : additionalValue !== null && additionalValue !== void 0 ? additionalValue : 0);
49
- var angle = (addedValue / total) * endAngle + startAngle;
51
+ var angle = (addedValue / (totalParam !== null && totalParam !== void 0 ? totalParam : total)) * endAngle + startAngle;
50
52
  var startInnerX = radius + Math.cos(angle) * innerRadius;
51
53
  var startInnerY = radius - Math.sin(angle) * innerRadius;
52
54
  var startOuterX = radius + Math.cos(angle) * radius;
53
55
  var startOuterY = radius - Math.sin(angle) * radius;
54
56
  var value = addValues(index - 1) +
55
- data[index].value +
57
+ ((_a = data[index]) === null || _a === void 0 ? void 0 : _a.value) +
56
58
  (addInOnlyStart ? 0 : additionalValue !== null && additionalValue !== void 0 ? additionalValue : 0);
57
- angle = (value / total) * endAngle + startAngle;
59
+ angle = (value / (totalParam !== null && totalParam !== void 0 ? totalParam : total)) * endAngle + startAngle;
58
60
  var endOuterX = radius + Math.cos(angle) * radius;
59
61
  var endOuterY = radius - Math.sin(angle) * radius;
60
62
  var endInnerX = radius + Math.cos(angle) * innerRadius;
@@ -71,7 +73,8 @@ export var usePiePro = function (props) {
71
73
  };
72
74
  };
73
75
  var getTextCoordinates = function (index, labelPos) {
74
- var value = addValues(index - 1) + data[index].value / 2;
76
+ var _a;
77
+ var value = addValues(index - 1) + ((_a = data[index]) === null || _a === void 0 ? void 0 : _a.value) / 2;
75
78
  var angle = (value / total) * endAngle + startAngle;
76
79
  var labelPosition = labelPos || labelsPosition;
77
80
  var x = radius +
@@ -104,17 +107,21 @@ export var usePiePro = function (props) {
104
107
  }
105
108
  return "M".concat(radius + innerRadius, ",").concat(radius, " h").concat(radius - innerRadius, " ");
106
109
  };
107
- var getPath = function (index) {
108
- var _a;
109
- var _b = getCoordinates(index), endOuterX = _b.endOuterX, endOuterY = _b.endOuterY;
110
- var isLargeArc = data[index].value / total > 0.5 ? 1 : 0;
111
- var arc = "A".concat(radius + ((_a = props.strokeWidth) !== null && _a !== void 0 ? _a : 0) / 2, ",").concat(radius, " 0 ").concat(isLargeArc, " 0 ");
110
+ var getPath = function (index, totalParam) {
111
+ var _a, _b;
112
+ var _c = getCoordinates(index, 0, false, false, totalParam), endOuterX = _c.endOuterX, endOuterY = _c.endOuterY;
113
+ var isLargeArc = semiCircle
114
+ ? 0
115
+ : ((_a = data[index]) === null || _a === void 0 ? void 0 : _a.value) / (totalParam !== null && totalParam !== void 0 ? totalParam : total) > 0.5
116
+ ? 1
117
+ : 0;
118
+ var arc = "A".concat(radius + ((_b = props.strokeWidth) !== null && _b !== void 0 ? _b : 0) / 2, ",").concat(radius, " 0 ").concat(isLargeArc, " 0 ");
112
119
  var path = "".concat(arc, " ").concat(endOuterX, ", ").concat(endOuterY, "\n L").concat(radius, ",").concat(radius, " ");
113
120
  initial = "M".concat(radius, ",").concat(radius, " L").concat(endOuterX, ",").concat(endOuterY);
114
121
  return path;
115
122
  };
116
- var getDonutPath = function (index, item) {
117
- var _a;
123
+ var getDonutPath = function (index, item, totalParam) {
124
+ var _a, _b;
118
125
  var additionalForStart = item.isStartEdgeCurved || item.startEdgeRadius
119
126
  ? (radius - innerRadius) / (radius / 20)
120
127
  : 0;
@@ -123,10 +130,10 @@ export var usePiePro = function (props) {
123
130
  : 0;
124
131
  var cropAtEnd = !!(index === data.length - 1 &&
125
132
  (item.isEndEdgeCurved || item.endEdgeRadius));
126
- var _b = getCoordinates(index, cropAtEnd ? additionalForEnd : additionalForStart, !cropAtEnd, cropAtEnd), startInnerX = _b.startInnerX, startInnerY = _b.startInnerY, endOuterX = _b.endOuterX, endOuterY = _b.endOuterY, endInnerX = _b.endInnerX, endInnerY = _b.endInnerY;
127
- var isLargeArc = data[index].value / total > 0.5 ? 1 : 0;
133
+ var _c = getCoordinates(index, cropAtEnd ? additionalForEnd : additionalForStart, !cropAtEnd, cropAtEnd, totalParam), startInnerX = _c.startInnerX, startInnerY = _c.startInnerY, endOuterX = _c.endOuterX, endOuterY = _c.endOuterY, endInnerX = _c.endInnerX, endInnerY = _c.endInnerY;
134
+ var isLargeArc = ((_a = data[index]) === null || _a === void 0 ? void 0 : _a.value) / (totalParam !== null && totalParam !== void 0 ? totalParam : total) > 0.5 ? 1 : 0;
128
135
  var innerArc = "A".concat(innerRadius, ",").concat(innerRadius, " 0 ").concat(isLargeArc, " 1 ");
129
- var outerArc = "A".concat(radius + ((_a = props.strokeWidth) !== null && _a !== void 0 ? _a : 0) / 2, ",").concat(radius, " 0 ").concat(isLargeArc, " 0 ");
136
+ var outerArc = "A".concat(radius + ((_b = props.strokeWidth) !== null && _b !== void 0 ? _b : 0) / 2, ",").concat(radius, " 0 ").concat(isLargeArc, " 0 ");
130
137
  var path = "".concat(outerArc, " ").concat(endOuterX, ", ").concat(endOuterY, "\n L").concat(endInnerX, ",").concat(endInnerY, " M").concat(endInnerX, ",").concat(endInnerY, " ").concat(innerArc, " ").concat(startInnerX, ",").concat(startInnerY);
131
138
  initial = "M".concat(endInnerX, ",").concat(endInnerY, " L").concat(endOuterX, ",").concat(endOuterY, " ");
132
139
  return path;
@@ -158,7 +165,9 @@ export var usePiePro = function (props) {
158
165
  : __spreadArray(__spreadArray([], __read(data), false), [{ value: total * 100 }], false);
159
166
  var dataForFinalPath = isAnimated ? data : __spreadArray(__spreadArray([], __read(data), false), [{ value: 0 }], false);
160
167
  var dInitial = dataForInitialPath.map(function (item, index) {
161
- return "".concat(initial || getInitial(item), " ").concat(donut ? getDonutPath(index, item) : getPath(index));
168
+ return "".concat(initial || getInitial(item), " ").concat(donut
169
+ ? getDonutPath(index, item, total * 101)
170
+ : getPath(index, total * 101));
162
171
  });
163
172
  initial = '';
164
173
  var dFinal = dataForFinalPath.map(function (item, index) {
@@ -28,13 +28,13 @@ export const usePiePro = (props: PieChartPropsType): IusePiePro => {
28
28
  donut,
29
29
  semiCircle,
30
30
  radius = 120,
31
- innerRadius = radius / 2.5,
31
+ innerRadius = donut ? radius / 2.5 : 0,
32
32
  strokeWidth = 0,
33
33
  edgesRadius = 0,
34
34
  startAngle = 0
35
35
  } = props
36
36
  let endAngle = props.endAngle ?? startAngle + Math.PI * (semiCircle ? 1 : 2)
37
- const total = data.reduce((acc, item) => acc + item.value, 0)
37
+ const total = data.reduce((acc, item) => acc + item?.value, 0)
38
38
  const animationDuration = props.animationDuration ?? defaultAnimationDuration
39
39
 
40
40
  // let endAngleLocal = 0
@@ -42,7 +42,7 @@ export const usePiePro = (props: PieChartPropsType): IusePiePro => {
42
42
  const addValues = (index: number) => {
43
43
  if (index < 0) return 0
44
44
  let sum = 0
45
- for (let i = 0; i <= index; i++) sum += data[i].value
45
+ for (let i = 0; i <= index; i++) sum += data[i]?.value
46
46
  return sum
47
47
  }
48
48
  const labelsPosition = props.labelsPosition
@@ -55,11 +55,12 @@ export const usePiePro = (props: PieChartPropsType): IusePiePro => {
55
55
  index: number,
56
56
  additionalValue?: number,
57
57
  addInOnlyStart?: boolean,
58
- addInOnlyEnd?: boolean
58
+ addInOnlyEnd?: boolean,
59
+ totalParam?: number
59
60
  ) => {
60
61
  const addedValue =
61
62
  addValues(index - 1) + (addInOnlyEnd ? 0 : additionalValue ?? 0)
62
- let angle = (addedValue / total) * endAngle + startAngle
63
+ let angle = (addedValue / (totalParam ?? total)) * endAngle + startAngle
63
64
  const startInnerX = radius + Math.cos(angle) * innerRadius
64
65
  const startInnerY = radius - Math.sin(angle) * innerRadius
65
66
  const startOuterX = radius + Math.cos(angle) * radius
@@ -67,9 +68,9 @@ export const usePiePro = (props: PieChartPropsType): IusePiePro => {
67
68
 
68
69
  const value =
69
70
  addValues(index - 1) +
70
- data[index].value +
71
+ data[index]?.value +
71
72
  (addInOnlyStart ? 0 : additionalValue ?? 0)
72
- angle = (value / total) * endAngle + startAngle
73
+ angle = (value / (totalParam ?? total)) * endAngle + startAngle
73
74
 
74
75
  const endOuterX = radius + Math.cos(angle) * radius
75
76
  const endOuterY = radius - Math.sin(angle) * radius
@@ -90,7 +91,7 @@ export const usePiePro = (props: PieChartPropsType): IusePiePro => {
90
91
  }
91
92
 
92
93
  const getTextCoordinates = (index: number, labelPos?: LabelsPosition) => {
93
- const value = addValues(index - 1) + data[index].value / 2
94
+ const value = addValues(index - 1) + data[index]?.value / 2
94
95
  const angle = (value / total) * endAngle + startAngle
95
96
 
96
97
  const labelPosition: LabelsPosition = labelPos || labelsPosition
@@ -131,10 +132,20 @@ export const usePiePro = (props: PieChartPropsType): IusePiePro => {
131
132
  }
132
133
  return `M${radius + innerRadius},${radius} h${radius - innerRadius} `
133
134
  }
134
- const getPath = (index: number) => {
135
- const { endOuterX, endOuterY } = getCoordinates(index)
135
+ const getPath = (index: number, totalParam?: number) => {
136
+ const { endOuterX, endOuterY } = getCoordinates(
137
+ index,
138
+ 0,
139
+ false,
140
+ false,
141
+ totalParam
142
+ )
136
143
 
137
- const isLargeArc = data[index].value / total > 0.5 ? 1 : 0
144
+ const isLargeArc = semiCircle
145
+ ? 0
146
+ : data[index]?.value / (totalParam ?? total) > 0.5
147
+ ? 1
148
+ : 0
138
149
 
139
150
  const arc = `A${
140
151
  radius + (props.strokeWidth ?? 0) / 2
@@ -146,7 +157,11 @@ export const usePiePro = (props: PieChartPropsType): IusePiePro => {
146
157
 
147
158
  return path
148
159
  }
149
- const getDonutPath = (index: number, item: pieDataItem) => {
160
+ const getDonutPath = (
161
+ index: number,
162
+ item: pieDataItem,
163
+ totalParam?: number
164
+ ) => {
150
165
  const additionalForStart =
151
166
  item.isStartEdgeCurved || item.startEdgeRadius
152
167
  ? (radius - innerRadius) / (radius / 20)
@@ -172,10 +187,11 @@ export const usePiePro = (props: PieChartPropsType): IusePiePro => {
172
187
  index,
173
188
  cropAtEnd ? additionalForEnd : additionalForStart,
174
189
  !cropAtEnd,
175
- cropAtEnd
190
+ cropAtEnd,
191
+ totalParam
176
192
  )
177
193
 
178
- const isLargeArc = data[index].value / total > 0.5 ? 1 : 0
194
+ const isLargeArc = data[index]?.value / (totalParam ?? total) > 0.5 ? 1 : 0
179
195
 
180
196
  const innerArc = `A${innerRadius},${innerRadius} 0 ${isLargeArc} 1 `
181
197
  const outerArc = `A${
@@ -225,7 +241,9 @@ export const usePiePro = (props: PieChartPropsType): IusePiePro => {
225
241
  const dInitial = dataForInitialPath.map(
226
242
  (item, index) =>
227
243
  `${initial || getInitial(item)} ${
228
- donut ? getDonutPath(index, item) : getPath(index)
244
+ donut
245
+ ? getDonutPath(index, item, total * 101)
246
+ : getPath(index, total * 101)
229
247
  }`
230
248
  )
231
249
 
package/src/index.d.ts CHANGED
@@ -34,5 +34,5 @@ export { getTopAndLeftForStripAndLabel } from './components/common/StripAndLabel
34
34
  /********************************* common utils, constants and types ********************************
35
35
  /***********************************************************************************************************************/
36
36
  export { getCumulativeWidth, getLighterColor, svgQuadraticCurvePath, svgPath, bezierCommand, getSegmentString, getCurvePathWithSegments, getPreviousSegmentsLastPoint, getPathWithHighlight, getRegionPathObjects, getSegmentedPathObjects, getArrowPoints, getAxesAndRulesProps, getExtendedContainerHeightWithPadding, getSecondaryDataWithOffsetIncluded, getArrowProperty, getAllArrowProperties, maxAndMinUtil, computeMaxAndMinItems, getLabelTextUtil, getXForLineInBar, getYForLineInBar, clone, getLineConfigForBarChart, adjustToOffset } from './utils';
37
- export { chartTypes, yAxisSides, loc, SEGMENT_START, SEGMENT_END, RANGE_ENTER, RANGE_EXIT, STOP, ruleTypes, AxesAndRulesDefaults, defaultArrowConfig, BarDefaults, defaultLineConfig, LineDefaults, defaultPointerConfig, pieColors, populationDefaults } from './utils/constants';
37
+ export { chartTypes, yAxisSides, loc, SEGMENT_START, SEGMENT_END, RANGE_ENTER, RANGE_EXIT, STOP, ruleTypes, AxesAndRulesDefaults, defaultArrowConfig, BarDefaults, defaultLineConfig, LineDefaults, defaultPointerConfig, pieColors, populationDefaults, defaultAnimationDuration } from './utils/constants';
38
38
  export { type RuleType, type RuleTypes, type RulesConfig, type CurveType, type EdgePosition, type LabelsPosition, type PointerEvents, type secondaryYAxisType, type secondaryLineConfigType, type referenceConfigType, type arrowConfigType, type horizSectionPropTypes, type HorizSectionsType, type BarAndLineChartsWrapperTypes, type Pointer, type HighlightedRange, type LineSegment, type LineSvgProps, type LineProperties, type DataSet, type Framework } from './utils/types';
package/src/index.js CHANGED
@@ -30,4 +30,4 @@ export { getTopAndLeftForStripAndLabel } from './components/common/StripAndLabel
30
30
  /********************************* common utils, constants and types ********************************
31
31
  /***********************************************************************************************************************/
32
32
  export { getCumulativeWidth, getLighterColor, svgQuadraticCurvePath, svgPath, bezierCommand, getSegmentString, getCurvePathWithSegments, getPreviousSegmentsLastPoint, getPathWithHighlight, getRegionPathObjects, getSegmentedPathObjects, getArrowPoints, getAxesAndRulesProps, getExtendedContainerHeightWithPadding, getSecondaryDataWithOffsetIncluded, getArrowProperty, getAllArrowProperties, maxAndMinUtil, computeMaxAndMinItems, getLabelTextUtil, getXForLineInBar, getYForLineInBar, clone, getLineConfigForBarChart, adjustToOffset } from './utils';
33
- export { chartTypes, yAxisSides, loc, SEGMENT_START, SEGMENT_END, RANGE_ENTER, RANGE_EXIT, STOP, ruleTypes, AxesAndRulesDefaults, defaultArrowConfig, BarDefaults, defaultLineConfig, LineDefaults, defaultPointerConfig, pieColors, populationDefaults } from './utils/constants';
33
+ export { chartTypes, yAxisSides, loc, SEGMENT_START, SEGMENT_END, RANGE_ENTER, RANGE_EXIT, STOP, ruleTypes, AxesAndRulesDefaults, defaultArrowConfig, BarDefaults, defaultLineConfig, LineDefaults, defaultPointerConfig, pieColors, populationDefaults, defaultAnimationDuration } from './utils/constants';
package/src/index.ts CHANGED
@@ -120,7 +120,8 @@ export {
120
120
  LineDefaults,
121
121
  defaultPointerConfig,
122
122
  pieColors,
123
- populationDefaults
123
+ populationDefaults,
124
+ defaultAnimationDuration
124
125
  } from './utils/constants'
125
126
 
126
127
  export {