Skip to content

chart._stacks isn't always cleared when it should be #12154

@markw65

Description

@markw65

Expected behavior

In the example two datasets, A and B are plotted. Every point on B is one unit higher than the corresponding point on A.

Initially, the chart should be plotted, and show the two lines running parallel to each other.

After two seconds, the order of A and B is swapped, and the chart updated. The lines should remain unchanged, and the labels should swap positions.

After two more seconds, A and B are swapped again, chart._stacks is deleted, and the chart is updated again. Again, the plot should not change, but the labels should go back to their original places.

Current behavior

After the first swap, the portion of line A beyond x=2.0 is incorrectly stacked, causing the line to be displayed above its true value. It crosses above line B (even though its values are all lower than B's).

The second swap clears chart._stacks, avoiding the issue, and the lines go back to where they should be. Without the delete, B ends up being stacked on top of A.

Reproducible sample

https://codepen.io/markw65/pen/PwNmodE

Optional extra steps/info to reproduce

The issue appears to be in clearStacks.

If any data point doesn't have stack data for the current dataset it returns. But if two datapoints in the same dataset have the same x-coordinate, they will share their _stacks data, so for the first point there will be data, and it will be cleared, and then the second point won't have any data.

It looks like the return is a bug, introduced by 1e296cc, when a forEach was replaced by a for...of. In the old code, the return would have just skipped the current iteration, in the new it skips the remainder of the data.

Possible solution

Replace the return with continue.

This may result in a small performance regression, if it's really expected that either every point has _stacks data, or no point does.

But I'm dubious. If I set parsing=false, for example, I could insert a new point in some datasets, and re-order them, and the new points wouldn't have _stacks, resulting in the same issue.

Context

I'm simply switching different datasets into the chart under user control. Sometimes portions of the data for some datasets gets plotted as if it were stacked.

The reason the axis is marked stacked: "single" is because there are several independent data sets that can be plotted separately, and each one can also be broken down into several components, and plotted as a stack. The user can choose to display any and all of these together.

chart.js version

v4.5.1 (still present in master)

Browser name and version

any

Link to your project

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions