首先在build.gradle 里面添加依赖
compile ‘com.github.PhilJay:MPAndroidChart:v3.0.1’
在外面的build里面添加
allprojects {
repositories {
maven { url “https://jitpack.io” }
}
}
然后在布局里引用想展示的图表类型
柱状图
<com.github.mikephil.charting.charts.BarChart
android:id="@+id/barChart1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="@dimen/margin20"/>
组合图,柱状图加折线图
<com.github.mikephil.charting.charts.CombinedChart
android:id="@+id/combineChart"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="@dimen/margin20"
>
com.github.mikephil.charting.charts.CombinedChart>
调用所需的方法,进行传值
package com.sanbanhui.helper.widget;
import android.graphics.Color;
import android.support.v4.content.ContextCompat;
import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.charts.CombinedChart;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.data.CombinedData;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;
import com.github.mikephil.charting.formatter.IValueFormatter;
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;
import com.github.mikephil.charting.utils.ViewPortHandler;
import com.sanbanhui.helper.R;
import com.sanbanhui.helper.utils.StringUtils;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* Created by liugang on 2017/3/31.
*/
public class MPChartsHelper {
public static final int[] PIE_COLORS = {
Color.rgb(181, 194, 202), Color.rgb(129, 216, 200), Color.rgb(241, 214, 145),
Color.rgb(108, 176, 223), Color.rgb(195, 221, 155), Color.rgb(251, 215, 191),
Color.rgb(237, 189, 189), Color.rgb(172, 217, 243)
};
public static final int[] LINE_COLORS = {
Color.rgb(140, 210, 118), Color.rgb(159, 143, 186), Color.rgb(233, 197, 23)
};//绿色,紫色,黄色
public static final int[] LINE_FILL_COLORS = {
Color.rgb(222, 239, 228), Color.rgb(246, 234, 208), Color.rgb(235, 228, 248)
};
/**
* 单数据集。设置柱状图样式,X轴为字符串,Y轴为数值
*
* @param barChart
* @param xAxisValue
* @param yAxisValue
* @param title 图例文字
* @param xAxisTextSize x轴标签字体大小
* @param barColor
*/
public static void setBarChart(BarChart barChart, List xAxisValue, List yAxisValue, String title, float xAxisTextSize, Integer barColor) {
barChart.getDescription().setEnabled(false);//设置描述
barChart.setPinchZoom(true);//设置按比例放缩柱状图
//设置自定义的markerView
MPChartMarkerView markerView = new MPChartMarkerView(barChart.getContext(), R.layout.custom_marker_view);
barChart.setMarker(markerView);
barChart.setDrawGridBackground(true); // 是否显示表格颜色
barChart.setGridBackgroundColor(Color.WHITE); // 表格的的颜色
barChart.setDrawBorders(true);//是否设置边框
barChart.setBorderColor(Color.parseColor("#DDDDDD"));//设置边框的颜色
barChart.setDrawValueAboveBar(false);//是否将值显示在外面
//x坐标轴设置
IAxisValueFormatter xAxisFormatter = new StringAxisValueFormatter(xAxisValue);//设置自定义的x轴值格式化器
XAxis xAxis = barChart.getXAxis();//获取x轴
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);//设置X轴标签显示位置
xAxis.setDrawGridLines(false);//不绘制格网线
xAxis.setGranularity(1f);//设置最小间隔,防止当放大时,出现重复标签。
xAxis.setValueFormatter(xAxisFormatter);
xAxis.setTextSize(xAxisTextSize);//设置标签字体大小
xAxis.setLabelCount(xAxisValue.size());//设置标签显示的个数
xAxis.setLabelRotationAngle(-40);//设置X轴字体的倾斜度
xAxis.setYOffset(10);
//y轴设置
YAxis leftAxis = barChart.getAxisLeft();//获取左侧y轴
leftAxis.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART);//设置y轴标签显示在外侧
//leftAxis.setAxisMinimum(0f);//设置Y轴最小值
leftAxis.setDrawGridLines(true);//是否在图表上显示数值
//leftAxis.setDrawLabels(false);//禁止绘制y轴标签
//leftAxis.setDrawAxisLine(false);//禁止绘制y轴
Float yMin = Double.valueOf(Collections.min(yAxisValue) * 1.1).floatValue();
Float yMax = Double.valueOf(Collections.max(yAxisValue) * 1.3).floatValue();
leftAxis.setAxisMaximum(yMax);
leftAxis.setAxisMinimum(yMin);
barChart.getAxisRight().setEnabled(false);//禁用右侧y轴
//图例设置
Legend legend = barChart.getLegend();
//图例设置
legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER);
legend.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM);
legend.setOrientation(Legend.LegendOrientation.HORIZONTAL);//图例位置
legend.setDrawInside(true);//设置图例是否在图表里面
legend.setDirection(Legend.LegendDirection.LEFT_TO_RIGHT);
legend.setForm(Legend.LegendForm.SQUARE);//图例的样式
legend.setTextSize(12f);//图例字体的大小
//设置柱状图数据
setBarChartData(barChart, yAxisValue, title, barColor);
barChart.setExtraBottomOffset(30);//距视图窗口底部的偏移,类似与paddingbottom
barChart.setExtraTopOffset(10);//距视图窗口顶部的偏移,类似与paddingtop
barChart.setFitBars(true);//使两侧的柱图完全显示
barChart.animateY(1000);//数据显示动画,从左往右依次显示
barChart.setTouchEnabled(false);
}
/**
* 设置柱图
*
* @param barChart
* @param yAxisValue
* @param title
* @param barColor
*/
private static void setBarChartData(BarChart barChart, List yAxisValue, String title, Integer barColor) {
ArrayList entries = new ArrayList<>();
for (int i = 0, n = yAxisValue.size(); i < n; ++i) {
entries.add(new BarEntry(i, yAxisValue.get(i)));
}
BarDataSet set1;
if (barChart.getData() != null && barChart.getData().getDataSetCount() > 0) {
set1 = (BarDataSet) barChart.getData().getDataSetByIndex(0);
set1.setValues(entries);
barChart.getData().notifyDataChanged();
barChart.notifyDataSetChanged();
} else {
set1 = new BarDataSet(entries, title);
set1.setAxisDependency(YAxis.AxisDependency.LEFT);//设置Y轴的位置
set1.setDrawValues(true);//不绘制线的数据
set1.setValueTextColor(Color.parseColor("#C0B8F2"));
//set1. setValueTextColors(List colors)//可设置多种颜色
if (barColor == null) {
set1.setColor(ContextCompat.getColor(barChart.getContext(), R.color.dark_blue));//设置set1的柱的颜色
} else {
set1.setColor(barColor);
}
ArrayList dataSets = new ArrayList<>();
dataSets.add(set1);
BarData data = new BarData(dataSets);
data.setValueTextSize(10f);
data.setBarWidth(0.9f);
data.setValueFormatter(new MyValueFormatter());//自定义Y轴样式
barChart.setData(data);
}
}
/**
* 设置柱线组合图样式,柱图依赖左侧y轴,线图依赖右侧y轴
*/
public static void setCombineChart(CombinedChart combineChart, final List xAxisValues, List lineValues, List barValues, List bar2Values, String lineTitle, String barTitle, String bar2Title) {
combineChart.getDescription().setEnabled(false);//设置描述
combineChart.setPinchZoom(true);//设置按比例放缩柱状图
MPChartMarkerView markerView = new MPChartMarkerView(combineChart.getContext(), R.layout.custom_marker_view);
combineChart.setMarker(markerView);
combineChart.setDrawGridBackground(true); // 是否显示表格颜色
combineChart.setGridBackgroundColor(Color.WHITE); // 表格的的颜色
combineChart.setDrawBorders(true);
combineChart.setBorderColor(Color.parseColor("#DDDDDD"));
//设置绘制顺序,让线在柱的上层
combineChart.setDrawOrder(new CombinedChart.DrawOrder[]{
CombinedChart.DrawOrder.BAR,CombinedChart.DrawOrder.BAR, CombinedChart.DrawOrder.LINE
});
//x坐标轴设置
XAxis xAxis = combineChart.getXAxis();
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
xAxis.setDrawGridLines(false);
xAxis.setLabelRotationAngle(-40);
xAxis.setGranularity(1f);
xAxis.setYOffset(20);
xAxis.setLabelCount(xAxisValues.size() + 2);
xAxis.setValueFormatter(new IAxisValueFormatter() {
@Override
public String getFormattedValue(float v, AxisBase axisBase) {
if (v < 0 || v > (xAxisValues.size() - 1))//使得两侧柱子完全显示
return "";
return xAxisValues.get((int) v);
}
});
//y轴设置
YAxis leftAxis = combineChart.getAxisLeft();
leftAxis.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART);
leftAxis.setDrawGridLines(true);
/*leftAxis.setAxisMinimum(0f);*/
leftAxis.setStartAtZero(true);
Float yMin = Double.valueOf(Collections.min(barValues) * 0.9).floatValue();
Float yMax = Double.valueOf(Collections.max(barValues) * 1.1).floatValue();
leftAxis.setAxisMaximum(yMax);
leftAxis.setAxisMinimum(yMin);
YAxis rightAxis = combineChart.getAxisRight();
rightAxis.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART);
rightAxis.setDrawGridLines(false);
rightAxis.setAxisMinimum(0f);
rightAxis.setAxisMaximum(100f);
rightAxis.setValueFormatter(new MyRightYAxisValueFormatter());
//图例设置
Legend legend = combineChart.getLegend();
legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER);
legend.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM);
legend.setOrientation(Legend.LegendOrientation.HORIZONTAL);
legend.setDrawInside(true);
legend.setDirection(Legend.LegendDirection.LEFT_TO_RIGHT);
legend.setForm(Legend.LegendForm.SQUARE);
legend.setTextSize(12f);
//自动换行
legend.setWordWrapEnabled(true);
//设置组合图数据
CombinedData data = new CombinedData();
data.setData(generateLineData(lineValues, lineTitle));
data.setData(generateBarData(barValues,bar2Values, barTitle,bar2Title));
//data.setData(generate2BarData(bar2Values, bar2Title));
combineChart.setData(data);//设置组合图数据源
//使得两侧柱子完全显示
xAxis.setAxisMinimum(combineChart.getCombinedData().getXMin() - 1f);
xAxis.setAxisMaximum(combineChart.getCombinedData().getXMax() + 1f);
combineChart.setExtraTopOffset(10);
combineChart.setExtraBottomOffset(30);
combineChart.animateY(1000);//数据显示动画,从左往右依次显示
combineChart.invalidate();
combineChart.setTouchEnabled(false);
}
/**
* 生成线图数据
*/
private static LineData generateLineData(List lineValues, String lineTitle) {
ArrayList lineEntries = new ArrayList<>();
for (int i = 0, n = lineValues.size(); i < n; ++i) {
lineEntries.add(new Entry(i, lineValues.get(i)));
}
LineDataSet lineDataSet = new LineDataSet(lineEntries, lineTitle);
lineDataSet.setColor(Color.parseColor("#FFC12D"));
lineDataSet.setLineWidth(1.5f);//设置线的宽度
lineDataSet.setCircleColor(Color.rgb(244, 219, 100));//设置圆圈的颜色
lineDataSet.setCircleColorHole(Color.parseColor("#FFC12D"));//设置圆圈内部洞的颜色
//lineDataSet.setValueTextColor(Color.rgb(254,116,139));
lineDataSet.setAxisDependency(YAxis.AxisDependency.RIGHT);//设置线数据依赖于右侧y轴
lineDataSet.setDrawValues(false);//不绘制线的数据
LineData lineData = new LineData(lineDataSet);
lineData.setValueTextSize(10f);
lineData.setValueFormatter(new IValueFormatter() {
@Override
public String getFormattedValue(float value, Entry entry, int i, ViewPortHandler viewPortHandler) {
return StringUtils.double2String(value, 2);
}
});
return lineData;
}
/**
* 生成柱图数据
*
* @param barValues
* @param bar2Values
*@param bar2Title @return
*/
private static BarData generateBarData(List barValues, List bar2Values, String barTitle, String bar2Title) {
ArrayList barEntries = new ArrayList<>();
ArrayList bar2Entries = new ArrayList<>();
for (int i = 0, n = barValues.size(); i < n; ++i) {
barEntries.add(new BarEntry(i, barValues.get(i)));
}
for (int i = 0, n = bar2Values.size(); i < n; ++i) {
bar2Entries.add(new BarEntry(i, bar2Values.get(i)));
}
BarDataSet barDataSet = new BarDataSet(barEntries, barTitle);
barDataSet.setColor(Color.parseColor("#C0B8F2"));
barDataSet.setValueTextColor(Color.rgb(159, 143, 186));
barDataSet.setAxisDependency(YAxis.AxisDependency.LEFT);
barDataSet.setDrawValues(false);//不绘制线的数据
BarDataSet bar2DataSet = new BarDataSet(bar2Entries, bar2Title);
bar2DataSet.setColor(Color.parseColor("#A09BE7"));
bar2DataSet.setValueTextColor(Color.rgb(159, 143, 186));
bar2DataSet.setAxisDependency(YAxis.AxisDependency.LEFT);
bar2DataSet.setDrawValues(false);//不绘制线的数据
BarData barData = new BarData(barDataSet);
barData.addDataSet(bar2DataSet);
barData.setValueTextSize(10f);
barData.setBarWidth(0.9f);
barData.setValueFormatter(new IValueFormatter() {
@Override
public String getFormattedValue(float value, Entry entry, int i, ViewPortHandler viewPortHandler) {
return StringUtils.double2String(value, 2);
}
});
return barData;
}
/**
* 设置柱线组合图样式,柱图依赖左侧y轴,线图依赖右侧y轴
*/
public static void setLineBarChart(CombinedChart combineChart, final List xAxisValues, List lineValues, List barValues, String lineTitle, String barTitle) {
combineChart.getDescription().setEnabled(false);//设置描述
combineChart.setPinchZoom(true);//设置按比例放缩柱状图
MPChartMarkerView markerView = new MPChartMarkerView(combineChart.getContext(), R.layout.custom_marker_view);
combineChart.setMarker(markerView);
combineChart.setDrawGridBackground(true); // 是否显示表格颜色
combineChart.setGridBackgroundColor(Color.WHITE); // 表格的的颜色
combineChart.setDrawBorders(true);
combineChart.setBorderColor(Color.parseColor("#DDDDDD"));
combineChart.setDrawValueAboveBar(true);//是否将值显示在外面
//设置绘制顺序,让线在柱的上层
combineChart.setDrawOrder(new CombinedChart.DrawOrder[]{
CombinedChart.DrawOrder.BAR, CombinedChart.DrawOrder.LINE
});
//x坐标轴设置
XAxis xAxis = combineChart.getXAxis();
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
xAxis.setDrawGridLines(false);
xAxis.setLabelRotationAngle(-40);
xAxis.setGranularity(1f);
xAxis.setYOffset(20);
xAxis.setLabelCount(xAxisValues.size() + 2);
xAxis.setValueFormatter(new IAxisValueFormatter() {
@Override
public String getFormattedValue(float v, AxisBase axisBase) {
if (v < 0 || v > (xAxisValues.size() - 1))//使得两侧柱子完全显示
return "";
return xAxisValues.get((int) v);
}
});
//y轴设置
YAxis leftAxis = combineChart.getAxisLeft();
leftAxis.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART);
leftAxis.setDrawGridLines(true);
/*leftAxis.setAxisMinimum(0f);*/
leftAxis.setStartAtZero(true);
Float yMin = Double.valueOf(Collections.min(barValues) * 0.9).floatValue();
Float yMax = Double.valueOf(Collections.max(barValues) * 1.1).floatValue();
leftAxis.setAxisMaximum(yMax);
leftAxis.setAxisMinimum(yMin);
YAxis rightAxis = combineChart.getAxisRight();
rightAxis.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART);
rightAxis.setDrawGridLines(false);
rightAxis.setValueFormatter(new MyRightYAxisValueFormatter());
rightAxis.setAxisMinimum(0f);
rightAxis.setAxisMaximum(100f);
//图例设置
Legend legend = combineChart.getLegend();
legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER);
legend.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM);
legend.setOrientation(Legend.LegendOrientation.HORIZONTAL);
legend.setDrawInside(true);
legend.setDirection(Legend.LegendDirection.LEFT_TO_RIGHT);
legend.setForm(Legend.LegendForm.SQUARE);
legend.setTextSize(12f);
//自动换行
legend.setWordWrapEnabled(true);
//设置组合图数据
CombinedData data = new CombinedData();
data.setData(getLineData(lineValues, lineTitle));
data.setData(getBarData(barValues, barTitle));
//data.setData(generate2BarData(bar2Values, bar2Title));
combineChart.setData(data);//设置组合图数据源
//使得两侧柱子完全显示
xAxis.setAxisMinimum(combineChart.getCombinedData().getXMin() - 1f);
xAxis.setAxisMaximum(combineChart.getCombinedData().getXMax() + 1f);
combineChart.setExtraTopOffset(10);
combineChart.setExtraBottomOffset(30);
combineChart.animateY(1500);//数据显示动画,从左往右依次显示
combineChart.invalidate();
combineChart.setTouchEnabled(false);
}
/**
* 生成线图数据
*/
private static LineData getLineData(List lineValues, String lineTitle) {
ArrayList lineEntries = new ArrayList<>();
for (int i = 0, n = lineValues.size(); i < n; ++i) {
lineEntries.add(new Entry(i, lineValues.get(i)));
}
LineDataSet lineDataSet = new LineDataSet(lineEntries, lineTitle);
lineDataSet.setColor(Color.parseColor("#FFC12D"));
lineDataSet.setLineWidth(2.5f);//设置线的宽度
lineDataSet.setCircleColor(Color.rgb(244, 219, 100));//设置圆圈的颜色
lineDataSet.setCircleColorHole(Color.WHITE);//设置圆圈内部洞的颜色
//lineDataSet.setValueTextColor(Color.rgb(254,116,139));
lineDataSet.setAxisDependency(YAxis.AxisDependency.RIGHT);//设置线数据依赖于右侧y轴
lineDataSet.setDrawValues(false);//不绘制线的数据
LineData lineData = new LineData(lineDataSet);
lineData.setValueTextSize(10f);
lineData.setValueFormatter(new IValueFormatter() {
@Override
public String getFormattedValue(float value, Entry entry, int i, ViewPortHandler viewPortHandler) {
return StringUtils.double2String(value, 2);
}
});
return lineData;
}
/**
* 生成柱图数据
*
* @param barValues
*/
private static BarData getBarData(List barValues, String barTitle) {
ArrayList barEntries = new ArrayList<>();
for (int i = 0, n = barValues.size(); i < n; ++i) {
barEntries.add(new BarEntry(i, barValues.get(i)));
}
BarDataSet barDataSet = new BarDataSet(barEntries, barTitle);
barDataSet.setColor(Color.parseColor("#A09BE7"));
barDataSet.setValueTextColor(Color.parseColor("#C0B8F2"));
barDataSet.setAxisDependency(YAxis.AxisDependency.LEFT);
barDataSet.setDrawValues(true);//不绘制线的数据
BarData barData = new BarData(barDataSet);
barData.setValueTextSize(10f);
barData.setBarWidth(0.9f);
barData.setValueFormatter(new IValueFormatter() {
@Override
public String getFormattedValue(float value, Entry entry, int i, ViewPortHandler viewPortHandler) {
return StringUtils.double2String(value, 2);
}
});
return barData;
}
}