4

I have managed to create a line chart using MP Android Chart, And want to create a draggable line to set a limit. So if the value crosses the line value, then the user gets an alert. My use case is similar to the Android system Data usage limit.

I came across LimitLines - https://github.com/PhilJay/MPAndroidChart/wiki/The-Axis and also dragging using touch event callbacks https://github.com/PhilJay/MPAndroidChart/wiki/Interaction-with-the-Chart

My question is whether I can add the limit line dynamically on the response to a translate (onChartTranslate) event so I can simulate the limit setting? Is this a better approach than trying to overload the MarkerView ?

Alex Mantaut
  • 3,657
  • 3
  • 35
  • 45
jd83
  • 116
  • 8
  • 1
    You can't by using limitline in MPAndroidChart. You can try to use a FrameLayout to contain MPAndroidChart and a draggable line. – tiny sunlight Jun 10 '16 at 18:05
  • See this question for how to make a FrameLayout over the chart that contains draggable lines: https://stackoverflow.com/questions/36083802/custom-view-of-limit-line-in-mpandroidchart – David Rawson Mar 02 '17 at 08:16

1 Answers1

1

I managed to create a draggable (horizontal) LimitLine by using OnChartGestureListener to listen for begin and end of drag events:

chart.onChartGestureListener = object: OnChartGestureListener {
    override fun onChartGestureEnd(me: MotionEvent?, lastPerformedGesture: ChartTouchListener.ChartGesture?) {
        if (lastPerformedGesture == ChartTouchListener.ChartGesture.DRAG && limitLineDragEnabled) {
            me?.let {
                displayLimitLine(me)
            }
            limitLineDragEnabled = false
        }
    }

    override fun onChartLongPressed(me: MotionEvent?) {
        if (binding.chart.isFullyZoomedOut) {
            limitLineDragEnabled = true

            me?.let {
                displayLimitLine(me)
            }
        }
    }
}

and a ChartTouchListener to catch MotionEvents between drag begin and end (sadly, OnChartGestureListener wasn't able to help me there):

chart.onTouchListener = object: BarLineChartTouchListener(chart,  chart.viewPortHandler.matrixTouch, 3f) {

    @SuppressLint("ClickableViewAccessibility")
    override fun onTouch(v: View?, me: MotionEvent?): Boolean {
        if (limitLineDragEnabled) {
            me?.let {
                displayLimitLine(me)
            }
        }

        return super.onTouch(v, me)
    }

}
  • Your answer was helpful to me. Thanks mate ! I then use chart.viewPortHandler.contentHeight() / chart.viewPortHandler.contentWidth() to get the actual size of the chart (without the axis paddings) – lolo.io Oct 26 '22 at 05:51