BarChart基本的初始化操作,红色的标记为重点
private void initBarCahrt(BarChart barChart){
// 数据描述
// Description description = new Description();
// description.setText("电梯故障统计柱状图");
// description.setTextSize(10f);
barChart.setDescription(null);
// 如果没有数据的时候,会显示这个,类似ListView的EmptyView
barChart.setNoDataText("没有图表数据,请添加");
//禁止图表一切交互
barChart.setTouchEnabled(false);
// 是否显示表格颜色
barChart.setDrawGridBackground(false);
// 设置是否可以触摸
barChart.setTouchEnabled(true);
// 是否可以拖拽
barChart.setDragEnabled(true);
// 是否可以缩放
barChart.setScaleEnabled(false);
// 集双指缩放
barChart.setPinchZoom(false);
// 设置背景
barChart.setBackgroundColor(Color.WHITE);
// 如果打开,背景矩形将出现在已经画好的绘图区域的后边。
barChart.setDrawGridBackground(false);
// 集拉杆阴影
barChart.setDrawBarShadow(false);
// 图例
barChart.getLegend().setEnabled(false);
barChart.setFitBars(false);
//设置图表周边间距
barChart.setMinOffset(2f);
//设置最小高度从0开始
barChart.setMinimumHeight(0);
// 隐藏右边的坐标轴
barChart.getAxisRight().setEnabled(false);
// 默认显示左边的左边轴
barChart.getAxisLeft().setEnabled(true);
// 设置数据
barChart.animateX(2000);
//设置图表周围边距
// barChart.setMinOffset(20f);
barChart.setExtraBottomOffset(20f);
}
BarChart数据初始化,BarChart和HorizationalBarChart数据格式一样,
yValuse表示Y轴显示的数据,一般是数字,X轴设置数据必须实现 xAxis.setValueFormatter方法;
以下是BarChartde数据构造方法示例
initBarCahrt(err_chart);
int co = countBeenList.size();
final ArrayList<String> xValue = new ArrayList<>();
int[] colors = new int[co];
// y轴的数据集合
ArrayList<BarEntry> yValues = new ArrayList<>();
for (int i=0;i<co;i++){
yValues.add(new BarEntry(i,Float.parseFloat(countBeenList.get(i).getErrornum())));
colors[i] = Color.parseColor(color[i]);
xValue.add(countBeenList.get(i).getMonth()+"");
}
BarDataSet dataSet = new BarDataSet(yValues,"柱状图");
// dataSet.setColors(colors);
dataSet.setColors(Color.parseColor("#01A1FF"));
errBarData = new BarData(dataSet);
//绘制所有 DataSets 数据对象包含的数据的值文本
errBarData.setDrawValues(true);
errBarData.setValueTextSize(10f);
errBarData.setBarWidth(0.8f);
errBarData.setValueTextColor(Color.BLACK);
YAxis yAxis = err_chart.getAxisLeft();
yAxis.setAxisMinimum(0);
//设置X轴的数据,需要重新设置其格式,才能显示你自定义的项,Y轴同理
XAxis xAxis = err_chart.getXAxis();
xAxis.setValueFormatter(new IAxisValueFormatter() {
@Override
public String getFormattedValue(float value, AxisBase axis) {
int n = (int) value;
return xValue.get(n);
}
});
// xAxis.setLabelRotationAngle(-20);//设置x轴字体显示角度
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);//设置X轴的位置TOP, BOTTOM, BOTH_SIDED, TOP_INSIDE, BOTTOM_INSIDE
xAxis.setDrawGridLines(false);//回执网格线
// xAxis.setLabelCount(12);
err_chart.animateY(2000); // 立即执行的动画
// Legend mLegend = barChart.getLegend(); // 设置比例图标示
设置窗体样式
// mLegend.setForm(Legend.LegendForm.CIRCLE);
字体
// mLegend.setFormSize(9f);
// mLegend.setTextSize(12f);
// mLegend.setXEntrySpace(10);
// mLegend.setTextColor(Color.BLACK);
// mLegend.setWordWrapEnabled(true);
字体颜色
// mLegend.setTextColor(Color.parseColor("#00000000"));
// barChart.fitScreen();
err_chart.setData(errBarData);
err_chart.invalidate();
以下是HorizationalBarChart数据构造方法示例
initBarCahrt(area_chart);
int cou = msgBeenList.size();
final ArrayList<String> xValue = new ArrayList<>();
ArrayList<BarEntry> yValue = new ArrayList<>();
int[] areaColors = new int[cou];
for (int i=0;i<cou;i++){
yValue.add(new BarEntry(i,Integer.parseInt(msgBeenList.get(i).getEnumber())));
xValue.add(msgBeenList.get(i).getAreaName());
areaColors[i] = Color.parseColor(color[i]);
}
BarDataSet dataSet = new BarDataSet(yValue,"区域电梯数量统计");
// dataSet.setColors(areaColors);
dataSet.setColors(Color.parseColor("#01A1FF"));
dataSet.setDrawValues(true);
//必须添加上,太能使图表轴线根据yValue标记刻度,否则,系统自动标记刻度
dataSet.setAxisDependency(YAxis.AxisDependency.RIGHT);
areaBarData = new BarData(dataSet);
areaBarData.setDrawValues(true);
areaBarData.setValueTextSize(10f);
areaBarData.setBarWidth(0.8f);
areaBarData.setValueTextColor(Color.BLACK);
Log.e("========","========="+areaBarData.getYMax());
YAxis yAxis = area_chart.getAxisRight();
yAxis.setAxisMinimum(0);
XAxis xAxis = area_chart.getXAxis();
xAxis.setValueFormatter(new IAxisValueFormatter() {
@Override
public String getFormattedValue(float value, AxisBase axis) {
int n = (int)value;
return xValue.get(n);
}
});
xAxis.setDrawGridLines(false);//回执网格线
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
//设置起点位置
// xAxis.setAxisMinimum(0);
area_chart.getAxisLeft().setEnabled(false);
area_chart.getAxisRight().setEnabled(true);
area_chart.animateX(2000);
area_chart.setData(areaBarData);
area_chart.setDrawValueAboveBar(true);
area_chart.invalidate();