react-native-gifted-charts 1.2.17 → 1.2.20
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/LICENSE +21 -0
- package/package.json +1 -1
- package/src/LineChart/index.tsx +59 -29
- package/src/PieChart/index.tsx +80 -42
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 Abhinandan Kushwaha
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-native-gifted-charts",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.20",
|
|
4
4
|
"description": "The most complete library for Bar, Line, Area, Pie, Donut and Stacked Bar charts in React Native. Allows 2D, 3D, gradient, animations and live data updates.",
|
|
5
5
|
"main": "src/index.tsx",
|
|
6
6
|
"files": [
|
package/src/LineChart/index.tsx
CHANGED
|
@@ -267,6 +267,7 @@ type propTypes = {
|
|
|
267
267
|
noOfSectionsBelowXAxis?: number;
|
|
268
268
|
labelsExtraHeight?: number;
|
|
269
269
|
adjustToWidth?: Boolean;
|
|
270
|
+
getPointerProps?: Function;
|
|
270
271
|
};
|
|
271
272
|
type referenceConfigType = {
|
|
272
273
|
thickness: number;
|
|
@@ -364,6 +365,7 @@ type Pointer = {
|
|
|
364
365
|
|
|
365
366
|
export const LineChart = (props: propTypes) => {
|
|
366
367
|
const scrollRef = useRef();
|
|
368
|
+
const [pointerIndex, setPointerIndex] = useState(-1);
|
|
367
369
|
const [pointerX, setPointerX] = useState(0);
|
|
368
370
|
const [pointerY, setPointerY] = useState(0);
|
|
369
371
|
const [pointerItem, setPointerItem] = useState({
|
|
@@ -1425,6 +1427,7 @@ export const LineChart = (props: propTypes) => {
|
|
|
1425
1427
|
hidePointer5: false,
|
|
1426
1428
|
};
|
|
1427
1429
|
const pointerConfig = props.pointerConfig || null;
|
|
1430
|
+
const getPointerProps = props.getPointerProps || null;
|
|
1428
1431
|
const pointerHeight =
|
|
1429
1432
|
pointerConfig && pointerConfig.height
|
|
1430
1433
|
? pointerConfig.height
|
|
@@ -2522,6 +2525,48 @@ export const LineChart = (props: propTypes) => {
|
|
|
2522
2525
|
}
|
|
2523
2526
|
pointerYLocal = Math.min(...arr);
|
|
2524
2527
|
|
|
2528
|
+
let left = 0,
|
|
2529
|
+
top = 0;
|
|
2530
|
+
if (autoAdjustPointerLabelPosition) {
|
|
2531
|
+
if (pointerX < pointerLabelWidth / 2) {
|
|
2532
|
+
left = 7;
|
|
2533
|
+
} else {
|
|
2534
|
+
if (
|
|
2535
|
+
!activatePointersOnLongPress &&
|
|
2536
|
+
pointerX >
|
|
2537
|
+
(props.width ||
|
|
2538
|
+
Dimensions.get('window').width - yAxisLabelWidth - 15) -
|
|
2539
|
+
pointerLabelWidth / 2
|
|
2540
|
+
) {
|
|
2541
|
+
left = -pointerLabelWidth - 4;
|
|
2542
|
+
} else if (
|
|
2543
|
+
activatePointersOnLongPress &&
|
|
2544
|
+
pointerX > totalWidth - yAxisLabelWidth - pointerLabelWidth / 2
|
|
2545
|
+
) {
|
|
2546
|
+
left = -pointerLabelWidth - 4;
|
|
2547
|
+
} else {
|
|
2548
|
+
left = -pointerLabelWidth / 2 + 5;
|
|
2549
|
+
}
|
|
2550
|
+
}
|
|
2551
|
+
} else {
|
|
2552
|
+
left = (pointerRadius || pointerWidth / 2) - 10 + shiftPointerLabelX;
|
|
2553
|
+
}
|
|
2554
|
+
|
|
2555
|
+
if (autoAdjustPointerLabelPosition) {
|
|
2556
|
+
if (pointerLabelHeight - pointerYLocal > 10) {
|
|
2557
|
+
top = 10;
|
|
2558
|
+
} else {
|
|
2559
|
+
top = -pointerLabelHeight;
|
|
2560
|
+
}
|
|
2561
|
+
} else {
|
|
2562
|
+
top =
|
|
2563
|
+
(pointerStripUptoDataPoint
|
|
2564
|
+
? pointerRadius || pointerStripHeight / 2
|
|
2565
|
+
: -pointerYLocal + 8) -
|
|
2566
|
+
pointerLabelWidth / 2 +
|
|
2567
|
+
shiftPointerLabelY;
|
|
2568
|
+
}
|
|
2569
|
+
|
|
2525
2570
|
return (
|
|
2526
2571
|
<View
|
|
2527
2572
|
style={{
|
|
@@ -2573,30 +2618,8 @@ export const LineChart = (props: propTypes) => {
|
|
|
2573
2618
|
style={[
|
|
2574
2619
|
{
|
|
2575
2620
|
position: 'absolute',
|
|
2576
|
-
left:
|
|
2577
|
-
|
|
2578
|
-
? 7
|
|
2579
|
-
: !activatePointersOnLongPress &&
|
|
2580
|
-
pointerX >
|
|
2581
|
-
(props.width ||
|
|
2582
|
-
Dimensions.get('window').width -
|
|
2583
|
-
yAxisLabelWidth -
|
|
2584
|
-
15) -
|
|
2585
|
-
pointerLabelWidth / 2
|
|
2586
|
-
? -pointerLabelWidth - 4
|
|
2587
|
-
: pointerLabelWidth / -2 + 5
|
|
2588
|
-
: (pointerRadius || pointerWidth / 2) -
|
|
2589
|
-
10 +
|
|
2590
|
-
shiftPointerLabelX,
|
|
2591
|
-
top: autoAdjustPointerLabelPosition
|
|
2592
|
-
? pointerLabelHeight - pointerYLocal > 10
|
|
2593
|
-
? 10
|
|
2594
|
-
: -pointerLabelHeight
|
|
2595
|
-
: (pointerStripUptoDataPoint
|
|
2596
|
-
? pointerRadius || pointerStripHeight / 2
|
|
2597
|
-
: -pointerYLocal + 8) -
|
|
2598
|
-
pointerLabelWidth / 2 +
|
|
2599
|
-
shiftPointerLabelY,
|
|
2621
|
+
left: left,
|
|
2622
|
+
top: top,
|
|
2600
2623
|
marginTop: pointerStripUptoDataPoint
|
|
2601
2624
|
? 0
|
|
2602
2625
|
: containerHeight - pointerStripHeight,
|
|
@@ -2794,8 +2817,8 @@ export const LineChart = (props: propTypes) => {
|
|
|
2794
2817
|
(pointerRadius || pointerWidth / 2) -
|
|
2795
2818
|
2;
|
|
2796
2819
|
setPointerX(z);
|
|
2820
|
+
setPointerIndex(factor);
|
|
2797
2821
|
let item, y;
|
|
2798
|
-
setPointerX(z);
|
|
2799
2822
|
item = data[factor];
|
|
2800
2823
|
y =
|
|
2801
2824
|
containerHeight -
|
|
@@ -2881,6 +2904,7 @@ export const LineChart = (props: propTypes) => {
|
|
|
2881
2904
|
2;
|
|
2882
2905
|
let item, y;
|
|
2883
2906
|
setPointerX(z);
|
|
2907
|
+
setPointerIndex(factor);
|
|
2884
2908
|
item = data[factor];
|
|
2885
2909
|
y =
|
|
2886
2910
|
containerHeight -
|
|
@@ -2944,10 +2968,11 @@ export const LineChart = (props: propTypes) => {
|
|
|
2944
2968
|
onResponderEnd={evt => {
|
|
2945
2969
|
// console.log('evt...end.......',evt);
|
|
2946
2970
|
setResponderStartTime(0);
|
|
2971
|
+
setPointerIndex(-1);
|
|
2947
2972
|
setResponderActive(false);
|
|
2948
2973
|
setTimeout(() => setPointerX(0), pointerVanishDelay);
|
|
2949
2974
|
}}
|
|
2950
|
-
onResponderTerminationRequest={
|
|
2975
|
+
onResponderTerminationRequest={evt => false}
|
|
2951
2976
|
// onResponderTerminate={evt => {
|
|
2952
2977
|
// console.log('evt...terminate.......',evt);
|
|
2953
2978
|
// }}
|
|
@@ -3018,8 +3043,8 @@ export const LineChart = (props: propTypes) => {
|
|
|
3018
3043
|
(pointerRadius || pointerWidth / 2) -
|
|
3019
3044
|
2;
|
|
3020
3045
|
setPointerX(z);
|
|
3046
|
+
setPointerIndex(factor);
|
|
3021
3047
|
let item, y;
|
|
3022
|
-
setPointerX(z);
|
|
3023
3048
|
item = data[factor];
|
|
3024
3049
|
y =
|
|
3025
3050
|
containerHeight -
|
|
@@ -3104,6 +3129,7 @@ export const LineChart = (props: propTypes) => {
|
|
|
3104
3129
|
2;
|
|
3105
3130
|
let item, y;
|
|
3106
3131
|
setPointerX(z);
|
|
3132
|
+
setPointerIndex(factor);
|
|
3107
3133
|
item = data[factor];
|
|
3108
3134
|
y =
|
|
3109
3135
|
containerHeight -
|
|
@@ -3167,10 +3193,11 @@ export const LineChart = (props: propTypes) => {
|
|
|
3167
3193
|
onResponderEnd={evt => {
|
|
3168
3194
|
// console.log('evt...end.......',evt);
|
|
3169
3195
|
setResponderStartTime(0);
|
|
3196
|
+
setPointerIndex(-1);
|
|
3170
3197
|
setResponderActive(false);
|
|
3171
3198
|
setTimeout(() => setPointerX(0), pointerVanishDelay);
|
|
3172
3199
|
}}
|
|
3173
|
-
onResponderTerminationRequest={
|
|
3200
|
+
onResponderTerminationRequest={evt => false}
|
|
3174
3201
|
// onResponderTerminate={evt => {
|
|
3175
3202
|
// console.log('evt...terminate.......',evt);
|
|
3176
3203
|
// }}
|
|
@@ -3432,7 +3459,7 @@ export const LineChart = (props: propTypes) => {
|
|
|
3432
3459
|
strokeDashArray5,
|
|
3433
3460
|
)
|
|
3434
3461
|
: null}
|
|
3435
|
-
{pointerX ? (
|
|
3462
|
+
{pointerX > 0 ? (
|
|
3436
3463
|
<View
|
|
3437
3464
|
style={{
|
|
3438
3465
|
position: 'absolute',
|
|
@@ -3478,6 +3505,9 @@ export const LineChart = (props: propTypes) => {
|
|
|
3478
3505
|
);
|
|
3479
3506
|
})}
|
|
3480
3507
|
</ScrollView>
|
|
3508
|
+
{pointerConfig &&
|
|
3509
|
+
getPointerProps &&
|
|
3510
|
+
getPointerProps({pointerIndex, pointerX, pointerY})}
|
|
3481
3511
|
</View>
|
|
3482
3512
|
);
|
|
3483
3513
|
};
|
package/src/PieChart/index.tsx
CHANGED
|
@@ -210,6 +210,7 @@ export const PieChart = (props: propTypes) => {
|
|
|
210
210
|
{data.map((item, index) => {
|
|
211
211
|
return (
|
|
212
212
|
<RadialGradient
|
|
213
|
+
key={index + ''}
|
|
213
214
|
id={'grad' + index}
|
|
214
215
|
cx="50%"
|
|
215
216
|
cy="50%"
|
|
@@ -232,54 +233,75 @@ export const PieChart = (props: propTypes) => {
|
|
|
232
233
|
);
|
|
233
234
|
})}
|
|
234
235
|
</Defs>
|
|
235
|
-
{data.
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
cx * (1 + Math.sin(2 * pi * pData[index] + initialAngle)) +
|
|
242
|
-
(item.shiftX || 0);
|
|
243
|
-
let sy =
|
|
244
|
-
cy * (1 - Math.cos(2 * pi * pData[index] + initialAngle)) +
|
|
245
|
-
(item.shiftY || 0);
|
|
246
|
-
let ax =
|
|
247
|
-
cx * (1 + Math.sin(2 * pi * nextItem + initialAngle)) +
|
|
248
|
-
(item.shiftX || 0);
|
|
249
|
-
let ay =
|
|
250
|
-
cy * (1 - Math.cos(2 * pi * nextItem + initialAngle)) +
|
|
251
|
-
(item.shiftY || 0);
|
|
252
|
-
|
|
253
|
-
// console.log('sx', sx);
|
|
254
|
-
// console.log('sy', sy);
|
|
255
|
-
// console.log('ax', ax);
|
|
256
|
-
// console.log('ay', ay);
|
|
257
|
-
return (
|
|
258
|
-
<Path
|
|
259
|
-
d={`M ${cx + (item.shiftX || 0)} ${
|
|
260
|
-
cy + (item.shiftY || 0)
|
|
261
|
-
} L ${sx} ${sy} A ${radius} ${radius} 0 ${
|
|
262
|
-
semiCircle ? 0 : data[index].value > total / 2 ? 1 : 0
|
|
263
|
-
} 1 ${ax} ${ay} L ${cx + (item.shiftX || 0)} ${
|
|
264
|
-
cy + (item.shiftY || 0)
|
|
265
|
-
}`}
|
|
266
|
-
stroke={strokeColor}
|
|
267
|
-
strokeWidth={strokeWidth}
|
|
236
|
+
{data.length === 1 ? (
|
|
237
|
+
<>
|
|
238
|
+
<Circle
|
|
239
|
+
cx={cx}
|
|
240
|
+
cy={cy}
|
|
241
|
+
r={radius}
|
|
268
242
|
fill={
|
|
269
|
-
showGradient
|
|
270
|
-
? `url(#grad${index})`
|
|
271
|
-
: item.color || colors[index % 9]
|
|
243
|
+
showGradient ? `url(#grad${0})` : data[0].color || colors[0 % 9]
|
|
272
244
|
}
|
|
273
245
|
onPress={() => {
|
|
274
|
-
|
|
275
|
-
?
|
|
246
|
+
data[0].onPress
|
|
247
|
+
? data[0].onPress
|
|
276
248
|
: props.onPress
|
|
277
|
-
? props.onPress(
|
|
249
|
+
? props.onPress(data[0], 0)
|
|
278
250
|
: null;
|
|
279
251
|
}}
|
|
280
252
|
/>
|
|
281
|
-
|
|
282
|
-
|
|
253
|
+
</>
|
|
254
|
+
) : (
|
|
255
|
+
data.map((item, index) => {
|
|
256
|
+
// console.log('index', index);
|
|
257
|
+
let nextItem;
|
|
258
|
+
if (index === pData.length - 1) nextItem = pData[0];
|
|
259
|
+
else nextItem = pData[index + 1];
|
|
260
|
+
let sx =
|
|
261
|
+
cx * (1 + Math.sin(2 * pi * pData[index] + initialAngle)) +
|
|
262
|
+
(item.shiftX || 0);
|
|
263
|
+
let sy =
|
|
264
|
+
cy * (1 - Math.cos(2 * pi * pData[index] + initialAngle)) +
|
|
265
|
+
(item.shiftY || 0);
|
|
266
|
+
let ax =
|
|
267
|
+
cx * (1 + Math.sin(2 * pi * nextItem + initialAngle)) +
|
|
268
|
+
(item.shiftX || 0);
|
|
269
|
+
let ay =
|
|
270
|
+
cy * (1 - Math.cos(2 * pi * nextItem + initialAngle)) +
|
|
271
|
+
(item.shiftY || 0);
|
|
272
|
+
|
|
273
|
+
// console.log('sx', sx);
|
|
274
|
+
// console.log('sy', sy);
|
|
275
|
+
// console.log('ax', ax);
|
|
276
|
+
// console.log('ay', ay);
|
|
277
|
+
return (
|
|
278
|
+
<Path
|
|
279
|
+
key={index + 'a'}
|
|
280
|
+
d={`M ${cx + (item.shiftX || 0)} ${
|
|
281
|
+
cy + (item.shiftY || 0)
|
|
282
|
+
} L ${sx} ${sy} A ${radius} ${radius} 0 ${
|
|
283
|
+
semiCircle ? 0 : data[index].value > total / 2 ? 1 : 0
|
|
284
|
+
} 1 ${ax} ${ay} L ${cx + (item.shiftX || 0)} ${
|
|
285
|
+
cy + (item.shiftY || 0)
|
|
286
|
+
}`}
|
|
287
|
+
stroke={strokeColor}
|
|
288
|
+
strokeWidth={strokeWidth}
|
|
289
|
+
fill={
|
|
290
|
+
showGradient
|
|
291
|
+
? `url(#grad${index})`
|
|
292
|
+
: item.color || colors[index % 9]
|
|
293
|
+
}
|
|
294
|
+
onPress={() => {
|
|
295
|
+
item.onPress
|
|
296
|
+
? item.onPress
|
|
297
|
+
: props.onPress
|
|
298
|
+
? props.onPress(item, index)
|
|
299
|
+
: null;
|
|
300
|
+
}}
|
|
301
|
+
/>
|
|
302
|
+
);
|
|
303
|
+
})
|
|
304
|
+
)}
|
|
283
305
|
|
|
284
306
|
{showText &&
|
|
285
307
|
data.map((item, index) => {
|
|
@@ -308,6 +330,21 @@ export const PieChart = (props: propTypes) => {
|
|
|
308
330
|
x += item.shiftX || 0;
|
|
309
331
|
y += item.shiftY || 0;
|
|
310
332
|
|
|
333
|
+
if (data.length === 1) {
|
|
334
|
+
if (donut) {
|
|
335
|
+
y =
|
|
336
|
+
(radius -
|
|
337
|
+
innerRadius +
|
|
338
|
+
(item.textBackgroundRadius ||
|
|
339
|
+
props.textBackgroundRadius ||
|
|
340
|
+
item.textSize ||
|
|
341
|
+
textSize)) /
|
|
342
|
+
2;
|
|
343
|
+
} else {
|
|
344
|
+
y = cy;
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
|
|
311
348
|
// console.log('sx', sx);
|
|
312
349
|
// console.log('sy', sy);
|
|
313
350
|
// console.log('ax', ax);
|
|
@@ -317,6 +354,7 @@ export const PieChart = (props: propTypes) => {
|
|
|
317
354
|
{/* <Line x1={mx} x2={cx} y1={my} y2={cy} stroke="black" /> */}
|
|
318
355
|
{showTextBackground && (
|
|
319
356
|
<Circle
|
|
357
|
+
key={index + 'b'}
|
|
320
358
|
cx={x}
|
|
321
359
|
cy={y - (item.textSize || textSize) / 4}
|
|
322
360
|
r={
|
|
@@ -340,7 +378,7 @@ export const PieChart = (props: propTypes) => {
|
|
|
340
378
|
/>
|
|
341
379
|
)}
|
|
342
380
|
<SvgText
|
|
343
|
-
fill={item.textColor || textColor || colors[(index +
|
|
381
|
+
fill={item.textColor || textColor || colors[(index + 2) % 9]}
|
|
344
382
|
fontSize={item.textSize || textSize}
|
|
345
383
|
fontFamily={item.font || props.font}
|
|
346
384
|
fontWeight={item.fontWeight || props.fontWeight}
|