Android uses AChartEngine pie chart implementation and achartengine pie chart

Source: Internet
Author: User

Android uses AChartEngine pie chart implementation and achartengine pie chart

1. Introduction to AChartEngine

AChartEngine is a drawing tool library designed for Android applications. Can be used to draw a variety of charts, I am using a achartengine-1.1.0.jar. ChartEngine supports chart types: line chart, Area Chart, scatter chart, time chart, column chart, pie chart, bubble chart, ring chart, and high-low alternating chart.

Each of the preceding charts can contain multiple sequences, and the X axis can be displayed horizontally or vertically. It also supports many custom features. In addition, a chart can be constructed as a View or as an Intent, which can be used to start an Activity ).

In fact, AChartEngine is similar to JFreeChart. JFreeChart is an open class library for drawing charts on the JAVA platform. It is fully written in JAVA and is designed for applications, applets, servlets, and JSP. JFreeChart can generate pie charts, bar charts, scatter plots, time series, and Gantt charts, in addition, it can generate PNG and JPEG output and be associated with PDF and EXCEL.

Both AChartEngine and JFreeChart use open chart production class libraries written in Java. The former is applied on android, and the latter can also be mainly applied on java se or java ee. AChartEngine is an open-source project developed by Google.

2. Implement PieChart using AChartEngine

The pie chart is encapsulated here. You only need to prepare the data to implement the corresponding functions.

Package com. example. chartdemo;

Import java. text. NumberFormat;
Import java. util. Map;
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. content. Context;
Import android. graphics. Color;
Import android. util. Log;
Import android. view. View;
Import android. widget. Toast;


Public class PieChart
{
Private static final int [] COLORS = new int [] {Color. RED, Color. GREEN, Color. BLUE, Color. MAGENTA, Color. CYAN,
Color. YELLOW, Color. DKGRAY };
// Set the font size of the legend.
Private int legendTextSize = 30;

// Set the legend height
Private int legendHeight = 50;

// Set the legend color
Private int labelColor = Color. BLACK;

// Set the title size of a pie chart
Private int titleSize = 50;

Private Context context;

// Used to display PieChart
Private GraphicalView pieChartView = null;

// Main descriptor of PieChart
Private DefaultRenderer mRenderer = new DefaultRenderer ();

Private CategorySeries mSeries = new CategorySeries ("");

/**
* Dataset key: name value: Number
*/
Private Map <String, Double> dataMaps;

/**
* Set the title of the pie chart.
*/
Private String pieTitle;

Public PieChart (Map <String, Double> dataMaps, Context context, String pieTitle)
{
This. dataMaps = dataMaps;
This. context = context;
This. pieTitle = pieTitle;

GeneratePieChartView ();
}


Private double getAllSum ()
{
Double sum = 0;
For (Map. Entry <String, Double> entry: dataMaps. entrySet ())
{
Sum + = entry. getValue (). doubleValue ();
}
Return sum;
}

Private void generatePieChartView ()
{

MRenderer. setZoomButtonsVisible (false); // 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 (legendTextSize );//
MRenderer. setLegendHeight (legendHeight );
MRenderer. setLabelsColor (labelColor );
MRenderer. setChartTitle (pieTitle); // you can specify the title of a pie chart.
MRenderer. setChartTitleTextSize (titleSize );
MRenderer. setPanEnabled (false); // whether the chart can be moved
MRenderer. setZoomEnabled (false); // whether the chart can be scaled

Double sum = getAllSum ();

Int color_ I = 0;
For (Map. Entry <String, Double> entry: dataMaps. entrySet ())
{
MSeries. add (entry. getKey (), entry. getValue (). doubleValue ()/sum );
SimpleSeriesRenderer renderer = new SimpleSeriesRenderer ();
If (color_ I <COLORS. length)
{
Renderer. setColor (COLORS [color_ 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. addSeriesRenderer (renderer); // Add the latest descriptor to DefaultRenderer

Log. v ("color", color_ I + "ddd ");
}

If (pieChartView = null)
{
// If it is null, You need to obtain PieChartView from ChartFactory
PieChartView = ChartFactory. getPieChartView (context, mSeries, mRenderer); // construct mChartView

// MLinear. addView (mChartView );
}
Else
{
PieChartView. repaint ();
}
}


Public GraphicalView getPieView ()
{
Return pieChartView;

}

Public void onClick (boolean isEnalbed)
{
MRenderer. setClickEnabled (isEnalbed); // click an event
PieChartView. setOnClickListener (new View. OnClickListener ()
{
@ Override
Public void onClick (View v)
{
SeriesSelection seriesSelection = pieChartView. getCurrentSeriesAndPoint (); // obtain the current category and pointer
If (seriesSelection = null)
{
Toast. makeText (context, "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 ());
}
// MRenderer. getSeriesRendererAt (seriesSelection. getPointIndex (). setHighlighted (true );
PieChartView. repaint ();
String selectedtitle = mSeries. getCategory (seriesSelection. getPointIndex ());
Toast. makeText (
Context,
"You selected" + selectedtitle + "," + "% is"
+ NumberFormat. getPercentInstance (). format (seriesSelection. getValue ()),
Toast. LENGTH_SHORT). show ();
}
}
});
}

Private int getRandomColor ()
{// Generates RBG values respectively.
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 );
}

Public int getLegendTextSize ()
{
Return legendTextSize;
}

Public void setLegendTextSize (int legendTextSize)
{
This. legendTextSize = legendTextSize;
}

Public int getLegendHeight ()
{
Return legendHeight;
}

Public void setLegendHeight (int legendHeight)
{
This. legendHeight = legendHeight;
}

Public int getLabelColor ()
{
Return labelColor;
}

Public void setLabelColor (int labelColor)
{
This. labelColor = labelColor;
}

Public int getTitleSize ()
{
Return titleSize;
}

Public void setTitleSize (int titleSize)
{
This. titleSize = titleSize;
}

Public Map <String, Double> getDataMaps ()
{
Return dataMaps;
}

Public void setDataMaps (Map <String, Double> dataMaps)
{
This. dataMaps = dataMaps;
}

Public String getPieTitle ()
{
Return pieTitle;
}

Public void setPieTitle (String pieTitle)
{
This. pieTitle = pieTitle;
}


}



}

 

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.