React Native Pie Chart
This is an Example to Make 3 Different Type of Pie Chart in React Native. React Native Pie Chart can be made using the library react-native-pie
. If you are making an app that has numbers and lots of statics to show then charts are the best way to show that data. Pie Charts are best to use when you are trying to compare different parts of a whole. They do not show changes over time. Here is the Example to make Pie Chart in your React Native App.
If you want to explore more Charts then you can visit 7 Type of Graph using React Native Chart Kit and for Speedometer graph visit visualize data with Speedometer Graph in React Native.
To make a Pie Chart we are going to use a react-native-pie library. It will provide a Pie component that is very easy to integrate. You can see the example below.
How to use Pie component?
<Pie
radius={70}
//completly filled pie chart with radius 70
series={[10, 20, 30, 40]}
//values to show and color sequentially
colors={['#f00', '#0f0', '#00f', '#ff0']}
/>
In this example, We are going to make a Solid/Filled Pie Chart, Donut Pie Chart, and Gauge Pie Chart. So let’s get started.
To Make a React Native App
Getting started with React Native will help you to know more about the way you can make a React Native project. We are going to use react native command line interface to make our React Native App.
If you have previously installed a global react-native-cli package, please remove it as it may cause unexpected issues:
npm uninstall -g react-native-cli @react-native-community/cli
Run the following commands to create a new React Native project
npx react-native init ProjectName
If you want to start a new project with a specific React Native version, you can use the --version argument:
npx react-native init ProjectName --version X.XX.X
Note If the above command is failing, you may have old version of react-native
or react-native-cli
installed globally on your pc. Try uninstalling the cli and run the cli using npx.
This will make a project structure with an index file named App.js in your project directory.
Installation of Dependency
To use Pie
component, you need to install react-native-pie
and @react-native-community/art
.
To install this open the terminal and jump into your project
cd ProjectName
Run the following command
npm install react-native-pie --save
npm install @react-native-community/art --save
These commands will copy all the dependencies into your node_module directory, –save is optional, it is just to update the dependency in your package.json file.
Code to Make a Pie Chart in React Native
Now Open App.js in any code editor and replace the code with the following code
App.js
// Example to Make 3 Different Type of Pie Chart in React Native
// https://aboutreact.com/react-native-pie-chart/
// import React in our code
import React from 'react';
// import all the components we are going to use
import {SafeAreaView, StyleSheet, View, Text} from 'react-native';
// import pie to make pie chart
import Pie from 'react-native-pie';
const App = () => {
return (
<SafeAreaView style={{flex: 1}}>
<View style={styles.container}>
<Text>Pie Chart Example</Text>
<View
style={{
paddingVertical: 15,
flexDirection: 'row',
width: 350,
justifyContent: 'space-between',
}}
>
<Pie
radius={80}
sections={[
{
percentage: 10,
color: '#C70039',
},
{
percentage: 20,
color: '#44CD40',
},
{
percentage: 30,
color: '#404FCD',
},
{
percentage: 40,
color: '#EBD22F',
},
]}
strokeCap={'butt'}
/>
<Pie
radius={80}
innerRadius={50}
sections={[
{
percentage: 10,
color: '#C70039',
},
{
percentage: 20,
color: '#44CD40',
},
{
percentage: 30,
color: '#404FCD',
},
{
percentage: 40,
color: '#EBD22F',
},
]}
strokeCap={'butt'}
/>
</View>
<View
style={{
paddingVertical: 15,
flexDirection: 'row',
width: 350,
justifyContent: 'space-between',
}}
>
<Pie
radius={80}
innerRadius={60}
sections={[
{
percentage: 10,
color: '#C70039',
},
{
percentage: 20,
color: '#44CD40',
},
{
percentage: 30,
color: '#404FCD',
},
{
percentage: 40,
color: '#EBD22F',
},
]}
dividerSize={4}
strokeCap={'round'}
/>
<Pie
radius={80}
innerRadius={60}
sections={[
{
percentage: 10,
color: '#C70039',
},
{
percentage: 20,
color: '#44CD40',
},
{
percentage: 30,
color: '#404FCD',
},
{
percentage: 40,
color: '#EBD22F',
},
]}
dividerSize={6}
strokeCap={'butt'}
/>
</View>
<View
style={{
paddingVertical: 15,
width: 350,
flexDirection: 'row',
justifyContent: 'space-between',
}}
>
<Pie
radius={80}
sections={[
{
percentage: 10,
color: '#C70039',
},
{
percentage: 20,
color: '#44CD40',
},
{
percentage: 30,
color: '#404FCD',
},
{
percentage: 40,
color: '#EBD22F',
},
]}
dividerSize={6}
strokeCap={'butt'}
/>
<View style={{ width: 175, alignItems: 'center' }}>
<Pie
radius={80}
innerRadius={75}
sections={[
{
percentage: 60,
color: '#f00',
},
]}
backgroundColor="#ddd"
/>
<View style={styles.gauge}>
<Text style={styles.gaugeText}>
60%
</Text>
</View>
</View>
</View>
</View>
</SafeAreaView>
);
};
export default App;
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'space-around',
marginTop: 30,
},
gauge: {
position: 'absolute',
width: 140,
height: 140,
alignItems: 'center',
justifyContent: 'center',
},
gaugeText: {
backgroundColor: 'transparent',
color: '#000',
fontSize: 24,
},
});
To Run the React Native App
Open the terminal again and jump into your project using.
cd ProjectName
1. Start Metro Bundler
First, you will need to start Metro, the JavaScript bundler that ships with React Native. To start Metro bundler run following command:
npx react-native start
Once you start Metro Bundler it will run forever on your terminal until you close it. Let Metro Bundler run in its own terminal. Open a new terminal and run the application.
2. Start React Native Application
To run the project on an Android Virtual Device or on real debugging device:
npx react-native run-android
or on the iOS Simulator by running (macOS only)
npx react-native run-ios
Possible issue
If you face any issues while running this project on the iOS device then you need to link the ART library. I prefer first run this example on the iOS device and is you face any issue then only do these steps
1. To Link, the ART library for the IOS open the ProjectName.xcworkspace in Xcode
2. You will see Project Structure in Left Menu. Click on the Libraries option to expand.
3. Navigate to node_modules/react-native/Libraries/ART/ART.xcodeproj and drag ART.xcodeproj into the Libraries
4. Now expand the ART.xcodeproj and navigate to Products. You will see libART.a. Click on the Project in the left sidebar (PieChartExample in this case), and now find the Build Phase tab in the workspace area where you can see Link Binary With Libraries. (Please make note that you have selected the Project and then opened the Build Phase)
5. Now drag the libART.a file into the Link Binary With Libraries.
Output Screenshots
Output in Online Emulator
That was the React Native Pie Chart. If you have any doubts or you want to share something about the topic you can comment below or contact us here. There will be more posts coming soon. Stay tuned!
Hope you liked it. 🙂
Ho wcan give center text in PIE chart
We have no option to make the label in the center in this library. If you want to make the center label you have to manage on your own.
For an idea you can use below hint
<View (style-> Align center/Justify center/text center )>
<PieChart></PieChart>
<Text>Text You want</Text>
</View>