餅狀圖MPAndroidChart

 MPAndroidChart使用也可以看我前幾篇文章

1.橫向雙值柱狀圖

2.漸變色雙值柱狀圖MPAndroidChart

 直接看圖:

具體怎麼時使用MPAndroidChart可已看代碼

直接上代碼:

初始化

 pieChart.setNoDataText("暫無數據");
        pieChart.setUsePercentValues(true);
        pieChart.getDescription().setEnabled(false);
        pieChart.setDragDecelerationFrictionCoef(0.95f);
        pieChart.setExtraOffsets(5, 10, 5, 5);
//        pieChart.setExtraBottomOffset(10);
//        pieChart.setExtraOffsets(0, 0, 0, 0);
//        holder.pieChart.setCenterText("ABC");
        pieChart.setDrawEntryLabels(false);

        pieChart.setDrawHoleEnabled(true);
        pieChart.setHoleColor(Color.WHITE);
        pieChart.setDrawCenterText(false);//餅狀圖中間可以添加文字

        pieChart.setHoleRadius(80f);
        pieChart.setTransparentCircleRadius(0f);
        pieChart.setRotationAngle(0);
        pieChart.animateY(1400, Easing.EaseInOutQuad);

        Legend l =  pieChart.getLegend();
        l.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM);
        if(type==1) {
            l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER);
        }else{
            l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER);
        }
        l.setOrientation(Legend.LegendOrientation.VERTICAL);
        l.setDrawInside(false);
        l.setXEntrySpace(5f);
        l.setYEntrySpace(0f);
        l.setYOffset(10f);

        // entry label styling
        pieChart.setEntryLabelColor(Color.WHITE);
//        holder.pieChart.setEntryLabelTypeface(tfRegular);
        pieChart.setEntryLabelTextSize(12f);

綁定數據

 ArrayList<PieEntry> entries = new ArrayList<>();
        PieDataSet dataSet = new PieDataSet(entries, "");
        dataSet.setSliceSpace(3f);

        ArrayList<Integer> colors = new ArrayList<>();

        for (int i = 0; i < values.size(); i++) {
            entries.add(new PieEntry((float)values.get(i),datas.get(i)));
            colors.add(resources.getColor(Constants.COLORS_ARRAY[i]));
        }

        dataSet.setColors(colors);
        dataSet.setValueTextColor(Color.TRANSPARENT);

        PieData pieData = new PieData(dataSet);
        pieData.setValueFormatter(new PercentFormatter(new DecimalFormat()));
        pieChart.setData(pieData);
        pieChart.notifyDataSetChanged();
        pieChart.invalidate();

關於MPAndroidChart使用可以看我前幾篇關於橫向雙值柱狀圖文章
 

如果對你有幫助可以關注一下方便查看,歡迎關注討論

1.橫向雙值柱狀圖

2.漸變色雙值柱狀圖MPAndroidChart

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