android - 将负值设置到水平条形图中条形的左侧

标签 android mpandroidchart

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

我怎样才能消除负值的这种重叠,如果我设置 horizo​​natal_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()
    }

请帮帮我!

最佳答案

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

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

引用:issue 提到了同样的问题并通过提供轴最小值和最大值解决

关于android - 将负值设置到水平条形图中条形的左侧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59541099/

相关文章:

java - 从字符串中获取图像链接和文本

android - 使用 MPAndroidChart 在雷达图中标记文本大小

java - Mpandroidchart(水平图表)设置每个条形图不同的颜色

android - 如何更改 MPAndroidchart 中 Y 轴标签的间距?

android - MPAndroidChart 定义 x 轴(时间)上值之间的精确/固定间隔

jquery - 进行异步 AJAX 调用时如何防止在 Android 中卡住 PhoneGap 应用程序?

android - fragment 内的按钮 onclick 不起作用

c# - Unity : ClassNotFoundException: com. google.android.play.core.review.ReviewManagerFactory 的 Google InApp 评论

android - ADB 服务器未确认

android - MPAndroidChart 值与图例相交并从条形图上方删除 y 值