Skip to content

Removing an active item's data makes the next update() throw in a scriptable option #12563

Backlog

Description

@kurkle

When an active element's data point is removed (data.pop() or a shorter data array), the next chart.update() still re-applies or removes the hover style for that index. getStyle then resolves options for an index past the end of the data, so a scriptable option gets the dataset context, where ctx.raw is undefined.

const chart = new Chart(ctx, {
  type: 'line',
  data: {datasets: [{
    data: [1, 2, 3, 4, 5, 6].map((y, x) => ({x, y})),
    pointBackgroundColor: (ctx) => (ctx.raw.y > 1 ? 'red' : 'blue')
  }]},
  options: {scales: {x: {type: 'linear'}}}
});
chart.setActiveElements([{datasetIndex: 0, index: 5}]); // or hover the last point
chart.data.datasets[0].data.pop();
chart.update(); // TypeError: Cannot read properties of undefined (reading 'y')
  • It throws in 4.6.1, 4.6.2 (the published UMD builds) and on master (31a75bf), with setActiveElements and with pointer hover, after pop() or after replacing the array, with update() and update('none'), and with animations on or off.
  • fix: skip missing active elements when setting hover #12411 (in 4.6.2, for Fix Bug setting active hover to undefined #12042) drops active elements that are missing when setActiveElements is called. Here the element exists when it is set, and its data is removed later, so that fix does not cover this case.
  • With setActiveElements, the throw comes from update() → _updateHoverStyles → setHoverStyle → getStyle(5). With pointer hover, it comes from the replayed event's removeHoverStyle for index 5.
  • The stale item stays in _active after the throw, so every later update() throws again.
  • Without a scriptable option nothing throws, but getActiveElements() keeps returning {datasetIndex: 0, index: 5} for an element that is no longer in the dataset.

Expected: an active item whose data was removed stops being active at the next update, and no option is resolved for it.

A possible fix, not yet measured: drop active items whose index is not below the dataset's element count at the start of update(). #12560 adds the same filter for hidden data, and it could be extended to cover removed indices.

Activity

  1. FullStraing commented on Nov 27, 2026

    @FullStraing

    I'll take this — working on a fix that drops stale active items in update() once the element counts are current, so no option is resolved for a removed index. PR shortly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions