MPAndroidChart plotting incorrect linear graph

Viewed 227

Trying to implement mpandroidchart, but the graph is ending up useless because it is plotting the chart incorrectly.

Below is the snippet of how i try to implement.

 fun setGraph(cdata : ArrayList<Entry>){
            val lineChart: LineChart = bind.lineChartViewPage
            lineChart.setExtraOffsets(0f, 40f, 0f, 40f)
            lineChart.setDrawBorders(false)
            lineChart.setDrawGridBackground(false)
            lineChart.legend.isEnabled = false
            lineChart.description.isEnabled = false
            lineChart.setTouchEnabled(false)
            lineChart.isDragEnabled = false
            lineChart.setScaleEnabled(false)
            lineChart.setPinchZoom(false)
            lineChart.isAutoScaleMinMaxEnabled = false

            val lds = LineDataSet(cdata, "my data")
            lds.mode = LineDataSet.Mode.LINEAR
            lds.setDrawCircleHole(false)
            lds.setDrawCircles(false)
            lds.color = Color.parseColor("#ffffff")
            lds.lineWidth = 1.5f
            lds.isHighlightEnabled = false

            val lineDataSets: ArrayList<ILineDataSet> = ArrayList<ILineDataSet>()
            lineDataSets.add(lds)
            lds.setDrawValues(false)
            val lineData = LineData(lineDataSets)

            val ll1 = LimitLine(2.5F)
            ll1.lineWidth = 2f
            ll1.lineColor = Color.parseColor("#99ce7aff")

            val yAxisRight: YAxis = lineChart.axisRight
            yAxisRight.textColor = Color.WHITE
            yAxisRight.setDrawZeroLine(false)
            yAxisRight.setDrawAxisLine(false)
            yAxisRight.setCenterAxisLabels(true)
            yAxisRight.setDrawTopYLabelEntry(true)
            yAxisRight.setDrawGridLines(false)
            yAxisRight.setGranularity(1.0f)
            yAxisRight.isGranularityEnabled = true
            yAxisRight.setLabelCount(6, true)
            yAxisRight.removeAllLimitLines()
            yAxisRight.addLimitLine(ll1)
            yAxisRight.axisMaximum = 3.0f
            yAxisRight.axisMinimum = 0.0f

            val xAxis: XAxis = lineChart.xAxis
            xAxis.isEnabled = false
            xAxis.valueFormatter = null
            xAxis.setDrawLabels(false)
            xAxis.setDrawGridLines(false)

            val yAxisLeft: YAxis = lineChart.axisLeft
            yAxisLeft.isEnabled = false
            yAxisLeft.setDrawLabels(false)
            yAxisLeft.setDrawGridLines(false)

            // hide legend
            val legend: Legend = lineChart.legend
            legend.isEnabled = false

            lineChart.data = lineData
            lineChart.data.notifyDataChanged()
            lineChart.notifyDataSetChanged()
            lineChart.invalidate()
        }

I tried out some combinations and found a different behavior if i comment out below lines :

yAxisRight.axisMaximum = 3.0f
yAxisRight.axisMinimum = 0.0f

Before commenting lines (incorrect plotting with limit line)

enter image description here

After commenting lines (correct plotting and limit line vanished)

enter image description here

Below is the dataset on which the graph is being plotted. here the max value is 0.8f and min value is 0.1f.

private fun fakeData():ArrayList<Entry>{
        val al: ArrayList<Entry> = ArrayList()
        al.add(Entry(1F, 0.2F))
        al.add(Entry(2F, 0.4F))
        al.add(Entry(3F, 0.5F))
        al.add(Entry(4F, 0.6F))
        al.add(Entry(5F, 0.3F))
        al.add(Entry(6F, 0.4F))
        al.add(Entry(7F, 0.6F))
        al.add(Entry(8F, 0.7F))
        al.add(Entry(9F, 0.4F))
        al.add(Entry(10F, 0.1F))
        al.add(Entry(11F, 0.3F))
        al.add(Entry(12F, 0.3F))
        al.add(Entry(13F, 0.5F))
        al.add(Entry(14F, 0.6F))
        al.add(Entry(15F, 0.47F))
        al.add(Entry(16F, 0.55F))
        al.add(Entry(17F, 0.66F))
        al.add(Entry(18F, 0.45F))
        al.add(Entry(19F, 0.46F))
        al.add(Entry(20F, 0.33F))
        al.add(Entry(21F, 0.4F))
        al.add(Entry(22F, 0.36F))
        al.add(Entry(23F, 0.37F))
        al.add(Entry(24F, 0.46F))
        al.add(Entry(25F, 0.74F))
        al.add(Entry(26F, 0.76F))
        al.add(Entry(27F, 0.79F))
        al.add(Entry(28F, 0.8F))
       
        return al
    }

If i dont set min and max values as below; then the limit line won't show up.

yAxisRight.axisMaximum = 3.0f
yAxisRight.axisMinimum = 0.0f

chart version : com.github.PhilJay:MPAndroidChart:v3.1.0

Please let me know if there is something wrong with this implementation or possible ways to correct this.

UPDATE :

Tried multiple dataset and found that defining custom yaxis min and max value is causing the chart to be drawn incorrectly. I cant understand why?

1 Answers

Had the same issue and the fix is pretty easy, but not straightforward, though, you should set the same max and min values for both y-axises:

axisLeft.apply {
            axisMinimum = 0.0f
            axisMaximum = 3.0f
        }

axisRight.apply {
            axisMinimum = 0.0f
            axisMaximum = 3.0f
        }
Related