如何在使用 MPAndroidChart 库绘制的饼图中居中显示图例?

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

How to center the legend in pie chart drawn using MPAndroidChart library?

问题

以下是翻译好的部分:

我所做的: 我使用 MPAndroidChart 库创建了一个饼图,结果图例位于左上角。我尝试改变饼图的宽度,但是当项目增加时,图例会换行到第二行。

我期望的: 我希望每次更新时图例都位于中心。即使图例换行到第二行,我也希望它位于中心。我会对任何关于此的建议表示感激。

感谢!

如何在使用 MPAndroidChart 库绘制的饼图中居中显示图例?

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

活动代码:

private void setupPieChart(){
    // 填充 PieEntry 列表
    List<PieEntry> pieEntries = new ArrayList<>();
    for (int i = 0; i < time.length; i++) {
        pieEntries.add(new PieEntry(time[i], activity[i]));
    }
    PieDataSet dataSet = new PieDataSet(pieEntries, "");
    dataSet.setColors(ColorTemplate.MATERIAL_COLORS);
    PieData data = new PieData(dataSet);
    // 饼片文本值
    data.setValueTextSize(10);
    // 获取图表
    pieChart.setData(data);
    pieChart.invalidate();
    pieChart.setCenterText("50%");
    pieChart.setDrawEntryLabels(false);
    pieChart.setContentDescription("");
    //pieChart.setDrawMarkers(true);
    //pieChart.setMaxHighlightDistance(34);
    pieChart.setEntryLabelTextSize(12);
    pieChart.setHoleRadius(75);
    // 移除左下角描述
    pieChart.getDescription().setEnabled(false);

    // 图例属性
    Legend legend = pieChart.getLegend();
    legend.setForm(Legend.LegendForm.CIRCLE);
    legend.setTextSize(12);
    legend.setFormSize(20);
    legend.setFormToTextSpace(2);
    // 换行图例文本
    legend.setWordWrapEnabled(true);
    Log.d("legend ", legend.getEntries().toString());
}

<details>
<summary>英文:</summary>

**What I have done:** I have created a pie chart using [MPAndroidChart][1] library and ended up having the legend in the left corner. I tried changing the pie chart width but then when the items increase legend tends to wrap to a second line. 

**What I expect:** I want the legend to be in the center every time when it updates. Even legend wrap to the second line, I want it to be in the center. I would appreciate any suggestions on this. 

Thank you!

[![enter image description here][2]][2]

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

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

Activity Code :

 
    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);
        //pie slices text value
        data.setValueTextSize(10);
        //Get the chart
        pieChart.setData(data);
        pieChart.invalidate();
        pieChart.setCenterText(&quot;50%&quot;);
        pieChart.setDrawEntryLabels(false);
        pieChart.setContentDescription(&quot;&quot;);
        //pieChart.setDrawMarkers(true);
        //pieChart.setMaxHighlightDistance(34);
        pieChart.setEntryLabelTextSize(12);
        pieChart.setHoleRadius(75);
        //to remove bottom left description
        pieChart.getDescription().setEnabled(false);

        //legend attributes
        Legend legend = pieChart.getLegend();
        legend.setForm(Legend.LegendForm.CIRCLE);
        legend.setTextSize(12);
        legend.setFormSize(20);
        legend.setFormToTextSpace(2);
        //to wrap legend text
        legend.setWordWrapEnabled(true);
        Log.d(&quot;legend &quot; ,legend.getEntries().toString());
    }

  [1]: https://github.com/PhilJay/MPAndroidChart
  [2]: https://i.stack.imgur.com/YFISM.png

</details>


# 答案1
**得分**: 3

我能够通过在上面问题中提到的 `setupPieChart()` 中添加以下行来将图例居中显示。

    legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER);

<details>
<summary>英文:</summary>

I was able to center the legend by adding the below line in `setupPieChart()` mentioned in the question above.

    legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER);

</details>



huangapple
  • 本文由 发表于 2020年4月8日 23:19:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/61104173.html
匿名

发表评论

匿名网友

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

确定