有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在Android中使用MPAndroidChart定制饼图?

我所做的:我正在使用MPAndroidChart,我能够根据自己的需求定制它,并尝试了进一步的功能来删除描述标签,增加字体和自定义图例。我所拥有的是现在

enter image description here

 <com.github.mikephil.charting.charts.PieChart
            安卓:id="@+id/chart"
            安卓:layout_width="200dp"
            安卓:layout_height="200dp"
            安卓:layout_gravity="center_horizontal"
            >

        </com.github.mikephil.charting.charts.PieChart>

公共类PFrag扩展片段{

    float time[] = {55, 95, 30 , 360 - (55+95+30)};
    String activity[] ={"Jan","Feb","March",""};
    PieChart pieChart;
    CircularProgressIndicator circularProgress;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.panorama_frag,container,false);

       pieChart = view.findViewById(R.id.chart);
        setupPieChart();

        //circularProgress = view.findViewById(R.id.circular_progress);
       // circularProgress.setMaxProgress(10000);
      //  circularProgress.setCurrentProgress(5000);

        return view;
    }

    private void setupPieChart(){

        //pupulating list of PieEntires
        List<PieEntry> pieEntires = new ArrayList<>();
        for( int i = 0 ; i<time.length;i++){
            pieEntires.add(new PieEntry(time[i],activity[i]));
        }
        PieDataSet dataSet = new PieDataSet(pieEntires,"");
        dataSet.setColors(ColorTemplate.MATERIAL_COLORS);
        PieData data = new PieData(dataSet);
        //Get the chart
        pieChart.setData(data);
        pieChart.invalidate();
        pieChart.setCenterText("50% \n ");
        pieChart.setDrawEntryLabels(false);
        pieChart.setContentDescription("");
        //pieChart.setDrawMarkers(true);
        //pieChart.setMaxHighlightDistance(34);
        pieChart.setEntryLabelTextSize(12);
        pieChart.setHoleRadius(75);

        //legend attributes
        Legend legend = pieChart.getLegend();
        legend.setForm(Legend.LegendForm.CIRCLE);
        legend.setTextSize(12);
        legend.setFormSize(20);
        legend.setFormToTextSpace(2);


    }

}

我想要的:虽然我尝试过,但似乎找不到编辑以下功能的方法

  • 如何删除左右角的“描述标签”
  • 如何增加图表的文本大小
  • 如何从图例中删除蓝色项目,使其成为默认的剩余值

简单地说,我要找的是下面这样的图表。 enter image description here

为了实现这一点,我在搜索了一些之后使用了MPAndoridChart库,并停留在这里。我使用的是Android Studio 3.6.1。如果您能给我提些建议,我将不胜感激

谢谢!


我能够解决以下两个问题:

  • 如何删除左右角的“描述标签”

    pieChart.getDescription().setEnabled(false);
    
  • 如何增加图表的文本大小>;加上

    data.setValueTextSize(10);
    

共 (1) 个答案

  1. # 1 楼答案

    要删除数据内部的标签,只需使用

    dataSet.setDrawValues(false)