MPAndroidChart 柱行图事例
作者:互联网
项目中需要柱行图 ,效果如下图:
build中MPchart 版本如下: implementation 'com.github.PhilJay:MPAndroidChart:v3.0.3'
1. 初始化柱形图 基本 参数
chart.setDrawBarShadow(false);
chart.setDrawValueAboveBar(true);
chart.setBackgroundColor(getResources().getColor(R.color.white));
chart.getDescription().setEnabled(false);
// if more than 60 entries are displayed in the chart, no values will be
// drawn
chart.setMaxVisibleValueCount(10);
// scaling can now only be done on x- and y-axis separately
chart.setPinchZoom(false);
chart.setDrawGridBackground(false);
// X 轴显示的类别内容
mXAxis = chart.getXAxis();
mXAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
mXAxis.setTextSize(12); // 设置字体大小
mXAxis.setDrawGridLines(false);
mXAxis.setGranularity(1f); // only intervals of 1 day 隔几个显示一下 X轴名称
mXAxis.setLabelCount(7);
mXAxis.setYOffset(2); // 默认是5
// 左 Y 轴设置
YAxis leftAxis = chart.getAxisLeft();
leftAxis.setLabelCount(8, false);
leftAxis.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART);
leftAxis.setSpaceTop(15f);
leftAxis.setAxisMinimum(0f); // this replaces setStartAtZero(true)
YAxis rightAxis = chart.getAxisRight();
rightAxis.setEnabled(false);// 不显示右边Y轴
// rightAxis.setDrawGridLines(false);
// rightAxis.setTypeface(tfLight);
// rightAxis.setLabelCount(8, false);
// rightAxis.setValueFormatter(custom);
// rightAxis.setSpaceTop(15f);
// rightAxis.setAxisMinimum(0f); // this replaces setStartAtZero(true)
// 标签显示
Legend l = chart.getLegend();
l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.LEFT);
l.setOrientation(Legend.LegendOrientation.HORIZONTAL);
l.setDrawInside(true);
l.setForm(Legend.LegendForm.SQUARE);
l.setFormSize(9f);
l.setTextSize(15f);
l.setXEntrySpace(4f);
2. 初始化数据 并进行图形的刷新显示
private void loadBarData(List<PayCountResultBean.BodyBean.ExpensesBean> rcyList) {
double allCount = 0;
ArrayList xStrs = new ArrayList();
ArrayList<BarEntry> values = new ArrayList<>();
for (int i = 0; i < rcyList.size(); i++) {
PayCountResultBean.BodyBean.ExpensesBean rcyListBean = rcyList.get(i);
allCount += rcyListBean.getAmount();
xStrs.add(rcyListBean.getPayTypeName());
values.add(new BarEntry(i, (float) rcyListBean.getAmount()));
}
mXAxis.setValueFormatter(new IndexAxisValueFormatter(xStrs));
BarDataSet set1 = new BarDataSet(values, "支付统计");
set1.setDrawIcons(false);
set1.setColors(ColorTemplate.MATERIAL_COLORS);
ArrayList<IBarDataSet> dataSets = new ArrayList<>();
dataSets.add(set1);
BarData data = new BarData(dataSets);
data.setValueTextSize(10f);
// data.setValueTypeface(tfLight);
data.setBarWidth(0.45f); //修改柱形每项的宽度
chart.setData(data);
chart.postInvalidate();
}
标签:MPAndroidChart,false,事例,ArrayList,chart,mXAxis,new,rightAxis,柱行 来源: https://blog.csdn.net/lsw8569013/article/details/88075829