Achartengineactivity engine to draw bar charts and curves

Source: Internet
Author: User

This article from:


1. Introduction

Achartengine (ACE) is an open source chart Library (for Android) of Google ). It has powerful functions and supports scatter chart, line chart, pie chart, bubble chart, column chart, short bar chart, and meter chart. The Project address is /. For more information about how to use the internal class, download the response documentation (available on the home page ).

2. Development steps

1) create a new folder in the project, such as Lib, used to store ace libraries, copy the achartegine-0.5.0.jar package to Lib. Then add the jar path to the build path of the project.

2) Modify androidmanifest. xml

Add <activity>:

<Activity Android: Name ="Org. achartengine. graphicalactivity"/>

3)Draw a bar chart

Public class main extends listactivity {
Private Static final int series_nr = 2;
/** Called when the activity is first created .*/
Private arraylist <Map <string, string> maps = new arraylist <Map <string, string> ();
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
// Setcontentview (R. layout. Main );
// Add the listitem "scheduling query"
Hashmap <string, string> map = new hashmap <string, string> ();
Map. Put ("name", "Bar Chart ");
Map. Put ("DESC", "display Bar Chart ");
Maps. Add (MAP );
// Construct the listview Adapter
Simpleadapter adapter = new simpleadapter (this, maps,
Android. R. layout. simple_list_item_2, // a layout provided by the SDK library that contains two textviews
New String [] {"name", "DESC"}, // two keys in maps
New int [] {Android. R. Id. text1, Android. R. Id. text2} // ID of two textviews
This. setlistadapter (adapter );

// Listitem listener Method
Protected void onlistitemclick (listview L, view V, int position, long ID ){
Super. onlistitemclick (L, V, position, ID );
Xymultipleseriesrenderer Renderer = getbardemorenderer ();
Intent intent = chartfactory. getbarchartintent (this, getbardemodataset (), Renderer, type. Default );
Startactivity (intent );
Private xymultipleseriesdataset getbardemodataset (){
Xymultipleseriesdataset dataset = new xymultipleseriesdataset ();
Final int Nr = 10;
Random r = new random ();
For (INT I = 0; I <series_nr; I ++ ){
Categoryseries series = new categoryseries ("demo series" + (I + 1 ));
For (int K = 0; k <NR; k ++ ){
Series. Add (100 + R. nextint () % 100 );
Dataset. addseries (Series. toxyseries ());
Return dataset;
Public xymultipleseriesrenderer getbardemorenderer (){
Xymultipleseriesrenderer Renderer = new xymultipleseriesrenderer ();
Simpleseriesrenderer r = new simpleseriesrenderer ();
R. setcolor (color. Blue );
Renderer. addseriesrenderer (R );
R = new simpleseriesrenderer ();
R. setcolor (color. Green );
Renderer. addseriesrenderer (R );
Setchartsettings (Renderer );
Return Renderer;

Private void setchartsettings (xymultipleseriesrenderer Renderer ){
Renderer. setcharttitle ("chart Demo ");
Renderer. setxtitle ("X values ");
Renderer. setytitle ("y values ");
Renderer. setxaxismin (0.5 );
Renderer. setxaxismax (10.5 );
Renderer. setyaxismin (0 );
Renderer. setyaxismax (210 );

Code parsing: In the onlistitemclick method, after you click "display Chart", construct an intent object and send a message to the activity stated in androidmanifest. XML using startactivity (That isGraphicalactivity). The chart is displayed. The key is the intent constructor chartfactory. getbarchartintent.

Chartfactory provides many useful factory methods. If you need to generate a line chart, you can use its getlinechartintent method.

The getbarchartintent method is a bit complicated and requires many parameters to be passed in. One of the objects is xymultipleseriesdataset, which is used to provide the dataset to be represented by the chart. Here we use getbardemodataset to get it. Another object of the xymultipleseriesrenderer type is used to provide some styles for Chart display. Here we use the getbardemorenderer method to obtain it.

The getlinechartintent method is boring and uses random numbers as chart data. Note that the bar chart supports multiple series. Two series of data are generated here.

The getbardemorenderer method constructs a xymultipleseriesrenderer to set the colors of the two series, and then calls the setchartsettings method to set the following axis style.

4)Draw a curve

Import java. util. arraylist;
Import java. util. List;

Import org. achartengine. chartfactory;
Import org. achartengine. Chart. pointstyle;
Import org. achartengine. model. xymultipleseriesdataset;
Import org. achartengine. model. xyseries;
Import org. achartengine. Renderer. xymultipleseriesrenderer;
Import org. achartengine. Renderer. xyseriesrenderer;

Import Android. App. activity;
Import Android. Graphics. color;
Import Android. OS. Bundle;
Import Android. View. view;

Public class chartdemo extends activity {

@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );

String [] titles = new string [] {"first", "second "};

List x = new arraylist ();
List y = new arraylist ();

X. Add (New Double [] {1, 3, 5, 7, 9, 11 });
X. Add (New Double [] {0, 2, 4, 6, 8, 10 });

Y. Add (New Double [] {3, 14, 5, 30, 20, 25 });
Y. Add (New Double [] {18, 9, 21, 15, 10, 6 });

Xymultipleseriesdataset dataset = builddataset (titles, x, y );

Int [] colors = new int [] {color. Blue, color. Green };
Pointstyle [] styles = new pointstyle [] {pointstyle. Circle, pointstyle. Diamond };
Xymultipleseriesrenderer Renderer = buildrenderer (colors, styles, true );

Setchartsettings (Renderer, "line chart Demo", "X", "Y",-1, 12, 0, 35, color. White, color. White );

View chart = chartfactory. getlinechartview (this, dataset, Renderer );

Setcontentview (Chart );

Protected xymultipleseriesdataset builddataset (string [] titles,
List xvalues,
List yvalues)
Xymultipleseriesdataset dataset = new xymultipleseriesdataset ();

Int length = titles. length; // number of lines
For (INT I = 0; I <length; I ++)
Xyseries series = new xyseries (Titles [I]); // It is created based on the name of each line.
Double [] XV = xvalues. Get (I); // obtain the data of line I
Double [] YV = yvalues. Get (I );
Int serieslength = XV. length; // There are several points

For (int K = 0; k <serieslength; k ++) // There are several points in each line
Series. Add (XV [K], YV [k]);

Dataset. addseries (series );

Return dataset;

Protected xymultipleseriesrenderer buildrenderer (INT [] colors, pointstyle [] styles, Boolean fill)
Xymultipleseriesrenderer Renderer = new xymultipleseriesrenderer ();
Int length = colors. length;
For (INT I = 0; I <length; I ++)
Xyseriesrenderer r = new xyseriesrenderer ();
R. setcolor (colors [I]);
R. setpointstyle (styles [I]);
R. setfillpoints (fill );
Renderer. addseriesrenderer (R );
Return Renderer;

Protected void setchartsettings (xymultipleseriesrenderer Renderer, String title,
String xtitle, string ytitle, double xmin,
Double xmax, double ymin, double Ymax,
Int axescolor, int labelscolor)
Renderer. setcharttitle (title );
Renderer. setxtitle (xtitle );
Renderer. setytitle (ytitle );
Renderer. setxaxismin (xmin );
Renderer. setxaxismax (xmax );
Renderer. setyaxismin (ymin );
Renderer. setyaxismax (Ymax );
Renderer. setaxescolor (axescolor );
Renderer. setlabelscolor (labelscolor );


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.