使用MPAndroidChart制作饼图时遇到对齐问题

8

我在我的应用程序中使用饼图,并使用以下库:
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);
}
2个回答

9
您可以使用外部值来清晰地保留数据集名称,以处理小型数据。

例如,您可以实现以下内容:

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

setDrawEntryLabels(false); setExtraOffsets(0.f, 5.f, 0.f, 5.f); 你能说出这是什么吗? - Sunisha Guptan
不理解你的评论。您可以根据需要测试和修改这些值。 - Marc Estrada
是的,应该像这样应用:mChart.setExtraOffsets(0.f, 5.f, 0.f, 5.f); - Sunisha Guptan
setDrawEntryLabels(false); 防止显示数据集的名称,如果要显示,请将其设置为true。 setExtraOffsets(0.f, 5.f, 0.f, 5.f); 正如我刚刚编辑的那样,设置额外的填充以防止线条过长时被裁剪。您可以测试一些值,直到找到您喜欢的值。 - Marc Estrada
我明白了,但是在我完成你建议的截图之后,我需要分享另一张图片。 - Sunisha Guptan
显示剩余2条评论

5

最终我找到了解决方案,感谢@Marc Estrada的回答。但是我还做了一些修改。

image

我使用了以下代码:

  public static void chartDetails1(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.HORIZONTAL);
    l.setWordWrapEnabled(true);
    l.setDrawInside(false);
    mChart.setTouchEnabled(true);
    mChart.setDrawEntryLabels(false);
    mChart.setExtraOffsets(25.f, 0.f, 25.f, 0.f);

    mChart.setUsePercentValues(true);
    mChart.setDragDecelerationFrictionCoef(0.95f);
    mChart.setDrawHoleEnabled(true);
    mChart.setHoleColor(Color.WHITE);
    mChart.setTransparentCircleColor(Color.WHITE);
    mChart.setTransparentCircleAlpha(110);
    mChart.setTransparentCircleRadius(61f);
    mChart.setRotationAngle(0);
    // enable rotation of the chart by touch
    mChart.setHighlightPerTapEnabled(true);
    // mChart.setUnit(" €");
    // mChart.setDrawUnitsInChart(true);
    l.setEnabled(true);
    mChart.highlightValues(null);
    mChart.setUsePercentValues(true);
    /*new*/
    mChart.setHoleRadius(30f);
    mChart.setDrawCenterText(false);
    mChart.setTransparentCircleRadius(35f);

    mChart.getDescription().setEnabled(false);
    mChart.setRotationEnabled(false);

    mChart.setEntryLabelColor(Color.WHITE);
    mChart.setEntryLabelTextSize(9f);

}

public static ArrayList<Integer> colorsList() {
    ArrayList<Integer> colors = new ArrayList<Integer>();

    for (int c : ColorTemplate.MATERIAL_COLORS)
        colors.add(c);

    for (int c : ColorTemplate.JOYFUL_COLORS)
        colors.add(c);

    for (int c : ColorTemplate.COLORFUL_COLORS)
        colors.add(c);

    for (int c : ColorTemplate.LIBERTY_COLORS)
        colors.add(c);

    for (int c : ColorTemplate.PASTEL_COLORS)
        colors.add(c);

    colors.add(ColorTemplate.getHoloBlue());
    return colors;

}
public static PieData pieDatasetSlice(PieDataSet dataSet){
    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 */
    dataSet.setSliceSpace(0f);
    PieData data = new PieData(dataSet);
    data.setValueTextSize(5f);
    data.setValueTextColor(Color.BLACK);
    data.setValueFormatter(new PercentFormatter());
    return data;
}

在添加条目后,请添加以下行:
    PieDataSet dataSet = new PieDataSet(entries1, null);
    ArrayList<Integer> colors = Constants.colorsList();
    dataSet.setColors(colors);
    PieData data = Constants.pieDatasetSlice(dataSet);

如果您想查看每个饼状图块的更多详细信息,请按照以下步骤操作:

  • 实现 OnChartValueSelectedListener 接口

你好,能否请您分享一下您创建条目的代码? - Sharp Edge

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接