This post is deprecated! Visit the always-up-to-date GraphView page.
I would like to introduce to my own graph library for Android.
// GraphView creates a scaled line graph with x and y axis labels.
public class GraphView extends LinearLayout {
What is GraphView
GraphView is a library for Android to programmatically create flexible and nice-looking diagramms. It is easy to understand, to integrate and to customize it.At the moment there are two different types:
- Line Charts
- Bar Charts
GraphView was originally created by arnodenhond, but I did a complete remake and so now I can say it is "my" library ;-)
Tested on Android 1.6, 2.2, 2.3 and 3.0 (honeycomb, tablet).
Features
- Two chart types
Line Chart and Bar Chart. - Custom labels
The labels for the x- and y-axis are generated automatically. But you can set your only labels, Strings are possible. - Background
Optionally draws a light background under the diagramm stroke. - Handle incomplete data
It's possible to give the data in different frequency. - Viewport
You can limit the viewport so that only a part of the data will be displayed. - Scrolling
You can scroll with a finger touch move gesture. - Scaling / Zooming
Since Android 2.3! With two-fingers touch scale gesture (Multi-touch), the viewport can be changed.
How to use
To show you how to integrate the library into an existing project I will demonstrate the GraphView-Demos project.Checkout GraphView library and Demos
First, you have to checkout the graphview library and the demos project from github and import them into your workspace in eclipse.cd workspace/ git clone git://github.com/jjoe64/GraphView.git git clone git://github.com/jjoe64/GraphView-Demos.git
Use library - project property
The project GraphView-Demos must use the GraphView project as Android library. For this, go to the project properties of GraphView-Demos, on "Android" make sure that the GraphView project is used as library.Maybe you have to add the GraphView project to the build path of GraphView-Demos project. But Eclipse will help you...
Understand SimpleGraph.java
When you look into this file you see how easy it is to create a simple graph.// graph with dynamically genereated horizontal and vertical labels
GraphView graphView = new LineGraphView(
this // context
, new GraphViewData[] {
new GraphViewData(1, 2.0d)
, new GraphViewData(2, 1.5d)
, new GraphViewData(2.5, 3.0d) // another frequency
, new GraphViewData(3, 2.5d)
, new GraphViewData(4, 1.0d)
, new GraphViewData(5, 3.0d)
} // data
, "GraphViewDemo" // heading
, null // dynamic labels
, null // dynamic labels
);
LinearLayout layout = (LinearLayout) findViewById(R.id.graph1);
layout.addView(graphView);
There isn't much to say about that. But one important thing: Always give to data array sorted by x-values (ASC)!!For performance, the library doesn't sort the data.
Zoom and Scrolling - Advanced.java
For demonstration the zooming and scrolling feature we need more example data. So in Advanced.java I generate a few data sets with random and sinus algorithm.// draw random curve
int num = 1000;
GraphViewData[] data = new GraphViewData[num];
double v=0;
for (int i=0; i<num; i++) {
v += 0.2;
data[i] = new GraphViewData(i, Math.sin(Math.random()*v));
}
// graph with dynamically genereated horizontal and vertical labels
GraphView graphView = new LineGraphView(
this
, data
, "GraphViewDemo"
, null
, null
);
// set view port, start=2, size=10
graphView.setViewPort(2, 10);
graphView.setScalable(true);
graphView.setDrawBackground(true);
LinearLayout layout = (LinearLayout) findViewById(R.id.graph2);
layout.addView(graphView);
Custom formatter for axis
To format the x and y axis you can use a custom formatter:GraphView graph = new LineGraphView(
mActivity
, values
, currentGraphDescription // title
, null // horlabels
, null // verlabels
) {
@Override
protected String formatLabel(double value, boolean isValueX) {
if (isValueX) {
// convert from unix timestamp to human time format
return dateTimeFormatter.format(new Date((long) value*1000));
} else {
// y-axis, use default formatter
return super.formatLabel(value, isValueX);
}
}
};
That's all about it for now.
Follow on github:


Dear Sir,
ReplyDeleteI downloaded your GraphView & GraphView-Demos, when I try to include GraphView into GraphView-Demos, R.java was disappear and cannot build normally. Could you show me how to fix it?
This comment has been removed by the author.
Deletetry to clean and rebuild the projects.
ReplyDeleteIn eclipse: Project -> Clean ...
error in import android.view.ScaleGestureDetector
ReplyDeleteit works in android 2.1??
tnks
oh well...it works.... how can i name the x-axis and y-axis in GraphViewDemo?.. and how can change the graph form? ( like the images: https://github.com/jjoe64/GraphView/raw/master/GVLine.jpg )
ReplyDeletethx
I did project -> clean and also fix project problem, but no help. I could not confirm if cause by my environment. I use Eclipse - Indigo, and newest ADT.
ReplyDelete@Shermano
ReplyDeleteI added a new section "Custom formatter for axis".
Well done :))
ReplyDeleteWhen can we see the bar charts integrated in this library ???
ReplyDeleteI don't need bar charts, so I won't integrate it. But it's on GitHub and everybody can contribute.
ReplyDeleteJones91, I have integrated bar charts in your code. I am going to commit it on Github :))
ReplyDeleteHi Hameed,
DeleteI've a usecase where each bar should come with a number on top it. Did you taken care of this usecase in Github. If so can you give me a hint how to enable it in the project.
Thanks in advance.
great library. Simple is beautiful ;)
ReplyDeleteDoes this support multiple plots on the same axis?
ReplyDelete@Boozel
ReplyDeletenot at the moment.. maybe in future...
Hi, Jonas,
ReplyDeleteCould you tell me, what type of license for this library?
License for this library: LGPL
ReplyDeletehttp://en.wikipedia.org/wiki/LGPL
Jonas, thanks!
ReplyDeleteCool! Anyone implemented sparklines yet?
ReplyDeleteSuperb library, It saved me months of research and work for my line chart. Thanks Jones
ReplyDeleteHow i can set y-axis with 4 point ?
ReplyDeletethanks you
Not able to zoom. Kindly suggest.
ReplyDeleteThanks in Advance !!
@Rathika
ReplyDeleteZooming only since Android 2.3!
Thanks Jones. Can we test zoom feature in 3.2 simulator/ we can test only in tablet. We are trying with Graph View demo example.
ReplyDeleteKindly suggest.
@Rathika
ReplyDeleteyou have to use the two-finger gesture to scale. So you can't do this in AVD.
I owe you a beer man....awesome...thanks
ReplyDeleteI got : Cannot instantiate the type GraphView
ReplyDeletecan someone help ?
Hi Jones,
ReplyDeleteCan we zoom y-axis similar to x-axis with existing library. Kindly suggest . Thanks !!!
Regards
Rathika
awesome library :]
ReplyDeleteThnx
Hello,
ReplyDeleteHow can i convert this graphics to an image ? Please help me :)
Thanks
Hi,
ReplyDeleteI'm just wondering if you can scale a Graph with custom label.
I want to scale a graph showing dates in the x axis, but it transform to integer.
Thanks
no, then you need to use a labelformatter
DeleteThe app run at main screen is ok (it mean the screen we will choose chart kind). But when i choose any chart kind, "force close" happens. Please check it
ReplyDeleteI have fixed it, thank you
DeleteI want x and y axis to be constant like x starts from 0 and ends at 100 and similarly for y.And i want to plot point on it. So can anyone help me in this.
ReplyDeleteHave you tested this with Android 4.0? I'm having problems with zooming and scrolling; the scrolling works but is a little erratic and the with zooming it only updates if you pinch and then scroll. Also, would it be possible to implement a feature to let you select a point on the graph and show the values at that point?
ReplyDeletereally good stuff ....
ReplyDeletehey can these graphs be made dynamic?
ReplyDeletethis is a good demo for draw a graph in android.
ReplyDeletethank you.
keep updating new thing in android.
Can you please tell me how can i use a Label Formatter to get my
ReplyDeletex-axis in the form of dates in graph view demo
can u please tell me how can i use in mono for android c#
ReplyDeleteThanks very nice library...
ReplyDeleteHello your library is very nice.
ReplyDeleteBut is it possible to set with multiple series for east serie this one vertical layout. like this one.
http://static.nbd-online.nl/pictures/Luxalon%20BKA%20Acoustic+,%20plafondsysteem%20voor%20betonkernactivering%20-%20grafiek_2-5.jpg
Any way I could press SEND button on mms/sms application programatically?
ReplyDeleteThanks for the excellent chart. I want to change the colour of the series and am unable to do it in 3.0 jar. Is 3.1 jar available for download Please provide link
ReplyDeleteor please advise on how to add colour style info to series,
Unable to use GraphViewData datatype ??
ReplyDelete