Chart.js icon indicating copy to clipboard operation
Chart.js copied to clipboard

Documentation on Legend Configuration is outdated - please update

Open coreymunn3 opened this issue 4 months ago • 1 comments

Documentation Is:

  • [ ] Missing or needed?
  • [X] Confusing
  • [ ] Not sure?

Please Explain in Detail...

Currently, at the configuration > legend section, if you scroll down to custom onClick actions, it says the default behavior is this

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;
    }
}

However this is not correct. When I copied and pasted that code into my options.plugins.legend.onClick function, I got several errors summarized perfectly in this stackoverflow post.

The post also explains the fix and the purpose of this issue, which is to update the chartjs Docs to say, the current default behavior is actually

(e, legendItem, legend) => {
                                legend.chart.toggleDataVisibility(
                                  legendItem.index
                                );
                                legend.chart.update();
                              }

Your Proposal for Changes

Change the documentation to be correct as stated above.

Example

https://www.chartjs.org/docs/latest/configuration/legend.html#custom-on-click-actions

coreymunn3 avatar Oct 01 '24 17:10 coreymunn3