控件项目地址:
https://github.com/PhilJay/MPAndroidChart
效果图
使用
- 1.导库
可以去下载jar包
下载地址:https://github.com/PhilJay/MPAndroidChart/releases
不过我是直接使用了MPChartLib-release.aar
下载地址:http://download.csdn.net/detail/xiehao_95/9568403
build.gradle
设置
repositories {
flatDir {
dirs 'libs'
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.4.0'
compile(name: 'MPChartLib-release', ext: 'aar')
}
- 2.布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.github.mikephil.charting.charts.PieChart
android:id="@+id/pieChart"
android:layout_width="280dp"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_marginBottom="80dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="50dp" />
</RelativeLayout>
- 3.java代码
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);
int[] date = new int[]{
1, 6, 8};
initPieChat