android - 使用 MPAndroidChart 的饼图对齐问题

标签 android mpandroidchart

在我的应用程序中使用饼图。使用以下库
api 'com.github.PhilJay:MPAndroidChart:v3.0.3'

但是我遇到了对齐问题。附上截图,请看一下:

graph_1 graph_2

代码:

public static void chartDetails(PieChart mChart, Typeface tf) {
    mChart.animateXY(1400, 1400);
    mChart.getDescription().setEnabled(false);
    mChart.setCenterTextTypeface(tf);
    mChart.setCenterText("");
    mChart.setCenterTextSize(10f);
    mChart.setCenterTextTypeface(tf);
    // radius of the center hole in percent of maximum radius
    mChart.setHoleRadius(45f);
    mChart.setTransparentCircleRadius(50f);
    Legend l = mChart.getLegend();
    l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
    l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT);
    l.setOrientation(Legend.LegendOrientation.VERTICAL);
    l.setDrawInside(false);
    mChart.setTouchEnabled(true);
}

最佳答案

您可以使用外部值,以便为小块数据保持清晰的数据集名称。

例如,您可以实现类似的目标:

Outside values

这是代码。此代码应该在覆盖 PieChart 时使用。如果您不覆盖它,您应该调整此代码以直接与您的 PieChart 一起使用,例如 mChart.setData(); 而不是 setData();

PieDataSet dataSet = new PieDataSet(entries, null);
dataSet.setSliceSpace(3f);
dataSet.setIconsOffset(new MPPointF(0, 40));
dataSet.setSelectionShift(5f);

// Outside values
dataSet.setXValuePosition(PieDataSet.ValuePosition.OUTSIDE_SLICE);
dataSet.setYValuePosition(PieDataSet.ValuePosition.OUTSIDE_SLICE);
dataSet.setValueLinePart1OffsetPercentage(100f); /** When valuePosition is OutsideSlice, indicates offset as percentage out of the slice size */
dataSet.setValueLinePart1Length(0.6f); /** When valuePosition is OutsideSlice, indicates length of first half of the line */
dataSet.setValueLinePart2Length(0.6f); /** When valuePosition is OutsideSlice, indicates length of second half of the line */
setExtraOffsets(0.f, 5.f, 0.f, 5.f); // Ofsets of the view chart to prevent outside values being cropped /** Sets extra offsets (around the chart view) to be appended to the auto-calculated offsets.*/

PieData data = new PieData(dataSet);
data.setValueTextSize(10f);
data.setValueTextColor(Color.BLACK);
setData(data);

关于android - 使用 MPAndroidChart 的饼图对齐问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50907258/

相关文章:

java - 在 Android 中保存和恢复首选项列表

java - 仅使用一个 ImageView 的 View Flipper

android - setVisibleXRangeMaximum() 和 setVisibleYRangeMaximum() 在 MPAndroidChart 中不起作用

android - 无法解决:com.github.PhilJay.MPAndroidChart:v2.2.4

android - 如何更改 MPAndroidchart 中 Y 轴标签的间距?

android - 滚动监听器 - 第一个 distanceY 对于向下滚动总是负数?

android - 想要在 android 应用程序运行时使某些 View 不可见

android - 使用 MpAndroidChart-CombinedChart,如何将图表分成上下两部分?

android - MPAndroid Chart如何制作流畅的折线图

android - 如果显示软键盘弹出窗口崩溃