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

Sparkline graph clipped at top on macOS #13

Closed
martindufort opened this issue Oct 25, 2023 · 10 comments
Closed

Sparkline graph clipped at top on macOS #13

martindufort opened this issue Oct 25, 2023 · 10 comments

Comments

@martindufort
Copy link

I'm using DSFSparkline within our macOS app to display a last 30-day activity chart. Currently the chart is generated with random numbers like:

			var array = [0.0, 2.0, 4.0, 6.0, 7.0, 11.0, 13.0]
			let values = (0..<10).map { _ in CGFloat(array.randomElement()!) }
			self.activityDataSource = DSFSparkline.DataSource(values: values)
			self.activitySpark.dataSource =	self.activityDataSource

That graph is contained within a view and constrained to the bottom with a specific height of 30
However I noticed that, in some cases, the top of the graph is clipped.

See screenshot:
CleanShot 2023-10-25 at 13 59 49@2x

Thanks

@dagronf
Copy link
Owner

dagronf commented Nov 9, 2023

Hey @martindufort,

Apologies for not responding sooner -- unfortunately the issue is that the algorithm I'm using to smooth the line introduces overshoot into the line and it ends up drawing outside the drawable bounds of the graph.

A simple workaround is to turn off interpolation (which is, of course, not a great solution). Without interpolation the overshoot doesn't occur.

I've worked out a proper solution but it has a bit of a wide-ranging impact on the line graph itself, which means that it will take more time to implement and test correctly. And unfortunately I've been swamped work-wise as well which hasn't been helping. Hoping I can get up a fix for this soon.

@dagronf
Copy link
Owner

dagronf commented Nov 10, 2023

@martindufort can you tell me how you're displaying the graph? Are you using a DSFSparklineLineGraphView(), or a DSFSparklineLineGraphView.SwiftUI in SwiftUI? I'm just running through some tests and want to make sure I capture your use case

@martindufort
Copy link
Author

martindufort commented Nov 13, 2023

Hey @dagronf, thanks for the reply.
I went back to the code (this is in a labs project) and the DSFSparklineLineGraphView is used in an AppKit view and set with proper constaints to be pinned at the bottom of the view.

  • Pinned to left, right and bottom
  • Height set to 30 pixels

Let me know if you need more information.

@martindufort
Copy link
Author

Just tried without interpolation within our Labs Project, there is no overshoot but the graph looks meh ;)
I tried this quick fix on the parent view

        self.view.layer?.masksToBounds = false

but the clipping is still occuring.

@dagronf
Copy link
Owner

dagronf commented Nov 14, 2023

Hey @martindufort,

The issue arises with the old code when there are two (or more) consecutive values near the upper or lower bounds of the graph. The interpolation overshoot ends up being higher than the upper bounds, which means that it gets clipped.

Interestingly enough, I tried the same as you self.view.layer?.masksToBounds = false first and was annoyed it wasn't an easy fix hehe. Turns out I was very careful during the drawing phases and made sure that I clip the drawing to the bounds. While this is annoying, it has helped me with Sonoma as its rendering changes means that without this care it would be drawing wildly outside its bounds.

I've made changes to the code to take into account the interpolation overshoot (basically insetting the graph by a percent of the graph height), and I think I haven't introduced any line rendering errors. This means that interpolated line graphs will have a slight inset at the top and bottom.

I've pushed the code to a branch called line-interpolation-fix-bug-13. Is it possible to try linking your code against the line-interpolation-fix-bug-13 branch and seeing if you see the error? If you can confirm that you're no longer seeing the error then I'll tag it (most likely 5.0.0) and let you know.

Cheers and thanks!

@martindufort
Copy link
Author

This means that interpolated line graphs will have a slight inset at the top and bottom.

I was actually looking at the code trying to figure out same.
Let me connect my package dependency to the branch line-interpolation-fix-bug-13 and see if everything is ok.

Thanks for this

@martindufort
Copy link
Author

martindufort commented Nov 15, 2023

Not sure what this issue is all about...
CleanShot 2023-11-15 at 12 07 10@2x

Update: Looks like "Deleting DerivedData" and restarting Xcode solved that...

@martindufort
Copy link
Author

martindufort commented Nov 15, 2023

Ok it's working for me...
Graph is beautifully rendered for all my random values.

See demo video here:
Updated: I jumped the disclosure gun with the demo video.... later perhaps

@dagronf
Copy link
Owner

dagronf commented Nov 15, 2023

Okay - that's good news! I'll push the changes to master and tag it as 5.0.0. When you next get a chance if you could link against master tag 5.0.0 and let me know if all seems well?

Many thanks for the feedback and patience mate.

@martindufort
Copy link
Author

Super... validated and working with 5.0.1.

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

2 participants