1

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.

Moeez
  • 494
  • 9
  • 55
  • 147
  • try add pieChart.setData(pieData); at the end where you are returning view. – ahmad bajwa Apr 29 '21 at 08:47
  • like this : 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); – ahmad bajwa Apr 29 '21 at 08:49
  • and place your code in onViewCreated not in onCreateView. – ahmad bajwa Apr 29 '21 at 08:50
  • @ahmadbajwa tried but still same result. You can check my `update1` – Moeez Apr 29 '21 at 09:04
  • I'm placing my code in the answer, hope that work for you. – ahmad bajwa Apr 29 '21 at 09:26

3 Answers3

1

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

Shams
  • 388
  • 1
  • 2
  • 13
0

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();
ahmad bajwa
  • 966
  • 2
  • 10
  • 30
0

please use invalidate() just after the setData

pieChart.setData(pieData);
pieChart.invalidate();
smoothumut
  • 3,423
  • 1
  • 25
  • 35