How to add icons below axis values in MP chart Android?

515 Views Asked by At

I've setup my mpchart but one thing is missing,

MP chart

I need to add those colorful indicators (drawables) below x-axis based on the y values like if y>5 it should be green otherwise red.

val xAxis = lineChart.xAxis
    xAxis.apply {
        setDrawGridLines(false)
        isEnabled = true
        position = XAxis.XAxisPosition.BOTTOM
        textSize = 12F
        textColor = Color.WHITE
    }
1

There are 1 best solutions below

0
Rahul Pawar On

I achieved this by extending XAxisRenderer class of MP chart library and overriding drawLabel function like this

class CustomXAxis(
    viewPortHandler: ViewPortHandler?,
    xAxis: XAxis?, trans: Transformer?
) : XAxisRenderer(viewPortHandler, xAxis, trans) {

    override fun drawLabel(
        c: Canvas?,
        formattedLabel: String?,
        x: Float,
        y: Float,
        anchor: MPPointF?,
        angleDegrees: Float
    ) {
        super.drawLabel(c, formattedLabel, x, y, anchor, angleDegrees)
        val drawable = ContextCompat.getDrawable(context, R.drawable.ic_single_bar)
        Utils.drawImage(c, drawable, x.toInt(), (y + 50).toInt(), 30, 30)
    }
}