Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the scroll offset on MPAndroidChart?

I have been using the MPAndroidChart library to display a line graph of the currently playing audio in an app I am building. However I want to make it so the user can scroll the chart, and the playback position of the playing audio changes in proportion to the chart scroll.

However I cant seem to find a way to get the closest x-index position to the center of the graph

enter image description here

As you can see in the screenshot, i want to be able to get the x-index behind the red line as the user scrolls, but i cant seem to figure out how to do this.

Any help would be much appreciated.

Thanks Corey B :)

like image 223
Fishingfon Avatar asked Jan 20 '15 01:01

Fishingfon


1 Answers

You can set onChartGestureListener or onTouchListener for chart to catch scroll events and than calculate current plot position:

float lowX = chart.getLowestVisibleX()
float xrange = chart.getVisibleXRange() //better to fix x-ranges, getVisibleXRange may be inaccurate
float centerX = lowX + xrange / 2
like image 100
adray Avatar answered Oct 18 '22 20:10

adray