Use of the "Android" Achartengine Histogram and pie chart

Source: Internet
Author: User

The comments in the code explain how the various settings in the icon are used.

A. Download of the Achartengine library:

Http://code.google.com/p/achartengine/downloads/list

After the download is complete, paste the jar file into the Libs folder

Two. How to use the Android project

First define a Graphicalview

Graphicalview Graphicalview;

Then, get from Chartfactory

Graphicalview=chartfactory.getbarchartview (Getbasecontext (), DataSet, renderer, type);//Histogram
Graphicalview=chartfactory.getpiechartview (Getbasecontext (), DataSet, renderer);//Pie chart

Where a dataset represents a data source, renderer represents a render parameter, type represents a

How to set up a dataset--pie chart

Double[] values={412.0,542.0,486.0,900.1}; Categoryseries dataset=buildcategorydataset ("Test pie chart", values);
1 protected categoryseries Buildcategorydataset (String title, double[] values) {2         categoryseries series = new Categor Yseries (title), 3         series.add ("Poor", Values[0]), 4         series.add ("substandard", values[1]), 5         Series.add ("Compliance", values[ 2]); 6         Series.add ("excellent", values[3]); 7         return series;8       }

How to set a dataset--histogram----histogram can have multiple sets of data

1 string[] titles={"test"};2         list<double[]> values=new arraylist<double[]> (); 3         Values.add (new double[]{5120.0,21251.0,25610.0}); 4         Xymultipleseriesdataset Dataset=buildbardataset (titles, values);
1  protected Xymultipleseriesdataset buildbardataset (string[] titles, list<double[]> values) {2             Xymultipleseriesdataset DataSet = new Xymultipleseriesdataset (); 3             int length = titles.length; 4 for             (int i = 0; i < length; i++) {5                 categoryseries series = new Categoryse Ries (Titles[i]); 6                 double[] v = values.get (i), 7                 int serieslength = v.length, 8 for                 (int k = 0; k < serieslength; k++) {9
   series.add (V[k]);                 }11                 dataset.addseries (Series.toxyseries ());             }13             return dataset;14         }

How to set renderer--Pie chart

1 int[] colors={color.blue,color.green,color.magenta,color.red};2 defaultrenderer renderer=buildCategoryRenderer ( colors);
 1 protected Defaultrenderer buildcategoryrenderer (int[] colors) {2 Defaultrenderer renderer = new Defaultrendere R (); 3 4 renderer.setlegendtextsize (20);//Set the text size of the bottom-left note to 5//renderer.setzoombuttonsvisible (TRUE);//Set display magnification   Small button 6 renderer.setzoomenabled (FALSE);//setting does not allow zooming in and out. 7 renderer.setcharttitletextsize (30);//Set the text size of the chart title 8 Renderer.setcharttitle ("Statistical results");//Set the title of the chart to the top of the center by default Display 9 renderer.setlabelstextsize (20);//The font size of the marked text on the pie chart is//renderer.setlabelscolor (color.white);//The label text on the pie chart           Color one by one renderer.setpanenabled (false);//Set Whether you can pan//renderer.setdisplayvalues (true);//Whether the value 13 is displayed Renderer.setclickenabled (TRUE);//settings can be clicked on Renderer.setmargins (new int[] {15,0}); -an array containing the margin size values, in this order:top, left, bottom, right16 for (int color:colors) { Simpleseriesrenderer r = new Simpleseriesrenderer ();. SetColor (color); Renderer.addseriesrenderer (R);}21 return renderer;22} 

Method of setting renderer----bar chart

 1 int[] Colors={color.blue}; 2 xymultipleseriesrenderer renderer=buildbarrenderer (colors); 3 Type Type=type.default; 4 5//renderer.setzoomenabled (FALSE);//How did it fail----Use the following method 6 renderer.setzoomenabled (false, false); /successful Control--hehe 7 setchartsettings (renderer, "I am the title of the Histogram", "", "Statistical results", 0, 6, 0, 30000, Color.gray, Color.ltgray); 8 Renderer.getseriesrendererat (0). Setdisplaychartvalues (True);         9//renderer.getseriesrendererat (1). Setdisplaychartvalues (True), Renderer.setxlabels (0);//Setting the number of subscripts on the x-axis 11 Renderer.setylabels (10); Sets the number of subscripts on the y-axis, renderer.setxlabelsalign (align.right), Renderer.setylabelsalign (Align.left), and the//y axis numbers indicating the coordinates is the right renderer.setpanenabled (false, false);//sets whether to allow panning of the Renderer.addxtextlabel (2.0, "220kv power Line");//display at specified coordinates Text//Renderer.clearxtextlabels ();//Clear Labels17//renderer.setzoomrate (1.1f);//Set the ratio of renderer.         Setbarspacing (1f);//Set column spacing 19Renderer.setlabelstextsize (30);//Set the size of the number on the axis renderer.setxlabelsangle (300.0f);//Set the text rotation angle to the text clockwise rotation of RE Nderer.setxlabelspadding (10);//Set the distance between the text and the axis renderer.setfitlegend (true);//Adjust the appropriate position
1   protected Xymultipleseriesrenderer buildbarrenderer (int[] colors) {2             xymultipleseriesrenderer renderer = New Xymultipleseriesrenderer (); 3             renderer.setaxistitletextsize (4 renderer.setcharttitletextsize);             5             Renderer.setlabelstextsize (15); 6             renderer.setlegendtextsize; 7             int length = colors.length; 8 for             (int i = 0; i < length; i++) {9
   
    simpleseriesrenderer r = new Simpleseriesrenderer ();                 R.setcolor (Colors[i]);                 Renderer.addseriesrenderer (R);             }13             return renderer;14         }
   

Finally, put the Graphicalview into the specified layout.

1 layout= (LinearLayout) Findviewbyid (r.id.linearlayout), 2 layout.removeallviews (); 3 Layout.setbackgroundcolor ( Color.Black); 4 Layout.addview (Graphicalview, New Layoutparams (layoutparams.fill_parent,layoutparams.fill_parent)) ;

Effect

Use of the "Android" Achartengine Histogram and pie chart

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.