enhanced Line and Pie charts

This commit is contained in:
Abhinandan Kushwaha 2021-08-02 16:41:56 +05:30
parent fb59d95d28
commit 300f2b0a99
4 changed files with 157 additions and 166 deletions

18
App.js
View File

@ -1,6 +1,6 @@
import React from 'react'; import React from 'react';
import {View} from 'react-native'; import {View} from 'react-native';
import {LineChart} from './src'; import {BarChart, LineChart, PieChart} from './src';
const App = () => { const App = () => {
const lineData = [ const lineData = [
@ -18,8 +18,19 @@ const App = () => {
{value: 100, text: 'May'}, {value: 100, text: 'May'},
]; ];
const data = [{value: 50}, {value: 80}, {value: 90}, {value: 70}];
return ( return (
<View style={{flex: 1, justifyContent: 'center', alignItems: 'center'}}> <View style={{flex: 1, justifyContent: 'center', alignItems: 'center'}}>
{/* <LineChart data={data} curved /> */}
{/* <BarChart
data={data}
frontColor="lightgray"
cappedBars
capThickness={2}
capColor="gray"
/> */}
{/* <PieChart data={data} donut isThreeD shadow /> */}
<LineChart <LineChart
xAxisColor="red" xAxisColor="red"
xAxisThickness={2} xAxisThickness={2}
@ -30,14 +41,15 @@ const App = () => {
animationDuration={1200} animationDuration={1200}
height={300} height={300}
rotateLabel rotateLabel
thickness={1} // thickness={1}
dataPointsHeight={8} dataPointsHeight={8}
dataPointsWidth={8} dataPointsWidth={8}
animateTogether
// textColor="skyblue" // textColor="skyblue"
// dataPointsShape="rectangular" // dataPointsShape="rectangular"
spacing={84} spacing={84}
initialSpacing={4} initialSpacing={4}
areaChart // areaChart
color1="skyblue" color1="skyblue"
color2="orange" color2="orange"
data={[...lineData]} data={[...lineData]}

View File

@ -1,7 +1,7 @@
{ {
"name": "react-native-gifted-charts", "name": "react-native-gifted-charts",
"version": "0.0.1", "version": "0.0.1",
"description": "The most complete library for Bar, Line, Area and Pie charts in React Native. Allows 2D, 3D, gradient, animations and live data updates.", "description": "The most complete library for Bar, Line, Area, Pie and Donut charts in React Native. Allows 2D, 3D, gradient, animations and live data updates.",
"main": "src/index.tsx", "main": "src/index.tsx",
"scripts": { "scripts": {
"android": "react-native run-android", "android": "react-native run-android",

View File

@ -1,4 +1,4 @@
import React, {useEffect, useState} from 'react'; import React, {Fragment, useEffect, useState} from 'react';
import { import {
View, View,
ScrollView, ScrollView,
@ -228,17 +228,17 @@ export const LineChart = (props: propTypes) => {
props.dataPointsHeight1 || props.dataPointsHeight || 2; props.dataPointsHeight1 || props.dataPointsHeight || 2;
const dataPointsWidth1 = props.dataPointsWidth1 || props.dataPointsWidth || 2; const dataPointsWidth1 = props.dataPointsWidth1 || props.dataPointsWidth || 2;
const dataPointsRadius1 = const dataPointsRadius1 =
props.dataPointsRadius1 || props.dataPointsRadius || 2; props.dataPointsRadius1 || props.dataPointsRadius || 3;
const dataPointsColor1 = const dataPointsColor1 =
props.dataPointsColor1 || props.dataPointsColor || 'black'; props.dataPointsColor1 || props.dataPointsColor || 'black';
const dataPointsShape = const dataPointsShape1 =
props.dataPointsShape || props.dataPointsShape || 'circular'; props.dataPointsShape1 || props.dataPointsShape || 'circular';
const dataPointsHeight2 = const dataPointsHeight2 =
props.dataPointsHeight2 || props.dataPointsHeight || 2; props.dataPointsHeight2 || props.dataPointsHeight || 2;
const dataPointsWidth2 = props.dataPointsWidth2 || props.dataPointsWidth || 2; const dataPointsWidth2 = props.dataPointsWidth2 || props.dataPointsWidth || 2;
const dataPointsRadius2 = const dataPointsRadius2 =
props.dataPointsRadius2 || props.dataPointsRadius || 2; props.dataPointsRadius2 || props.dataPointsRadius || 3;
const dataPointsColor2 = const dataPointsColor2 =
props.dataPointsColor2 || props.dataPointsColor || 'blue'; props.dataPointsColor2 || props.dataPointsColor || 'blue';
const dataPointsShape2 = const dataPointsShape2 =
@ -648,6 +648,91 @@ export const LineChart = (props: propTypes) => {
); );
}; };
const renderDataPoints = (
dataForRender,
dataPointsShape,
dataPointsWidth,
dataPointsHeight,
dataPointsColor,
dataPointsRadius,
textColor,
textFontSize,
) => {
return dataForRender.map((item: itemType, index: number) => {
// console.log('comes in');
if (dataPointsShape === 'rectangular') {
return (
<Fragment key={index}>
<Rect
x={initialSpacing - dataPointsWidth + spacing * index}
y={
containerHeight -
dataPointsHeight / 2 +
10 -
(item.value * containerHeight) / maxValue
}
width={dataPointsWidth}
height={dataPointsHeight}
fill={dataPointsColor}
/>
{item.text && (
<CanvasText
fill={item.textColor || textColor}
fontSize={item.textFontSize || textFontSize}
x={
initialSpacing -
dataPointsWidth +
spacing * index +
(item.textShiftX || 0)
}
y={
containerHeight -
dataPointsHeight / 2 +
10 -
(item.value * containerHeight) / maxValue +
(item.textShiftY || 0)
}>
{item.text}
</CanvasText>
)}
</Fragment>
);
}
return (
<Fragment key={index}>
<Circle
cx={initialSpacing - dataPointsWidth / 2 + spacing * index}
cy={
containerHeight + 10 - (item.value * containerHeight) / maxValue
}
r={dataPointsRadius}
fill={dataPointsColor}
/>
{item.text && (
<CanvasText
fill={item.textColor || textColor}
fontSize={item.textFontSize || textFontSize}
x={
initialSpacing -
dataPointsWidth +
spacing * index +
(item.textShiftX || 0)
}
y={
containerHeight -
dataPointsHeight / 2 +
10 -
(item.value * containerHeight) / maxValue +
(item.textShiftY || 0)
}>
{item.text}
</CanvasText>
)}
</Fragment>
);
});
};
const renderLine = ( const renderLine = (
points: any, points: any,
currentLineThickness: number | undefined, currentLineThickness: number | undefined,
@ -702,6 +787,28 @@ export const LineChart = (props: propTypes) => {
strokeWidth={currentLineThickness || thickness} strokeWidth={currentLineThickness || thickness}
/> />
)} )}
{!hideDataPoints1 &&
renderDataPoints(
data,
dataPointsShape1,
dataPointsWidth1,
dataPointsHeight1,
dataPointsColor1,
dataPointsRadius1,
textColor1,
textFontSize1,
)}
{!hideDataPoints2 &&
renderDataPoints(
data2,
dataPointsShape2,
dataPointsWidth2,
dataPointsHeight2,
dataPointsColor2,
dataPointsRadius2,
textColor2,
textFontSize2,
)}
</Svg> </Svg>
</View> </View>
); );
@ -764,155 +871,27 @@ export const LineChart = (props: propTypes) => {
/> />
)} )}
{!hideDataPoints1 && {!hideDataPoints1 &&
data.map((item: itemType, index: number) => { renderDataPoints(
if (dataPointsShape === 'rectangular') { data,
return ( dataPointsShape1,
<> dataPointsWidth1,
<Rect dataPointsHeight1,
x={initialSpacing - dataPointsWidth1 + spacing * index} dataPointsColor1,
y={ dataPointsRadius1,
containerHeight - textColor1,
dataPointsHeight1 / 2 + textFontSize1,
10 - )}
(item.value * containerHeight) / maxValue
}
width={dataPointsWidth1}
height={dataPointsHeight1}
fill={dataPointsColor1}
/>
{item.text && (
<CanvasText
fill={item.textColor || textColor1}
fontSize={item.textFontSize || textFontSize1}
x={
initialSpacing -
dataPointsWidth1 +
spacing * index +
(item.textShiftX || 0)
}
y={
containerHeight -
dataPointsHeight1 / 2 +
10 -
(item.value * containerHeight) / maxValue +
(item.textShiftY || 0)
}>
{item.text}
</CanvasText>
)}
</>
);
}
return (
<>
<Circle
cx={initialSpacing - dataPointsWidth1 / 2 + spacing * index}
cy={
containerHeight +
10 -
(item.value * containerHeight) / maxValue
}
r={dataPointsRadius1}
fill={dataPointsColor1}
/>
{item.text && (
<CanvasText
fill={item.textColor || textColor1}
fontSize={item.textFontSize || textFontSize1}
x={
initialSpacing -
dataPointsWidth1 +
spacing * index +
(item.textShiftX || 0)
}
y={
containerHeight -
dataPointsHeight1 / 2 +
10 -
(item.value * containerHeight) / maxValue +
(item.textShiftY || 0)
}>
{item.text}
</CanvasText>
)}
</>
);
})}
{!hideDataPoints2 && {!hideDataPoints2 &&
data2.map((item: itemType, index: number) => { renderDataPoints(
if (dataPointsShape2 === 'rectangular') { data2,
return ( dataPointsShape2,
<> dataPointsWidth2,
<Rect dataPointsHeight2,
x={initialSpacing - dataPointsWidth2 + spacing * index} dataPointsColor2,
y={ dataPointsRadius2,
containerHeight - textColor2,
dataPointsHeight2 / 2 + textFontSize2,
10 - )}
(item.value * containerHeight) / maxValue
}
width={dataPointsWidth2}
height={dataPointsHeight2}
fill={dataPointsColor2}
/>
{item.text && (
<CanvasText
fill={item.textColor || textColor2}
fontSize={item.textFontSize || textFontSize2}
x={
initialSpacing -
dataPointsWidth2 +
spacing * index +
(item.textShiftX || 0)
}
y={
containerHeight -
dataPointsHeight2 / 2 +
10 -
(item.value * containerHeight) / maxValue +
(item.textShiftY || 0)
}>
{item.text}
</CanvasText>
)}
</>
);
}
return (
<>
<Circle
cx={initialSpacing - dataPointsWidth2 / 2 + spacing * index}
cy={
containerHeight +
10 -
(item.value * containerHeight) / maxValue
}
r={dataPointsRadius2}
fill={dataPointsColor2}
/>
{item.text && (
<CanvasText
fill={item.textColor || textColor2}
fontSize={item.textFontSize || textFontSize2}
x={
initialSpacing -
dataPointsWidth2 +
spacing * index +
(item.textShiftX || 0)
}
y={
containerHeight -
dataPointsHeight2 / 2 +
10 -
(item.value * containerHeight) / maxValue +
(item.textShiftY || 0)
}>
{item.text}
</CanvasText>
)}
</>
);
})}
</Svg> </Svg>
</Animated.View> </Animated.View>
); );

View File

@ -6,7 +6,7 @@ const pi = Math.PI;
type propTypes = { type propTypes = {
radius?: number; radius?: number;
is3D?: Boolean; isThreeD?: Boolean;
donut?: Boolean; donut?: Boolean;
innerRadius?: number; innerRadius?: number;
shadow?: Boolean; shadow?: Boolean;
@ -32,7 +32,7 @@ type itemType = {
textColor?: string; textColor?: string;
}; };
export const PieChart = (props: propTypes) => { export const PieChart = (props: propTypes) => {
const {data, is3D, textColor} = props; const {data, isThreeD, textColor} = props;
const radius = props.radius || 120; const radius = props.radius || 120;
const shadowWidth = props.shadowWidth || (4 * radius) / 3; const shadowWidth = props.shadowWidth || (4 * radius) / 3;
const backgroundColor = props.backgroundColor || 'white'; const backgroundColor = props.backgroundColor || 'white';
@ -59,7 +59,7 @@ export const PieChart = (props: propTypes) => {
} }
}); });
const canvasHeight = is3D ? radius * 2.5 + 60 : radius * 2 + 60; const canvasHeight = isThreeD ? radius * 2.5 + 60 : radius * 2 + 60;
const canvasWidth = radius * 2 + 60; const canvasWidth = radius * 2 + 60;
const [visibility, setVisibility] = useState(false); const [visibility, setVisibility] = useState(false);
@ -73,7 +73,7 @@ export const PieChart = (props: propTypes) => {
return null; return null;
} }
if (is3D) { if (isThreeD) {
canvas.height = canvasHeight; canvas.height = canvasHeight;
} else { } else {
canvas.height = canvasHeight; canvas.height = canvasHeight;
@ -86,7 +86,7 @@ export const PieChart = (props: propTypes) => {
const initialValue = 30; const initialValue = 30;
/****************** SHADOW ***************/ /****************** SHADOW ***************/
if (is3D && shadow) { if (isThreeD && shadow) {
ctx.beginPath(); ctx.beginPath();
ctx.moveTo(initialValue, radius + initialValue); ctx.moveTo(initialValue, radius + initialValue);
ctx.lineTo(initialValue, shadowWidth + initialValue); ctx.lineTo(initialValue, shadowWidth + initialValue);
@ -128,7 +128,7 @@ export const PieChart = (props: propTypes) => {
/**************** being visible inside the chart content if ***************/ /**************** being visible inside the chart content if ***************/
/**************** the color of sections is transparent ***************/ /**************** the color of sections is transparent ***************/
if (is3D && shadow) { if (isThreeD && shadow) {
ctx.beginPath(); ctx.beginPath();
ctx.moveTo( ctx.moveTo(
@ -211,7 +211,7 @@ export const PieChart = (props: propTypes) => {
}; };
return ( return (
<View style={is3D && {transform: [{scaleY: 0.5}]}}> <View style={isThreeD && {transform: [{scaleY: 0.5}]}}>
<Canvas ref={handleCanvas} /> <Canvas ref={handleCanvas} />
{visibility && donut && !isDataShifted && ( {visibility && donut && !isDataShifted && (
<View <View
@ -225,13 +225,13 @@ export const PieChart = (props: propTypes) => {
left: canvasWidth / 2 - innerRadius + shiftCenterX, left: canvasWidth / 2 - innerRadius + shiftCenterX,
top: top:
canvasHeight / 2 - canvasHeight / 2 -
innerRadius * (is3D ? 1.5 : 1) + innerRadius * (isThreeD ? 1.5 : 1) +
shiftCenterY, shiftCenterY,
borderWidth: innerCircleBorderWidth, borderWidth: innerCircleBorderWidth,
borderColor: innerCircleBorderColor, borderColor: innerCircleBorderColor,
backgroundColor: innerCircleColor, backgroundColor: innerCircleColor,
}, },
is3D && { isThreeD && {
borderTopWidth: innerCircleBorderWidth * 5, borderTopWidth: innerCircleBorderWidth * 5,
borderLeftWidth: shiftCenterX borderLeftWidth: shiftCenterX
? innerCircleBorderWidth * 2 ? innerCircleBorderWidth * 2