参考数据:
https://github.com/PhilJay/MPAndroidChart
引用库
implementation 'com.github.PhilJay:MPAndroidChart:v3.1.0'
柱状类
import android.graphics.Color;
import com.github.mikephil.charting.animation.Easing;
import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.components.Description;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.LimitLine;
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.formatter.ValueFormatter;
import java.util.ArrayList;
import java.util.List;
public class BarCharDkey {
private BarChart barChart;
private YAxis leftAxis; //左侧Y轴
private YAxis rightAxis; //右侧Y轴
private XAxis xAxis; //X轴
private Legend legend; //图例
private LimitLine limitLine; //限制线
protected final String[] months = new String[] {
"1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"
};
public BarCharDkey(BarChart chart) {
barChart = chart;
//背景颜色
barChart.setBackgroundColor(Color.TRANSPARENT);
//不显示图表网格
barChart.setDrawGridBackground(false);
//设置图表描述信息
Description description = new Description();
description.setText("");
barChart.getDescription().setEnabled(false);
barChart.setDescription(description);
//设置 chart 边框线的颜色。
barChart.setBorderColor(Color.TRANSPARENT);
//禁止缩放
barChart.setScaleEnabled(false);
//背景阴影
barChart.setDrawBarShadow(false);
barChart.setHighlightFullBarEnabled(false);
//显示边框
barChart.setDrawBorders(true);
//设置动画效果
barChart.animateY(1000, Easing.Linear);
barChart.animateX(1000, Easing.Linear);
//X轴设置显示位置在底部
xAxis = barChart.getXAxis();
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
//单个时可微调-0.5f
xAxis.setAxisMinimum(-0.5f);
xAxis.setGranularity(0f);
xAxis.setTextColor(Color.WHITE);
xAxis.setTextSize(14.0f);
//隐藏X轴竖网格线
xAxis.setDrawGridLines(false);
xAxis.setDrawAxisLine(true);
xAxis.setAxisLineColor(Color.WHITE);
xAxis.setGridColor(Color.TRANSPARENT);
xAxis.setValueFormatter(new ValueFormatter() {
@Override
public String getFormattedValue(float value) {
return months[(int) value % months.length];
}
});
leftAxis = barChart.getAxisLeft();
rightAxis = barChart.getAxisRight();
//保证Y轴从0开始,不然会上移一点
leftAxis.setAxisMinimum(0f);
rightAxis.setAxisMinimum(0f);
//边线颜色
rightAxis.setAxisLineColor(Color.WHITE);
leftAxis.setAxisLineColor(Color.WHITE);
rightAxis.setTextColor(Color.WHITE);
leftAxis.setTextColor(Color.WHITE);
leftAxis.setTextSize(14.0f);
rightAxis.setTextSize(14.0f);
//隐藏左边坐标轴横网格线
leftAxis.setDrawGridLines(false);
//隐藏右边坐标轴横网格线
rightAxis.setDrawGridLines(false);
//隐藏右边数据
rightAxis.setEnabled(false);
legend = barChart.getLegend();
legend.setForm(Legend.LegendForm.SQUARE);
legend.setFormSize(16.0f);
legend.setTextSize(16.0f);
legend.setTextColor(Color.WHITE);
//设置X轴上条目的间隔,默认为6dp
legend.setXEntrySpace(20);
//显示位置
legend.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT);
legend.setOrientation(Legend.LegendOrientation.HORIZONTAL);
//是否绘制在图表里面
legend.setDrawInside(false);
}
/**
* 单个柱状加载数据
*
* @param xAxisValues
* @param yAxisValues
* @param label
* @param color
*/
public void showBarChart(List xAxisValues, List yAxisValues, String label, int color) {
//将数据源添加到图标
ArrayList entries = new ArrayList<>();
for (int i = 0; i < xAxisValues.size(); i++) {
entries.add(new BarEntry(xAxisValues.get(i), yAxisValues.get(i)));
}
BarDataSet barDataSet = new BarDataSet(entries, label);
//设置柱状图颜色
barDataSet.setColor(color);
//设置数值字体大小
barDataSet.setValueTextSize(16f);
barDataSet.setValueTextColor(Color.WHITE);
//线条宽度
barDataSet.setFormLineWidth(1f);
//图例窗体的大小
// barDataSet.setFormSize(15.f);
ArrayList dataSets = new ArrayList<>();
dataSets.add(barDataSet);
BarData data = new BarData(dataSets);
//柱状宽度
data.setBarWidth(0.5f);
//设置X轴的刻度数
xAxis.setLabelCount(entries.size() - 1, false);
barChart.setData(data);
}
/**
* 多个柱状加载数据
* @param xAxisValues
* @param yAxisValues
* @param label
* @param color
* @param type 0手机;1电脑
*/
public void showBarChart(List xAxisValues, List> yAxisValues, List label, List color,int type) {
ArrayList dataSets = new ArrayList<>();
BarData data = new BarData();
for (int i = 0; i < yAxisValues.size(); i++) {
ArrayList entries = new ArrayList<>();
for (int j = 0; j < yAxisValues.get(i).size(); j++) {
entries.add(new BarEntry(xAxisValues.get(j), yAxisValues.get(i).get(j)));
}
BarDataSet barDataSet = new BarDataSet(entries, label.get(i));
//设置柱状图颜色
barDataSet.setColor(color.get(i));
//设置数值字体大小
barDataSet.setValueTextSize(18f);
barDataSet.setValueTextColor(Color.WHITE);
//线条宽度
barDataSet.setFormLineWidth(2f);
//格式化x轴标签显示字符
if (type == 0) {
if (i == 0) {
barDataSet.setValueFormatter(new ValueFormatter() {
@Override
public String getFormattedValue(float value) {
return String.valueOf((int)value);
}
});
} else if (i == 1) {
barDataSet.setValueFormatter(new ValueFormatter() {
@Override
public String getFormattedValue(float value) {
if (value == 0) {
return "0";
} else if ((value - (int) value) > 0) {
return value + "h";
} else {
return (int) value + "h";
}
}
});
}
} else if (type == 1) {
barDataSet.setValueFormatter(new ValueFormatter() {
@Override
public String getFormattedValue(float value) {
if (value == 0) {
return "0";
} else if ((value - (int) value) > 0) {
return value + "h";
} else {
return (int) value + "h";
}
}
});
}
data.addDataSet(barDataSet);
}
int amount = yAxisValues.size();
//柱状图组之间的间距
float groupSpace = 0.2f;
float barSpace = 0.00f;
float barWidth = 0.4f;
//设置x轴显示标签数量
// (0.45 + 0.02) * 2 + 0.06 = 1.00 -> interval per "group"
xAxis.setLabelCount(xAxisValues.size() - 1, false);
data.setBarWidth(barWidth);
data.groupBars(-0.5f, groupSpace, barSpace);
// 添加到图表中
barChart.setData(data);
}
}
线性类
import android.graphics.Color;
import com.github.mikephil.charting.animation.Easing;
import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.Description;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.LimitLine;
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.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.formatter.IValueFormatter;
import com.github.mikephil.charting.formatter.ValueFormatter;
import com.github.mikephil.charting.utils.ViewPortHandler;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
public class LineChartDkey {
private LineChart mLineChart;
private YAxis leftAxis; //左侧Y轴
private YAxis rightAxis; //右侧Y轴
private XAxis xAxis; //X轴
private Legend legend; //图例
private LimitLine limitLine; //限制线
public static String[] days = null;
public LineChartDkey(LineChart chart) {
mLineChart = chart;
//背景颜色
mLineChart.setBackgroundColor(Color.TRANSPARENT);
//不显示图表网格
mLineChart.setDrawGridBackground(false);
mLineChart.setNoDataText("");
//设置图表描述信息
Description description = new Description();
description.setText("");
mLineChart.getDescription().setEnabled(false);
mLineChart.setDescription(description);
//设置 chart 边框线的颜色。
mLineChart.setBorderColor(Color.TRANSPARENT);
//禁止缩放
mLineChart.setScaleEnabled(false);
//显示边框
mLineChart.setDrawBorders(true);
//设置动画效果
mLineChart.animateY(1000, Easing.Linear);
mLineChart.animateX(1000, Easing.Linear);
//X轴设置显示位置在底部
xAxis = mLineChart.getXAxis();
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
//单个时可微调-0.5f
xAxis.setAxisMinimum(0f);
xAxis.setGranularity(0f);
xAxis.setTextColor(Color.WHITE);
xAxis.setTextSize(14.0f);
//隐藏X轴竖网格线
xAxis.setDrawGridLines(false);
xAxis.setDrawAxisLine(true);
xAxis.setAxisLineColor(Color.WHITE);
xAxis.setGridColor(Color.TRANSPARENT);
xAxis.setValueFormatter(new ValueFormatter() {
@Override
public String getFormattedValue(float value) {
return days[(int) value % days.length];
}
});
leftAxis = mLineChart.getAxisLeft();
rightAxis = mLineChart.getAxisRight();
//保证Y轴从0开始,不然会上移一点
leftAxis.setAxisMinimum(0f);
rightAxis.setAxisMinimum(0f);
//边线颜色
rightAxis.setAxisLineColor(Color.WHITE);
leftAxis.setAxisLineColor(Color.WHITE);
rightAxis.setTextColor(Color.WHITE);
leftAxis.setTextColor(Color.WHITE);
leftAxis.setTextSize(14.0f);
rightAxis.setTextSize(14.0f);
//隐藏左边坐标轴横网格线
leftAxis.setDrawGridLines(false);
//隐藏右边坐标轴横网格线
rightAxis.setDrawGridLines(false);
//隐藏右边数据
rightAxis.setEnabled(false);
legend = mLineChart.getLegend();
legend.setForm(Legend.LegendForm.SQUARE);
legend.setFormSize(16.0f);
legend.setTextSize(16.0f);
legend.setTextColor(Color.WHITE);
//设置X轴上条目的间隔,默认为6dp
legend.setXEntrySpace(20);
//显示位置
legend.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT);
legend.setOrientation(Legend.LegendOrientation.HORIZONTAL);
//是否绘制在图表里面
legend.setDrawInside(false);
}
/**
* 单个柱状加载数据
*
* @param xAxisValues
* @param yAxisValues
* @param label
* @param color
*/
public void showLineChart(List xAxisValues, List yAxisValues, String label, int color) {
//将数据源添加到图标
ArrayList entries = new ArrayList<>();
for (int i = 0; i < xAxisValues.size(); i++) {
entries.add(new BarEntry(xAxisValues.get(i), yAxisValues.get(i)));
}
LineDataSet barDataSet = new LineDataSet(entries, label);
//设置柱状图颜色
barDataSet.setColor(color);
//设置数值字体大小
barDataSet.setValueTextSize(16f);
barDataSet.setValueTextColor(Color.RED);
//线条宽度
barDataSet.setFormLineWidth(1f);
//图例窗体的大小
// barDataSet.setFormSize(15.f);
ArrayList dataSets = new ArrayList<>();
dataSets.add(barDataSet);
LineData data = new LineData(dataSets);
//设置X轴的刻度数
xAxis.setLabelCount(entries.size() - 1, false);
mLineChart.setData(data);
}
/**
* 多个线性加载数据
* @param xAxisValues
* @param yAxisValues
* @param label
* @param color
* @param type 0手机;1电脑
*/
public void showLineChart(List xAxisValues, List> yAxisValues, List label, List color,int type) {
ArrayList dataSets = new ArrayList<>();
LineData data = new LineData();
for (int i = 0; i < yAxisValues.size(); i++) {
ArrayList entries = new ArrayList<>();
for (int j = 0; j < yAxisValues.get(i).size(); j++) {
entries.add(new BarEntry(xAxisValues.get(j), yAxisValues.get(i).get(j)));
}
LineDataSet barDataSet = new LineDataSet(entries, label.get(i));
//设置柱状图颜色
barDataSet.setColor(color.get(i));
//设置数值字体大小
barDataSet.setValueTextSize(20f);
barDataSet.setValueTextColor(Color.WHITE);
//线条宽度
barDataSet.setFormLineWidth(2f);
//设置使用 范围背景填充
barDataSet.setDrawFilled(true);
//设置曲线值的圆点是实心还是空心
barDataSet.setDrawCircleHole(true);
barDataSet.setCircleColor(color.get(i));
//设置焦点圆心的大小
barDataSet.setCircleRadius(4f);
格式化x轴标签显示字符
if (type == 0) {
if (i == 0) {
barDataSet.setValueFormatter(new ValueFormatter() {
@Override
public String getFormattedValue(float value) {
return String.valueOf((int)value);
}
});
} else if (i == 1) {
barDataSet.setValueFormatter(new ValueFormatter() {
@Override
public String getFormattedValue(float value) {
if (value == 0) {
return "0";
} else if ((value - (int) value) > 0) {
return value + "h";
} else {
return (int) value + "h";
}
}
});
}
} else if (type == 1) {
barDataSet.setValueFormatter(new ValueFormatter() {
@Override
public String getFormattedValue(float value) {
if (value == 0) {
return "0";
} else if ((value - (int) value) > 0) {
return value + "h";
} else {
return (int) value + "h";
}
}
});
}
data.addDataSet(barDataSet);
}
//设置x轴显示标签数量
xAxis.setLabelCount(xAxisValues.size() - 1, false);
// 添加到图表中
mLineChart.setData(data);
}
}
横向柱状图
import android.graphics.Color;
import com.github.mikephil.charting.animation.Easing;
import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.components.Description;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.LimitLine;
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.formatter.ValueFormatter;
import java.util.ArrayList;
import java.util.List;
public class HbarCharDkey {
private BarChart barChart;
private YAxis leftAxis; //左侧Y轴
private YAxis rightAxis; //右侧Y轴
private XAxis xAxis; //X轴
private Legend legend; //图例
private LimitLine limitLine; //限制线
public String[] months = null;
public HbarCharDkey(BarChart chart) {
barChart = chart;
//背景颜色
barChart.setBackgroundColor(Color.TRANSPARENT);
//不显示图表网格
barChart.setDrawGridBackground(false);
//设置图表描述信息
Description description = new Description();
description.setText("");
barChart.getDescription().setEnabled(false);
barChart.setDescription(description);
//设置 chart 边框线的颜色。
barChart.setBorderColor(Color.TRANSPARENT);
//禁止缩放
barChart.setScaleEnabled(false);
//背景阴影
barChart.setDrawBarShadow(false);
barChart.setHighlightFullBarEnabled(false);
//显示边框
barChart.setDrawBorders(true);
//设置动画效果
barChart.animateY(1000, Easing.Linear);
barChart.animateX(1000, Easing.Linear);
//X轴设置显示位置在底部
xAxis = barChart.getXAxis();
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
//单个时可微调-0.5f
xAxis.setAxisMinimum(-0.5f);
xAxis.setGranularity(0f);
xAxis.setTextColor(Color.WHITE);
xAxis.setTextSize(14.0f);
//隐藏X轴竖网格线
xAxis.setDrawGridLines(false);
xAxis.setDrawAxisLine(true);
xAxis.setAxisLineColor(Color.WHITE);
xAxis.setGridColor(Color.TRANSPARENT);
xAxis.setValueFormatter(new ValueFormatter() {
@Override
public String getFormattedValue(float value) {
return months[(int) value % months.length];
}
});
leftAxis = barChart.getAxisLeft();
rightAxis = barChart.getAxisRight();
//保证Y轴从0开始,不然会上移一点
leftAxis.setAxisMinimum(0f);
rightAxis.setAxisMinimum(0f);
//边线颜色
rightAxis.setAxisLineColor(Color.WHITE);
leftAxis.setAxisLineColor(Color.WHITE);
rightAxis.setTextColor(Color.WHITE);
leftAxis.setTextColor(Color.WHITE);
leftAxis.setTextSize(14.0f);
rightAxis.setTextSize(14.0f);
//隐藏左边坐标轴横网格线
leftAxis.setDrawGridLines(false);
//隐藏右边坐标轴横网格线
rightAxis.setDrawGridLines(false);
//隐藏右边数据
rightAxis.setEnabled(false);
legend = barChart.getLegend();
legend.setForm(Legend.LegendForm.SQUARE);
legend.setFormSize(16.0f);
legend.setTextSize(16.0f);
legend.setTextColor(Color.WHITE);
//设置X轴上条目的间隔,默认为6dp
legend.setXEntrySpace(20);
//显示位置
legend.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT);
legend.setOrientation(Legend.LegendOrientation.HORIZONTAL);
//是否绘制在图表里面
legend.setDrawInside(false);
legend.setEnabled(false);
}
/**
* 单个柱状加载数据
*
* @param xAxisValues
* @param yAxisValues
* @param label
* @param color
*/
public void showBarChart(List xAxisValues, List yAxisValues, String label, int color) {
//将数据源添加到图标
ArrayList entries = new ArrayList<>();
for (int i = 0; i < xAxisValues.size(); i++) {
entries.add(new BarEntry(xAxisValues.get(i), yAxisValues.get(i)));
}
BarDataSet barDataSet = new BarDataSet(entries, label);
//设置柱状图颜色
barDataSet.setColor(color);
//设置数值字体大小
barDataSet.setValueTextSize(16f);
barDataSet.setValueTextColor(Color.WHITE);
//线条宽度
barDataSet.setFormLineWidth(1f);
barDataSet.setValueFormatter(new ValueFormatter() {
@Override
public String getFormattedValue(float value) {
if (value == 0) {
return "0";
} else if ((value - (int) value) > 0) {
return value + "h";
} else {
return (int) value + "h";
}
}
});
//图例窗体的大小
// barDataSet.setFormSize(15.f);
ArrayList dataSets = new ArrayList<>();
dataSets.add(barDataSet);
BarData data = new BarData(dataSets);
//柱状宽度
data.setBarWidth(0.5f);
//设置X轴的刻度数
xAxis.setLabelCount(entries.size() - 1, false);
barChart.setData(data);
}
/**
* 多个柱状加载数据
*
* @param xAxisValues
* @param yAxisValues
* @param label
* @param color
* @param type 0手机;1电脑
*/
public void showBarChart(List xAxisValues, List> yAxisValues, List label, List color, int type) {
ArrayList dataSets = new ArrayList<>();
BarData data = new BarData();
for (int i = 0; i < yAxisValues.size(); i++) {
ArrayList entries = new ArrayList<>();
for (int j = 0; j < yAxisValues.get(i).size(); j++) {
entries.add(new BarEntry(xAxisValues.get(j), yAxisValues.get(i).get(j)));
}
BarDataSet barDataSet = new BarDataSet(entries, label.get(i));
//设置柱状图颜色
barDataSet.setColor(color.get(i));
//设置数值字体大小
barDataSet.setValueTextSize(18f);
barDataSet.setValueTextColor(Color.WHITE);
//线条宽度
barDataSet.setFormLineWidth(2f);
//格式化x轴标签显示字符
if (type == 0) {
if (i == 0) {
barDataSet.setValueFormatter(new ValueFormatter() {
@Override
public String getFormattedValue(float value) {
return String.valueOf((int) value);
}
});
} else if (i == 1) {
barDataSet.setValueFormatter(new ValueFormatter() {
@Override
public String getFormattedValue(float value) {
if (value == 0) {
return "0";
} else if ((value - (int) value) > 0) {
return value + "h";
} else {
return (int) value + "h";
}
}
});
}
} else if (type == 1) {
barDataSet.setValueFormatter(new ValueFormatter() {
@Override
public String getFormattedValue(float value) {
if (value == 0) {
return "0";
} else if ((value - (int) value) > 0) {
return value + "h";
} else {
return (int) value + "h";
}
}
});
}
data.addDataSet(barDataSet);
}
int amount = yAxisValues.size();
//柱状图组之间的间距
float groupSpace = 0.2f;
float barSpace = 0.00f;
float barWidth = 0.4f;
//设置x轴显示标签数量
// (0.45 + 0.02) * 2 + 0.06 = 1.00 -> interval per "group"
xAxis.setLabelCount(xAxisValues.size() - 1, false);
data.setBarWidth(barWidth);
data.groupBars(-0.5f, groupSpace, barSpace);
// 添加到图表中
barChart.setData(data);
}
}
activity_char.xml
柱状图和折线图布局文件
import android.app.Activity;
import android.os.Bundle;
import com.github.mikephil.charting.charts.BarChart;
public class ChartActivity extends Activity {
private BarCharDkey mBarCharMonth;
private LineChartDkey mLineChartDay;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chart);
BarChart chart = (BarChart) findViewById(R.id.line_chart);
LineChart chart = (LineChart) findViewById(R.id.bar_chart);
mBarCharMonth = new BarCharDkey(chart);
ArrayList xValue = new ArrayList<>();
for(int i=0;i<12;i++){
xValue.add((float)i);
}
List> yValues = new ArrayList<>();
for(int i=0;i<2;i++){
List yValue = new ArrayList<>();
for(int j=0;j<12;j++){
yValue.add((float) (Math.random() * 2000));
}
yValues.add(yValue);
}
ArrayList title = new ArrayList<>();
for(int i=0;i<2;i++){
title.add("title"+i);
}
ArrayList color = new ArrayList<>();
// for(int i=0;i<2;i++){
color.add(Color.GREEN);
color.add(Color.RED);
// }
mBarCharMonth.showBarChart(xValue,yValues.get(0),"aaa", Color.GREEN);
// mBarCharMonth.showBarChart(xValue,yValues,title, color);
mLineChartDay = new LineChartDkey(chart);
ArrayList xValue = new ArrayList<>();
for(int i=0;i<31;i++){
xValue.add((float)i);
}
List> yValues = new ArrayList<>();
for(int i=0;i<2;i++){
List yValue = new ArrayList<>();
for(int j=0;j<31;j++){
yValue.add((float) (Math.random() * 2000));
}
yValues.add(yValue);
}
ArrayList title = new ArrayList<>();
for(int i=0;i<2;i++){
title.add("title"+i);
}
ArrayList color = new ArrayList<>();
// for(int i=0;i<2;i++){
color.add(Color.GREEN);
color.add(Color.RED);
// }
// mLineChartDay.showLineChart(xValue,yValues.get(0),"aaa", Color.GREEN);
mLineChartDay.showLineChart(xValue,yValues,title, color);
}
}
横向柱状图布局文件
用法同上。