Draw charts for Android

Source: Internet
Author: User
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 at: Doc for download. The current version is 0.6. It should be noted that version 0.6 does not support Android 2.0 or lower (including version 2.0 ). That is to say, if your target platform uses SDK versions earlier than 7, such as Android 1.5-2.0. You need to download the old version of ACE 0.5. This article takes ace 0.5 as an example. 1. Create a new Android project in the project to create a folder 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 to add <activity >:< activity Android: Name = "Org. achartengine. graphicalactivity"/> We will use ACE to generate a bar chart, which will be displayed in a special activit. This activity is in the ace library. Graphicalactivity. We need to declare it in androidmanifest. XML, otherwise android will report activity error not found. 3. Create an activity and create a class, for example, main:
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); // create the listview adapter simpleadapter = new simpleadapter (this, maps, android. r. layout. simple_list_item_2, // The Layout New String [] {"name", "DESC"} that contains two textviews In the SDK library "}, // two key new int [] {android. r. id. text1, android. r. id. text2} // two textview IDs); 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 = 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 enderer (); 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 );}}

First, copy and paste the above Code. Okay, run the program and try:

Now let's look at the code. You don't need to talk about the oncreate method. It's just a listactivity. 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 is Graphicalactivity). 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.

Attachment download:
Bar Chart instance

 

Turn: http://llwbrothers.blog.51cto.com/2360705/565476

Related Article

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.