Android Draw Line Chart one

Source: Internet
Author: User

Recently need to implement the use of Android to draw a line chart, so Baidu a bit, and found that indeed a lot, is also very messy, now finishing two methods (the second method in the "Android Painting Line Chart II"), only for your reference, study together.

The first is obtained using the Chartfactory.getlinechartintent () method, which first requires a achartengine-1.0.0.jar (in the project) and then builds a simple Android project

The packages you need to import in your project are:

Importjava.util.ArrayList;ImportJava.util.HashMap;ImportJava.util.Map;ImportJava.util.Random;Importorg.achartengine.ChartFactory;ImportOrg.achartengine.model.XYMultipleSeriesDataset;Importorg.achartengine.model.XYSeries;ImportOrg.achartengine.renderer.XYMultipleSeriesRenderer;ImportOrg.achartengine.renderer.XYSeriesRenderer;Importandroid.app.ListActivity;Importandroid.content.Intent;ImportAndroid.graphics.Color;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.widget.ListView;ImportAndroid.widget.SimpleAdapter;

This is then written in your activity:

 Public classMypaintactivityextendslistactivity {//Reference AddressHttp://www.eoeandroid.com/forum.php?mod=viewthread&tid=98457    Private Static Final intSERIES_NR = 2; PrivateArraylist<map<string, string>> maps =NewArraylist<map<string, string>>(); @Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);//Setcontentview (R.layout.main); //join ListItem "dispatch query"hashmap<string, string> map =NewHashmap<string, string>(); Map.put ("Name", "line chart"); Map.put ("Desc", "Show Line chart");        Maps.add (map); //build the adapter for the ListViewSimpleadapter adapter =NewSimpleadapter ( This, Maps, Android. R.layout.simple_list_item_2,NewString[] {"Name",                        "Desc"},New int[] {Android. R.id.text1, Android.        R.ID.TEXT2}); //The SDK Library provides a layout new string[]{"name", "Desc", which contains two TextView,//the two key new int[]{android in maps. R.id.text1,android. R.ID.TEXT2}//ID of two textview);         This. Setlistadapter (adapter); } @Overrideprotected voidOnlistitemclick (ListView L, View V,intPositionLongID) {//TODO auto-generated Method Stub        Super. Onlistitemclick (l, V, position, id); Try{Intent Intent= Chartfactory.getlinechartintent (mypaintactivity. This, GetDataSet (), Getrenderer ());                    StartActivity (Intent); } Catch(Exception e) {e.printstacktrace (); }    }    PrivateXymultipleseriesdataset GetDataSet () {xymultipleseriesdataset DataSet=NewXymultipleseriesdataset (); Final intNR = 10; //Each series consists of 10 random numbersRandom r =NewRandom ();  for(inti = 0; i < SERIES_NR; i++) {            //Create a new series (lines)Xyseries Series =NewXyseries ("Series" + (i + 1));  for(intk = 0; K < nr; k++) {                intx = r.nextint ()% 10; //a random integer between x:0-10                inty = + r.nextint ()% 50; //a random integer between y:50-100series.add (x, y); //add a randomly distributed point to the series            }            //Put a dot-added polyline into a datasetdataset.addseries (series); }        returnDataSet; }     Publicxymultipleseriesrenderer Getrenderer () {//Create a new xymultipleseriesXymultipleseriesrenderer renderer =NewXymultipleseriesrenderer (); Renderer.setaxistitletextsize (16); //Set axis title text sizeRenderer.setcharttitletextsize (20); //Set Chart title text sizeRenderer.setlabelstextsize (15); //Set axis label text sizeRenderer.setlegendtextsize (15); //set legend Text sizeRenderer.setmargins (New int[] {20, 30, 15, 0 }); //set 4 edges to remain white//set the color of a series to blueXyseriesrenderer r =NewXyseriesrenderer ();        R.setcolor (Color.Blue); //Add a series to the XymultiplerenderRenderer.addseriesrenderer (R); //set the color of another series to redR =NewXyseriesrenderer ();        R.setcolor (Color.green); //add another series to the XymultiplerenderRenderer.addseriesrenderer (R); returnrenderer; }}

After this painting, the direct operation will find that it is not possible, the program directly stop the operation, This is because the program needs to org.achartengine.GraphicalActivity this activity support, so add a sentence in your resource file Androidmanifest.xml:

<activity android:name= "Org.achartengine.GraphicalActivity" >

Then run the project and click on "Show Line chart" to see the effect!

Note: The project source code in Mypaint.zip, Achartengine-1.0.0.jar also in the source project

Android Draw Line Chart one

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.