chartjs/Chart.js

Documentation on Legend Configuration is outdated - please update

coreymunn3 opened this issue · 2 comments

Documentation Is:

  • Missing or needed?
  • 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

Do you have a reproducible sample of your error, since this implementation in the docs is not outdated. This is the exact implementation the legend plugin currently uses.

https://github.com/chartjs/Chart.js/blob/ea88dba68d41d4974c1fff5ce1c60f5d68279c13/src/plugins/plugin.legend.js#L645C5-L655C7

image