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 1 year 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

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

LeeLenaleee avatar Oct 01 '24 22:10 LeeLenaleee

Closing as per my above comment, if you still have the issue feel free to re open the issue

LeeLenaleee avatar Nov 14 '24 22:11 LeeLenaleee