Question Detail

I want to show data as pie chart form How to create Pie chart in android?

6 years ago Views 2131 Visit Post Reply

I'd like to draw a pie chart in my android application. Would you please suggest me a way to do that in a simple way?I have no idea how to create it So I would appreciate if you tell me a good and high-performance chart library or Pre Define Android widget.Image result for how to create pie chart in android


Thread Reply

Anonymous

- 6 years ago

Add MpAndroid library

We need to add MpAndroid library in our project, so open build.gradle(Module:app) file and add following code:

1

2

3

4

5

6

7

8

9

repositories {

    maven { url "https://jitpack.io" }

}

 

dependencies {

   .....

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

   .....

}

Create Layout

We will use com.github.mikephil.charting.charts xml tag in layout to create Pie Chart

1

2

3

4

<com.github.mikephil.charting.charts.PieChart

        android:id="@+id/piechart"

        android:layout_width="match_parent"

        android:layout_height="match_parent"/>

 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

package in.studytutorial.androidpiechart;

 

import android.graphics.Color;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.util.Log;

import android.widget.SeekBar;

 

import com.github.mikephil.charting.charts.PieChart;

import com.github.mikephil.charting.data.Entry;

import com.github.mikephil.charting.data.PieData;

import com.github.mikephil.charting.data.PieDataSet;

import com.github.mikephil.charting.formatter.PercentFormatter;

import com.github.mikephil.charting.highlight.Highlight;

import com.github.mikephil.charting.listener.OnChartValueSelectedListener;

import com.github.mikephil.charting.utils.ColorTemplate;

 

import java.util.ArrayList;

 

public class MainActivity extends AppCompatActivity implements OnChartValueSelectedListener {

 

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

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

        pieChart.setUsePercentValues(true);

 

        // IMPORTANT: In a PieChart, no values (Entry) should have the same

        // xIndex (even if from different DataSets), since no values can be

        // drawn above each other.

        ArrayList<Entry> yvalues = new ArrayList<Entry>();

        yvalues.add(new Entry(8f, 0));

        yvalues.add(new Entry(15f, 1));

        yvalues.add(new Entry(12f, 2));

        yvalues.add(new Entry(25f, 3));

        yvalues.add(new Entry(23f, 4));

        yvalues.add(new Entry(17f, 5));

 

        PieDataSet dataSet = new PieDataSet(yvalues, "Election Results");

 

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

 

        xVals.add("January");

        xVals.add("February");

        xVals.add("March");

        xVals.add("April");

        xVals.add("May");

        xVals.add("June");

 

        PieData data = new PieData(xVals, dataSet);

        // In Percentage term

        data.setValueFormatter(new PercentFormatter());

        // Default value

        //data.setValueFormatter(new DefaultValueFormatter(0));

        pieChart.setData(data);

        pieChart.setDescription("This is Pie Chart");

 

        pieChart.setDrawHoleEnabled(true);

        pieChart.setTransparentCircleRadius(25f);

        pieChart.setHoleRadius(25f);

 

        dataSet.setColors(ColorTemplate.VORDIPLOM_COLORS);

        data.setValueTextSize(13f);

        data.setValueTextColor(Color.DKGRAY);

        pieChart.setOnChartValueSelectedListener(this);

 

        pieChart.animateXY(1400, 1400);

 

    }

 

    @Override

    public void onValueSelected(Entry e, int dataSetIndex, Highlight h) {

 

        if (e == null)

            return;

        Log.i("VAL SELECTED",

                "Value: " + e.getVal() + ", xIndex: " + e.getXIndex()

                        + ", DataSet index: " + dataSetIndex);

    }

 

    @Override

    public void onNothingSelected() {

        Log.i("PieChart", "nothing selected");

    }

 

}