【问题标题】:Android Studio MPAndroidChart pie chart not showingAndroid Studio MPAndroidChart 饼图未显示
【发布时间】:2021-04-29 07:48:23
【问题描述】:

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

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>

输出

当我尝试启动它时,没有显示饼图。

更新 1

尝试将代码放入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);
}

但结果相同

我关注了这个tutorial

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

【问题讨论】:

  • 尝试添加 pieChart.setData(pieData);在您返回视图的最后。
  • 像这样: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);
  • 并将您的代码放在 onViewCreated 而不是 onCreateView 中。
  • @ahmadbajwa 试过了,结果还是一样。你可以查看我的update1
  • 我将我的代码放在答案中,希望对你有用。

标签: android mpandroidchart


【解决方案1】:

试试这个代码,我已经用它在我的应用程序中实现piechart,并在饼图中添加了一个修复height。希望这项工作。

  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();

【讨论】:

  • fix 的高度是多少?
  • 向 xml 中的饼图添加 200 或 150 dp 的高度。
【解决方案2】:

为饼图添加固定高度。看起来 pieChart 没有显示在 ScrollView 中。 stackoverflow中的类似问题

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-26
    • 1970-01-01
    • 2020-06-22
    • 1970-01-01
    • 1970-01-01
    • 2015-07-14
    相关资源
    最近更新 更多