如何使用MpAndroidChart -Pie Chart在android中显示标签 [英] How to display labels in android using MpAndroidChart -Pie Chart

查看:137
本文介绍了如何使用MpAndroidChart -Pie Chart在android中显示标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码标签中的嗨"未显示.标签希望显示在饼图的右侧.

Hi in the below code labels Are not displaying .Labels want to display on right side of the pie chart .

我正在使用3种颜色显示饼图.一种用于已完成",已完成",计划中"

I am using 3 colors to displaying pie chart .One is for "In Completed","Completed","Scheduled"

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

want to which color indicates to the which labels and what is percentage. below is result chart:

示例:

饼图的右侧

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

推荐答案

使用Kotlin.您可以尝试这样:

With Kotlin. You can try like this:

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

1- Add data with display value

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-根据需要设置图例

2- Set legend however you want

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

结果:

这篇关于如何使用MpAndroidChart -Pie Chart在android中显示标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆