gpt4 book ai didi

Android Studio MPAndroidChart 饼图未显示

转载 作者:行者123 更新时间:2023-12-04 07:43:39 25 4
gpt4 key购买 nike

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

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>
输出
enter image description here
当我尝试启动它时,没有显示饼图。
更新 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在我的应用程序中添加修复 高度到饼图。希望这项工作。

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

关于Android Studio MPAndroidChart 饼图未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67313132/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com