Android Chart Example: MP Android Chart library


Photo Credit: Ken Teegardin - Android Chart Example MPAndroidChart

Now days when building business apps, or fitness apps, one of the common UI component used is a chart. But drawing a chart through graphics could take a lot of time and would slow down your development. Therefore to fast pace your development, many open source libraries are available online. One of those libraries is MPAndroidChart by Phil Jay, this library when compared to other chart libraries, is comparatively new. But still it is very stable and is starred by more than 3K users on GitHub. By using MP Chart library for Android, displaying graphs or charts is much simpler now as it also supports customization and animation of graphs. Therefore in this Android chart example, I would make a bar chart, using MPAndroidChart library.

As I mentioned above, MP Android Chart library is not the only library available for drawing charts on Android. I would like to mention here that this Android Chart Example is the fifth in line to my series on displaying graphs on Android. Please go through them to choose a best approach for your project, as different projects have different requirements:

  1. Google Chart Tools- Latest Chart Tools
  2. Google Chart Tools: Image ToolsDeprecated
  3. AFreeChart Library
  4. AChartEngine Library
  5. Android MP Chart Library

 

MP Android Chart Library Example

Using these libraries, drawing charts on Android could never be simpler. MPAndroidChart library works on Android API 8 and above, but if you are using animations, it works on API 11 and above. Animations is one of the greatest features apart from easy data input, this library could have. Some how I feel animations give good user experience to an application. Through MPAndroidChart library, we can use more than 25 inbuilt animations, also can define custom animations. When making charts on Android MPAndroidChart library which falls under the Apache 2.0 license, we can draw a:

  • Simple Bar Chart
  • Grouped Bar Chart
  • Horizontal Bar Chart
  • Simple Line Chart
  • Line Chart with Cubic Lines
  • Grouped Line Chart
  • Combined Line and Bar Chart
  • Pie Chart
  • Scatter Chart
  • Candlestick Chart
  • Radar Chart

 

 

Moving forward in this tutorial, we will make a grouped bar chart, with animation as shown in the animation above. To start first add these in your gradle file:

Next lets create a layout for MP Android Chart Example:

As you can see in the above layout, I have used com.github.mikephil.charting.charts.BarChart xml tag to create an Android bar chart example. But if you plan on to create different charts with MPAndroidChart library don’t forget to change the tag as shown below.

Android MPChart Types

 

Next lets have a look at the activity code:

This would result in a great looking bar chart like this:

Android Bar Chart MP

 

Now lets understand how MPAndroidChart library works. As shown in the above class, first a BarChart is initialized. Then data according to the bar chart is generated using the BarEntry class. This is a subclass of Entry class, which is the base class for all chart data types in MPAndroidChart library. Further this BarEntry is added into BarDataSet. Then all of these values along with X axis values are set in the bar chart using chart.setData(data) method. Now to make the bar chart reflect this latest data, we need to call chart.invalidate() method. This would result into a great looking bar chart as shown above.

As I mentioned, MPAndroidChart library is customizable, lets have a look at some of its key features:

MP Android Chart Animations

To animate a chart from MPAndroidChart library these methods are used:

  • chart.animateX(2000) – For X axis animation
  • chart.animateY(2000) – For Y axis animation
  • chart.animateXY(2000, 2000) – For XY axis animation

Although, above code would do a standard animation. But if you wish to change the animation style or want to make a custom animation, refer to this page. Please note animations would work only on API 11 and above.

 

MP Android Chart Colors

While making charts on Android using MPAndroidChart library, you can also change the color of bars in the chart by using these methods:

  • setColor – used to set a single color to full data set.
  • setColors – used to set entries in different colors, in a data set.

These methods are used while creating the DataSet object as shown above.

 

Conclusion

With this I would like to conclude by saying, MPAndroidChart is a great library. Above I showed an Android chart example, for bar chart, using this library. But if you wish to make other types of charts, you may just need to change the class names. As MP Chart library is designed in such a way that most of the charts input data in same way. Hope this helped. Connect with us on Facebook, Google+ and Twitter for more updates.

About Mohit Gupt

An android enthusiast, and an iPhone user with a keen interest in development of innovative applications.


Leave a comment

Your email address will not be published. Required fields are marked *

23 thoughts on “Android Chart Example: MP Android Chart library