Android using the Achartengine pie chart implementation

Source: Internet
Author: User
Tags java se

1, Achartengine Introduction

Achartengine is a library of drawing tools designed for Android apps. Can be used to draw a variety of charts, I am using Achartengine-1.1.0.jar. Chartengine supported chart types: line, area, scatter, time, histogram, pie, bubble, doughnut, high-low, alternating.

Each of these charts can contain multiple sequences that can be displayed horizontally or vertically, as well as a number of custom features. In addition, the chart can be built as a view or built as a intent so that it can be used to start an activity.

Actually Achartengine and Jfreechart are similar. Jfreechart is an open chart-drawing class library on the Java platform. It is written entirely in the Java language and is designed for use in applications, applets, Servlets, and JSP. Jfreechart can generate a pie chart (pie charts), a histogram (bar charts), a scatter plot (scatter plots), a timing chart (time series), a Gantt chart (Gantt charts), and more. It also produces output in PNG and JPEG formats, and can be associated with PDFs and Excel.

Achartengine and Jfreechart are all open-chart production libraries written in Java, which are used on Android, which can also be used primarily on Java SE or Java EE. Achartengine is an open source project developed by Google.

2, Achartengine realize Piechart

This is a pie chart package, only need to prepare the data to achieve the corresponding function

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 Legend Font Size
private int legendtextsize = 30;

Set legend Height
private int legendheight = 50;

Set the color of the legend
private int labelcolor = Color.Black;

Set the pie chart title size
private int titlesize = 50;

Private context context;

Used to display Piechart
Private Graphicalview Piechartview = null;

Piechart's main profiler
Private Defaultrenderer Mrenderer = new Defaultrenderer ();

Private Categoryseries mseries = new Categoryseries ("");

/**
* Data Set 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);//Display Zoom Out function button
Mrenderer.setstartangle (180);//set to horizontal start
Mrenderer.setdisplayvalues (TRUE);//Display data
Mrenderer.setfitlegend (TRUE);//sets whether the legend is displayed
Mrenderer.setlegendtextsize (legendtextsize);//
Mrenderer.setlegendheight (Legendheight);
Mrenderer.setlabelscolor (Labelcolor);
Mrenderer.setcharttitle (pietitle);//Set up a pie chart title
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++]);//Set the color of the renderer

}
Else
{
Renderer.setcolor (Getrandomcolor ());//Set the color of the renderer
}
Renderer.setchartvaluesformat (Numberformat.getpercentinstance ());//Set percentage

Mrenderer.addseriesrenderer (renderer);//Add the latest renderer to the Defaultrenderer

LOG.V ("Color", color_i+ "ddd");
}

if (Piechartview = = null)
{
Empty required to get Piechartview from Chartfactory
Piechartview = Chartfactory.getpiechartview (Context, mseries, mrenderer);//Build Mchartview

Mlinear.addview (Mchartview);
}
Else
{
Piechartview.repaint ();
}
}


Public Graphicalview Getpieview ()
{
return piechartview;

}

public void OnClick (Boolean isenalbed)
{
mrenderer.setclickenabled (isenalbed);//Allow Click events
Piechartview.setonclicklistener (New View.onclicklistener ()
{
@Override
public void OnClick (View v)
{
Seriesselection seriesselection = Piechartview.getcurrentseriesandpoint ();//Gets 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 have selected" + Selectedtitle + "," + "as a percentage of"
+ numberformat.getpercentinstance (). Format (Seriesselection.getvalue ()),
Toast.length_short). Show ();
}
}
});
}

private int Getrandomcolor ()
{//Generate RBG values separately
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;
}


}



}

Android using the Achartengine pie chart implementation

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.