MpAndroidChart饼图

好久没写帖子了,一直忙的要死
今天简单总结一下MpAndroidChart的饼图使用吧

先上照片吧,没做Gif,请原谅我的懒
在这里插入图片描述

好了,直接上xml吧,就是个很简单的布局文件,饼图中间显示统计数据

<RelativeLayout
        android:id="@+id/rl_content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/dp_10">

        <com.github.mikephil.charting.charts.PieChart
            android:id="@+id/chart"
            android:layout_width="match_parent"
            android:layout_height="@dimen/dp_250"
            android:layout_centerHorizontal="true" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="@dimen/dp_100"
            android:gravity="center"
            android:orientation="vertical">

            <ImageView
                android:layout_width="@dimen/dp_60"
                android:layout_height="@dimen/dp_60"
                android:background="@mipmap/icon_heji" />

            <TextView
                android:id="@+id/tv_count"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/dp_10"
                android:text="4500"
                android:textColor="@color/text_black_color"
                android:textSize="@dimen/sp_24" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="合计"
                android:textColor="@color/text_black_color"
                android:textSize="20sp" />
        </LinearLayout>

    </RelativeLayout>

按照,套路,下面就是java代码了


private final String[] parties = new String[]{
            "重大危险", "较大危险", "一般危险", "低危险"
    };
    
// 实际数据
private List<Integer> data;
    
private void testBarChart() {
        mBarChart.setBackgroundColor(Color.WHITE);

        mBarChart.setUsePercentValues(true);// 使用百分比
        mBarChart.getDescription().setEnabled(false);
        mBarChart.setDrawEntryLabels(false);// 设置是否显示饼图上的Lable文字

        mBarChart.setCenterText(generateCenterSpannableText());
        mBarChart.setCenterTextSize(20f);

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

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

        mBarChart.setHoleRadius(58f);// 设置圆盘中间区域大小
        mBarChart.setTransparentCircleRadius(61f); // 设置中间透明圈的大小


        mBarChart.setDrawCenterText(false);// 设置是否显示中间文字

        mBarChart.setRotationEnabled(false);
        mBarChart.setHighlightPerTapEnabled(true);

		// HALF CHART : 180(半副图)
		// HALF CHART : 270(就是上图的显示状态)
        mBarChart.setMaxAngle(270f); 
        mBarChart.setRotationAngle(135f);
        mBarChart.setCenterTextOffset(0, -20);

        // 设置饼图块数
        setData(4, 100);

        mBarChart.animateY(1400, Easing.EaseInOutQuad);

        Legend l = mBarChart.getLegend();
        l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
        l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER);
        l.setOrientation(Legend.LegendOrientation.HORIZONTAL);
        l.setDrawInside(false);
        l.setXEntrySpace(7f);
        l.setYEntrySpace(0f);
        l.setYOffset(0f);

        // entry label styling
        mBarChart.setEntryLabelColor(Color.WHITE);
        mBarChart.setEntryLabelTextSize(12f);
    }

    private void setData(int count, float range) {

        mMajorTv.setText(data.get(0) + "");
        mBiggerTv.setText(data.get(1) + "");
        mOrdinaryTv.setText(data.get(2) + "");
        mLowTv.setText(data.get(3) + "");

        mCountTv.setText(data.get(0) + data.get(1) + data.get(2) + data.get(3) + "");

        ArrayList<PieEntry> values = new ArrayList<>();

        for (int i = 0; i < data.size(); i++) {
            values.add(new PieEntry((float) ((data.get(i) * range) + range / 5), parties[i % parties.length]));
        }

        PieDataSet dataSet = new PieDataSet(values, "");
        dataSet.setSliceSpace(3f);
        dataSet.setSelectionShift(5f);

//        dataSet.setColors(ColorTemplate.MATERIAL_COLORS);// 系统自带颜色
        List<Integer> list = new ArrayList<>();
//        list.add(getResources().getColor(R.color.chart_blue));
//        list.add(getResources().getColor(R.color.chart_yellow));
//        list.add(getResources().getColor(R.color.chart_orange));
//        list.add(getResources().getColor(R.color.chart_red));

        list.add(getResources().getColor(R.color.chart_red));
        list.add(getResources().getColor(R.color.chart_orange));
        list.add(getResources().getColor(R.color.chart_yellow));
        list.add(getResources().getColor(R.color.chart_blue));
        dataSet.setColors(list);

        // 设置标线在外显示数据
//        dataSet.setValueLinePart1OffsetPercentage(80.f);
//        dataSet.setValueLinePart1Length(0.2f);
//        dataSet.setValueLinePart2Length(0.5f);
//        dataSet.setYValuePosition(PieDataSet.ValuePosition.OUTSIDE_SLICE);

        //dataSet.setSelectionShift(0f);

        PieData data = new PieData(dataSet);
        data.setValueFormatter(new PercentFormatter());
        data.setValueTextSize(14f);
        data.setValueTextColor(Color.WHITE);

        Legend legend = mBarChart.getLegend();//设置比例图
        legend.setEnabled(false);//图例不显示

        mBarChart.setData(data);

        mBarChart.invalidate();
    }

如果有什么问题,请及时沟通

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章