Moeez
Moeez

Reputation: 478

Android Studio MPAndroidChart pie chart not showing

I am using MPAndroidChart to draw a pie chart via fragment. Below is my code

public class SalesFragment extends Fragment {
   PieChart pieChart;
   PieData pieData;
   PieDataSet pieDataSet;
   ArrayList <PieEntry> pieEntries = new ArrayList<>();
   ArrayList PieEntryLabels;
   View view;

   @Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
   {
      mContext = getActivity();
      view = inflater.inflate(R.layout.sales_layout, container, false);
       ButterKnife.bind(this, view);
      getActivity().setTitle(getString(R.string.title_install_stats));
       pieChart = (PieChart)view.findViewById(R.id.pieChart);
    getEntries();
    pieDataSet = new PieDataSet(pieEntries, "");
    pieData = new PieData(pieDataSet);
    pieChart.setData(pieData);
    pieDataSet.setColors(ColorTemplate.JOYFUL_COLORS);
    pieDataSet.setSliceSpace(2f);
    pieDataSet.setValueTextColor(Color.WHITE);
    pieDataSet.setValueTextSize(10f);
    pieDataSet.setSliceSpace(5f);

    return view;
   }

}

private void getEntries() {
    pieEntries = new ArrayList<>();
    pieEntries.add(new PieEntry(2f, 0));
    pieEntries.add(new PieEntry(4f, 1));
    pieEntries.add(new PieEntry(6f, 2));
    pieEntries.add(new PieEntry(8f, 3));
    pieEntries.add(new PieEntry(7f, 4));
    pieEntries.add(new PieEntry(3f, 5));
}

XML

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/white"
        android:orientation="vertical"
        android:padding="15dp">
        <com.github.mikephil.charting.charts.PieChart
            android:id = "@+id/pieChart"
            android:layout_width = "fill_parent"
            android:layout_height = "fill_parent" />

    </LinearLayout>
</ScrollView>

Output

enter image description here

When I try to launch it, a pie chart is not showing.

Update 1

Tried to put the code into onViewCreated

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {


    super.onViewCreated(view, savedInstanceState);
    mContext = getActivity();
    getActivity().setTitle(getString(R.string.title_install_stats));
    pieChart = (PieChart)view.findViewById(R.id.pieChart);
    getEntries();
    pieDataSet = new PieDataSet(pieEntries, "");
    pieDataSet.setColors(ColorTemplate.JOYFUL_COLORS);
    pieDataSet.setSliceSpace(2f);
    pieDataSet.setValueTextColor(Color.WHITE);
    pieDataSet.setValueTextSize(10f);
    pieDataSet.setSliceSpace(5f);
    pieData = new PieData(pieDataSet);
    pieChart.setData(pieData);
}

But same result

I have followed this tutorial

I don't know what is the main problem, but I am stuck to it. Any help would be highly appreciated.

Upvotes: 1

Views: 1378

Answers (4)

Isaac Sichangi
Isaac Sichangi

Reputation: 75

Set the a fixed height for the pie chart it will show

Upvotes: 0

smoothumut
smoothumut

Reputation: 3491

please use invalidate() just after the setData

pieChart.setData(pieData);
pieChart.invalidate();

Upvotes: 0

Shams
Shams

Reputation: 398

Add fixed height to the piechart. Looks like pieChart not shown in ScrollView. Similar question in stackoverflow

Upvotes: 1

ahmad bajwa
ahmad bajwa

Reputation: 1068

Try this code, I have used this to implement piechart in my application and add a fix height to the piechart. Hope this work.

  private PieChart chart;
    
           chart.setOnChartValueSelectedListener(this);
            chart.setUsePercentValues(true);
            chart.getDescription().setEnabled(false);
            chart.getLegend().setEnabled(false);
            chart.setExtraOffsets(5, 10, 5, 5);
            chart.setDragDecelerationFrictionCoef(0.95f);
            chart.setDrawHoleEnabled(true);
            chart.setHoleColor(Color.WHITE);
            chart.setTransparentCircleColor(Color.WHITE);
            chart.setTransparentCircleAlpha(110);
            chart.setHoleRadius(58f);
            chart.setTransparentCircleRadius(61f);
            chart.setDrawCenterText(true);
            chart.setRotationAngle(0);
            chart.setRotationEnabled(true);
            chart.setHighlightPerTapEnabled(true);
            chart.animateY(1400, Easing.EaseInOutQuad);
            chart.setEntryLabelColor(Color.WHITE);
            chart.setEntryLabelTextSize(12f);
    
     PieDataSet dataSet = new PieDataSet(pieEntries, "");
            dataSet.setDrawIcons(false);
            dataSet.setSliceSpace(3f);
            dataSet.setIconsOffset(new MPPointF(0, 40));
            dataSet.setSelectionShift(5f);
            ArrayList<Integer> colors = new ArrayList<>();
            for (int c : ColorTemplate.JOYFUL_COLORS)
                colors.add(c);
            for (int c : ColorTemplate.LIBERTY_COLORS)
                colors.add(c);
            for (int c : ColorTemplate.COLORFUL_COLORS)
                colors.add(c);
            for (int c : ColorTemplate.VORDIPLOM_COLORS)
                colors.add(c);
            for (int c : ColorTemplate.PASTEL_COLORS)
                colors.add(c);
            colors.add(ColorTemplate.getHoloBlue());
            for (int i = 0; i < pie_modelFeedArrayList.size(); i++) {
                valdelmc valdelmc = pie_modelFeedArrayList.get(i);
                valdelmc.setColor(colors.get(i));
                pie_adapter.notifyItemChanged(i);
            }
            dataSet.setColors(colors);
            dataSet.setSelectionShift(0f);
            PieData data = new PieData(dataSet);
            data.setValueFormatter(new PercentFormatter());
            data.setValueTextSize(11f);
            data.setValueTextColor(Color.BLACK);
            chart.setData(data);
            chart.highlightValues(null);
            chart.invalidate();

Upvotes: 0

Related Questions