Thursday, 1 October 2015

Pie Chart Implementation In Android

Hello Friends,

Today I share a Pie Chart Implementation In Android Apps.

All of you know about what is charts and why we use it. So I am directly move on implementation.

Please see the sample code.

Sample Code:

File: MainActivity.java

package com.sneha.piechart;

import java.text.DecimalFormat;
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.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;

public class MainActivity extends Activity 
{
private GraphicalView mChart;
private String[] code;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        // Ploting the chart
        openChart();        
    }
    
    private void openChart()
    {
    // Pie Chart Slice Names
    code = new String[] 
    {
    "Donut", "Eclair", "Froyo", "GingerBread", "Honeycomb", "Ice-Creame Sandwich", "Jelly                 Bean", "KitKat","Lollipop"
    };    
   
    // Pie Chart Slice Values
    double[] distribution = { 5.0, 12.0, 18.0, 24.0, 36.0, 45.0, 54.0, 70.0, 90.0} ;
   
    // Color of each Pie Chart Slices
    int[] colors = { Color.BLUE, Color.MAGENTA, Color.GREEN, Color.YELLOW,                               Color.WHITE, Color.DKGRAY, Color.RED, Color.LTGRAY, Color.CYAN};
   
    // Instantiating CategorySeries to plot Pie Chart    
    CategorySeries distributionSeries = new CategorySeries(" ");
   
    for(int i=0 ;i < distribution.length;i++)
    {
    // Adding a slice with its values and name to the Pie Chart
    distributionSeries.add(code[i], distribution[i]);
    }   
   
    // Instantiating a renderer for the Pie Chart
    DefaultRenderer defaultRenderer  = new DefaultRenderer();
   
    for(int i = 0 ;i<distribution.length;i++)
    {
    // Instantiating a render for the slice
    SimpleSeriesRenderer seriesRenderer = new SimpleSeriesRenderer();    
    seriesRenderer.setColor(colors[i]);
    seriesRenderer.setDisplayChartValues(true);
   
    // Adding the renderer of a slice to the renderer of the pie chart
    defaultRenderer.addSeriesRenderer(seriesRenderer);
    }
   
    defaultRenderer.setZoomButtonsVisible(true);        
   
    // Getting a reference to view group linear layout chart_container
    LinearLayout chartContainer = (LinearLayout) findViewById(R.id.chart_container);
   
    // Getting PieChartView to add to the custom layout
    mChart = ChartFactory.getPieChartView(getBaseContext(), distributionSeries,                                     defaultRenderer);
   
    defaultRenderer.setClickEnabled(true);
        defaultRenderer.setSelectableBuffer(10);
        
        mChart.setOnClickListener(new View.OnClickListener()
        {
                @Override
                public void onClick(View v)
        {
        SeriesSelection seriesSelection = mChart.getCurrentSeriesAndPoint();
       
        if (seriesSelection != null)
        {
        // Getting the name of the clicked slice
        int seriesIndex = seriesSelection.getPointIndex();
        String selectedSeries="";
        selectedSeries = code[seriesIndex];        
                    
        // Getting the value of the clicked slice
        double value = seriesSelection.getXValue();
        DecimalFormat dFormat = new DecimalFormat("#.#");
         
        // Displaying the message
        Toast.makeText(getBaseContext(), selectedSeries + " : "  + Double.valueOf                                        (dFormat.format(value)) + " % " , Toast.LENGTH_SHORT).show();
        }
        }
        });
        
        // Adding the pie chart to the custom layout
    chartContainer.addView(mChart);
    }
}

Output:


























Download Full Source Code From Here: PieChart

Happy Coding...!!!

No comments:

Post a Comment