参考文档:
https://blog.csdn.net/u013184970/article/details/52095170
https://blog.csdn.net/cen_yuan/article/details/52204281
注意:
在要使用的module的build.gradle添加:
dependencies {
compile ‘com.github.PhilJay:MPAndroidChart:v3.0.0-beta1’
}
与compile 'com.github.PhilJay:MPAndroidChart:v2.2.5’有些不同!
案例使用的是2.2.5
一、说明
bulid.gradle(project)中添加
allprojects {
repositories {
jcenter()
maven { url “https://jitpack.io” }
}
}
bulid.gradle(app)中添加
dependencies {
compile ‘com.github.PhilJay:MPAndroidChart:v2.2.5’
}
二、XML代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"tools:context="com.zjs.mpandroidchart.MainActivity"><com.github.mikephil.charting.charts.PieChartandroid:id="@+id/consume_pie_chart"android:layout_width="match_parent"android:layout_height="400dp"android:layout_gravity="center_horizontal"android:layout_marginLeft="16dp"android:layout_marginRight="16dp"></com.github.mikephil.charting.charts.PieChart><TextViewandroid:id="@+id/botton_text"android:textSize="25dp"android:textColor="#f00"android:text=""android:gravity="center_horizontal"android:layout_width="match_parent"android:layout_height="wrap_content"/></LinearLayout>
三、activity代码
package com.zjs.mpandroidchart;import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.DisplayMetrics;
import android.widget.TextView;import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.formatter.ValueFormatter;
import com.github.mikephil.charting.utils.ViewPortHandler;import java.util.ArrayList;public class MainActivity extends AppCompatActivity {public PieChart mChart;private TextView mText;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mChart = (PieChart) findViewById(R.id.consume_pie_chart);
// 初始化饼状图数据类PieData mpieData = getPieData();mText = (TextView) findViewById(R.id.botton_text);mText.setText("全年消费情况");showMchat(mChart, mpieData);}private void showMchat(PieChart pieChart, PieData pieData) {
// 设置饼状图中心透明pieChart.setHoleColor(Color.TRANSPARENT);
// //设置描述
// pieChart.setDescription("全年消费情况");
// //设置描述字体大小
// pieChart.setDescriptionTextSize(50f);
// //设置描述颜色
// pieChart.setDescriptionColor(Color.RED);
// //设置描述字体
// pieChart.setDescriptionTypeface(Typeface.DEFAULT);
// pieChart.setDescriptionPosition(666,1300);// 设置饼状图的半径pieChart.setHoleRadius(60f);
// 饼状图添加文字pieChart.setDrawHoleEnabled(true);pieChart.setCenterText("家庭支出");pieChart.setCenterTextSize(26f);pieChart.setCenterTextColor(Color.RED);// 初始选择角度pieChart.setRotationAngle(90);
// 设置手动可以旋转pieChart.setRotationEnabled(true);
// 设置百分比显示pieChart.setUsePercentValues(true);pieChart.setData(pieData);
// 设置比例图Legend mlegend = pieChart.getLegend();mlegend.setPosition(Legend.LegendPosition.RIGHT_OF_CHART);mlegend.setTextSize(15);mlegend.setTextColor(Color.RED);mlegend.setXEntrySpace(7f);mlegend.setYEntrySpace(5f);// 初始动画pieChart.animateXY(1000, 1000);}private PieData getPieData() {
// 衣食住行以及其他ArrayList<String> xValues = new ArrayList<>();xValues.add("衣");xValues.add("食");xValues.add("住");xValues.add("行");xValues.add("其他");ArrayList<Entry> yValues = new ArrayList<>();float y1 = 10;float y2 = 20;float y3 = 25;float y4 = 40;float y5 = 5;yValues.add(new Entry(y1, 0));yValues.add(new Entry(y2, 1));yValues.add(new Entry(y3, 2));yValues.add(new Entry(y4, 3));yValues.add(new Entry(y5, 4));
// PieDataSet pieDataSet = new PieDataSet(yValues, "2018年家庭支出");PieDataSet pieDataSet = new PieDataSet(yValues, "");pieDataSet.setSliceSpace(1f);ArrayList<Integer> colour = new ArrayList<>();
// 饼状图颜色colour.add(Color.RED);colour.add(Color.BLUE);colour.add(Color.YELLOW);colour.add(Color.GRAY);colour.add(Color.GREEN);pieDataSet.setColors(colour);pieDataSet.setValueTextColor(Color.BLACK);pieDataSet.setValueTextSize(25);pieDataSet.setValueFormatter(new ValueFormatter() {@Overridepublic String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) {return "" + (int) value + "%";}});DisplayMetrics metrics = getResources().getDisplayMetrics();float px = 5 * (metrics.densityDpi / 180f);pieDataSet.setSelectionShift(px);PieData pieData = new PieData(xValues, pieDataSet);return pieData;}
}