Android Tutorial- Introduction to AFreeChart with Sample

business chart showing success

Today’s Android based business apps are incomplete without graphical aids like graphs/charts. Therefore in this tutorial I would be introducing a free charting library called AFreeChart, for Android platform. This library is based on JFreeChart 1.0.13. Although Android AFreeChart project is not being maintained at present, still its very easy to include this library in an Android project and create beautiful graph/charts effortlessly.

This Android tutorial- Introduction to AFreeChart with sample is a part of my series of tutorials on creating chart/graphs on Android. In my previous tutorial I stated that there are many ways to show charts on android but I would be showing only 5 most popular ways to display graphs on Android:

  1. Google Chart Tools- Latest Chart Tools – Android Tutorial- Google Chart Tools
  2. Google Chart Tools: Image Tools- Deprecated – Android Tutorial- Google Chart Tools Image API
  3. AFreeChart- Android Tutorial- Introduction to AFreeChart with Sample
  4. AChartEngine- AChartEngine Tutorial
  5. MPAndroidChart – Android MP Chart Library

As stated earlier this Android tutorial focuses on introduction to AFreeChart with sample. So lets start with a basic introduction to Android AFreeChart library.

AFreeChart library for Android is ported over time from famous JFreeChart 1.0.13 library for java, many leading projects like JBoss, Sports Tracker etc. are using this library. As we all know mobile apps are built with a mindset to provide us information which is absolutely necessary to us when we are on move, therefore Android AFreeChart library is ported with very basic and absolutely necessary charts. Although Android AFreeChart library has the functionality to draw 15 types of charts: Bar Charts, Overlaid Charts, Legends, Multiple Axis Charts, Financial Charts, Pie Charts, Miscellaneous Charts, Markers, Showcase Charts, Scatter Charts, Statistical Charts, Combine Charts, Line Charts, Annotations, and Vectors, but since this tutorial is a basic introduction to Android AFreeChart library we are only going to discuss Bar Charts.

The lowest Andorid OS version supported by AFreeChart library is 2.1. I am building this project for 4.1.2, for this I had to make minor changes in their demo code .I would point them out later in the tutorial. To create an app with Android AFreeChart we have to include a .jar file in our project by configuring the build path. This jar could be downloaded from official Google project page: afreechart-0.0.4.jar.

To draw a graph with Android AFreeChart library we have to create a view by code, in which the graph is actually drawn, and utilize that view in our class. Drawing a view is a very complicated task, way beyond the limits of a beginner Android programmer. This includes calling various methods from the AFreeChart library and android graphics libraries. Hence I took an easier path by including the from the demo code provided by good AFreeChart library people. But since we are compiling this app for later versions of android, two minor changes had to be done in their code, have a look I have highlighted them:

Have a look at the highlighted lines in this code, the only change that I have done is, typecasting the return type of getScaleX() and getScaleY() methods to Float. Credit for this class goes to good people at ICOMSYSTECH Co.,Ltd. (Niwano Masayoshi). Please note: To get full class, double click on the code box/dialog above.

Now that we have our view ready, we need a class, which can call the createBarChart method from the ChartFactory class of Android AFreeChart library.

The purpose of this is to create a bar chart by proving all the required arguments. In this class we can create a data set for graph/chart, set colors and set various other properties for chart.

We can now move on to our main activity class which would finally display bar chart from Android AFreeChart library.

The code in this class needs no explanation, we are just creating a view from TruitonBarChartView class and setting it as content view. With this done our graph/chart from Android AFreeChart library would be drawn as:

The major advantages of Android AFreeChart over Google Chart Tools is that, no internet connection is required to render chart/graphs and graphs are live i.e. they are scrollable, more interactive which leads to better user experience. To get a better understanding of Android AFreeChart please have a look at the chart gallery and AFreeChart javadocs. Next I would be writing a tutorial about AChartEngine which would conclude my series on charts on Android. If this tutorial helped you please like n share it on Facebook, and connect with us on by liking our Facebook page.

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 *

7 thoughts on “Android Tutorial- Introduction to AFreeChart with Sample

  • Akhil

    I cannot find the

    i was looking for another graph engine achartengine, but stumble upon this
    that does not support gradient color vertical, can you provide source code for; or any jar for org.afree

  • Tim


    I have been trying to use AFreechart to create dial charts without success. The dial classes are subtly different to the ones in JFreeChart.

    Can you direct me to some examples?

    Kind regards,


  • Hemant

    i am creating an app in which i have created a activity.xml file now i want to add some textviews & imageviews along with this bar chart. But using this code
    i can only generate bar chart in a different view(“mview in this code”). Is there any way to use this view inside my activity.xml file, so that i can have my textviews, imageviews and bar chat together

  • Diego Chalderonier

    Hello, your post helps me a lot. But, it’s the same procedure to create a Line chart? Like an equation with X and Y values. And joint the points X and Y.
    Thank You!