AChartEngine – A Charting Library for Android Applications

As its name suggests, AChartEngine is a charting library that can be used in Android applications.

It supports all the Android SDK versions from 1.6 and up. The 1.6 version offers only pan and button based zoom, while the 2.1 and newer add support for pinch zoom as this became available in the Android SDK 2.x and newer. A while ago, when AChartEngine dropped the support for older Android SDK versions than 2.1, many users asked it back in just a couple of days after the release. At that time, according to the official Android platform distribution available here, there were still about 5% of devices available worldwide that were running versions that are older than 2.x.

Adding charting to an Android application with AChartEngine, is as simple as adding the achartengine-x.y.z.jar to the application classpath and start coding against its APIs. The current stable version is 1.0.0 and the one under development 1.1.0. The jar file is only 110 KB is size, which is quite a small footprint nowadays. However, AChartEngine offers support for many chart types.

A little bit of history

In late 2008, Android developers were already looking for charting / graphing / plotting libraries. At that time there was no such free / open-source solution available.
I started evaluating Android for fun and after developing a couple of Android applications that needed some graphing, I decided I could open-source the charting code under the name of AChartEngine. Version 0.2.0 was launched in March 2009, being the first open-source charting library for Android. At that time, Android SDK was at version 1.1.

Features

There are three main types of charts that are supported by AChartEngine:

  • XY charts – display data on 2 axis (line, cubic line, area, bar, scatter, bubble, range (high-low))
  • “Round” charts – pie, doughnut, dial
  • Combined chart – can display a combination of the XY charts

For a quick tour through some AChartEngine demo application screenshots, please visit the official website and the AChartEngine Google Code page

Overall Class Design

The diagram below shows the way the classes that handle the visual rendering of the charts are organized.

  • The AbstractChart class describes behavior that is shared by all charts, including drawing background, legend, titles,…
  • The XYChart class describes state and behavior that is common to the XY chart types like the rendering of axes, labels,…
  • The RoundChart is similar to XYChart, but for the chart types that have a circular shape.

Design Components

The entire design is not limited to the visual / view part only. There are a few more components contributing to the overall picture.

  • The model – datasets / series of data.
  • The view – described above.
  • Renderers – help in customizing the charts appearance (colors, fonts, axis, labels, formatting,…).
  • ChartFactory – gets an instance of a dataset and an instance of a renderer and returns the desired chart embedded into an Intent (for the case when the chart fills an Activity) or a View (when the chart is a part of an Activity, together with other widgets).
  • Tools – interaction tools for pan and zoom.

Code sample

The code below shows a simple example of how a chart can be initialized and added to an Activity. The package declaration and the imports have been removed for keeping the sample smaller.


public class SampleChart extends Activity {
private GraphicalView mChart;

private XYMultipleSeriesDataset mDataset = new XYMultipleSeriesDataset();

private XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer();

private XYSeries mCurrentSeries;

private XYSeriesRenderer mCurrentRenderer;

private void initChart() {
mCurrentSeries = new XYSeries("Sample Data");
mDataset.addSeries(mCurrentSeries);
mCurrentRenderer = new XYSeriesRenderer();
mRenderer.addSeriesRenderer(mCurrentRenderer);
}

private void addSampleData() {
mCurrentSeries.add(1, 2);
mCurrentSeries.add(2, 3);
mCurrentSeries.add(3, 2);
mCurrentSeries.add(4, 5);
mCurrentSeries.add(5, 4);
}

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

protected void onResume() {
super.onResume();
LinearLayout layout = (LinearLayout) findViewById(R.id.chart);
if (mChart == null) {
initChart();
addSampleData();
mChart = ChartFactory.getCubeLineChartView(this, mDataset, mRenderer, 0.3f);
layout.addView(mChart);
} else {
mChart.repaint();
}
}
}

In order to have the above example work, a simple layout file must be defined and that will need to include a LinearLayout with the android:id=”@+id/chart”.

Applications

Applications of all types are using AChartEngine for chart rendering. The appbrain.com Android market states that 0.52% of their total number of published applications (around 600K) are using AChartEngine, which means that over 3000 of them are using AChartEngine.

A list of the most popular applications using AChartEngine:

  • MotoACTV – fitness tool developed by Motorola and pre-installed on their devices
  • EKG viewers, bioharness applications
  • Path trackers, fitness, biking applications
  • Finance, business applications
  • Others,…

Resources

Contributors welcome

Contributing to an open-source project may look good in your CV. AChartEngine is an open-source project, so it is the result of a community effort. A suggested path to contributing to AChartEngine could be:

  • Checkout the source code from SVN and try to understand it.
  • Take a look at the opened issues and try fixing some of them. When done, provide patches.
  • Ask for commiter rights.
  • Answer questions on StackOverflow or other websites.

Meta: this post is part of the Java Advent Calendar and is licensed under the Creative Commons 3.0 Attribution license. If you like it, please spread the word by sharing, tweeting, FB, G+ and so on! Want to write for the blog? We are looking for contributors to fill all 24 slot and would love to have your contribution! Contact Attila Balazs to contribute!

Functional Java collections

There is a lot of functional hype these days so I would give a short overview on what is out there at least when it comes to collections in Java. Personally I like standard collections API but i some cases can be awkward and add additional verboseness. This should not be a problem in latter version of Java 8+. There we would probably worry about not creating callback hell but hey there is no silver bullet for most stuff why should there be one for programming?

The Guava Way

Guava project is one of Google’s core libraries where there are plenty of different core language aspects and problems covered. There are utilities and extensions for everyday usage like : collections, primitives, caching, common annotations, string processing, I/O, Math, Reflections and many others. We will only take a look at the Collections goodies so lets see some of them :

    // list
ImmutableList<String> of =
ImmutableList.of("a", "b", "c", "d");
// Same one for map
ImmutableMap<String, String> map =
ImmutableMap.of("key1", "value1", "key2", "value2");
//list of ints
List<Integer> theList = Ints.asList(1, 2, 3, 4, 522, 5, 6);

The Guava Collections are compatible with the JDK collections since they mostly extend or implement the standard classes. There are several cool utilities that are part of the API and have similar names with the ones from java.util.Collections. Basically any programmer who knows the JDK collections should be able to transfer to Guava easily. The ones for List is called Lists, one for Set is Sets, for Map is Maps and so on for the rest. For example:

//create new List
List<someLongName> list = Lists.newArrayList();
//create new LinkedHashMap
Map<someKeyType, SomeValueType> map = Maps.newLinkedHashMap();

//initalize Array List on the spot
List<String> someList = Lists.newArrayList("one", "two", "three");

//set inital size for readability as well as performance
List<Type> exactly100 = Lists.newArrayListWithCapacity(100);
List<Type> approx100 = Lists.newArrayListWithExpectedSize(100);

Methods corresponding to a particular interface are grouped in a very intuitive manner. There are also some extremely good ways of building cache with various of features :

 Cache<Integer, Customer> cache = CacheBuilder.newBuilder()
.weakKeys()
.maximumSize(10000)
.expireAfterWrite(10, TimeUnit.MINUTES)
.build(new CacheLoader<Integer, Customer>() {

@Override
public Customer load(Integer key) throws Exception {

return retreveCustomerForKey(key);
}
});

Since Guava is available in most of the maven repositories its very easy to add it to your build

lambdaj

The idea behind the project is to manipulate collections in a functional and statically typed way. This is achieved in a way the avoids repetitions of simple tasks we usually do with collections. Repetition makes programmers to go with copy/pasting and creates makes them create bugs by doing so. Accessing collections without explicit looping provides way of filtering, sorting, extraction, grouping, transforming, invoking a method on each item or sum up the elements or fields of those element in a collections. Additionally to all these features lambdaj is also a DSL in a way since it adds very cool “sugar” features to the syntax making it more readable in pseudo-english. This is done with static methods so in order to use them we include them directly:

import static ch.lambdaj.Lambda.*;

As it comes to checking and matching lambdaj relies deeply on Hamcrestmatchers. So for example to create a check for an odd integers and then filter a list with that check:

Matcher<Integer> odd = new Predicate<Integer>() {
public boolean apply(Integer item) {
return item % 2 == 1;
}
};
List<Integer> oddNumbers = filter(odd, asList(1, 2, 3, 4, 5));

and as expected the list will return the list [1,3,5]. Lambdaj take a step further with it’s DSL, for example :

List<Beneficiary> beneficiaries = with(transactions)
.retain(having(on(Transaction.class)
.getQunatity(), lessThan(100)))
.extract(on(Transaction.class).getBeneficiary())
.sort(on(Beneficiary.class).getName());

Performance costs

Although the best way to make your application fast is to have the cleanest code as possible there comes a time when you must optimize.In order to do that there is some info provided by the creators on the memory usage and time. Lambdaj has a performance wiki page with code examples. There are also some test done by other programmers where they compare lambdaj with JDK8 for example. There are also some measurements on memory usage of Guava. As for performance of Guava most of it’s functionality is standard JDK classes builders and utilities so the overhead is minimal. At the end of the day it’s up to you to decide how much effect each of these libraries will have on your project and if that is positive. I’m for the idea that almost every project must have Guava on it’s classpath.

Related links summary

Meta: this post is part of the Java Advent Calendar and is licensed under the Creative Commons 3.0 Attribution license. If you like it, please spread the word by sharing, tweeting, FB, G+ and so on! Want to write for the blog? We are looking for contributors to fill all 24 slot and would love to have your contribution! Contact Attila Balazs to contribute!