Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get scroll position of dragable line chart #1272

Closed
delsner opened this issue Jul 27, 2016 · 10 comments
Closed

Get scroll position of dragable line chart #1272

delsner opened this issue Jul 27, 2016 · 10 comments

Comments

@delsner
Copy link

delsner commented Jul 27, 2016

Probably a very simple question: I have created a simple dragable/scrollable linechart and need to get the current scroll position to determine whether I have reached the end of the chart. For more details see: http://stackoverflow.com/questions/38606896/get-scroll-position-of-dragable-linechart

@delsner
Copy link
Author

delsner commented Jul 27, 2016

it doesn't necessarily need to be the scroll position but more importantly an indicator telling that I have reached the newest (most right) value in the line chart.

@liuxuan30
Copy link
Member

would highestVisibleXIndex solve your problem? If the highest visible xIndex is the the last data entry xIndex.

@delsner
Copy link
Author

delsner commented Jul 28, 2016

Unfortunately not as the highestVisibleXIndex is the same for one entire screen width of chart entries. I tried reverse calculating the total possible translation (max transX) and subtract it by the current transX, but I do not get the exact value for the possible translation. Note that I add more data when scrolling to the left which changes the total possible translation while scrolling.

@liuxuan30
Copy link
Member

liuxuan30 commented Jul 29, 2016

Why highestVisibleXIndex does not work for you? The entry contains xIndex, so you just have to compare highestVisibleXIndex and your max xIndex value.

I couldn't find a more dedicated API for this right now. But I think what we could try here is to manually calculate the current offset, like contentOffset concept, though we don't have contentOffset property.

contentRect is the rect that defines the area of the chart content, excluding the axis stuff. By computing the matrix and contentRect, we should know when it reaches last entry.

Sorry it has been some time for me doing the matrix calculation, it's a little rusty. You can take a look at ChartViewPortHandler and ChartTransformer for the APIs that handles the data value coordinate space and pixel value coordinates.

One example I can come up right now:

We know the last entry, so we know the highest xIndex; this xIndex can be project to pixel coordinate system via pointValueToPixel(inout point: CGPoint), so we know the edge of the last data x coordinate, and we can figure out a way to calculate when user scrolls to last?

@liuxuan30
Copy link
Member

liuxuan30 commented Jul 29, 2016

I think I have seen such questions before, but couldn't find the old issues now

Also, please take a look at moveViewToX(xIndex: CGFloat) and centerViewTo(), these are examples for the above calculation I mentioned.

@delsner
Copy link
Author

delsner commented Jul 29, 2016

It doesn't work because I am using a dragOffset of half a screen width. Thus, when scrolling the highestVisibleXIndex stays the same for half a screen width when reaching the end, even though I can keep scrolling until the dragOffset is fully scrolled through. Sounds complicated, see http://stackoverflow.com/questions/38606896/get-scroll-position-of-dragable-linechart for a screenshot. I will have a look at pointValueToPixel. Thanks so far. By the way apparently the issue is common also in the android library: http://stackoverflow.com/questions/28036212/how-to-get-the-scroll-offset-on-mpandroidchart

@liuxuan30
Copy link
Member

liuxuan30 commented Jul 30, 2016

you implemented your own dragOffset? Seems I can't find it.

@delsner
Copy link
Author

delsner commented Aug 7, 2016

like this: self.chartView.setDragOffsetX(self.view.frame.width / 2)
It enables the user to scroll half of a screen size over the edge of the line chart. Thus, the highestVisibleXIndex stays the same for this offset.

@liuxuan30
Copy link
Member

hmm.. seems I can't provide more help here.. maybe ask @danielgindi

@danielgindi
Copy link
Collaborator

Well if you're adding a dragoffset - then of course highestVisibleXIndex won't be good for what you want, as it does not "bounce".

But you need to work with the viewport here.
Check out limitTransAndScale and canZoomOutMore. These do what you want, but not exactly:
limitTransAndScale calculates the new position in the given constraints - and you can reverse it to fetch the min/max values and determine if you're at the beginning/end.
canZoomOutMore is a convenience function that tells, well, if you can zoom out more...

So what I'm saying is- if you make a PR with the counterparts for canZoomOutMore, I'll be happy to merge it in :-)

@jjatie jjatie closed this as completed Apr 1, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants