下面您可以找到屏幕截图以及我的代码;
代码在下面;
mChart = view.findViewById(R.id.bar_chart);
barWidth = 0.3f;
barSpace = 0.02f;
groupSpace = 0.4f;
mChart.setDescription(null);
mChart.setPinchZoom(false);
mChart.setScaleEnabled(false);
mChart.setDrawBarShadow(false);
mChart.setDrawGridBackground(false);
mChart.getAxisLeft().setDrawGridLines(false);
mChart.getXAxis().setDrawGridLines(false);
mChart.setBackgroundColor(Color.TRANSPARENT);
ArrayList<BarEntry> yValues = new ArrayList<>();
yValues.add(new BarEntry(5, new float[]{10, 20, 30, 50}));
yValues.add(new BarEntry(15, new float[]{12, 13}));
yValues.add(new BarEntry(25, new float[]{15, 15}));
yValues.add(new BarEntry(35, new float[]{17, 17}));
BarDataSet set = new BarDataSet(yValues, "");
set.setColors(new int[]{Color.rgb(67, 67, 72), Color.rgb(124, 181, 236),
Color.rgb(124, 181, 236), Color.rgb(124, 181, 236)});
set.setStackLabels(new String[]{
"Men", "Women", "fgdgfx", "gfdrhd"
});
BarData data = new BarData(set);
data.setBarWidth(1.9f);
mChart.setData(data);
mChart.invalidate();
final ArrayList xVals = new ArrayList();
xVals.add("New");
xVals.add("Accepted");
xVals.add("Completed");
xVals.add("Cancelled");
XAxis xAxis = mChart.getXAxis();
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
xAxis.setTextColor(Color.RED);
xAxis.setDrawAxisLine(true);
xAxis.setDrawGridLines(false);
xAxis.setCenterAxisLabels(true);
xAxis.setAxisMinimum(0.4f);
xAxis.setGranularity(4f);
xAxis.setValueFormatter(new IndexAxisValueFormatter(xVals));
mChart.getAxisRight().setEnabled(false);
YAxis leftAxis = mChart.getAxisLeft();
leftAxis.setValueFormatter(new LargeValueFormatter());
leftAxis.setDrawGridLines(false);
leftAxis.setSpaceTop(35f);
leftAxis.setAxisMinimum(0f);
leftAxis.setGranularity(1.0f);
leftAxis.setGranularityEnabled(true);
Legend l = mChart.getLegend();
l.setPosition(Legend.LegendPosition.BELOW_CHART_RIGHT);
l.setWordWrapEnabled(true);
答案 0 :(得分:1)
问题是格式器的getFormattedValue方法中的float value
与。您得到的值为(0,10,20,30),并且您为x轴的值提供的数组的大小仅为4。这就是为什么它仅打印第一个x值,而不打印其余的x值的原因。
请按照以下步骤更改代码。
xAxis.setValueFormatter(new IAxisValueFormatter() {
@Override
public String getFormattedValue(float value, AxisBase axis) {
try {
return xVals.get((int) (value / 10));//dividing the value by 10 to get the multiplied value.
} catch (Exception e){
return "";
}
}
});