chartjs / chartjs/Chart.js

The example for overriding legend onClick is outdated

Open
#12,144 0 comments 0 reactions 0 assignees View on GitHub
type: documentation
Dominant language
JavaScript
Stars
67.7k
Forks
11.9k
Avg merge
7h 39m
Merged PRs (30d)
5

Description

### Documentation Is:

- [ ] Missing or needed?
- [ ] Confusing
- [ ] Not sure?

### Please Explain in Detail...

Concerning https://www.chartjs.org/docs/latest/configuration/legend.html

The example for overriding legend onClick is outdated. The code throws error `Uncaught TypeError: Cannot read properties of null (reading '_resolveAnimations'`

it is currently:

``` javascript
function(e, legendItem, legend) {
const index = legendItem.datasetIndex;
const ci = legend.chart;
if (ci.isDatasetVisible(index)) {
ci.hide(index);
legendItem.hidden = true;
} else {
ci.show(index);
legendItem.hidden = false;
}
}
````

### Your Proposal for Changes

It should be

``` javascript
legend.chart.toggleDataVisibility(legendItem.index);
legend.chart.update();
```

### Example

_No response_

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.