Skip to content
Advertisement

Remove redundant legends on the chart using generateLabels with ChartJS v3

I have a chart as follows, I only need one pair of legends (data 1 and data 2) to show on the chart.

In ChartJS v2, I can use generateLabels like this but it doesn’t seem to work the same way in v3 that I’m using.

Is there an easy way to achieve this in v3 using generateLabels or do I have to change the DOM structure?

JavaScript
JavaScript
JavaScript

Advertisement

Answer

options.plugins.legend.labels.generateLabels can still be used in Chart.js v3. For your case, this could look as follows:

JavaScript

JavaScript
JavaScript
JavaScript

The problem with the approach is, that the displayed legend elements are still tied to individual datasets only. If you want to show/hide other datasets as well, you also need to define a legend.onClick function, similar to the one from this answer.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement