gifted-charts-core 0.1.0 → 0.1.2

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.1.0",
3
+ "version": "0.1.2",
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": [
@@ -1,10 +1,16 @@
1
1
  import { PieChartPropsType, pieDataItem } from './types';
2
2
  import { LabelsPosition } from '../utils/types';
3
+ interface IsvgProps {
4
+ height: number;
5
+ width: number;
6
+ viewBox: string;
7
+ }
3
8
  interface IusePiePro {
4
9
  radius: number;
5
10
  total: number;
6
11
  donut?: boolean;
7
12
  strokeWidth: number;
13
+ maxStrokeWidth: number;
8
14
  isAnimated?: boolean;
9
15
  animationDuration: number;
10
16
  initial: string;
@@ -17,6 +23,9 @@ interface IusePiePro {
17
23
  y: number;
18
24
  };
19
25
  labelsPosition: LabelsPosition;
26
+ heightFactor: number;
27
+ height: number;
28
+ svgProps: IsvgProps;
20
29
  }
21
30
  export declare const usePiePro: (props: PieChartPropsType) => IusePiePro;
22
31
  export {};
@@ -26,10 +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 ? 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;
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, ring = props.ring;
30
30
  var endAngle = (_a = props.endAngle) !== null && _a !== void 0 ? _a : startAngle + Math.PI * (semiCircle ? 1 : 2);
31
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
+ var maxStrokeWidth = Math.max.apply(Math, __spreadArray(__spreadArray([], __read(data.map(function (item) { var _a; return (_a = item.strokeWidth) !== null && _a !== void 0 ? _a : 0; })), false), [strokeWidth], false));
34
+ var heightFactor = semiCircle ? 1 : 2;
35
+ var height = radius + maxStrokeWidth;
36
+ var svgProps = {
37
+ height: (radius + maxStrokeWidth) * 2,
38
+ width: (radius + maxStrokeWidth) * 2,
39
+ viewBox: "".concat(-maxStrokeWidth, " ").concat(-maxStrokeWidth - (semiCircle ? height / 2 : 0), " ").concat((radius + maxStrokeWidth) * 2, " ").concat((radius + maxStrokeWidth) * 2)
40
+ };
33
41
  // let endAngleLocal = 0
34
42
  var addValues = function (index) {
35
43
  var _a;
@@ -105,7 +113,9 @@ export var usePiePro = function (props) {
105
113
  var _a = getCoordinates(0, (radius - innerRadius) / (radius / 20)), startInnerX = _a.startInnerX, startInnerY = _a.startInnerY, startOuterX = _a.startOuterX, startOuterY = _a.startOuterY;
106
114
  return "M".concat(startInnerX, ",").concat(startInnerY, " L").concat(startOuterX, ",").concat(startOuterY, " ");
107
115
  }
108
- return "M".concat(radius + innerRadius, ",").concat(radius, " h").concat(radius - innerRadius, " ");
116
+ return ring
117
+ ? "M".concat(radius * 2, ",").concat(radius)
118
+ : "M".concat(radius + innerRadius, ",").concat(radius, " h").concat(radius - innerRadius, " ");
109
119
  };
110
120
  var getPath = function (index, totalParam) {
111
121
  var _a, _b;
@@ -116,8 +126,13 @@ export var usePiePro = function (props) {
116
126
  ? 1
117
127
  : 0;
118
128
  var arc = "A".concat(radius + ((_b = props.strokeWidth) !== null && _b !== void 0 ? _b : 0) / 2, ",").concat(radius, " 0 ").concat(isLargeArc, " 0 ");
119
- var path = "".concat(arc, " ").concat(endOuterX, ", ").concat(endOuterY, "\n L").concat(radius, ",").concat(radius, " ");
120
- initial = "M".concat(radius, ",").concat(radius, " L").concat(endOuterX, ",").concat(endOuterY);
129
+ var path = "".concat(arc, " ").concat(endOuterX, ", ").concat(endOuterY, " ");
130
+ if (!ring) {
131
+ path += "L".concat(radius, ",").concat(radius, " ");
132
+ }
133
+ initial = ring
134
+ ? "M".concat(endOuterX, ",").concat(endOuterY, " ")
135
+ : "M".concat(radius, ",").concat(radius, " L").concat(endOuterX, ",").concat(endOuterY);
121
136
  return path;
122
137
  };
123
138
  var getDonutPath = function (index, item, totalParam) {
@@ -131,7 +146,11 @@ export var usePiePro = function (props) {
131
146
  var cropAtEnd = !!(index === data.length - 1 &&
132
147
  (item.isEndEdgeCurved || item.endEdgeRadius));
133
148
  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;
149
+ var isLargeArc = semiCircle
150
+ ? 0
151
+ : ((_a = data[index]) === null || _a === void 0 ? void 0 : _a.value) / (totalParam !== null && totalParam !== void 0 ? totalParam : total) > 0.5
152
+ ? 1
153
+ : 0;
135
154
  var innerArc = "A".concat(innerRadius, ",").concat(innerRadius, " 0 ").concat(isLargeArc, " 1 ");
136
155
  var outerArc = "A".concat(radius + ((_b = props.strokeWidth) !== null && _b !== void 0 ? _b : 0) / 2, ",").concat(radius, " 0 ").concat(isLargeArc, " 0 ");
137
156
  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);
@@ -160,17 +179,13 @@ export var usePiePro = function (props) {
160
179
  var path = "M".concat(endInnerX, ",").concat(endInnerY, " A").concat(edgeRadius, ",").concat(edgeRadius, " 0 0 1 ").concat(endOuterX, ",").concat(endOuterY);
161
180
  return path;
162
181
  };
163
- var dataForInitialPath = isAnimated
164
- ? data
165
- : __spreadArray(__spreadArray([], __read(data), false), [{ value: total * 100 }], false);
166
- var dataForFinalPath = isAnimated ? data : __spreadArray(__spreadArray([], __read(data), false), [{ value: 0 }], false);
167
- var dInitial = dataForInitialPath.map(function (item, index) {
182
+ var dInitial = data.map(function (item, index) {
168
183
  return "".concat(initial || getInitial(item), " ").concat(donut
169
184
  ? getDonutPath(index, item, total * 101)
170
185
  : getPath(index, total * 101));
171
186
  });
172
187
  initial = '';
173
- var dFinal = dataForFinalPath.map(function (item, index) {
188
+ var dFinal = data.map(function (item, index) {
174
189
  return "".concat(initial || getInitial(item), " ").concat(donut ? getDonutPath(index, item) : getPath(index));
175
190
  });
176
191
  return {
@@ -178,6 +193,7 @@ export var usePiePro = function (props) {
178
193
  total: total,
179
194
  donut: donut,
180
195
  strokeWidth: strokeWidth,
196
+ maxStrokeWidth: maxStrokeWidth,
181
197
  isAnimated: isAnimated,
182
198
  animationDuration: animationDuration,
183
199
  initial: initial,
@@ -186,6 +202,9 @@ export var usePiePro = function (props) {
186
202
  getStartCaps: getStartCaps,
187
203
  getEndCaps: getEndCaps,
188
204
  getTextCoordinates: getTextCoordinates,
189
- labelsPosition: labelsPosition
205
+ labelsPosition: labelsPosition,
206
+ heightFactor: heightFactor,
207
+ height: height,
208
+ svgProps: svgProps
190
209
  };
191
210
  };
@@ -2,11 +2,18 @@ import { defaultAnimationDuration } from '../utils/constants'
2
2
  import { PieChartPropsType, pieDataItem } from './types'
3
3
  import { LabelsPosition } from '../utils/types'
4
4
 
5
+ interface IsvgProps {
6
+ height: number
7
+ width: number
8
+ viewBox: string
9
+ }
10
+
5
11
  interface IusePiePro {
6
12
  radius: number
7
13
  total: number
8
14
  donut?: boolean
9
15
  strokeWidth: number
16
+ maxStrokeWidth: number
10
17
  isAnimated?: boolean
11
18
  animationDuration: number
12
19
  initial: string
@@ -19,6 +26,9 @@ interface IusePiePro {
19
26
  labelPos?: LabelsPosition
20
27
  ) => { x: number; y: number }
21
28
  labelsPosition: LabelsPosition
29
+ heightFactor: number
30
+ height: number
31
+ svgProps: IsvgProps
22
32
  }
23
33
 
24
34
  export const usePiePro = (props: PieChartPropsType): IusePiePro => {
@@ -31,12 +41,29 @@ export const usePiePro = (props: PieChartPropsType): IusePiePro => {
31
41
  innerRadius = donut ? radius / 2.5 : 0,
32
42
  strokeWidth = 0,
33
43
  edgesRadius = 0,
34
- startAngle = 0
44
+ startAngle = 0,
45
+ ring
35
46
  } = props
36
47
  let endAngle = props.endAngle ?? startAngle + Math.PI * (semiCircle ? 1 : 2)
37
48
  const total = data.reduce((acc, item) => acc + item?.value, 0)
38
49
  const animationDuration = props.animationDuration ?? defaultAnimationDuration
39
50
 
51
+ const maxStrokeWidth = Math.max(
52
+ ...data.map((item) => item.strokeWidth ?? 0),
53
+ strokeWidth
54
+ )
55
+
56
+ const heightFactor = semiCircle ? 1 : 2
57
+ const height = radius + maxStrokeWidth
58
+
59
+ const svgProps = {
60
+ height: (radius + maxStrokeWidth) * 2,
61
+ width: (radius + maxStrokeWidth) * 2,
62
+ viewBox: `${-maxStrokeWidth} ${
63
+ -maxStrokeWidth - (semiCircle ? height / 2 : 0)
64
+ } ${(radius + maxStrokeWidth) * 2} ${(radius + maxStrokeWidth) * 2}`
65
+ }
66
+
40
67
  // let endAngleLocal = 0
41
68
 
42
69
  const addValues = (index: number) => {
@@ -130,7 +157,9 @@ export const usePiePro = (props: PieChartPropsType): IusePiePro => {
130
157
 
131
158
  return `M${startInnerX},${startInnerY} L${startOuterX},${startOuterY} `
132
159
  }
133
- return `M${radius + innerRadius},${radius} h${radius - innerRadius} `
160
+ return ring
161
+ ? `M${radius * 2},${radius}`
162
+ : `M${radius + innerRadius},${radius} h${radius - innerRadius} `
134
163
  }
135
164
  const getPath = (index: number, totalParam?: number) => {
136
165
  const { endOuterX, endOuterY } = getCoordinates(
@@ -150,10 +179,15 @@ export const usePiePro = (props: PieChartPropsType): IusePiePro => {
150
179
  const arc = `A${
151
180
  radius + (props.strokeWidth ?? 0) / 2
152
181
  },${radius} 0 ${isLargeArc} 0 `
153
- const path = `${arc} ${endOuterX}, ${endOuterY}
154
- L${radius},${radius} `
182
+ let path = `${arc} ${endOuterX}, ${endOuterY} `
183
+
184
+ if (!ring) {
185
+ path += `L${radius},${radius} `
186
+ }
155
187
 
156
- initial = `M${radius},${radius} L${endOuterX},${endOuterY}`
188
+ initial = ring
189
+ ? `M${endOuterX},${endOuterY} `
190
+ : `M${radius},${radius} L${endOuterX},${endOuterY}`
157
191
 
158
192
  return path
159
193
  }
@@ -190,8 +224,11 @@ export const usePiePro = (props: PieChartPropsType): IusePiePro => {
190
224
  cropAtEnd,
191
225
  totalParam
192
226
  )
193
-
194
- const isLargeArc = data[index]?.value / (totalParam ?? total) > 0.5 ? 1 : 0
227
+ const isLargeArc = semiCircle
228
+ ? 0
229
+ : data[index]?.value / (totalParam ?? total) > 0.5
230
+ ? 1
231
+ : 0
195
232
 
196
233
  const innerArc = `A${innerRadius},${innerRadius} 0 ${isLargeArc} 1 `
197
234
  const outerArc = `A${
@@ -233,12 +270,7 @@ export const usePiePro = (props: PieChartPropsType): IusePiePro => {
233
270
  return path
234
271
  }
235
272
 
236
- const dataForInitialPath = isAnimated
237
- ? data
238
- : [...data, { value: total * 100 }]
239
- const dataForFinalPath = isAnimated ? data : [...data, { value: 0 }]
240
-
241
- const dInitial = dataForInitialPath.map(
273
+ const dInitial = data.map(
242
274
  (item, index) =>
243
275
  `${initial || getInitial(item)} ${
244
276
  donut
@@ -248,7 +280,7 @@ export const usePiePro = (props: PieChartPropsType): IusePiePro => {
248
280
  )
249
281
 
250
282
  initial = ''
251
- const dFinal = dataForFinalPath.map(
283
+ const dFinal = data.map(
252
284
  (item, index) =>
253
285
  `${initial || getInitial(item)} ${
254
286
  donut ? getDonutPath(index, item) : getPath(index)
@@ -260,6 +292,7 @@ export const usePiePro = (props: PieChartPropsType): IusePiePro => {
260
292
  total,
261
293
  donut,
262
294
  strokeWidth,
295
+ maxStrokeWidth,
263
296
  isAnimated,
264
297
  animationDuration,
265
298
  initial,
@@ -268,6 +301,9 @@ export const usePiePro = (props: PieChartPropsType): IusePiePro => {
268
301
  getStartCaps,
269
302
  getEndCaps,
270
303
  getTextCoordinates,
271
- labelsPosition
304
+ labelsPosition,
305
+ heightFactor,
306
+ height,
307
+ svgProps
272
308
  }
273
309
  }
@@ -5,6 +5,7 @@ export interface PieChartPropsType {
5
5
  radius?: number;
6
6
  isThreeD?: boolean;
7
7
  donut?: boolean;
8
+ ring?: boolean;
8
9
  innerRadius?: number;
9
10
  shadow?: boolean;
10
11
  innerCircleColor?: ColorValue;
@@ -16,6 +17,7 @@ export interface PieChartPropsType {
16
17
  shadowWidth?: number;
17
18
  strokeWidth?: number;
18
19
  strokeColor?: string;
20
+ strokeDashArray?: number[];
19
21
  backgroundColor?: string;
20
22
  data: pieDataItem[];
21
23
  semiCircle?: boolean;
@@ -79,6 +81,7 @@ export interface pieDataItem {
79
81
  onPress?: Function;
80
82
  onLabelPress?: Function;
81
83
  strokeWidth?: number;
84
+ strokeDashArray?: number[];
82
85
  strokeColor?: string;
83
86
  focused?: boolean;
84
87
  peripheral?: boolean;
@@ -6,6 +6,7 @@ export interface PieChartPropsType {
6
6
  radius?: number
7
7
  isThreeD?: boolean
8
8
  donut?: boolean
9
+ ring?: boolean
9
10
  innerRadius?: number
10
11
  shadow?: boolean
11
12
  innerCircleColor?: ColorValue
@@ -17,6 +18,7 @@ export interface PieChartPropsType {
17
18
  shadowWidth?: number
18
19
  strokeWidth?: number
19
20
  strokeColor?: string
21
+ strokeDashArray?: number[]
20
22
  backgroundColor?: string
21
23
  data: pieDataItem[]
22
24
  semiCircle?: boolean
@@ -82,6 +84,7 @@ export interface pieDataItem {
82
84
  onPress?: Function
83
85
  onLabelPress?: Function
84
86
  strokeWidth?: number
87
+ strokeDashArray?: number[]
85
88
  strokeColor?: string
86
89
  focused?: boolean
87
90
  peripheral?: boolean