github地址:https://github.com/PhilJay/MPAndroidChartjava
添加依賴:android
build.gradle
:allprojects {
repositories {
maven { url "https://jitpack.io" } } }
build.gradle
:dependencies {
compile 'com.github.PhilJay:MPAndroidChart:v3.0.2' }
下載Demo演示,修改爲本身的需求便可
如餅圖的示例代碼:
import android.graphics.Color; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.util.DisplayMetrics; import com.github.mikephil.charting.charts.PieChart; import com.github.mikephil.charting.components.Description; import com.github.mikephil.charting.components.Legend; import com.github.mikephil.charting.data.PieData; import com.github.mikephil.charting.data.PieDataSet; import com.github.mikephil.charting.data.PieEntry; import java.util.ArrayList; public class MainActivity extends AppCompatActivity { private PieChart mChart; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mChart = (PieChart) findViewById(R.id.pieChart); PieData mPieData = getPieData(4, 100); showChart(mChart, mPieData); } private void showChart(PieChart pieChart, PieData pieData) { pieChart.setHoleColor(Color.TRANSPARENT); pieChart.setHoleRadius(60f); //半徑 pieChart.setTransparentCircleRadius(64f); // 半透明圈 //pieChart.setHoleRadius(0) //實心圓 Description description = new Description(); description.setText("測試餅狀圖"); pieChart.setDescription(description); // mChart.setDrawYValues(true); pieChart.setDrawCenterText(true); //餅狀圖中間能夠添加文字 pieChart.setDrawHoleEnabled(true); pieChart.setRotationAngle(90); // 初始旋轉角度 pieChart.setRotationEnabled(true); // 能夠手動旋轉 pieChart.setUsePercentValues(true); //顯示成百分比 pieChart.setCenterText("測試"); //餅狀圖中間的文字 pieChart.setCenterTextSize(18); //設置數據 pieChart.setData(pieData); Legend mLegend = pieChart.getLegend(); //設置比例圖 mLegend.setPosition(Legend.LegendPosition.RIGHT_OF_CHART); //最右邊顯示 // mLegend.setForm(LegendForm.LINE); //設置比例圖的形狀,默認是方形 mLegend.setXEntrySpace(7f); mLegend.setYEntrySpace(5f); pieChart.animateXY(1000, 1000); //設置動畫 } /** * * @param count 分紅幾部分 * @param range */ private PieData getPieData(int count, float range) { ArrayList<String> xValues = new ArrayList<String>(); //xVals用來表示每一個餅塊上的內容 for (int i = 0; i < count; i++) { xValues.add("Quarterly" + (i + 1)); //餅塊上顯示成Quarterly1, Quarterly2, Quarterly3, Quarterly4 } ArrayList<PieEntry> yValues = new ArrayList(); //yVals用來表示封裝每一個餅塊的實際數據 // 餅圖數據 /** * 將一個餅形圖分紅四部分, 四部分的數值比例爲14:14:34:38 * 因此 14表明的百分比就是14% */ float quarterly1 = 14; float quarterly2 = 14; float quarterly3 = 34; float quarterly4 = 38; yValues.add(new PieEntry(quarterly1, 0)); yValues.add(new PieEntry(quarterly2, 1)); yValues.add(new PieEntry(quarterly3, 2)); yValues.add(new PieEntry(quarterly4, 3)); //y軸的集合 PieDataSet pieDataSet = new PieDataSet(yValues, "Quarterly Revenue 2014"/*顯示在比例圖上*/); pieDataSet.setSliceSpace(0f); //設置個餅狀圖之間的距離 ArrayList<Integer> colors = new ArrayList<Integer>(); // 餅圖顏色 colors.add(Color.rgb(205, 205, 205)); colors.add(Color.rgb(114, 188, 223)); colors.add(Color.rgb(255, 123, 124)); colors.add(Color.rgb(57, 135, 200)); pieDataSet.setColors(colors); DisplayMetrics metrics = getResources().getDisplayMetrics(); float px = 5 * (metrics.densityDpi / 160f); pieDataSet.setSelectionShift(px); // 選中態多出的長度 PieData pieData = new PieData(pieDataSet); return pieData; } }
activity_main.xmlgit
<com.github.mikephil.charting.charts.PieChart android:id="@+id/pieChart" android:layout_width="match_parent" android:layout_height="320dp" />