如何在Android中使用MPAndroidChart自定义饼图?

huangapple go评论87阅读模式
英文:

How to customize pie chart using MPAndroidChart in Android?

问题

我所做的: 我正在使用 MPAndroidChart,并且我已经能够根据我的要求进行自定义,并尝试进一步的功能来移除描述标签,增加字体大小并自定义图例。我现在拥有的是:

<com.github.mikephil.charting.charts.PieChart
    android:id="@+id/chart"
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:layout_gravity="center_horizontal">
</com.github.mikephil.charting.charts.PieChart>
public class PFrag extends Fragment {
    // ...(其他代码部分)

    private void setupPieChart(){
        // ...(其他代码部分)

        // 设置图表文本
        pieChart.setCenterText("50%\n");
        pieChart.setDrawEntryLabels(false);
        pieChart.setContentDescription("");

        // 设置中心孔半径和图例属性
        pieChart.setHoleRadius(75);
        Legend legend = pieChart.getLegend();
        legend.setForm(Legend.LegendForm.CIRCLE);
        legend.setTextSize(12);
        legend.setFormSize(20);
        legend.setFormToTextSpace(2);
    }
}

我正在寻找的: 尽管我尝试过,但似乎无法找到编辑以下功能的方法。

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

简单来说,我想要的是一个如下的图表。

如何在Android中使用MPAndroidChart自定义饼图?

为了实现这一点,我在一些搜索之后使用了 MPAndroidChart 库,但目前卡在这里。我正在使用 Android Studio 3.6.1。非常感谢对此的任何建议。

谢谢!

我已经成功解决以下两个问题:

  • 如何移除右下角的“描述标签”:
pieChart.getDescription().setEnabled(false);
  • 如何增加图表的文本大小?添加:
data.setValueTextSize(10);
英文:

What I have done: I'm using the MPAndroidChart and I was able to customize it to my requirement and tried further functionalities to remove Description Label, and to increase the font and customize the legend. What I have is now ;

如何在Android中使用MPAndroidChart自定义饼图?

 &lt;com.github.mikephil.charting.charts.PieChart
            android:id=&quot;@+id/chart&quot;
            android:layout_width=&quot;200dp&quot;
            android:layout_height=&quot;200dp&quot;
            android:layout_gravity=&quot;center_horizontal&quot;
            &gt;

        &lt;/com.github.mikephil.charting.charts.PieChart&gt;

public class PFrag extends Fragment {

    float time[] = {55, 95, 30 , 360 - (55+95+30)};
    String activity[] ={&quot;Jan&quot;,&quot;Feb&quot;,&quot;March&quot;,&quot;&quot;};
    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&lt;PieEntry&gt; pieEntires = new ArrayList&lt;&gt;();
        for( int i = 0 ; i&lt;time.length;i++){
            pieEntires.add(new PieEntry(time[i],activity[i]));
        }
        PieDataSet dataSet = new PieDataSet(pieEntires,&quot;&quot;);
        dataSet.setColors(ColorTemplate.MATERIAL_COLORS);
        PieData data = new PieData(dataSet);
        //Get the chart
        pieChart.setData(data);
        pieChart.invalidate();
        pieChart.setCenterText(&quot;50% \n &quot;);
        pieChart.setDrawEntryLabels(false);
        pieChart.setContentDescription(&quot;&quot;);
        //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);


    }

}

What I'm looking for: Though I tried it seems unable to find a way to edit the below functionalities.

  • How to remove the "Description Label" in the right-left corner?
  • How to increase the text size of the chart?
  • How to remove the blue item from the legend so that it will be the default remaining value?

Simply what I'm looking for is a graph like below.
如何在Android中使用MPAndroidChart自定义饼图?

To achieve that I used MPAndoridChart library after some searching and stuck here. I'm using Android Studio 3.6.1. I would really appreciate any suggestion on this.

Thank you!


I was able to solve below two queries:

  • How to remove the "Description Label" in the right-left corner

      pieChart.getDescription().setEnabled(false);
    
  • How to increase the text size of the chart? > add

      data.setValueTextSize(10);
    

答案1

得分: 2

要移除数据内部的标签,只需使用以下代码:

dataSet.setDrawValues(false)
英文:

To remove label inside data just use

dataSet.setDrawValues(false)

huangapple
  • 本文由 发表于 2020年4月7日 02:03:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/61066069.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定