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

Stack Overflow error when trying to draw Legend #637

Closed
andrew-baylis opened this issue Sep 22, 2022 · 2 comments
Closed

Stack Overflow error when trying to draw Legend #637

andrew-baylis opened this issue Sep 22, 2022 · 2 comments
Labels
bug Something isn't working

Comments

@andrew-baylis
Copy link

Describe the bug
WPF - using CartesianChart.
When the Legend is visible and any series has IsVisibleAtLegend = false, we get a stack overflow and crash.
Note that a PieChart works as expected with some series having IsVisibleAtLegend = fasle.

To Reproduce
Steps to reproduce the behavior:
WPF - using CartesianChart. LegendPosition is anything other than Hidden
Add a couple of series (e.g. Line Series, Scatter Series, Column Series)
In one of them, set IsVisibleAtLegend = false
When trying to draw the chart, a StackOverflowError occurs

Expected behavior
Legend draws properly

Desktop (please complete the following information):

  • OS: Windows 11
  • Version 2.0.0beta 400
@andrew-baylis
Copy link
Author

Found that the issue is in the Measure method of CartesianChart.cs in LiveChartsCore.

        if (Legend is not null && (SeriesMiniatureChanged(Series, LegendPosition) || SizeChanged())) 
        {
            Legend.Draw(this);
            PreviousLegendPosition = LegendPosition;
            PreviousSeriesAtLegend = Series.Where(x => x.IsVisibleAtLegend).ToList();
            foreach (var series in PreviousSeriesAtLegend.Cast<ISeries>()) series.PaintsChanged = false;
            preserveFirstDraw = IsFirstDraw;
            SetPreviousSize();
            Measure();
            return;
        }

The check SeriesMiniatureChanged(Series, LegendPosition) will always return true if one or more of the series has IsVisibleAtLegend false as we compare the full list of series with the partial list containing only those that should appear.

One way to fix is to do:

        var seriesForLegend = Series.Where(x => x.IsVisibleAtLegend).ToList(); //AJB change 24 Sep 2022

        //if (Legend is not null && (SeriesMiniatureChanged(Series, LegendPosition) || SizeChanged())) //old version
        if (Legend is not null && (SeriesMiniatureChanged(seriesForLegend, LegendPosition) || SizeChanged()))
        {
            Legend.Draw(this);
            PreviousLegendPosition = LegendPosition;
            PreviousSeriesAtLegend = Series.Where(x => x.IsVisibleAtLegend).ToList();
            foreach (var series in PreviousSeriesAtLegend.Cast<ISeries>()) series.PaintsChanged = false;
            preserveFirstDraw = IsFirstDraw;
            SetPreviousSize();
            Measure();
            return;
        }

@beto-rodriguez beto-rodriguez added the bug Something isn't working label Oct 4, 2022
beto-rodriguez added a commit that referenced this issue Oct 4, 2022
@beto-rodriguez
Copy link
Owner

Thanks for the report and the fix. it works!

this is fixed with the referenced commit and will be included in the next version of the library.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants