首先要添加依赖
在工程的build.gradle中添加
allprojects {
repositories {
jcenter()
maven { url "https://jitpack.io"}
}
}
在module的build.gradle中添加,3.0饼状图数据是引出来的,而2.0是在饼上的
compile 'com.github.PhilJay:MPAndroidChart:v3.0.1'
xml文件
<com.github.mikephil.charting.charts.PieChart
android:id="@+id/year_carsAll"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="2">
</com.github.mikephil.charting.charts.PieChart>
public class PieChartManager {
public static void initDataStyle(PieChart pieChart,PieData pieData) {
pieChart.setHoleRadius(50f);//半径
pieChart.setHoleRadius(0);//实心圈
// pieChart.setTransparentCircleRadius(604f);//半透明圈
pieChart.setDrawCenterText(false);
pieChart.setDrawHoleEnabled(false);
pieChart.setRotationAngle(90);
pieChart.setRotationEnabled(true);//可以手动旋转
pieChart.setUsePercentValues(true);//显示百分比
pieChart.setData(pieData);
pieChart.setExtraOffsets(18f,2f,20f,2f);
// pieChart.setDragDecelerationFrictionCoef(0.95f);
// pieChart.animateY(2000, Easing.EasingOption.EaseOutQuad);
// pieChart.highlightValue(null);
pieChart.setHighlightPerTapEnabled(true);
Legend legend = pieChart.getLegend();//显示比例图
legend.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT);
legend.setOrientation(Legend.LegendOrientation.VERTICAL);
//
// legend.setPosition(Legend.LegendPosition.RIGHT_OF_CHART_INSIDE);
legend.setEnabled(false);
// legend.setDirection(Legend.LegendDirection.LEFT_TO_RIGHT);
legend.setDrawInside(false);
// legend.setXEntrySpace(7f);
// legend.setYEntrySpace(0f);
pieChart.animateXY(2000,2000);
pieChart.invalidate();
legend.setYOffset(0f);
}
public static PieData initPieChart(PieDataSet value){
// initDataStyle(pieChart);
// PieDataSet pieDataSet=new PieDataSet(value ,"");
value.setSliceSpace(1.0f);
// value.setColors(colors);
value.setSelectionShift(2f);
// PieDataSet dataSet=new PieDataSet();
PieData pieData=new PieData(value);
// value.setSelectionShift(1.0f);
value.setYValuePosition(PieDataSet.ValuePosition.OUTSIDE_SLICE);
// pieDataSet.setValueLinePart1Length(7f);
value.setValueLinePart1Length(0.5f);
value.setValueLinePart1OffsetPercentage(80f);
value.setValueLinePart2Length(0.5f);
pieData.setValueTextSize(12f);
pieData.setValueFormatter(new PercentFormatter());//显示百分比
// pieChart.setData(pieData);
return pieData;
}
}
ArrayList<Integer> colors = new ArrayList<>();
List<PieEntry> values = new ArrayList<>();
for (int i = 0; i < floats.size(); i++) {
values.add(new PieEntry(floats.get(i).getScale(),floats.get(i).getName()));
}
colors.add(getResources().getColor(R.color.pieChart4));
colors.add(getResources().getColor(R.color.buttonColor));
colors.add(getResources().getColor(R.color.pieChart2));
colors.add(getResources().getColor(R.color.pieChart1));
PieDataSet pieDataSet = new PieDataSet(values,"");
pieDataSet.setColors(colors);
PieData pieData = PieChartManager.initPieChart(pieDataSet);
pieCarsAll.getDescription().setEnabled(false);
PieChartManager.initDataStyle(pieCarsAll, pieData);
折线图:
<com.github.mikephil.charting.charts.LineChart
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_weight="1"
android:id="@+id/line1">
</com.github.mikephil.charting.charts.LineChart>
public static void lineChartManager(LineChart lineChart) {
lineChart.setScaleEnabled(false);
Description name = new Description();
name.setText("");
lineChart.setDescription(name);
XAxis xAxis = lineChart.getXAxis();
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
xAxis.setDrawGridLines(true);
xAxis.setEnabled(true);
xAxis.setAvoidFirstLastClipping(true);
YAxis left = lineChart.getAxisLeft();
left.setDrawGridLines(true);
lineChart.getAxisRight().setEnabled(false);
lineChart.animateX(2000);
lineChart.invalidate();
}
private void initData(LineChart lineChart) {
ArrayList<String> xVals = new ArrayList<>();
ArrayList<Entry> yVals = new ArrayList<>();
for (int i = 1; i < 11; i++) {
xVals.add("" + i);
}
for (int i = 0; i < 10; i++) {
yVals.add(new Entry(i, (float) Math.random() * 30));
}
LineDataSet dataSet = new LineDataSet(yVals, "走势图");
dataSet.setDrawCircles(true);
dataSet.setLineWidth(2f);
dataSet.setColor(Color.rgb(244, 117, 117));
LineData lineData = new LineData(dataSet);
lineChart.setData(lineData);
}