Android MPChart |将XAxis标签与条形对齐

时间:2020-02-28 22:02:59

标签: android kotlin bar-chart

我在将xAxis标签与实际条形对齐时遇到问题。 我正在使用Android的MPChart。 这是它的外观图像: enter image description here

如您所见,xAxis标签不合适。每组条形下应有一个值。 我试图用修复 我在Kotlin中的代码:

fun populateGraphData() {

        var barChartView = findViewById<BarChart>(R.id.chartConsumptionGraph)

        val barWidth: Float
        val barSpace: Float
        val groupSpace: Float

        barWidth = 0.45f
        barSpace = 0.1f
        groupSpace = 0.56f
        var titleobject = AvsF_response_year()
        var xAxisValues = ArrayList<String>()
        var yValueGroup1 = ArrayList<BarEntry>()
        var yValueGroup2 = ArrayList<BarEntry>()
        var barDataSet1: BarDataSet
        var barDataSet2: BarDataSet
        var i=1
        for(value in avsfDateResponseList){
            xAxisValues.add(value.dateTimeUTC)
            yValueGroup1.add(BarEntry(i.toFloat(),value.dayAheadTotalLoadForecastValue.toFloat()))
            yValueGroup2.add(BarEntry(i.toFloat(),value.actualTotalLoadValue.toFloat()))
            i=i+1
        }

        barDataSet1 = BarDataSet(yValueGroup1, "")
        barDataSet1.setColors(Color.CYAN, Color.CYAN)
        barDataSet1.label = "DAY AHEAD TOTAL LOAD FORECAST"
        barDataSet1.setDrawIcons(false)
        barDataSet1.setDrawValues(false)

        barDataSet2 = BarDataSet(yValueGroup2, "")
        barDataSet2.label = "ACTUAL TOTAL LOAD"
        barDataSet2.setColors(Color.BLUE, Color.BLUE)
        barDataSet2.setDrawIcons(false)
        barDataSet2.setDrawValues(false)

        var barData = BarData(barDataSet1, barDataSet2)
        barChartView.description.text = "entso-e"
        barChartView.description.isEnabled = true
        barChartView.description.textSize = 3f
        barData.setValueFormatter(LargeValueFormatter())
        barChartView.setData(barData)
        barChartView.getBarData().setBarWidth(barWidth)
        barChartView.getXAxis().setAxisMinimum(0f)
        val size = avsfDateResponseList.size.toFloat() +avsfDateResponseList.size.toFloat()
        barChartView.getXAxis().setAxisMaximum(size)
        barChartView.groupBars(0f, groupSpace, barSpace)
        barChartView.setFitBars(true)
        barChartView.getData().setHighlightEnabled(true)

        var legend = barChartView.legend
        legend.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM)
        legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT)
        legend.setOrientation(Legend.LegendOrientation.HORIZONTAL)
        legend.setDrawInside(false)

        var legenedEntries = arrayListOf<LegendEntry>()
        legenedEntries.add(LegendEntry("DAY AHEAD TOTAL LOAD FORECAST", Legend.LegendForm.SQUARE, 8f, 8f, null, Color.CYAN))
        legenedEntries.add(LegendEntry("ACTUAL TOTAL LOAD", Legend.LegendForm.SQUARE, 8f, 8f, null, Color.BLUE))
        legend.setCustom(legenedEntries)

        legend.setYOffset(2f)
        legend.setXOffset(2f)
        legend.setYEntrySpace(0f)
        legend.setTextSize(9f)

        val xAxis = barChartView.getXAxis()
        xAxis.setGranularity(1f)
        xAxis.setGranularityEnabled(true)
        xAxis.setCenterAxisLabels(true)
        xAxis.setDrawGridLines(false)
        xAxis.labelRotationAngle = 60F
        xAxis.textSize = 10f

        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM)
        xAxis.setValueFormatter(IndexAxisValueFormatter(xAxisValues))
        xAxis.setCenterAxisLabels(true)
        xAxis.setLabelCount(avsfDateResponseList.size.toInt())
        xAxis.setCenterAxisLabels(true)
        xAxis.setAvoidFirstLastClipping(true)
        barChartView.setDragEnabled(true)

        barChartView.getAxisRight().setEnabled(false)
        barChartView.setScaleEnabled(true)

        val leftAxis = barChartView.getAxisLeft()
        leftAxis.setValueFormatter(LargeValueFormatter())
        leftAxis.setDrawGridLines(false)
        leftAxis.setSpaceTop(1f)
        leftAxis.setAxisMinimum(0f)

        barChartView.data = barData
        barChartView.setVisibleXRange(1f, 18f)
    }

我尝试删除并添加这些行中的每一行,似乎没有任何工作正常。 有人能帮我吗?

0 个答案:

没有答案