导入MPAndroidChart的jar包以后就写个工具类:
package itsoha.com.tffic.Utils;
import android.content.Context;
import android.graphics.Color;
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;
public class PieEngine {
private String title;
private PieChart mChart;
public PieEngine(String title) {
this.title = title;
}
public PieChart getView(Context context, ArrayList<String> name, ArrayList<Integer> colors, ArrayList<Integer> values) {
mChart = new PieChart(context);
//设置孔颜色透明
mChart.setHoleColorTransparent(true);
//半径
mChart.setHoleRadius(60f);
//半透明圆
mChart.setTransparentCircleRadius(64f);
mChart.setDescription("");
//饼图中间可以添加文字
mChart.setDrawCenterText(true);
//拉孔启用
mChart.setDrawHoleEnabled(true);
//初始旋转角度
mChart.setRotationAngle(90f);
//手动旋转
mChart.setRotationEnabled(true);
mChart.setDrawHoleEnabled(true);
//显示百分比
mChart.setUsePercentValues(true);
Legend legend = mChart.getLegend();
legend.setFormSize(18);
legend.setTextSize(29);
legend.setXEntrySpace(35);
legend.setYEntrySpace(35);
legend.setPosition(Legend.LegendPosition.LEFT_OF_CHART);
ArrayList<Entry> entryArrayList = new ArrayList<>();
for (int i = 0; i < values.size(); i++) {
entryArrayList.add(new Entry(values.get(i),i));
}
PieDataSet dataSet = new PieDataSet(entryArrayList, title);
dataSet.setValueTextSize(28);
dataSet.setColors(colors);
dataSet.setSelectionShift(5);
PieData data = new PieData(name, dataSet);
data.setValueTextSize(25);
mChart.setData(data);
mChart.notifyDataSetChanged();
return mChart;
}
}
调用方式如下:
package itsoha.com.tffic.fragment;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import com.github.mikephil.charting.charts.PieChart;
import java.util.ArrayList;
import java.util.Random;
import itsoha.com.tffic.R;
import itsoha.com.tffic.Utils.PieEngine;
public class PieFragment extends Fragment {
private Handler handler = new Handler();
private ArrayList<String> names = new ArrayList<>();
private ArrayList<Integer> colors = new ArrayList<>();
private Random random = new Random();
private PieEngine engine;
private PieChart chart;
private FrameLayout fl;
private Runnable runnable = new Runnable() {
@Override
public void run() {
fl.removeAllViews();
ArrayList<Integer> values = new ArrayList<>();
values.add(random.nextInt(100));
values.add(random.nextInt(100));
values.add(random.nextInt(100));
chart = engine.getView(getActivity(),names,colors,values);
fl.addView(chart);
handler.postDelayed(runnable, 1500);
}
};
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_pie, container, false);
fl = view.findViewById(R.id.fl_pie);
engine = new PieEngine("Test");
names.add("one");
names.add("to");
names.add("three");
colors.add(Color.RED);
colors.add(Color.BLUE);
colors.add(Color.GREEN);
handler.post(runnable);
return view;
}
}