gpt4 book ai didi

android - 如何在条形图中启用滚动并将 xAxis 标签与条形图组对齐?

转载 作者:行者123 更新时间:2023-11-29 22:36:22 24 4
gpt4 key购买 nike

问题:1. x 轴值未映射到条形组,为什么?

  1. 我已经通过设置 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()
    }

请帮忙。

最佳答案

这很有效,只需进行一些调整:

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()
}
}

关于android - 如何在条形图中启用滚动并将 xAxis 标签与条形图组对齐?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59477589/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com