问题:1.为什么x轴值未与条形图组映射?
我设法通过设置xAxis.axisMaximum = 50f来显示所有条形数据,但是它在图形中显示了很多“ 0” xAxis标签,那么我该如何解决呢? 这是代码:
private fun populateCharts(chartData: List<ResponseGetPenetrationGrowth>){
try {
val prev_year = ArrayList<BarEntry>()
val current_year = ArrayList<BarEntry>()
val xAxis_label = ArrayList<String>()
var i = 0f
val currentYear = Calendar.getInstance().get(Calendar.YEAR)
for (item in chartData!!) {
xAxis_label.add(item.brand)
prev_year.add(BarEntry(i,item.prevYear.toFloat()))
current_year.add(BarEntry(i,item.currentYear.toFloat()))
i++
}
val prevDataSet = BarDataSet(prev_year,(currentYear-1).toString())
prevDataSet.setColors(ContextCompat.getColor(penetration_chart.context, R.color.prev_year_bar))
val currentDataSet = BarDataSet(current_year, currentYear.toString())
currentDataSet.setColors(ContextCompat.getColor(penetration_chart.context, R.color.current_year_bar))
val barData = BarData(prevDataSet, currentDataSet)
penetration_chart.data = barData
val xAxis = penetration_chart.getXAxis()
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM)
xAxis.valueFormatter = IAxisValueFormatter { value, axis ->
if (value.toInt()> 0 && (value.toInt() < xAxis_label.size)) {
xAxis_label.get(value.toInt())
} else {
"0"
}
}
xAxis.setCenterAxisLabels(true)
val barSpace = 0.06f
val groupSpace = 0.8f
xAxis.axisMinimum = 0f
xAxis.axisMaximum = 50f
// val yAxisLeft = penetration_chart.axisLeft
// yAxisLeft.axisMinimum = 0f
// val yAxisRight = penetration_chart.axisRight
// yAxisRight.axisMinimum = 0f
penetration_chart.setDrawGridBackground(false)
val description = Description()
description.text = ""
penetration_chart.description = description
barData.setBarWidth(0.5f)
penetration_chart.groupBars(0f, groupSpace, barSpace)
penetration_chart.getAxisRight().setDrawGridLines(false);
penetration_chart.getAxisLeft().setDrawGridLines(false);
penetration_chart.getXAxis().setDrawGridLines(false);
penetration_chart.setVisibleXRangeMaximum(5f)
penetration_chart.invalidate()
}catch (e: Exception){
e.printStackTrace()
}
}
请帮助。
答案 0 :(得分:1)
这就像魅力一样,很少调整:
private fun populateCharts(chartData: List<ResponseGetPenetrationGrowth>){
try {
val prev_year = ArrayList<BarEntry>()
val current_year = ArrayList<BarEntry>()
val xAxis_label = ArrayList<String>()
var i = 0f
val currentYear = Calendar.getInstance().get(Calendar.YEAR)
xAxis_label.add("")
for (item in chartData!!) {
xAxis_label.add(item.brand)
prev_year.add(BarEntry(i,item.prevYear.toFloat()))
current_year.add(BarEntry(i,item.currentYear.toFloat()))
i++
}
xAxis_label.add("")
val prevDataSet = BarDataSet(prev_year,(currentYear-1).toString())
prevDataSet.setColors(ContextCompat.getColor(penetration_chart.context, R.color.prev_year_bar))
prevDataSet.valueTextSize = 8f
val currentDataSet = BarDataSet(current_year, currentYear.toString())
currentDataSet.valueTextSize = 8f
currentDataSet.setColors(ContextCompat.getColor(penetration_chart.context, R.color.current_year_bar))
val barData = BarData(prevDataSet, currentDataSet)
penetration_chart.data = barData
barData.setBarWidth(0.3f)
val xAxis = penetration_chart.getXAxis()
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM)
xAxis.valueFormatter = IndexAxisValueFormatter(xAxis_label)
xAxis.granularity= 1f
xAxis.setCenterAxisLabels(true)
val barSpace = 0.05f
val groupSpace = 0.3f
xAxis.axisMinimum = 1f
xAxis.axisMaximum = xAxis_label.size - 1.1f
penetration_chart.setDrawGridBackground(false)
val description = Description()
description.text = ""
penetration_chart.description = description
penetration_chart.groupBars(1f, groupSpace, barSpace)
penetration_chart.getAxisRight().setDrawGridLines(false);
penetration_chart.getAxisLeft().setDrawGridLines(false);
penetration_chart.getXAxis().setDrawGridLines(false);
val legend = penetration_chart.legend
legend.position = Legend.LegendPosition.BELOW_CHART_CENTER
penetration_chart.extraBottomOffset = 20f
penetration_chart.setVisibleXRangeMaximum(5f)
penetration_chart.invalidate()
}catch (e: Exception){
e.printStackTrace()
}
}