Android利用MPAndroidChart制作饼图

最近在在App中学习制作做图表时的学习心得

一、构建gradle


Android利用MPAndroidChart制作饼图_第1张图片


二、API方法介绍


setUsePercentValues();      //Boolean类型  设置图表是否使用百分比

setDescription("");         //添加图表描述

setExtraOffsets();          //设置饼图圆圈离屏幕的边距

setDrawSliceText();         //Boolean类型   设置是否隐藏饼图上文字,只显示百分比 

setDrawHoleEnabled();       //Boolean类型   设置图饼中心是否是空心

setHoleColorTransparent();  //Boolean类型    设置中间空心圆孔的颜色是否透明

setTransparentCircleColor();//设置环形图与中间空心圆之间的环形的颜色

setTransparentCircleAlpha();//设置环形图与中间空心圆之间圆环的的透明度

setHoleRadius();            //设置圆孔半径

setTransparentCircleRadius();//设置半透明圈的宽度

setDrawCenterText();         //Boolean类型    设置中心圆是否可以显示文字

setCenterTextSize();         //设置中心圆的文字大小

setCenterTextColor();        //设置中心圆的文字的颜色

setNoDataText();             //设置饼图没有数据时显示的文本

setRotationAngle();        //设置初始时的角度

setRotationEnabled();      //设置是否可以手动旋转

setHighlightPerTapEnabled();//Boolean类型 设置点击Item高亮是否可用

setDragDecelerationFrictionCoe();//设置阻力摩擦系数[0,1]

setEntryLabelColor();         //设置图表文字颜色

setEntryLabelTypeface();       //设置图表文字样式

setEntryLabelTextSize();       //设置图表文字大小

invalidate();                 //重绘图表

Legend l = new mChart.getLegdeng();   //获取饼图图例

l.setEnabled();                       //Boolean类型   设置是否启用图列

l.setForm(Legend.LegendForm.DEFAULT); //设置图例的形状默认正方形

l.setFormSize();                    //设置图例的大小

l.setFormToTextSpace();            //设置每个图例实体中标签和形状之间的间距

l.setWordWrapEnabled();           //Boolean类型设置图列换行(注意使用影响性能,仅适用legend位于图表下面)

l.setXEntrySpace();              //设置图例实体之间延X轴的间距(setOrientation = HORIZONTAL有效)

l.setTextSize();                 //设置图例标签文本的大小  

l.setTextColor();               //设置图例标签文本的颜色 

mChart.setOnChartValueSelectedListener();   //监听器

三、代码部分


布局代码


Activity代码
package com.example.zhaowei.piechartdemo;
/**
 * 利用MPAndroid制作饼图
 *
 * @author ZW_com
 * @date 2017/3/4
 */

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

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 java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    PieChart mPiechart;
    private String[] Stars = new String[]{"1颗星", "2颗星", "3颗星", "4颗星", "5颗星"};
    private int[] number = new int[]{1, 2, 3, 4, 5};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mPiechart = (PieChart) findViewById(R.id.mPieChart);
        initView();
    }

    /**
     * 初始化图表
     */
    private void initView() {
        setData();
        //设置X轴的动画
        mPiechart.animateX(1400);
        //使用百分比
        mPiechart.setUsePercentValues(true);
        //设置图列可见
        mPiechart.getLegend().setEnabled(true);
        //设置图列标识的大小
        mPiechart.getLegend().setFormSize(14);
        //设置图列标识文字的大小
        mPiechart.getLegend().setTextSize(14);
        //设置图列的位置
        mPiechart.getLegend().setPosition(Legend.LegendPosition.RIGHT_OF_CHART);
        //设置图列标识的形状
        mPiechart.getLegend().setForm(Legend.LegendForm.CIRCLE);
        //设置图表描述
        mPiechart.setDescription("这是一个饼图");
        //设置图表描述的字体
        mPiechart.setDescriptionTextSize(14);
        //设置图表描述的位置
        mPiechart.setDescriptionPosition(950, 950);
        //设置是否可转动
        mPiechart.setRotationEnabled(true);
    }

    /**
     * 图表设置数据
     */
    private void setData() {
        //设置标题
        ArrayList titles = new ArrayList<>();
        for (int i = 0; i < Stars.length; i++) {
            titles.add(Stars[i]);
        }
        ArrayList entrys = new ArrayList<>();
        for (int i = 0; i < number.length; i++) {
            entrys.add(new Entry(number[i], i));
        }
        //饼图数据集
        PieDataSet dataset = new PieDataSet(entrys, "星级评定");
	//设置饼状图Item之间的间隙
	dataset.setSlicespace(3f);
	//饼图Item被选中时变化的距离
	dataset.setSelectionShift(10f);
	//颜色填充
        dataset.setColors(new int[]{Color.rgb(0, 255, 255), Color.rgb(60, 179, 113), Color.rgb(255, 165, 0), Color.rgb(124, 252, 0), Color.rgb(255, 182, 1193)});
        //数据填充
        PieData piedata = new PieData(titles, dataset);
        //设置饼图上显示数据的字体大小
        piedata.setValueTextSize(15);
        mPiechart.setData(piedata);
    }
}

Android利用MPAndroidChart制作饼图_第2张图片


你可能感兴趣的:(java&Android小程序,app,MPAdroidChart)