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

如果有什麼問題,請及時溝通

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