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

LineStyle.Hairline: Option for single pixel line widths regardless of scale factor #3995

Merged
merged 4 commits into from
Jun 27, 2024

Conversation

bforl
Copy link
Contributor

@bforl bforl commented Jun 25, 2024

This PR is in relation to #3977

Hairline is only applicable when ScaleFactor is greater than 1. If the ScaleFactor is 1, you won't see the visual artefacts mentioned below.

When Hairline is set to true, and the Width is 0, it will instruct Skia to render a single pixel line no matter the scaling factor.

The first image shows a scaling of 1.5, without Hairline, and AntiAliasing disabled on the second Box
Notice the lines are inconsistent in their width. Its more evident if you resize the window.

NoHairline

The second image shows the same setup, with Hairline enabled on both the Grid and second Box

Hairline

I have made Hairline available on both GridStyles and LineStyles, and it is OptIn - so the current behaviour is not changed.
I have also added a helper method to AxisManager to enable Hairline for all this line drawing.

public MainWindow()
 {
     InitializeComponent();

     WpfPlot1.Plot.ScaleFactor = 1.5;
     WpfPlot1.Plot.Axes.Hairline(true);

     ScottPlot.Box box = new()
     {
         Position = 4.5,
         BoxMin = 81,
         BoxMax = 93,
         WhiskerMin = 76,
         WhiskerMax = 107,
         BoxMiddle = 84,
     };

     WpfPlot1.Plot.Add.Box(box);

     box = new()
     {
         Position = 5.5,
         BoxMin = 81,
         BoxMax = 93,
         WhiskerMin = 76,
         WhiskerMax = 107,
         BoxMiddle = 84,
     };

     box.LineStyle.Width = 0;
     box.LineStyle.AntiAlias = false;
     box.LineStyle.Hairline = true;

     WpfPlot1.Plot.Add.Box(box);

     WpfPlot1.Plot.Axes.SetLimits(0, 10, 70, 110);




     WpfPlot1.Refresh();
 }

@swharden
Copy link
Member

Thanks for this @bforlgreen! I'm going to refine the API slightly, add a Cookbook example demonstrating this new feature, then merge in a few minutes 🚀

@swharden swharden changed the title Add support for hairline LineStyle.Hairline: Option for single pixel line widths regardless of scale factor Jun 26, 2024
@swharden
Copy link
Member

Hi @bforlgreen, I refactored this a bit so that Hairline, when enabled, ignores Width entirely. It peeks into the SKCanvas scale factor and sets pixel width accordingly. I think my changes left this branch in a good state, but I didn't have time to test it extensively so I'll leave it where it is for now and take a closer look and merge when I get home this evening.

Thanks again for this PR! This is looking fantastic!

@bforl
Copy link
Contributor Author

bforl commented Jun 26, 2024

Hi Scott, I think there might be some confusion. Paint.StrokeWidth needs to be 0 for hairline

@swharden
Copy link
Member

Hi Scott, I think there might be some confusion. Paint.StrokeWidth needs to be 0 for hairline

After 805a27c I think this is no longer the case.

I added a cookbook recipe demonstrating the new feature, and I think it's looking pretty good! I'll merge this now, but if you have any recommendations for improvements still we can open a new PR.

Thanks again for this great new feature! 🚀

@swharden swharden merged commit 9f9b114 into ScottPlot:main Jun 27, 2024
3 checks passed
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

Successfully merging this pull request may close these issues.

2 participants