react-native-mp-android-chart

React Native wrapper around MPAndroidChart chart library

Usage no npm install needed!

<script type="module">
  import reactNativeMpAndroidChart from 'https://cdn.skypack.dev/react-native-mp-android-chart';
</script>

README

React Native MPAndroidChart

This library is React Native wrapper of popular Android charting library MPAndroidChart.

Table of contents

Setup

Library can be easily installed using NPM:

npm i react-native-mp-android-chart --save

Additional setup is required because library is using native Android code.

android/build.gradle

allprojects {
    repositories {
        ...

        maven { url "https://jitpack.io" }    // used for MPAndroidChart
    }
}

android/settings.gradle

include ':reactNativeMPAndroidChart'
project(':reactNativeMPAndroidChart').projectDir = new File(
  rootProject.projectDir,
  '../node_modules/react-native-mp-android-chart/android'
)

android/app/build.gradle

dependencies {
    ...
    compile project(':reactNativeMPAndroidChart')
}

MainApplication.java

On top where imports are:

import com.github.reactNativeMPAndroidChart.MPAndroidChartPackage;

Add package in getPackages method:

protected List<ReactPackage> getPackages() {
    return Arrays.<ReactPackage>asList(
        new MainReactPackage(),
        new MPAndroidChartPackage()             // <----- Add this
    );
}

Usage

There are 8 supported charts with many configuration options. Almost all configuration available in base MPAndroidChart library are available through this wrapper. More details on available configuration can be found on their wiki.

Example of how charts are used and how to apply configuration can be found in example Android application.

Supported charts with examples:

Example code

This is simple example of how BarChart is used.

import {BarChart} from 'react-native-mp-android-chart';

class BarChartScreen extends React.Component {

  constructor() {
    super();

    this.state = {
      data: {
        datasets: [{
          yValues: [100, 105, 102, 110],
          label: 'Data set 1',
          config: {
            color: 'teal'
          }
        }, {
          yValues: [110, 100, 105, 108],
          label: 'Data set 2',
          config: {
            color: 'orange'
          }
        }],
        xValues: ['Q1', 'Q2', 'Q3', 'Q4']
      }
    };
  }

  render() {
    return (
      <View>
        <BarChart
          style={styles.chart}
          data={this.state.data}
          animation={{durationX: 2000}}
        />
      </View>
    );
  }
}

const styles = StyleSheet.create({
  chart: {
    height: 300,
    width: 300
  }
});

Example application

Example Android application with source code and apk is available here.

License

The MIT License

Copyright (c) 2016 Martin Skec

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.