Achartengineactivity engine to draw bar charts and curves

Source: Internet
Author: User

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 located: Http://code.google.com/p/achartengine /. 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 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 );
// Create a 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 );
}
}

CodeResolution:In the onlistitemclick method, after you click "display Chart", construct an intent object and send a message to the activity stated in androidmanifest. XML (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; // Several lines
For ( Int I = 0; I <length; I ++)
{
Xyseries series =New Xyseries (Titles [I]); // Create 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: info-contact@alibabacloud.com 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.