PhilJay / MPAndroidChart

A powerful 🚀 Android chart view / graph view library, supporting line- bar- pie- radar- bubble- and candlestick charts as well as scaling, panning and animations.
Other
37.55k stars 9.01k forks source link

Set Negative values to the left of the bars in horizontal bar chart. #4773

Open suvDev opened 4 years ago

suvDev commented 4 years ago

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. As shown:

enter image description here

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

Please help me !

suvDev commented 4 years ago

@davidgoli @mikemonteith @nielsz @Pitel @dseider @almic @PhilJay Please suggest something.

suvDev commented 4 years ago

On bounty please answer https://stackoverflow.com/questions/59541099/set-the-negative-values-to-the-left-of-the-bars-in-horizontal-bar-chart @almic @PhilJay