MPAndroidChart之PieChart

 MPAndroidChart之PieChart餅狀圖

曾夢想仗劍走天涯,看一看世界的繁華。

效果圖:



Activity:

package com.wentong.administrator.mpandroid.activity;

import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;
import android.text.style.StyleSpan;

import com.github.mikephil.charting.animation.Easing;
import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.data.PieEntry;
import com.github.mikephil.charting.utils.ColorTemplate;
import com.wentong.administrator.mpandroid.R;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by Administrator on 2017/3/15.
 */

public class PieActivity extends AppCompatActivity{
    //申明控件
    PieChart pieChart ;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //靜態加載佈局
        setContentView(R.layout.activity_pie);
        //初始化界面
        show() ;
    }

    private void show() {
        pieChart = (PieChart) findViewById(R.id.act_pie_pieChart) ;
        //如果啓用此選項,則圖表中的值將以百分比形式繪製,而不是以原始值繪製
        pieChart.setUsePercentValues(true);
        //如果這個組件應該啓用(應該被繪製)FALSE如果沒有。如果禁用,此組件的任何內容將被繪製默認
        pieChart.getDescription().setEnabled(false);
        //將額外的偏移量(在圖表視圖周圍)附加到自動計算的偏移量
        pieChart.setExtraOffsets(5, 10, 5, 5);
        //較高的值表明速度會緩慢下降 例如如果它設置爲0,它會立即停止。1是一個無效的值,並將自動轉換爲0.999f。
        pieChart.setDragDecelerationFrictionCoef(0.95f);
        //設置中間字體
        pieChart.setCenterText("劉某人\n我彷彿聽到有人說我帥");
        //設置爲true將餅中心清空
        pieChart.setDrawHoleEnabled(true);
        //套孔,繪製在PieChart中心的顏色
        pieChart.setHoleColor(Color.WHITE);
        //設置透明圓應有的顏色。
        pieChart.setTransparentCircleColor(Color.WHITE);
        //設置透明度圓的透明度應該有0 =完全透明,255 =完全不透明,默認值爲100。
        pieChart.setTransparentCircleAlpha(110);
        //設置在最大半徑的百分比餅圖中心孔半徑(最大=整個圖的半徑),默認爲50%
        pieChart.setHoleRadius(58f);
        //設置繪製在孔旁邊的透明圓的半徑,在最大半徑的百分比在餅圖*(max =整個圖的半徑),默認55% -> 5%大於中心孔默認
        pieChart.setTransparentCircleRadius(61f);
        //將此設置爲true,以繪製顯示在pie chart
        pieChart.setDrawCenterText(true);
        //集度的radarchart旋轉偏移。默認270f -->頂(北)
        pieChart.setRotationAngle(0);
        //設置爲true,使旋轉/旋轉的圖表觸摸。設置爲false禁用它。默認值:true
        pieChart.setRotationEnabled(true);
        //將此設置爲false,以防止由抽頭姿態突出值。值仍然可以通過拖動或編程高亮顯示。默認值:真
        pieChart.setHighlightPerTapEnabled(true);
        //創建Legend對象
        Legend l = pieChart.getLegend();
        //設置垂直對齊of the Legend
        l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
        //設置水平of the Legend
        l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT);
        //設置方向
        l.setOrientation(Legend.LegendOrientation.VERTICAL);
        //其中哪一個將畫在圖表或外
        l.setDrawInside(false);
        //設置水平軸上圖例項之間的間距
        l.setXEntrySpace(7f);
        //設置在垂直軸上的圖例項之間的間距
        l.setYEntrySpace(0f);
        //設置此軸上標籤的所使用的y軸偏移量 更高的偏移意味着作爲一個整體的Legend將被放置遠離頂部。
        l.setYOffset(0f);
        //設置入口標籤的顏色。
        pieChart.setEntryLabelColor(Color.WHITE);
        //設置入口標籤的大小。默認值:13dp
        pieChart.setEntryLabelTextSize(12f);
        //模擬的數據源
        PieEntry x1 = new PieEntry(15.8f , "one" , R.color.colorAccent) ;
        PieEntry x2 = new PieEntry(15.8f , "two") ;
        PieEntry x3 = new PieEntry(15.8f , "three") ;
        PieEntry x4 = new PieEntry(15.8f , "four") ;
        PieEntry x5 = new PieEntry(15.8f , "five") ;
        PieEntry x6 = new PieEntry(15.8f , "six") ;
        PieEntry x7 = new PieEntry(15.8f , "seven") ;
        PieEntry x8 = new PieEntry(15.8f , "eight") ;
        PieEntry x9 = new PieEntry(15.8f , "nine") ;
        PieEntry x10 = new PieEntry(15.8f , "ten") ;
        //添加到List集合
        List<PieEntry> list = new ArrayList<>() ;
        list.add(x1) ;
        list.add(x2) ;
        list.add(x3) ;
        list.add(x4) ;
        list.add(x5) ;
        list.add(x6) ;
        list.add(x7) ;
        list.add(x8) ;
        list.add(x9) ;
        list.add(x10) ;
        //設置到PieDataSet對象
        PieDataSet set = new PieDataSet(list , "表一") ;
        set.setDrawValues(false);//設置爲true,在圖表繪製y
        set.setAxisDependency(YAxis.AxisDependency.LEFT);//設置Y軸,這個數據集應該被繪製(左或右)。默認值:左
        set.setAutomaticallyDisableSliceSpacing(false);//當啓用時,片間距將是0時,最小值要小於片間距本身
        set.setSliceSpace(5f);//間隔
        set.setSelectionShift(10f);//點擊伸出去的距離
        /**
         * 設置該數據集前應使用的顏色。顏色使用只要數據集所代表的條目數目高於顏色數組的大小。
         * 如果您使用的顏色從資源, 確保顏色已準備好(通過調用getresources()。getColor(…))之前,將它們添加到數據集
         * */
        ArrayList<Integer> colors = new ArrayList<Integer>();
        for (int c : ColorTemplate.VORDIPLOM_COLORS)
            colors.add(c);
        for (int c : ColorTemplate.JOYFUL_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.PASTEL_COLORS)
            colors.add(c);
        colors.add(ColorTemplate.getHoloBlue());
        set.setColors(colors);
        //傳入PieData
        PieData data = new PieData(set);
        //爲圖表設置新的數據對象
        pieChart.setData(data);
        //刷新
        pieChart.invalidate();
        //動畫圖上指定的動畫時間軸的繪製
        pieChart.animateY(1400, Easing.EasingOption.EaseInOutQuad);

    }
}

XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.github.mikephil.charting.charts.PieChart
        android:id="@+id/act_pie_pieChart"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></com.github.mikephil.charting.charts.PieChart>

</LinearLayout>




發佈了42 篇原創文章 · 獲贊 24 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章