使用MPAndroidChartLibery实现分组折线图和柱状图。
一、实现步骤: 1、同样需要 引入mpandroidchartlibrary-2-1-6.jar ;下载地址及介绍见Android中折线图实现方法(各类图表实现) 2、编写实现折线图的封装类:LineChars.java;(完整代码下附) 3、编写实现折线图的封装类:BarChart3s.java;(完整代码下附) 4、在主类中调用上面封装好的类即可。(完整代码下附) 5、MPAndroidChartsLibery 中文文档API地址: 中文文档API
二、完整代码: (1/3)LineCharts.java:
import java.util.ArrayList;
import android.graphics.Color;
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.Legend.LegendForm;
import com.github.mikephil.charting.components.Legend.LegendPosition;
import com.github.mikephil.charting.components.XAxis.XAxisPosition;
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.ValueFormatter;
import com.github.mikephil.charting.utils.ViewPortHandler;
/**
* 折线图,3条
*
* @author zp
*
* 2016年2月2日
*/
public class LineCharts {
/**
* 设置chart显示的样式
*
* @param mLineChart
*/
public LineCharts(LineChart mLineChart) {
// 是否在折线图上添加边框
mLineChart.setDrawBorders(false);
mLineChart.setDescription("");// 数据描述
// 如果没有数据的时候,会显示这个,类似listview的emtpyview
mLineChart.setNoDataTextDescription("暂无数据");
// 是否绘制背景颜色。
// 如果mLineChart.setDrawGridBackground(false),
// 那么mLineChart.setGridBackgroundColor(Color.CYAN)将失效;
mLineChart.setDrawGridBackground(false);
mLineChart.setGridBackgroundColor(Color.CYAN);
// 触摸
mLineChart.setTouchEnabled(true);
// 拖拽
mLineChart.setDragEnabled(true);
// 缩放
mLineChart.setScaleEnabled(true);
mLineChart.setPinchZoom(false);
// 设置背景
mLineChart.setBackgroundColor(Color.WHITE);
// // 设置x,y轴的数据
// mLineChart.setData(lineData);
// 设置比例图标示,就是那个一组y的value的
Legend mLegend = mLineChart.getLegend();
mLegend.setPosition(LegendPosition.BELOW_CHART_CENTER);
mLegend.setForm(LegendForm.CIRCLE);// 样式
mLegend.setFormSize(15.0f);// 字体
mLegend.setTextColor(Color.BLUE);// 颜色
// 沿x轴动画,时间2000毫秒。
mLineChart.animateX(2000);
mLineChart.getAxisRight().setEnabled(true); // 隐藏右边 的坐标轴(true不隐藏)
mLineChart.getXAxis().setPosition(XAxisPosition.BOTTOM); // 让x轴在下面
// // 隐藏左边坐标轴横网格线
// mLineChart.getAxisLeft().setDrawGridLines(false);
// 隐藏右边坐标轴横网格线
mLineChart.getAxisRight().setDrawGridLines(false);
// 隐藏X轴竖网格线
// mLineChart.getXAxis().setDrawGridLines(false);
// enable / disable grid lines
// mLineChart.setDrawVerticalGrid(false); // 是否显示水平的表格线
// mChart.setDrawHorizontalGrid(false);/ 让x轴在下面
}
/**
* @param count
* 数据点的数量。
* @return
*/
public LineData getLineData(int count) {
// ArrayList x = new ArrayList();
// for (int i = 0; i < count; i++) {
// // x轴显示的数据
// x.add("周" + i);
// }
// y轴的数据
ArrayList y_had = new ArrayList();
ArrayList y_wait = new ArrayList();
ArrayList y_yuqi = new Ar