我在项目中使用MpAndroidChart-CombinedChart
,但遇到问题,请查看以下屏幕截图。
如您所见,X轴右侧部分没有填充。换句话说,我想设置条形空间,但我不能。
我尝试barData.setBarWidth(0.4f)
,它不起作用,它将使条形宽度变大,但不会使条形空间更大。
public static void initCombinedChart(List<FieldFragment.CombinedBean> list, CombinedChart combinedChart) {
if (list == null || list.size() == 0) return;
combinedChart.setPinchZoom(false);
combinedChart.setDragEnabled(false);
combinedChart.setTouchEnabled(false);
combinedChart.getLegend().setEnabled(false);
combinedChart.getDescription().setEnabled(false);
XAxis xAxis = combinedChart.getXAxis();
xAxis.setDrawGridLines(false);
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
xAxis.setYOffset(5);
xAxis.setAvoidFirstLastClipping(true);
YAxis leftYAxis = combinedChart.getAxisLeft();
leftYAxis.setLabelCount(6, true);
leftYAxis.setDrawAxisLine(false);
leftYAxis.setDrawGridLines(true);
leftYAxis.setGridColor(Color.parseColor("#DDDDDD"));
leftYAxis.setXOffset(10f);
leftYAxis.setAxisMinimum(0);
leftYAxis.setSpaceBottom(0);
leftYAxis.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART);
YAxis rightYAxis = combinedChart.getAxisRight();
rightYAxis.setSpaceBottom(0);
rightYAxis.setDrawAxisLine(false);
rightYAxis.setDrawGridLines(true);
rightYAxis.setAxisMinimum(0);
rightYAxis.setXOffset(10);
rightYAxis.setLabelCount(6, true);
rightYAxis.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART);
List<Entry> lineEntries = new ArrayList<>();
List<BarEntry> barEntries = new ArrayList<>();
List<String> xValues = new ArrayList<>();
for (int i = 0; i < list.size(); i++) {
lineEntries.add(new Entry(i, list.get(i).getRightYValue()));
barEntries.add(new BarEntry(i, list.get(i).getLeftYValue()));
xValues.add(list.get(i).getxValue());
}
xAxis.setValueFormatter(new IAxisValueFormatter() {
@Override
public String getFormattedValue(float value, AxisBase axisBase) {
try {
int position = (int) value;
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date = dateFormat.parse(xValues.get(position));
SimpleDateFormat format = new SimpleDateFormat("MM.dd");
return format.format(date);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
});
// 折线数据
LineDataSet lineDataSet = new LineDataSet(lineEntries, "");
lineDataSet.setDrawValues(false);
lineDataSet.setDrawCircles(false);
lineDataSet.setLineWidth(3f);
lineDataSet.setColor(Color.parseColor("#00c2bd"));
lineDataSet.setMode(LineDataSet.Mode.HORIZONTAL_BEZIER);
lineDataSet.setDrawFilled(true); //这个不要忘了
lineDataSet.setFillColor(Color.parseColor("#00c2bd"));
lineDataSet.setFillAlpha(50);
lineDataSet.setAxisDependency(YAxis.AxisDependency.LEFT);
List<ILineDataSet> lineDataSets = new ArrayList<>();
lineDataSets.add(lineDataSet);
LineData lineData = new LineData(lineDataSets);
// 柱子数据
BarDataSet barDataSet = new BarDataSet(barEntries, "");
barDataSet.setDrawValues(false);
barDataSet.setAxisDependency(YAxis.AxisDependency.RIGHT);
barDataSet.setColor(Color.parseColor("#1490ee"));
BarData barData = new BarData(barDataSet);
barData.setBarWidth(0.4f); //值越大柱子越宽
// 解决bar显示不全问题
float barWidth = barData.getBarWidth() / 2;
xAxis.setAxisMinimum(-barWidth);
xAxis.setAxisMaximum(xValues.size() - barWidth);
CombinedData combinedData = new CombinedData();
combinedData.setData(lineData);
combinedData.setData(barData);
combinedChart.setData(combinedData);
combinedChart.invalidate();
}