【问题标题】:How to display labels in android using MpAndroidChart -Pie Chart如何使用 MpAndroidChart -Pie Chart 在 android 中显示标签
【发布时间】:2020-04-15 08:59:05
【问题描述】:

下面代码中的标签没有显示。标签要显示在饼图的右侧。

我使用 3 种颜色来显示饼图。一种用于“In Completed”、“Completed”、“Scheduled”

想要哪种颜色表示哪些标签以及什么是百分比。 下面是结果图:

例子:

饼图右侧

In Completed:12.0%
Completed:43.0%
Scheduled:42.0%

java:

if (name.equals("taskstatus")) {
                            status_list = String.valueOf(values);
                          //  Log.d("status_list",status_list);
                            status.add(status_list);

                           Count_schedueled = Collections.frequency(status, "Scheduled");
                           System.out.println("Count of Scheduled is:  "+ Count_schedueled);
                           Count_Incomplete = Collections.frequency(status, "In Complete");
                           System.out.println("Count of In Complete is:  "+ Count_Incomplete);

                           Complted_Count = Collections.frequency(status, "Completed");
                           System.out.println("Count of Completed is:  "+ Complted_Count);

                           String[] valueArray = new String[] { "In Complete", "Completed", "Scheduled" };
                           // Values for pie chart
                           ArrayList<PieEntry> yVals1 = new ArrayList<PieEntry>();
                           yVals1.add(new PieEntry(Count_Incomplete, 0));
                           System.out.println("Count_Incomplete:  "+ Count_Incomplete);
                           yVals1.add(new PieEntry(Count_schedueled, 2));
                           yVals1.add(new PieEntry(Complted_Count, 1));

                           ArrayList<String> xVals = new ArrayList<String>();

                           for (int i = 0; i < valueArray.length + 1; i++)
                               xVals.add(valueArray[i % valueArray.length]);



                           PieDataSet dataSet = new PieDataSet(yVals1, "");
                           dataSet.setSliceSpace(2f);
                           dataSet.setSelectionShift(5f);
                           int endColor3 = ContextCompat.getColor(getContext(), android.R.color.holo_green_dark);
                           int endColor4 = ContextCompat.getColor(getContext(), android.R.color.holo_red_dark);
                           int endColor5 = ContextCompat.getColor(getContext(), android.R.color.holo_orange_dark);


                           dataSet.setColors(endColor3,endColor5,endColor4);

                           //dataSet.setSelectionShift(0f);

                           PieData data = new PieData(dataSet);
                           data.setValueFormatter(new PercentFormatter());
                           data.setValueTextSize(12f);
                           data.setValueTextColor(Color.WHITE);
                           data.setValueTypeface(tfLight);
                           chart2.setData(data);

                           // undo all highlights
                           chart2.highlightValues(null);

                           chart2.invalidate();


                       }

  //chart2
        chart2.setUsePercentValues(true);
        chart2.setDrawSliceText(true);
        chart2.getDescription().setEnabled(false);
        chart2.setExtraOffsets(5, 10, 5, 5);

        chart2.setDragDecelerationFrictionCoef(0.95f);

        chart2.setCenterTextTypeface(tfLight);
        chart2.setCenterText(generateCenterSpannableText());

        chart2.setDrawHoleEnabled(true);
        chart2.setHoleColor(Color.WHITE);

        chart2.setTransparentCircleColor(Color.WHITE);
        chart2.setTransparentCircleAlpha(110);

        chart2.setHoleRadius(58f);
        chart2.setTransparentCircleRadius(61f);

        chart2.setDrawCenterText(true);

        chart2.setRotationAngle(0);
        // enable rotation of the chart by touch
        chart2.setRotationEnabled(true);
        chart2.setHighlightPerTapEnabled(true);

        // chart.setUnit(" €");
        // chart.setDrawUnitsInChart(true);

        // add a selection listener
        chart2.setOnChartValueSelectedListener(this);

        //  seekBarX2.setProgress(3);
        seekBarX2.setProgress(View.GONE);
        //  seekBarY2.setProgress(10);
        seekBarY2.setProgress(View.GONE);

        chart2.animateY(1400, Easing.EaseInOutQuad);
        // chart.spin(2000, 0, 360);

        Legend l = chart2.getLegend();
        l.setVerticalAlignment(Legend.LegendVerticalAlignment.CENTER);
        l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT);
        l.setOrientation(Legend.LegendOrientation.VERTICAL);
        l.setTextSize(18f);
        l.setEnabled(true);

        l.setDrawInside(false);
        l.setXEntrySpace(7f);
        l.setYEntrySpace(0f);
        l.setYOffset(0f);

【问题讨论】:

    标签: java android pie-chart


    【解决方案1】:

    使用 Kotlin。你可以这样尝试:

    1- 添加具有显示值的数据

    val xvalues = ArrayList<PieEntry>()
    xvalues.add(PieEntry(34.0f, "London : 34.0"))
    xvalues.add(PieEntry(98.2f, "Coventry : 98.2"))
    xvalues.add(PieEntry(37.9f, "Manchester : 37.9"))
    val dataSet = PieDataSet(xvalues, "")
    val data = PieData(dataSet)
    pieChart?.data = data
    

    2- 根据需要设置图例

    val l = pieChart.legend
    pieChart.legend.isWordWrapEnabled = true
    pieChart.legend.isEnabled = true
    l.verticalAlignment = Legend.LegendVerticalAlignment.TOP
    l.horizontalAlignment = Legend.LegendHorizontalAlignment.RIGHT // position
    l.formSize = 20F
    l.formToTextSpace = 0f
    l.form = Legend.LegendForm.LINE // form type : line, square, circle ..
    l.textSize = 10f
    l.orientation = Legend.LegendOrientation.VERTICAL // side by side or bottom to bottom
    l.isWordWrapEnabled = true
    

    结果:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-12-09
      • 1970-01-01
      • 2014-11-26
      • 1970-01-01
      • 2017-09-13
      • 2015-06-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多