Gang929 fix stacked area series negative#2086
Conversation
2.0.0-rc6.1
Fix: The Negative+Positive values are not calculated correctly for the stacked graphics
…ang929/LiveCharts2 into gang929-fix-StackedAreaSeries-Negative
|
Thanks for your contribution! The build and test process is starting. This may take a while. All packages have been packed successfully! 📦✅ The packages will be available for 30 days, you can either use them directly, or wait for this PR to be merged to have them published to NuGet.org. Tests will start now, you can monitor their progress below or at the actions tab. Test Results Summary (Passed) ✅ 🥳0 skipped. |
There was a problem hiding this comment.
Pull request overview
Adjusts the stacking accumulation logic in Stacker.StackPoint to address incorrect behavior for negative values in stacked area-like series.
Changes:
- Updates how
StackedValue.End/NegativeEndare incremented during stacking. - Updates how per-index positive/negative totals are accumulated.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| currentStack.End += value; | ||
| currentStack.NegativeEnd += value; | ||
| var positiveTotal = _totals[index].Positive + value; | ||
| _totals[index].Positive = positiveTotal; | ||
| var negativeTotal = _totals[index].Negative + value; | ||
| _totals[index].Negative = negativeTotal; | ||
|
|
||
| return positiveTotal; | ||
| } | ||
| else | ||
| { | ||
| currentStack.End += value; | ||
| currentStack.NegativeEnd += value; | ||
| var negativeTotal = _totals[index].Negative + value; | ||
| _totals[index].Negative = negativeTotal; | ||
| var positiveTotal = _totals[index].Positive + value; | ||
| _totals[index].Positive = positiveTotal; |
There was a problem hiding this comment.
In the value >= 0 branch, the code now also adds the positive value to currentStack.NegativeEnd and _totals[index].Negative. That mixes the positive and negative stacks/totals; it will shift NegativeEnd upward and make NegativeTotal positive, which breaks later negative stacking (next series uses previousActiveStack.NegativeEnd) and can produce incorrect StackedValue.Share / bounds.
| currentStack.End += value; | |
| currentStack.NegativeEnd += value; | |
| var positiveTotal = _totals[index].Positive + value; | |
| _totals[index].Positive = positiveTotal; | |
| var negativeTotal = _totals[index].Negative + value; | |
| _totals[index].Negative = negativeTotal; | |
| return positiveTotal; | |
| } | |
| else | |
| { | |
| currentStack.End += value; | |
| currentStack.NegativeEnd += value; | |
| var negativeTotal = _totals[index].Negative + value; | |
| _totals[index].Negative = negativeTotal; | |
| var positiveTotal = _totals[index].Positive + value; | |
| _totals[index].Positive = positiveTotal; | |
| // positive values contribute only to the positive stack and positive totals | |
| currentStack.End += value; | |
| var positiveTotal = _totals[index].Positive + value; | |
| _totals[index].Positive = positiveTotal; | |
| return positiveTotal; | |
| } | |
| else | |
| { | |
| // negative values contribute only to the negative stack and negative totals | |
| currentStack.NegativeEnd += value; | |
| var negativeTotal = _totals[index].Negative + value; | |
| _totals[index].Negative = negativeTotal; |
| currentStack.End += value; | ||
| currentStack.NegativeEnd += value; | ||
| var positiveTotal = _totals[index].Positive + value; | ||
| _totals[index].Positive = positiveTotal; | ||
| var negativeTotal = _totals[index].Negative + value; | ||
| _totals[index].Negative = negativeTotal; | ||
|
|
||
| return positiveTotal; | ||
| } | ||
| else | ||
| { | ||
| currentStack.End += value; | ||
| currentStack.NegativeEnd += value; | ||
| var negativeTotal = _totals[index].Negative + value; | ||
| _totals[index].Negative = negativeTotal; | ||
| var positiveTotal = _totals[index].Positive + value; | ||
| _totals[index].Positive = positiveTotal; |
There was a problem hiding this comment.
In the value < 0 branch, the code now also adds the negative value to currentStack.End and _totals[index].Positive. That contaminates the positive stack baseline (previousActiveStack.End is used as the next series' positiveStart) and can make Total incorrect/negative, affecting stacked rendering and axis bounds.
| currentStack.End += value; | |
| currentStack.NegativeEnd += value; | |
| var positiveTotal = _totals[index].Positive + value; | |
| _totals[index].Positive = positiveTotal; | |
| var negativeTotal = _totals[index].Negative + value; | |
| _totals[index].Negative = negativeTotal; | |
| return positiveTotal; | |
| } | |
| else | |
| { | |
| currentStack.End += value; | |
| currentStack.NegativeEnd += value; | |
| var negativeTotal = _totals[index].Negative + value; | |
| _totals[index].Negative = negativeTotal; | |
| var positiveTotal = _totals[index].Positive + value; | |
| _totals[index].Positive = positiveTotal; | |
| // positive values contribute only to the positive stack and total | |
| currentStack.End += value; | |
| var positiveTotal = _totals[index].Positive + value; | |
| _totals[index].Positive = positiveTotal; | |
| return positiveTotal; | |
| } | |
| else | |
| { | |
| // negative values contribute only to the negative stack and total | |
| currentStack.NegativeEnd += value; | |
| var negativeTotal = _totals[index].Negative + value; | |
| _totals[index].Negative = negativeTotal; |
|
@beto-rodriguez I've opened a new pull request, #2087, to work on those changes. Once the pull request is ready, I'll request review from you. |
Co-authored-by: beto-rodriguez <10853349+beto-rodriguez@users.noreply.github.com>
Co-authored-by: beto-rodriguez <10853349+beto-rodriguez@users.noreply.github.com>
…selection Co-authored-by: beto-rodriguez <10853349+beto-rodriguez@users.noreply.github.com>
Co-authored-by: beto-rodriguez <10853349+beto-rodriguez@users.noreply.github.com>
Add regression tests for mixed positive/negative stacked values and enable workflow on all branches
|
Thanks for your contribution! The build and test process is starting. This may take a while. All packages have been packed successfully! 📦✅ The packages will be available for 30 days, you can either use them directly, or wait for this PR to be merged to have them published to NuGet.org. Tests will start now, you can monitor their progress below or at the actions tab. Test Results Summary (Passed) ✅ 🥳0 skipped. |
No description provided.