React Native Chart Kit Bar Chart Example

May 31, 2021 . Admin

React Native Chart Kit bar Chart Example

Hi Guys,

Today, I will learn you how to use chart kit bar chart in react native. we will show example of react native chart kit bar chart.You can easily create bar chart in react native. First i will import react-native-chart-kit and react-native-svg, after I will create chart kit bar chart in react native.

Here, I will give you full example for simply display chart kit bar chart using react native as bellow.

Step 1 - Create project

In the first step Run the following command for create project.

expo init ChartProject
Step 2 - Install Package

In the step,I will install yarn add react-native-chart-kit package.

yarn add react-native-chart-kit
After,I will install yarn add react-native-svg package.
yarn add react-native-svg
Step 3 - App.js

In this step, You will open App.js file and put the code.

import React, { Component } from "react";
import {View,StyleSheet,Text,Dimensions} from 'react-native';
import { Provider,Appbar} from 'react-native-paper';
import { BarChart} from "react-native-chart-kit";

const MyWebtutsComponent = () => {

  const data = {
   labels: ["January", "February", "March", "April", "May", "June"],
   datasets: [
   {
      data: [20, 45, 28, 80, 99, 43],
      color: (opacity = 1) => `rgba(134, 65, 244, ${opacity})`, // optional
      strokeWidth: 2 // optional
    }
    ],
    legend: ["Rainy Days"] // optional
  };

  const _goBack = () => console.log('Went back');

  const _handleSearch = () => console.log('Searching');

  const _handleMore = () => console.log('Shown more');

  return (
    <Provider>
    <Appbar.Header style={styles.header}>
      <Appbar.BackAction onPress={_goBack} />
      <Appbar.Content title="My App" subtitle="Subtitle" />
      <Appbar.Action icon="magnify" onPress={_handleSearch} />
      <Appbar.Action icon="dots-vertical" onPress={_handleMore} />
    </Appbar.Header>
     <View style={styles.mainbox}>
        <Text>Bar Chart - MyWebtuts.com</Text>
        <BarChart
          data={{
            labels: ["January", "February", "March", "April", "May"],
            datasets: [
              {
                data: [
                  Math.random() * 100,
                  Math.random() * 100,
                  Math.random() * 100,
                  Math.random() * 100,
                  Math.random() * 100,
                ]
              }
            ]
          }}
          width={335} // from react-native
          height={220}
          yAxisLabel="$"
          yAxisSuffix="k"
          yAxisInterval={1} // optional, defaults to 1
          chartConfig={{
            backgroundColor: "#e26a00",
            backgroundGradientFrom: "#218838",
            backgroundGradientTo: "#218838",
            decimalPlaces: 2, // optional, defaults to 2dp
            color: (opacity = 1) => `rgba(255, 255, 255, ${opacity})`,
            labelColor: (opacity = 1) => `rgba(255, 255, 255, ${opacity})`,
            style: {
              borderRadius: 16
            },
            propsForDots: {
              r: "6",
              strokeWidth: "2",
              stroke: "#ffa726"
            }
          }}
          bezier
          style={{
            marginVertical: 8,
            borderRadius: 16
          }}
        />
      </View>
    </Provider>
  );
};


const styles = StyleSheet.create({
  title:{
    fontSize: 20,
    textAlign:'center',
  },
  mainbox:{
    textAlign:'center',
    margin: 10,
    justifyContent: 'space-between',
  },
});

export default MyWebtutsComponent;
Step 4 - Run project

In the last step run your project using bellow command.

expo start
Output

It will help you..

#React Native