Scrollable Plot Area

Is there a way to create scrolling/paging behavior in the charts just like is done in the Health app? I have a similar app that also displays health data (such as heart rate and steps) and with my current custom charts the user can page back to see data from previous weeks (by default it shows the current week). Just like in the health app the user can go back as long as there is data, which can be several years (so hundreds go pages). I'd love to replace my custom implementation with Swift Charts but for that I do need this scrolling behavior.

Post not yet marked as solved Up vote post of lammertw Down vote post of lammertw
3.0k views
  • This question was asked in the lounges and it was said that this is apparently possible, but no details were shared. Can someone from Apple please answer this??

Add a Comment

Replies

Have you found the answer yet?

Would like to know as well!

I'm about to implement a similar use-case: I already chart data for the current week, current month, quarter and year, as selected by a picker. The key to the paging solution is to replace the chart dataset (series), when the user swipes left or right, with the appropriate next or previous set (period) of data. In my case, I'll use my existing Coredata fetch function with startDate, endDate parameters - called from within the Gesture Recogniser. We'll need a var with the currently displayed period in order to set the required retrieval startDate, endDate. And there also needs be a @State var to trigger a chart redraw once the new dataset has been returned (maybe the dataset itself or the period or a flag). Or, the period and dataset creation/recreation can be handled in the DataModel (my usual approach) with @Published vars.

This should work fine for discrete periods (i.e. week, month, etc), but continuous scrolling over a large dataset is a different proposition. I've done it with a SwiftUI Canvas app, but it was hard to get smooth scrolling with many fetches from a large database.

I'll post more when my paging solution works (or doesn't......).

Regards, Michaela

Would like to know as well!

yes you can implement scrolling behavior by putting your entire chart inside a horizontal scroll view

ScrollView(.horizontal) {
Chart{...}

}

this was covered by Jordi Bruin, you can check it out here on his Github repo https://github.com/jordibruin/Swift-Charts-Examples i hope this is what your re looking for

Cheers,

Jono

  • Thanks for the link. The downside of this method is that the y-axis labels are also scrolled out of view. Furthermore, you have to manually calculate the charts frame width.

Add a Comment

https://blog.csdn.net/mydo/article/details/126569381?spm=1001.2014.3001.5501

See new APIs in iOS 17 for a better solution to all this.