Android MP 饼图如何从饼图中删除文本和值

标签 android pie-chart mpandroidchart

Actual image

如何使用 MPAndroidChart 实现此目的?

使用版本:com.github.PhilJay:MPAndroidChart:v3.1.0-alpha 添加图例和饼图边距的代码:

private void setPieChartData() {
        //https://stackoverflow.com/a/50551488/1427037
        pieChart.setUsePercentValues(true);
        pieChart.getDescription().setEnabled(false);
        pieChart.setExtraOffsets(5,5,10,5);
        pieChart.setDragDecelerationFrictionCoef(0.9f);
        pieChart.setDrawCenterText(false);
        pieChart.setDrawHoleEnabled(false);
        //pieChart.animateY(1000, Easing.EasingOption.EaseInOutCubic);
        ArrayList<PieEntry> yValues = new ArrayList<>();
        yValues.add(new PieEntry(34f,"Ilala"));
        yValues.add(new PieEntry(56f,"Temeke"));
        yValues.add(new PieEntry(66f,"Kinondoni"));
        yValues.add(new PieEntry(45f,"Kigamboni"));
        yValues.add(new PieEntry(45f,"Kigamboni2"));
        pieChart.setDrawEntryLabels(false);

        PieDataSet dataSet = new PieDataSet(yValues, "Desease Per Regions");
        dataSet.setSliceSpace(0.1f);
        dataSet.setDrawValues(false);
        dataSet.setSelectionShift(5f);
        dataSet.setColors(ColorTemplate.VORDIPLOM_COLORS);

        PieData pieData = new PieData((dataSet));
        pieData.setValueTextSize(10f);
        pieData.setValueTextColor(Color.BLACK);
        pieChart.setData(pieData);
        pieChart.setDrawSliceText(false);
        setLegend();
    }

    private void setLegend() {
        Legend l = pieChart.getLegend();
        l.setFormSize(10f); // set the size of the legend forms/shapes
        l.setForm(Legend.LegendForm.SQUARE); // set what type of form/shape should be used
        l.setYOffset(5f);
        //l.setPosition(Legend.LegendPosition.RIGHT_OF_CHART);
        l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP); // set vertical alignment for legend
        l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT); // set horizontal alignment for legend
        l.setOrientation(Legend.LegendOrientation.VERTICAL);
        l.setTextSize(12f);
        l.setTextColor(Color.BLACK);
        //l.setXEntrySpace(5f); // set the space between the legend entries on the x-axis
        l.setYEntrySpace(1f); // set the space between the legend entries on the y-axis
        // set custom labels and colors
        List<LegendEntry> entries = new ArrayList<>();
        ArrayList<PieEntry> yValues = new ArrayList<>();
        yValues.add(new PieEntry(34f,"Ilala"));
        yValues.add(new PieEntry(56f,"Temeke"));
        yValues.add(new PieEntry(66f,"Kinondoni"));
        yValues.add(new PieEntry(45f,"Kigamboni"));
        yValues.add(new PieEntry(45f,"Kigamboni2"));
        for (int i = 0; i < 3; i++) {
            LegendEntry entry = new LegendEntry();
            entry.formColor = ColorTemplate.VORDIPLOM_COLORS[i];
            entry.label = yValues.get(i).getLabel() ;
            entries.add(entry);
        }
        l.setCustom(entries);
    }
   [![code result][2]][2]

如何将图例的位置设置为与实际图像相同我在设置其布局时遇到问题。请更正代码并分享任何链接以寻求帮助 请分享详细信息以解决此问题

code result

如何解决此 UI 问题.. 图例标签应位于右侧,与 Required mock 相同 enter image description here

最佳答案

要删除条目值和标签,请使用方法:

myPieChart.setDrawSliceText(false); // To remove slice text
myPieChart.setDrawMarkers(false); // To remove markers when click
myPieChart.setDrawEntryLabels(false); // To remove labels from piece of pie
myPieChart.getDescription().setEnabled(false); // To remove description of pie

要在图表旁边显示图例,请尝试以下方法:

Legend l = myPieChart.getLegend(); // get legend of pie
l.setVerticalAlignment(Legend.LegendVerticalAlignment.CENTER); // set vertical alignment for legend
l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT); // set horizontal alignment for legend
l.setOrientation(Legend.LegendOrientation.VERTICAL); // set orientation for legend
l.setDrawInside(false); // set if legend should be drawn inside or not

here 查看更多信息& 来自 MPAndroidChart 的主要文档页面.

关于Android MP 饼图如何从饼图中删除文本和值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53741738/

相关文章:

android - 更改 Android PopUpWindow 的内容 View

java - 无法完成饼图的圆圈

android - 选择值时显示标签 [MPAndroidChart]

java - 有人知道如何使用 MPAndroidChart 吗?

android - Google Play 服务 Unity 插件不显示排行榜

java - 在 Google map 上使用 JSON 表示经纬度

java - 饼图未显示预期输出

css - CSS 饼图中的切片偏移

android - 如何在同一图 TableView 中创建散点图和折线图?

android - 找不到 'com.android.support:design:22.2.0'