[FEATURE] Disabled legend by datasets
See original GitHub issueCurrent Behavior
I’m looking for a feature, I did not found how is it possible to not display all legends of a datasets, i develop a code, when a user click on a certain legend of a dataset, the datasets item + 1 is hidden. but i don’t want the user to click on the datasets + 1. See next pictures.
I don’t want to display legend 1 , 2 , 3 it correspond to the 3 straigth lines on the chart.
Here’s the code to hide n+1 line
options: {
legend: {
onHover: function (e) {
e.target.style.cursor = 'pointer';
},
onClick: function (e, legendItem) {
var index = legendItem.datasetIndex;
var ci = this.chart;
var meta = ci.getDatasetMeta(index);
if (index != 1 && index != 3 && index != 5) {
var meta1 = ci.getDatasetMeta(index + 1);
meta1.hidden = meta.hidden === null ? !ci.data.datasets[index].hidden : null;
}
// See controller.isDatasetVisible comment
meta.hidden = meta.hidden === null ? !ci.data.datasets[index].hidden : null;
// We hid a dataset ... rerender the chart
ci.update();
}
},
}
Now I just don’t want to display legend item 1 - 3 -5
Possible Solution
Question : Is this already possible and i did not find how to do it ?
If it’s not possible already, Is it possible to add an option on the datasets structure like “displayLegend : true/false” like that :
datasets: [{
label: 'Indicateur de régularité',
data: dataIndicRegu,
backgroundColor: window.chartColors.purple,
borderColor: window.chartColors.purple,
borderWidth: 2,
fill: false,
lineTension: 0,
displayLegend: true,
},
{
label: 'Régularité total',
data: dataGlobalRegu,
backgroundColor: window.chartColors.purple,
borderColor: window.chartColors.purple,
borderWidth: 2,
borderDash: [4,6],
pointRadius : 0,
fill: false,
lineTension: 0,
displayLegend:false
},
Thank you
Issue Analytics
- State:
- Created 6 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
Show or hide a chart legend or data table - Microsoft Support
Show or hide a legend · To hide the legend, click None. Tip: To quickly remove a legend or a legend entry from...
Read more >Legend | Chart.js
The chart legend displays data about the datasets that are appearing on the chart. # Configuration options. Namespace: options.plugins.legend , ...
Read more >How to disable chartjs legendclick - javascript - Stack Overflow
My requirement is that I do not want to disable the dataset. I have tried the preventDefault(); on the chart click but it...
Read more >Legend | Vega-Lite
By default, Vega-Lite automatically generates gradient legends for color channels with non-binned quantitative fields and temporal fields.
Read more >Configure Legend - FusionCharts
To enable this feature, set the plotHighlightEffect attribute to fadeout . Refer to the code below: { "chart": { "plotHighlightEffect" ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Thanks for help , finally done, if that can help see code below
For this result :
@patryk-wlaz Actually it works. Certainly, I have no idea why the dataset array is being filtered and after, when returning the array of objects (legends) it’s asking again for:
i % 2 == 0, it supposes to be already filtered.If you don’t want that unnecessary blank space, just return the filtered legends:
Don’t do this:
This causes the unnecessary blank space However the dataset is already filtered, that’s the point!