How to enable Scroll in a bar chart and align xAxis labels with the bar groups?

99封情书 提交于 2020-01-05 07:12:34

问题


Problem: 1. The x-axis values are not mapped with the group of the bars why?

  1. I have managed to show all the bar data by setting xAxis.axisMaximum = 50f but it is displaying a lot of "0" xAxis labels in the graph so how can i solve this? Here's the code:

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

    }

Please help.


回答1:


This worked like charm, few tweakings:

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


来源:https://stackoverflow.com/questions/59477589/how-to-enable-scroll-in-a-bar-chart-and-align-xaxis-labels-with-the-bar-groups

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!