PieChart of AChartEngine application (pie chart)

Source: Internet
Author: User

PieChart of AChartEngine application (pie chart)

The steps for creating a pie chart are as follows: Introduce the AChartEngine dependent jar package in the project and add the following in Manifest: <activityandroid: name = "org. achartengine. graphicalActivity "/>

1. Set DefaultRenderer

DefaultRenderer mRenderer = new DefaultRenderer (); // The main descriptor of PieChart

MRenderer. setZoomButtonsVisible (true); // The zoom in/out button is displayed.

MRenderer. setStartAngle (180); // set it to start horizontally.

MRenderer. setDisplayValues (true); // display data

MRenderer. setFitLegend (true); // you can specify whether the legend is displayed.

MRenderer. setLegendTextSize (10); // set the font size of the legend.

MRenderer. setLegendHeight (10); // sets the legend height.

MRenderer. setChartTitle ("pie chart example"); // you can specify the title of a pie chart.

MRenderer. setChartTitleTextSize (14); // you can specify the title size of a pie chart.

 

2. Construct the data source CategorySeries

For (int I = 0; I <data. length; I ++)

VALUE + = data [I];

For (int I = 0; I <data. length; I ++ ){

MSeries. add ("example" + (I + 1), data [I]/VALUE); // set the type name and corresponding value. The preceding VALUE is a key-value pair.

SimpleSeriesRenderer renderer = new SimpleSeriesRenderer ();

If (I <COLORS. length ){

Renderer. setColor (COLORS [I]); // you can specify the color of the descriptor.

} Else {

Renderer. setColor (getRandomColor (); // you can specify the color of the descriptor.

}

Renderer. setChartValuesFormat (NumberFormat. getPercentInstance (); // set the percentage

MRenderer. setChartTitleTextSize (14); // you can specify the title size of a pie chart.

MRenderer. addSeriesRenderer (renderer); // Add the latest descriptor to DefaultRenderer

}

 

3. Use ChartFactory to obtain the pie chart

MChartView = ChartFactory. getPieChartView (getApplicationContext (),

MSeries, mRenderer); // build mChartView

MRenderer. setClickEnabled (true); // click an event

MChartView. setOnClickListener (new View. OnClickListener () {// content

}

Run:


Code

Package com. qiuzhping. achart; import java. text. numberFormat; import java. util. random; import org. achartengine. chartFactory; import org. achartengine. graphicalView; import org. achartengine. model. categorySeries; import org. achartengine. model. seriesSelection; import org. achartengine. renderer. defaultRenderer; import org. achartengine. renderer. simpleSeriesRenderer; import android. app. activity; import android. graphics. color; import android. OS. bundle; import android. view. view; import android. view. viewGroup. layoutParams; import android. widget. linearLayout; import android. widget. toast;/*** @ Project name: AChart * @ Class Name: PieChartBuilder * @ Author: Qiuzhping * @ time: 11:20:48 * @ role: Build a pie chart, and generate interaction with the user. Click the corresponding area to correctly display the information */public class PieChartBuilder extends Activity {private static int [] COLORS = new int [] {Color. RED, Color. GREEN, Color. BLUE, Color. MAGENTA, Color. CYAN, Color. YELLOW, Color. DKGRAY}; double data [] = new double [] {20, 30, 40, 50, 60, 70, 80, 90,100 }; private CategorySeries mSeries = new CategorySeries (""); // The DataSet of PieChart is actually some key-value pairs, which are similar to the use of Map private DefaultRenderer mRenderer = new DefaultRenderer (); // The main descriptor of PieChart is private GraphicalView mChartView; // you need to add // <activity // android: name = "org. achartengine. graphicalActivity "//> private LinearLayout mLinear; private static double VALUE = 0; // total @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. xy_chart); mLinear = (LinearLayout) findViewById (R. id. chart); mLinear. setBackgroundColor (Color. BLACK); mRenderer. setZoomButtonsVisible (true); // display the zoom-in and zoom-out button mRenderer. setStartAngle (180); // set it to start horizontally. setDisplayValues (true); // displays the data mRenderer. setFitLegend (true); // sets whether to display the legend mRenderer. setLegendTextSize (10); // set the font size of the legend mRenderer. setLegendHeight (10); // set the legend height mRenderer. setChartTitle ("pie chart example"); // set the pie chart title for (int I = 0; I <data. length; I ++) VALUE + = data [I]; for (int I = 0; I <data. length; I ++) {mSeries. add ("example" + (I + 1), data [I]/VALUE); // set the type name and the corresponding value. The preceding VALUE is (key, value) key-Value Pair SimpleSeriesRenderer renderer = new SimpleSeriesRenderer (); if (I <COLORS. length) {renderer. setColor (COLORS [I]); // sets the color of the descriptor} else {renderer. setColor (getRandomColor (); // sets the color of the descriptor} renderer. setChartValuesFormat (NumberFormat. getPercentInstance (); // sets the percentage of mRenderer. setChartTitleTextSize (14); // set the title size of the pie chart mRenderer. addSeriesRenderer (renderer); // Add the latest descriptor to DefaultRenderer.} if (mChartView = null) {// null, You need to obtain PieChartViewmChartView = ChartFactory from ChartFactory. getPieChartView (getApplicationContext (), mSeries, mRenderer); // construct mChartViewmRenderer. setClickEnabled (true); // Click Event mChartView. setOnClickListener (new View. onClickListener () {// content @ Overridepublic void onClick (View v) {SeriesSelection seriesSelection = mChartView. getCurrentSeriesAndPoint (); // obtain the current category and pointer if (seriesSelection = null) {Toast. makeText (getApplicationContext (), "You have not selected data", Toast. LENGTH_SHORT ). show () ;}else {for (int I = 0; I <mSeries. getItemCount (); I ++) {mRenderer. getSeriesRendererAt (I ). setHighlighted (I = seriesSelection. getPointIndex ();} mChartView. repaint (); Toast. makeText (getApplicationContext (), "The number you selected" + (seriesSelection. getPointIndex () + 1) + "item" + "%" + NumberFormat. getPercentInstance (). format (seriesSelection. getValue (), Toast. LENGTH_SHORT ). show () ;}}); mLinear. addView (mChartView, new LayoutParams (LayoutParams. FILL_PARENT, LayoutParams. FILL_PARENT);} else {mChartView. repaint () ;}} private int getRandomColor () {// generate the RBG value Random random = new Random (); int R = random. nextInt (255); int G = random. nextInt (255); int B = random. nextInt (255); return Color. rgb (R, G, B );}}


Corresponding project source code: http://download.csdn.net/detail/qiu_11/6853469

To be continued .......




CombinedXYChart(Statistical 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.