Android ---- bar chart and pie chart

Source: Internet
Author: User

Android ---- bar chart and pie chart

Use GraphicalView to draw a bar chart and a pie chart

 

1. Download The achartengine Library ,:

Www.bkjia.com

After the download is complete, paste the jar file to the libs folder.

Achartengine is a drawing tool library designed for Android.

Ii. How to use it in android Projects

First define a GraphicalView

GraphicalView graphicalView;

Then, obtain from chartfactory

 

GraphicalView = ChartFactory. getBarChartView (getBaseContext (), dataset, renderer, type); // bar chart graphicalView = ChartFactory. histogram (getBaseContext (), dataset, renderer); // pie chart

 

Dataset indicates the data source, renderer indicates the rendering parameter, and type indicates the type.

Set dataset method-pie chart

Double [] values = {412.0, 542.0, 486.0, 900.1}; CategorySeries dataset = buildCategoryDataset (test pie chart, values );

Protected CategorySeries buildCategoryDataset (String title, double [] values) {CategorySeries series = new CategorySeries (title); series. add (difference, values [0]); series. add (not up to standard, values [1]); series. add (compliant, values [2]); series. add (excellent, values [3]); return series ;}

How to Set dataset-bar chart can have multiple groups of data

 

1 String[] titles={test};2         List
 
   values=new ArrayList
  
   ();3         values.add(new double[]{5120.0,21251.0,25610.0});4         XYMultipleSeriesDataset dataset=buildBarDataset(titles, values);
  
 

protected XYMultipleSeriesDataset buildBarDataset(String[] titles, List
 
   values) {            XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();            int length = titles.length;            for (int i = 0; i < length; i++) {                CategorySeries series = new CategorySeries(titles[i]);                double[] v = values.get(i);                int seriesLength = v.length;                for (int k = 0; k < seriesLength; k++) {                    series.add(v[k]);                }                dataset.addSeries(series.toXYSeries());            }            return dataset;        }
 

How to Set renderer-pie chart

 

 

1 int[] colors={Color.BLUE,Color.GREEN,Color.MAGENTA,Color.RED};2 DefaultRenderer renderer=buildCategoryRenderer(colors);

 

How to Set renderer-pie chart

Protected DefaultRenderer buildCategoryRenderer (int [] colors) {DefaultRenderer renderer = new DefaultRenderer (); renderer. setLegendTextSize (20); // set the text size of the table note in the lower left corner. // renderer. setZoomButtonsVisible (true); // you can specify the zoom-in or zoom-out button for renderer. setZoomEnabled (false); // The setting does not allow zoom in or out. renderer. setChartTitleTextSize (30); // set the text size of the chart title renderer. setChartTitle (statistical result); // set the title of the chart to show renderer at the top of the center by default. setLabelsTextSize (20); // font size of the text marked on the pie chart // renderer. setLabelsColor (Color. WHITE); // the color of the text marked on the pie chart renderer. setPanEnabled (false); // you can specify whether renderer can be translated. setDisplayValues (true); // whether to display the renderer value. setClickEnabled (true); // sets whether the renderer can be clicked. setMargins (new int [] {20, 30, 15, 0}); // margins-an array containing the margin size values, in this order: top, left, bottom, right for (int color: colors) {SimpleSeriesRenderer r = new SimpleSeriesRenderer (); r. setColor (color); renderer. addSeriesRenderer (r);} return renderer ;}

How to Set renderer-bar chart

 

 

Int [] colors = {Color. BLUE}; XYMultipleSeriesRenderer renderer = buildBarRenderer (colors); Type type = Type. DEFAULT; // renderer. setZoomEnabled (false); // how is it invalid? ---- use the following method to renderer. setZoomEnabled (false, false); // successful control -- setsetchartsettings (renderer, I am the title of the bar chart, statistical result, 0, 6, 0, 30000, Color. GRAY, Color. LTGRAY); renderer. getSeriesRendererAt (0 ). setDisplayChartValues (true); // renderer. getSeriesRendererAt (1 ). setDisplayChartValues (true); renderer. setXLabels (0); // set the number of subscripts on the X axis to renderer. setYLabels (10); // set the number of subscripts on the Y axis to renderer. setXLabelsAlign (Align. RIGHT); renderer. setYLabelsAlign (Align. LEFT); // the y-axis number indicates the coordinates or the right-side renderer. setPanEnabled (false, false); // you can specify whether to enable renderer translation. addXTextLabel (2.0, 220kv power line); // display text at specified coordinates // renderer. clearXTextLabels (); // clear labels // renderer. setZoomRate (1.1f); // sets the renderer. setBarSpacing (1f); // set the bar spacing // renderer. setLabelsTextSize (30); // you can specify the number size on the coordinate axis. setXLabelsAngle (300366f); // set the text rotation angle to rotate renderer clockwise. setXLabelsPadding (10); // set the distance between the text and the axis to renderer. setFitLegend (true); // adjust the proper position

protected XYMultipleSeriesRenderer buildBarRenderer(int[] colors) {            XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();            renderer.setAxisTitleTextSize(16);            renderer.setChartTitleTextSize(20);            renderer.setLabelsTextSize(15);            renderer.setLegendTextSize(15);            int length = colors.length;            for (int i = 0; i < length; i++) {                SimpleSeriesRenderer r = new SimpleSeriesRenderer();                r.setColor(colors[i]);                renderer.addSeriesRenderer(r);            }            return renderer;        }

Finally, put graphicalView in 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

 


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.