Search code examples
iosmpandroidchartios-charts

User Interaction at runtime with MPAndroidChart


First I should say MPAndroidChart is awesome, you have brought in several functionalities and customization which makes it really cool.

I'm looking to add user interaction with the MPAndroidChart,

My requirement is:

In a combined chart (line chart and bubble chart), I want the user to drag and move the data point in x,y co-ordinate space of the MPAndroidChart.

I want the user to drag the data shown, How to achieve this, which class can be sub-classed to achieve this.


Solution

  • To add interactivity to the graph data - you need a view with pan gesture

    Need to subclass CombinedChartView, LineChartRenderer, BubbleChartRenderer

    Create the Renderer objects, - this renderer objects needed to be replaced by the renderers created in CombinedChartView, Hence we subclass it and replace the renderer objects with that of our subclassed version.

    In our CombinedChartView(subclass) override the getter,setter of the data variable, Hence while assigning the data, we create our UIView once with Pan gesture, take a reference of it's corresponding ChartDataEntry in UIView, Hence in UIView's Pan handler function we change this DataEntry's xIndex etc and call setNeedsDisplay.(UIView's frame position is not know yet, keep it all at origin)

    In our Renderer class (subclass of LineChartRenderer or BubbleChartRenderer) override the DrawDataSet function -this is where the actual drawing happens, this is the place where we could get the exact x,y co-ordinates for our UIView, We finally set the frame position for the views created in CombinedChartView.

    That's it, when we pan the UIView, the view will pan and in-turn move the xIndex of the ChartDataEntry