在水平条形图中将负值设置为条形左侧 [英] Set the negative values to the left of the bars in horizontal bar chart

查看:225
本文介绍了在水平条形图中将负值设置为条形左侧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to set the negative values of the bars to the left of the bars but at present some of the bars are overlapping these values.

如何消除负值的这种重叠,如果我设置了horizonatal_chart.setDrawValueAboveBar(true),则正线将与正值重叠. 这是代码:

How can i remove this overlapping of the negative values and if i set horizonatal_chart.setDrawValueAboveBar(true) then the positive bar will overlap the positive values. Here's the code:

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

请帮助我!

推荐答案

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

The problem seems that you've not provided minimum and maximum cap for rightAxis.

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

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

Reference : The same issue is mentioned at issue and was solved by providing axis minimum and maximum

这篇关于在水平条形图中将负值设置为条形左侧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆