React Native Chart Kit Line Chart Example

May 28, 2021 . Admin

React Native Chart Kit Line Chart Example

Hi Guys,

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

Here, I will give you full example for simply display chart kit line 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 {LineChart} from "react-native-chart-kit";

const MyWebtutsComponent = () => {

  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>Bezier Line Chart</Text>
        <LineChart
          data={{
            labels: ["January", "February", "March", "April", "May", "June"],
            datasets: [
              {
                data: [
                  Math.random() * 100,
                  Math.random() * 100,
                  Math.random() * 100,
                  Math.random() * 100,
                  Math.random() * 100,
                  Math.random() * 100
                ]
              }
            ]
          }}
          width={330} // from react-native
          height={220}
          yAxisLabel="$"
          yAxisSuffix="k"
          yAxisInterval={1} // optional, defaults to 1
          chartConfig={{
            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: "#218838"
            }
          }}
          bezier
          style={{
            marginVertical: 8,
            borderRadius: 16
          }}
        />
      </View>
    </Provider>
  );
};


const styles = StyleSheet.create({
  mainbox:{
    textAlign:'center',
    margin: 15,
    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