做个笔记~
// 显示统计图数据 private void showDayData(ArrayList<DayFireCountBean> data) { List<Column> columns = new ArrayList<>(); // 获取Y轴最大值 final Integer[] maxValue = {0}; Observable.fromIterable(data) .filter(dayFireCountBean -> maxValue[0] < dayFireCountBean.getCount()) .subscribe(dayFireCountBean -> maxValue[0] = dayFireCountBean.getCount()); List<AxisValue> axisValueYs = new ArrayList<>(); Axis axisY = new Axis(axisValueYs); axisY.setHasLines(true); axisY.setName("次数"); axisY.setLineColor(Color.parseColor("#3308c2ff")); axisY.setTextColor(Color.parseColor("#08c2ff")); axisY.setTextSize((int) (getResources().getDimension(R.dimen.px_to_sp_32) / getResources().getDisplayMetrics().density)); // 设置Y轴每个刻度的值,要注意最大值问题 for (int i = 0; i < maxValue[0]; i++) { axisValueYs.add(new AxisValue(i)); } List<AxisValue> axisValueXs = new ArrayList<>(); Axis axisX = new Axis(axisValueXs); axisX.setLineColor(Color.parseColor("#08c2ff")); axisX.setTextColor(Color.parseColor("#08c2ff")); axisX.setTextSize((int) (getResources().getDimension(R.dimen.px_to_sp_32) / getResources().getDisplayMetrics().density)); for (int i = 0; i < data.size(); i++) { DayFireCountBean bean = data.get(i); Column column = new Column(); columns.add(column); List<SubcolumnValue> columnValues = new ArrayList<>(); column.setValues(columnValues); column.setHasLabels(true); SubcolumnValue value = new SubcolumnValue(bean.getCount(), Color.parseColor("#0d4c7d")); columnValues.add(value); AxisValue axisValueX = new AxisValue(i).setLabel(bean.getDate().substring(5)); axisValueXs.add(axisValueX); } ColumnChartData ccData = new ColumnChartData(columns); // 设置柱状图显示比例,越接近1越宽,间隙越小 ccData.setFillRatio(0.7f); ccData.setAxisYLeft(axisY); ccData.setAxisXBottom(axisX); binding.columnChart.setColumnChartData(ccData); binding.columnChart.setZoomEnabled(false); // 禁止手势缩放 binding.columnChart.setVisibility(View.VISIBLE); }