PieChart屬性

// 設置 pieChart 圖表基本屬性
mChart.setUsePercentValues(false); //使用百分比顯示
mChart.getDescription().setEnabled(false); //設置pieChart圖表的描述
mChart.setBackgroundColor(Color.YELLOW); //設置pieChart圖表背景色
mChart.setExtraOffsets(5, 10, 60, 10); //設置pieChart圖表上下左右的偏移,類似於外邊距
mChart.setDragDecelerationFrictionCoef(0.95f);//設置pieChart圖表轉動阻力摩擦係數[0,1]
mChart.setRotationAngle(0); //設置pieChart圖表起始角度
mChart.setRotationEnabled(true); //設置pieChart圖表是否可以手動旋轉
mChart.setHighlightPerTapEnabled(true); //設置piecahrt圖表點擊Item高亮是否可用
mChart.animateY(1400, Easing.EasingOption.EaseInOutQuad);// 設置pieChart圖表展示動畫效果

// 設置 pieChart 圖表Item文本屬性
mChart.setDrawEntryLabels(true); //設置pieChart是否只顯示餅圖上百分比不顯示文字(true:下面屬性纔有效果)
mChart.setEntryLabelColor(Color.WHITE); //設置pieChart圖表文本字體顏色
mChart.setEntryLabelTypeface(mTfRegular); //設置pieChart圖表文本字體樣式
mChart.setEntryLabelTextSize(10f); //設置pieChart圖表文本字體大小

// 設置 pieChart 內部圓環屬性
mChart.setDrawHoleEnabled(true); //是否顯示PieChart內部圓環(true:下面屬性纔有意義)
mChart.setHoleRadius(28f); //設置PieChart內部圓的半徑(這裏設置28.0f)
mChart.setTransparentCircleRadius(31f); //設置PieChart內部透明圓的半徑(這裏設置31.0f)
mChart.setTransparentCircleColor(Color.BLACK);//設置PieChart內部透明圓與內部圓間距(31f-28f)填充顏色
mChart.setTransparentCircleAlpha(50); //設置PieChart內部透明圓與內部圓間距(31f-28f)透明度[0~255]數值越小越透明
mChart.setHoleColor(Color.WHITE); //設置PieChart內部圓的顏色
mChart.setDrawCenterText(true); //是否繪製PieChart內部中心文本(true:下面屬性纔有意義)
mChart.setCenterTextTypeface(mTfLight); //設置PieChart內部圓文字的字體樣式
mChart.setCenterText(“Test”); //設置PieChart內部圓文字的內容
mChart.setCenterTextSize(10f); //設置PieChart內部圓文字的大小
mChart.setCenterTextColor(Color.RED); //設置PieChart內部圓文字的顏色

// pieChart添加數據
setData();

// 獲取pieCahrt圖列
Legend l = mChart.getLegend();
l.setEnabled(true); //是否啓用圖列(true:下面屬性纔有意義)
l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT);
l.setOrientation(Legend.LegendOrientation.VERTICAL);
l.setForm(Legend.LegendForm.DEFAULT); //設置圖例的形狀
l.setFormSize(10); //設置圖例的大小
l.setFormToTextSpace(10f); //設置每個圖例實體中標籤和形狀之間的間距
l.setDrawInside(false);
l.setWordWrapEnabled(true); //設置圖列換行(注意使用影響性能,僅適用legend位於圖表下面)
l.setXEntrySpace(10f); //設置圖例實體之間延X軸的間距(setOrientation = HORIZONTAL有效)
l.setYEntrySpace(8f); //設置圖例實體之間延Y軸的間距(setOrientation = VERTICAL 有效)
l.setYOffset(0f); //設置比例塊Y軸偏移量
l.setTextSize(14f); //設置圖例標籤文本的大小
l.setTextColor(Color.parseColor("#ff9933"));//設置圖例標籤文本的顏色

//pieChart 選擇監聽
mChart.setOnChartValueSelectedListener(this);

//設置MARKERVIEW
CustomMarkerView mv = new CustomMarkerView(this, new PercentFormatter());
mv.setChartView(mChart);
mChart.setMarker(mv);

/**

  • 設置餅圖的數據
    */
    private void setData() {
    ArrayList pieEntryList = new ArrayList();
    ArrayList colors = new ArrayList();
    colors.add(Color.parseColor("#f17548"));
    colors.add(Color.parseColor("#FF9933"));
    //餅圖實體 PieEntry
    PieEntry CashBalance = new PieEntry(70, “現金餘額 1500”);
    PieEntry ConsumptionBalance = new PieEntry(30, “消費餘額 768”);
    pieEntryList.add(CashBalance);
    pieEntryList.add(ConsumptionBalance);
    //餅狀圖數據集 PieDataSet
    PieDataSet pieDataSet = new PieDataSet(pieEntryList, “資產總覽”);
    pieDataSet.setSliceSpace(3f); //設置餅狀Item之間的間隙
    pieDataSet.setSelectionShift(10f); //設置餅狀Item被選中時變化的距離
    pieDataSet.setColors(colors); //爲DataSet中的數據匹配上顏色集(餅圖Item顏色)
    //最終數據 PieData
    PieData pieData = new PieData(pieDataSet);
    pieData.setDrawValues(true); //設置是否顯示數據實體(百分比,true:以下屬性纔有意義)
    pieData.setValueTextColor(Color.BLUE); //設置所有DataSet內數據實體(百分比)的文本顏色
    pieData.setValueTextSize(12f); //設置所有DataSet內數據實體(百分比)的文本字體大小
    pieData.setValueTypeface(mTfLight); //設置所有DataSet內數據實體(百分比)的文本字體樣式
    pieData.setValueFormatter(new PercentFormatter());//設置所有DataSet內數據實體(百分比)的文本字體格式
    mChart.setData(pieData);
    mChart.highlightValues(null);
    mChart.invalidate(); //將圖表重繪以顯示設置的屬性和數據
    }
    ————————————————
    版權聲明:本文爲CSDN博主「zcmain」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處鏈接及本聲明。
    原文鏈接:https://blog.csdn.net/zcmain/article/details/53611245
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章