Android Studio MPAndroidChart饼图未显示

Android Studio MPAndroidChart饼图未显示,android,mpandroidchart,Android,Mpandroidchart,我正在使用片段绘制饼图。下面是我的代码 public class SalesFragment extends Fragment { PieChart pieChart; PieData pieData; PieDataSet pieDataSet; ArrayList <PieEntry> pieEntries = new ArrayList<>(); ArrayList PieEntryLabels; View view; @

我正在使用片段绘制饼图。下面是我的代码

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));
}
但同样的结果

我遵循了这一点


我不知道主要问题是什么,但我坚持到底。任何帮助都将不胜感激。

请尝试此代码,我已使用此代码在我的应用程序中实现了
piechart
,并向piechart添加了一个固定的高度。希望这能奏效

  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();
private图形;
chart.setOnChartValueSelectedListener(此);
chart.setUsePercentValues(真);
chart.getDescription().setEnabled(false);
chart.getLegend().setEnabled(false);
图表.设置外部偏移(5,10,5,5);
图表.设定阻力减阻摩擦系数(0.95f);
图表。setDrawHoleEnabled(真);
图表。setHoleColor(颜色。白色);
图表.设置透明圆环颜色(颜色.白色);
图表:setTransparentCircleAlpha(110);
图.塞托莱拉迪乌斯(58f);
图.setTransparentCircleRadius(61f);
chart.setDrawCenterText(true);
图表。设置旋转角度(0);
图表。setRotationEnabled(真);
图表.setHighlightPerTapEnabled(真);
图表.动画(1400,Easing.EaseInOutQuad);
图表。setEntryLabelColor(颜色。白色);
图表.setEntryLabelTextSize(12f);
PieDataSet数据集=新的PieDataSet(pieEntries,“”);
dataSet.setDrawIcons(false);
dataSet.setLiceSpace(3f);
setIconsOffset(新的MPPointF(0,40));
dataSet.setSelectionShift(5f);
ArrayList colors=新的ArrayList();
for(int c:ColorTemplate.journy_COLORS)
添加(c);
for(int c:ColorTemplate.LIBERTY_COLORS)
添加(c);
用于(int c:ColorTemplate.color\u颜色)
添加(c);
for(int c:ColorTemplate.VORDIPLOM_COLORS)
添加(c);
用于(int c:ColorTemplate.PASTEL_COLORS)
添加(c);
添加(ColorTemplate.getHoloBlue());
对于(int i=0;i
将固定高度添加到图表中。看起来pieChart未显示在ScrollView中。在

中尝试添加pieChart.setData(pieData)中的类似问题;在返回view.like的末尾:pieDataSet=newpiedataset(pieeentries,“”);pieDataSet.setColors(ColorTemplate.joiny_COLORS);pieDataSet.setLiceSpace(2f);pieDataSet.setValueTextColor(Color.WHITE);pieDataSet.setValueTextSize(10f);pieDataSet.setLiceSpace(5f);pieData=新的pieData(pieDataSet);pieChart.setData(pieData);并将代码放在onViewCreated而不是onCreateView中。@ahmadbajwa尝试过,但结果仍然相同。您可以查看我的
update1
我将我的代码放在答案中,希望对您有用。
fix
高度是多少?将200或150 dp高度添加到xml格式的图表中。
@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);
}
  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();