Maryam Mirzaie
Maryam Mirzaie

Reputation: 596

Custom label indicators on xAxis in MpAndroidChart

I am using a chart library called MPAndroidChart and it responds to most of my needs. However, I need to customize some parts.

  1. I want to draw some indicator lines for labels on xAxis like this :

enter image description here

As I dug in, I could write a CustomAxisRenderer but it seems I need to copy most of the super class codes.

  1. I want the min value to be drawn exactly on xAxis. This min value could be 0 or any other number as well.

enter image description here

How can this be done? Is it even possible to do it?

Any help or hint would be appreciated.

Upvotes: 0

Views: 378

Answers (1)

Maryam Mirzaie
Maryam Mirzaie

Reputation: 596

I solved the first issue:

internal class IndicatorAxisRenderer(
    viewPortHandler: ViewPortHandler,
    xAxis: XAxis,
    trans: Transformer
) : XAxisRenderer(viewPortHandler, xAxis, trans) {

    private var indicatorWidth = 1f
    private var indicatorHeight = 1f

    private fun getXLabelPositions(): FloatArray {
        var i = 0
        val positions = FloatArray(mXAxis.mEntryCount * 2)
        val centeringEnabled = mXAxis.isCenterAxisLabelsEnabled
        while (i < positions.size) {
            if (centeringEnabled) {
                positions[i] = mXAxis.mCenteredEntries[i / 2]
            } else {
                positions[i] = mXAxis.mEntries[i / 2]
            }
            positions[i + 1] = 0f
            i += 2
        }
        mTrans.pointValuesToPixel(positions)
        return positions
    }

    override fun renderAxisLine(c: Canvas?) {
        super.renderAxisLine(c)
        val positions = getXLabelPositions()
        var i = 0
        while (i < positions.size) {
            val x = positions[i]
            if (mViewPortHandler.isInBoundsX(x)) {
                val y = mViewPortHandler.contentBottom()
                c?.drawLine(
                    x, y,
                    x, y + indicatorHeight,
                    mAxisLinePaint
                )
            }
            i += 2
        }
    }

    fun setIndicatorSize(width: Float, height: Float) {
        this.indicatorWidth = width
        this.indicatorHeight = height
    }
}

This code renders indicator lines on top of the xAxis.

Upvotes: 1

Related Questions