Close

Pie Chart – Android

Pie Chart – Android

Step 1: Add dependency to build.gradle (Module app)

compile 'com.github.PhilJay:MPAndroidChart:v2.2.2'

Step 2:Add following lines in Your Xml file
<com.github.mikephil.charting.charts.PieChart
    android:id="@+id/chart1"
    android:layout_width="300dp"
    android:layout_height="300dp"
    android:layout_margin="5dp" />

Step 3: Write the Java code in Your Activity
package com.gayatrisoft.sip_calculator;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

public class Pie_chart extends AppCompatActivity {


    PieChart piechart;
    
    private int[] number_of_members = {12, 10, 14,10};
    private String[] department_name = {"Android Developers", "Java Developers", "PHP developers","Management Team Memebers"};

    
    public static  final int[] piechart_colors = {
            Color.rgb(84,124,101), Color.rgb(64,64,64), Color.rgb(153,19,0),
            
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pie_chart);

        piechart = (PieChart) findViewById(R.id.chart1);


        piechart.setDescription("");

        piechart.setRotationEnabled(true);

        piechart.setOnChartValueSelectedListener(new OnChartValueSelectedListener() {

            @Override
            public void onValueSelected(Entry e, int dataSetIndex, Highlight h) {
                if (e == null)
                    return;

                Toast.makeText(Pie_chart.this,
                        department_name[e.getXIndex()] + " is " + e.getVal() + "", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onNothingSelected() {

            }
        });


        setDataForPieChart();


    }


    public void setDataForPieChart() {
        ArrayList<Entry> yVals1 = new ArrayList<Entry>();

        for (int i = 0; i < number_of_members.length; i++)
            yVals1.add(new Entry(number_of_members[i], i));

        ArrayList<String> xVals = new ArrayList<String>();

        for (int i = 0; i < department_name.length; i++)
            xVals.add(department_name[i]);

        PieDataSet dataSet = new PieDataSet(yVals1, "");
        dataSet.setSliceSpace(3);
        dataSet.setSelectionShift(5);

        ArrayList<Integer> colors = new ArrayList<Integer>();

        for (int c : piechart_colors)
            colors.add(c);


        dataSet.setColors(colors);

        PieData data = new PieData(xVals, dataSet);


        data.setValueFormatter(new MyValueFormatter());
        data.setValueTextSize(12f);
        data.setValueTextColor(Color.WHITE);

        piechart.setData(data);


        piechart.highlightValues(null);


        piechart.invalidate();


        piechart.animateXY(1500, 1500);



        Legend l = piechart.getLegend();
        l.setPosition(Legend.LegendPosition.LEFT_OF_CHART_CENTER);
        l.setXEntrySpace(7);
        l.setYEntrySpace(5);
    }


    public static class MyValueFormatter implements ValueFormatter {

        private DecimalFormat mFormat;

        public MyValueFormatter() {
            mFormat = new DecimalFormat("###,###,##0"); 
        }

        @Override
        public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) {

            return mFormat.format(value) + ""; 
        }
    }
    

}

 

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *