Search code examples
kotlinmpandroidchart

Highlighted marker window cut off at the left corner of Chart MPAndroidChart


I'm using a custom class for creating an MPAndroidChart marker. It's just a simple class where I'm using my own XML layout for creating a marker. I'm sending 35 entries to my chart and it's not scrollable. The problem is: if the highlight value at the beginning of the chart it's ok and marker window always sticks to left corner of the chart. But if highlight value is last then the highlighted marker gets cut off because of it's layout outside of chart. Why doesn't this behave same as left corner?

Now I'm drawing my marker always on top and at circle like this:

override fun getOffset(): MPPointF {
        return MPPointF(-(width / 2).toFloat(), -lineChart.height.toFloat())
    }

left corner behavior

right corner behavior


Solution

  • override fun getOffsetForDrawingAtPoint(posX: Float, posY: Float): MPPointF {
            if (posX > chartView.width - width) {
                offset.x = (-width).toFloat()
            } else {
                offset.x = (-(width / 2)).toFloat()
            }
    
            offset.y = -(height / 2).toFloat()
    
            return offset
        }
    

    You need to set chart view to MarkerView.chartView before calculation.