【问题标题】:Set the negative values to the left of the bars in horizontal bar chart在水平条形图中将负值设置在条形的左侧
【发布时间】:2019-12-31 07:42:00
【问题描述】:

我想将条形的负值设置在条形的左侧,但目前一些条形与这些值重叠。

如何消除负值的重叠,如果我设置horizonatal_chart.setDrawValueAboveBar(true),那么正条将与正值重叠。 代码如下:

private fun filterHorizontalBarData(myList: List<Response>?) {
        try {

            val brand = ArrayList<String>()
            var index = 0f
            var color_index = 0
            val data  = BarData()
            val values_adapter = ArrayList<String>()

            for (item in myList!!) {
                if (item.kPI.equals("Value % Growth") || item.kPI.equals("Volume % Growth")) {
                    val kpiValue = ArrayList<BarEntry>()
                    kpiValue.add(BarEntry(index,item.kPIvalue.toFloat()))
                    brand.add(item.brand)

                    values_adapter.add(item.kPIvalue.toString())

                    val barDataSet = BarDataSet(kpiValue, item.brand)
                    if(color_index<7)
                       barDataSet.setColor(getColor(horizonatal_chart.context, getColorID(color_index)))
                    else
                        barDataSet.setColor(getColorID(color_index))
                    barDataSet.valueTextSize = 8f
//                    barDataSet.setDrawValues(false)
                    data.addDataSet(barDataSet)
                    index++
                    color_index++
                }
            }
//            values_adapter.sortDescending()
//            first_bar_values.adapter  = AdapterValuesHorizontalBar(values_adapter)
           setHorizontalChart(data, brand)
        }catch (e: Exception){
            e.printStackTrace()
        }
    }


    private fun setHorizontalChart(data : BarData, brand: ArrayList<String>){

        horizonatal_chart.setDrawBarShadow(false)
        val description = Description()
        description.text = ""
        horizonatal_chart.description = description

        horizonatal_chart.legend.setEnabled(false)
        horizonatal_chart.setPinchZoom(false)

        horizonatal_chart.setScaleEnabled(false)
        horizonatal_chart.setDrawValueAboveBar(true)


        //Display the axis on the left (contains the labels 1*, 2* and so on)
        val xAxis = horizonatal_chart.getXAxis()
        xAxis.setDrawGridLines(false)
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM)
        xAxis.setEnabled(true)
        xAxis.setDrawAxisLine(false)
        xAxis.textColor = Color.parseColor("#a1a1a1")

        xAxis.xOffset = 5f
        val yLeft = horizonatal_chart.axisLeft

        //Set the minimum and maximum bar lengths as per the values that they represent
        yLeft.axisMaximum = 100f
        yLeft.axisMinimum = 0f
        yLeft.isEnabled = false
        yLeft.setDrawZeroLine(true)
        yLeft.granularity = 1f

        //Now add the labels to be added on the vertical axis
        xAxis.valueFormatter = IAxisValueFormatter { value, axis ->
            if (value.toInt() < brand.size) {
                brand.get(value.toInt())
            } else {
                "0"
            }
        }

        val yRight = horizonatal_chart.axisRight
        yRight.setDrawAxisLine(true)
        yRight.setDrawGridLines(false)
        yRight.isEnabled = false

        //Set bar entries and add necessary formatting
        horizonatal_chart.axisLeft.setAxisMinimum(data.yMin)

        data.barWidth = 0.9f
//        val myCustomChartRender = MyCustomChartRender(horizonatal_chart, horizonatal_chart.animator, horizonatal_chart.viewPortHandler)
//        //Add animation to the graph
//        horizonatal_chart.renderer = myCustomChartRender
        horizonatal_chart.animateY(2000)
        horizonatal_chart.data = data
        horizonatal_chart.setTouchEnabled(false)
        horizonatal_chart.invalidate()
    }

请帮帮我!

【问题讨论】:

  • 如果你使用setDrawValueAboveBar(),那么会出现什么问题呢?使用这个方法后可以发截图吗?
  • @Kushal 上面的图片是针对horinatal_chart.setDrawValueAboveBar(true)
  • @Kushal 添加了真假图片,请检查。

标签: android mpandroidchart


【解决方案1】:

问题似乎是您没有为rightAxis 提供minimummaximum 上限。

horizonatal_chart.axisRight.setAxisMinimum(-100)
horizonatal_chart.axisRight.setAxisMaximum(100)

参考:issue 提到了同样的问题,并通过提供轴最小值和最大值来解决

【讨论】:

  • 即使是负条也不会显示
  • axisMinimum 更改为-100 或最低值,我已经编辑了我的答案。试试这个
  • 现在负条正在显示,但重叠没有变化
  • 我还有什么可以尝试的吗?