Peter_vdL

Enterprise Grade Charts

by Peter_vdL Motorola 02-14-2012 10:03 AM - edited 02-24-2012 02:51 PM

A key part of mobilizing the enterprise is about bringing data reports and analysis to the edge of the network.  That’s why I was so delighted to learn about AChartEngine open source software.  It is a freely-available library which can be linked with your apps to produce beautiful graphical output in the form of customizable graphs and charts.  You can find AChartEngine at http://www.achartengine.org.   It uses the Apache Version 2 license, which is business-friendly and does not require redistribution of any modified sources or linked binaries.

The documentation is in the form of Javadoc pages explaining the API.   There is also a sample project, and the source code itself.  The sample project, the library jar files and the Javadocs are at http://code.google.com/p/achartengine/downloads/list.  The source code is maintained at http://achartengine.googlecode.com/svn/trunk/.

Here’s a simple pie chart that I built using AChartEngine.  It’s a pie chart showing some of my favorite local taverns in Silicon Valley - a pie chart of bars, if you like.

                  Figure 1: a pie chart of my favorite bars


As you can see, I like Molly Magees best, with the Tied House brewpub a close second.  I hardly ever visit Francesca’s.  I created this chart with about one dozen lines of code.  You can find several instructional videos on YouTube by searching for “AChartEngine”, and the basic steps are these:
  1. do the one time setup in your Eclipse project - create a library folder, put the achartengine jar file into it, and add that jar file to your project build path.   Add      <activity android:name="org.achartengine.GraphicalActivity"/> to the manifest file.
  2. Create and initialize 3 arrays, one each for the colors, label strings, and slice sizes of your pie chart
  3. Create a “CategorySeries” object that will hold the strings and relative pie sizes.  Load these strings and size values into the CategorySeries object.  The graphing library takes all its data from Series objects that you give it.
  4. Create a “DefaultRenderer” object, and use it in the next step.  CategorySeries and DefaultRenderer are types defined in AChartEngine.
  5. For each color in the chart, create a SimpleSeriesRenderer, and set it to your chosen color.  Add each SimpleSeriesRenderer to the DefaultRenderer.  There is one Renderer for each Series.   As here, the Renderer may contain other Renderers for its subpieces.
  6. Create a PieChartIntent that holds the CategorySeries and the DefaultRenderer
  7. Fire off the PieChartIntent to get your graph drawn.  Your graph is drawn as a separate Activity.

Here is the code I whipped up to do these steps.

package com.example.bars;

import org.achartengine.ChartFactory;
import org.achartengine.chart.PieChart;
import org.achartengine.model.CategorySeries;
import org.achartengine.renderer.DefaultRenderer;
import org.achartengine.renderer.SimpleSeriesRenderer;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
   	super.onCreate(savedInstanceState);
      	Intent intent = buildIntent();
    	startActivity(intent);          // step 7
    }

    public Intent buildIntent() {
   	 int[] values = new int[] { 5, 15, 25, 50, 75 };        // step 2
   	 String[] bars = new String[] {"Francesca's",  "King of Clubs", 
                                 "Zen Lounge", "Tied House", "Molly Magees"};
   	 int[] colors = new int[] { Color.BLUE, Color.GREEN, Color.MAGENTA, 
                                   Color.YELLOW, Color.CYAN };

   	 CategorySeries series = new CategorySeries("Pie Chart");  // step 3
   	 DefaultRenderer dr = new DefaultRenderer();   // step 4

   	 for (int v=0; v<5; v++){    // step 5
   		 series.add(bars[v], values[v]);
   		 SimpleSeriesRenderer r = new SimpleSeriesRenderer();
   		 r.setColor(colors[v]);
   		 dr.addSeriesRenderer(r);
   	 }
   	 dr.setZoomButtonsVisible(true);
   	 dr.setZoomEnabled(true);
   	 dr.setChartTitleTextSize(20);
   	 return ChartFactory.getPieChartIntent(    // step 6
                              this, series, dr, "Pie of bars");
    }

}

 


I think you’ll agree that is an excellent pie chart for a very small investment of code.  There is a youtube video on the one time setup at http://www.youtube.com/watch?v=9xdp7Ay19BQ .

Enterprise developers usually want to standardize their tools as much as possible.  While there isn’t a version of AChartEngine for other mobile platforms, since it is open source, there could be, if any public-spirited developer wants to retarget it.    IT groups frequently use Microsoft Excel to create charts and graphs.  AChartEngine doesn’t have any special support for importing data from Excel or from a cloud server.  The developer, Dan Dromereschi, explained that he designed ACE with the goal that it should do one thing and do it well, and that one thing is charting.  Some users have also commented favorably on the fact that the library does not need an internet connection to work.  So it’s your job to get the data onto the device, then the software will graph it for you.   

If you’re interested in working on AChartEngine, email Dan at contact@achartengine.org. Come on back to this blog in a few days time, and I’ll show another code example for drawing a different kind of AChartEngine graph.  
What solution are you currently using to draw graphs in your Android apps?  Please follow up with a post describing your thoughts.


Peter van der Linden
Android Technology Evangelist

Comments
by pauk on 05-03-2012 09:34 AM

Hi Peter,

Please excuse the very basic question, but I am very new to
AChartEngine. I tried out both a Pie and Bar Chart on my Samsung
Galaxy S2 and it displayed perfectly. However, on a HTC Wildfire, both
charts and labels only just fitted into the screen and the display was
very messy.
How do you scale the charts for different sizes/resolution of
screens ?

Thanks very much

Paul

by Peter_vdL Motorola on 05-04-2012 06:14 AM

Hi Pauk,

 

AChartEngine package looks at how much room there is on the screen, and does the scaling for you.  If that's not happening on the handset you tested on, it suggests that there's a latent bug either on that platform or in the package. 

 

In this case, more testing would be indicated, followed by reporting the issue to one or the other.  Or just identify and fix it - that's the beauty of open source software.  Well done on trying some new software.

 

Peter

Android Technology Evangelist

Post a Comment
Be sure to enter a unique name. You can't reuse a name that's already in use.
Be sure to enter a unique email address. You can't reuse an email address that's already in use.
reCAPTCHA challenge image
Type the characters you see in the picture above.Type the words you hear.
MOTODEV for Enterprise
Welcome...

Comment on our blog and tell us how we can help you build and distribute enterprise mobile applications on Motorola devices.

Subscribe to our RSS feed Subscribe via RSS

Follow Us

Our Blog & Comment Policy
Opinions expressed here and in any corresponding comments are the personal opinions of the original authors, not of Motorola. The content is provided for informational purposes only and is not meant to be an endorsement or representation by Motorola or any other party.

Remember, when you comment, please stay on topic and avoid spam, profanity, and anything else that violates our user guidelines. All comments require approval by Motorola and can be rejected for any reason.

For customer support issues with your Motorola phone go to the Motorola customer support website.
Blogroll