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

Snap to position for x values (paging) #4030

Closed
1 task done
humanootc opened this issue Jun 17, 2019 · 3 comments
Closed
1 task done

Snap to position for x values (paging) #4030

humanootc opened this issue Jun 17, 2019 · 3 comments

Comments

@humanootc
Copy link

humanootc commented Jun 17, 2019

What did you do?

I have created a draggable LineChartView (only on the xAxis). I have been trying to have a snap to position (chart should automatically snap to the closest x value to center when scrolling ends) functionality similar to UICollectionView with paging enabled.
Tried implementing the chartViewDidEndPanning delegate method and using highestVisibleX to somehow get the value closest to center but it doesn't behave as expected when scrolling fast.
Is there a better way to achieve this functionality?

func chartViewDidEndPanning(_ chartView: ChartViewBase) {
        let highestVisibleX = lineChartView.highestVisibleX.rounded()
        lineChartView.centerViewToAnimated(xValue: highestVisibleX - 1, yValue: 0, axis: .left, duration: 0.33)
    }

Charts Environment

Charts version/Branch/Commit Number: 3.3.0 / master
Xcode version: 10.2.1
Swift version: 5
Platform(s) running Charts: iOS
macOS version running Xcode: 10.14.5

@liuxuan30
Copy link
Member

ask on stack overflow for such questions plz.

@ivarvanwooning
Copy link

ivarvanwooning commented Feb 22, 2021

I managed to fix this issue myself by using DispatchQueue and a delay of 0.75 seconds. The downside of this is that the update is only triggered after 0.75 seconds; the upside is that it is accurate.

func chartViewDidEndPanning(_ chartView: ChartViewBase) { // when panning is stopped, print lowest and highest visible X DispatchQueue.main.asyncAfter(deadline: .now() + 0.75) { [self] in print(parent.chartView.lowestVisibleX.rounded()) print(parent.lineChart.highestVisibleX.rounded()) } }

@aaronsheah
Copy link

am looking to implement something similar any chance @ivarvanwooning could post an example? 🙏

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