android中使用饼形图

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wode_dream/article/details/50038527

        在很多android开发中都要用于图表,下面特别讲一下饼形图的用法。效果如下:


第一步:从网上下载MPChartlib.jar

第二步:在布局文件abc.xml中引入饼形图:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white">
    <com.github.mikephil.charting.charts.PieChart
        android:id="@+id/controll_pie"
        android:layout_width="200dip"
        android:layout_height="200dip"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginRight="10dip"
        android:layout_marginTop="10dip" />
</RelativeLayout>


第三步:在程序中使用:

PieChart  pieChart = (PieChart) itemView.findViewById(R.id.controll_pie);
private void createPie()
        {
            pieChart.setCenterText("当月己完成事务\n30");

            // configure pie chart
            pieChart.setUsePercentValues(true);
            pieChart.setDescription("");

            // enable hole and configure
            pieChart.setDrawHoleEnabled(true);
            pieChart.setHoleColorTransparent(true);
            pieChart.setHoleRadius(60);
            pieChart.setTransparentCircleRadius(10);

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

            // set a chart value selected listener
            pieChart.setOnChartValueSelectedListener(new OnChartValueSelectedListener() {

                @Override
                public void onValueSelected(Entry e, int dataSetIndex, Highlight h) {
                    // display msg when value selected
                    if (e == null)
                        return;

                    Toast.makeText(activity,
                            xData[e.getXIndex()] + " = " + e.getVal() + "%", Toast.LENGTH_SHORT).show();
                }

                @Override
                public void onNothingSelected() {

                }
            });
            addData();
        }
        private void addData()
        {
            ArrayList<Entry> yVals1 = new ArrayList<Entry>();

            for (int i = 0; i < yData.length; i++)
                yVals1.add(new Entry(yData[i], i));

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

            for (int i = 0; i < xData.length; i++)
                xVals.add("");

            // create pie data set
            PieDataSet dataSet = new PieDataSet(yVals1, "");
            dataSet.setSliceSpace(3);
            dataSet.setSelectionShift(4);

            // add many colors
            ArrayList<Integer> colors = new ArrayList<Integer>();

        /*for (int c : ColorTemplate.VORDIPLOM_COLORS)
            colors.add(c);

        for (int c : ColorTemplate.PASTEL_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.JOYFUL_COLORS)
            colors.add(c);

        colors.add(ColorTemplate.getHoloBlue());*/
            colors.add(activity.getResources().getColor(R.color.msg_color));
            colors.add(activity.getResources().getColor(R.color.traning_color));
            colors.add(activity.getResources().getColor(R.color.appro_color));
            colors.add(activity.getResources().getColor(R.color.trans_color));
            dataSet.setColors(colors);

            // instantiate pie data object now
            PieData data = new PieData(xVals, dataSet);
            data.setValueFormatter(new PercentFormatter());
            data.setValueTextSize(11f);
            data.setValueTextColor(Color.GRAY);

            pieChart.setData(data);

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

            // update pie chart
            pieChart.invalidate();
        }

根据自己的要求,定义相应的数值和颜色。

猜你喜欢

转载自blog.csdn.net/wode_dream/article/details/50038527