score:1
Update for Chart.js 3.x
This solution still works, but legend options have been placed inside "plugins". So, if you have a dataset whose label you don't want to appear on the legend, give it a specific name, like label: "none"
, for example, and then use the filter function:
options: {
plugins: {
legend: {
labels: {
filter: item => {
return item.text != "none"
}
}
}
}
}
score:2
First No official example of this issue under chart.js docs.
How to filter by value (Hide all legends for a value greater than 1000)
Filters legend items out of the legend. Receives 2 parameters, a Legend Item and the chart data. https://www.chartjs.org/docs/latest/configuration/legend.html#legend-label-configuration
basic example - "works like magic":
Data:
var data = {
labels: ["Africa", "Asia", "Europe"],
datasets: [{
label: "Population (millions)",
backgroundColor: ["#3e95cd", "#8e5ea2","#3cba9f"],
data: [1000,1500,2000]
}]
};
The "trick" - get the array of values:
data.datasets[0]
Like this i get the data
array.
Next - The filter
loop throw data - Each time i get the current value by index
.
Basic Array idea: How to get value at a specific index of array In JavaScript?
var index = legendItem.index; /* 0..1...2 */
var currentDataValue = data.datasets[0].data[index]; /* 1000...1500...2000 */
if "consept"
Show all legends:
if (true){
return true;
}
And this hide all legends
if (false){
return true;
}
So add any if
statement you want (data value in our case):
if (currentDataValue > 1000){
return true;
}
Complete example:
Hide legend if the value is greater than 1,000:
var data = {
labels: ["Africa", "Asia", "Europe"],
datasets: [{
label: "Population (millions)",
backgroundColor: ["#3e95cd", "#8e5ea2","#3cba9f"],
data: [1000,1500,2000]
}]
};
var options = {
responsive: true,
title: {
text: 'Hide africa legened (Greater than 1000)',
position: 'top',
display: true
},
legend: {
display: true,
labels: {
/* filter */
filter: function(legendItem, data) {
/* filter already loops throw legendItem & data (console.log) to see this idea */
var index = legendItem.index;
var currentDataValue = data.datasets[0].data[index];
console.log("current value is: " + currentDataValue)
if (currentDataValue > 1000)
{
return true; //only show when the label is cash
}
},
/* generateLabels */
generateLabels(chart) {
const data = chart.data;
if (data.labels.length && data.datasets.length) {
/* inner loop throw lables */
return data.labels.map((label, i) => {
var backgroundColor = data.datasets[0].backgroundColor[i];
return {
text: label + " | " + data.datasets[0].data[i],
fillStyle: backgroundColor,
// Extra data used for toggling the correct item
index: i
};
});
}
return [];
}
},
},
scales: {
xAxes: [{
display: true
}],
yAxes: [{
display: true
}]
}
};
new Chart(document.getElementById("chart"), {
type: 'pie',
data: data,
options: options
});
<canvas id="chart" width="800" height="450"></canvas>
<script src="https://cdn.jsdelivr.net/npm/chart.js@2.8.0"></script>
By the way in the example above i also use generateLabels
idea (Add the value next to label text).
score:2
In my case, changing the label to "hidden" caused that to show in the tooltip, instead of the text I wanted.
So here's a way you can target each dataset's legend by it's index number without changing the label!
const chart = new Chart(ctx, {
type: 'bar',
data: data,
options: {
legend: {
labels: {
filter: function(item, chart) {
return item.datasetIndex !== INDEX_OF_DATASET && item.datasetIndex !== INDEX_OF_ANOTHER_DATASET;
}
}
}
}
});
In my case, I'm using wpDataTables to create a chart in WordPress, so the code looks like this:
wpDataChartsCallbacks[INDEX_OF_CHART_HERE] = function (obj) {
obj.options.options.legend.labels.filter = function(item, chart) {
return item.datasetIndex !== INDEX_OF_DATASET && item.datasetIndex !== INDEX_OF_ANOTHER_DATASET;
}
}
It's working great for me!
score:2
**You can use something like this to filter out legends in your chart: **
plugins: {
legend: {
labels: {
filter: function( item, chart){
if (item.text == 'Open' || item.text == 'High') {
return false;
}else{
return item;
}
}
}
},
}
score:72
Short answer: Yes it is possible. Unfortunately it's not quite as simple as the developers could make it.
If you know what the text
value of the item being displayed in the legend is, then you can filter that out. After reading through the Chart.js docs I found the section Legend Label Configuration that details a filter
function that can be used to "filter out the legend items", although this function must be set on the parent chart options object, not as an option on the dataset itself:
const chart = new Chart(ctx, {
type: 'bar',
data: data,
options: {
legend: {
labels: {
filter: function(item, chart) {
// Logic to remove a particular legend item goes here
return !item.text.includes('label to remove');
}
}
}
}
});
Now it appears each time the data changes and the chart is updated via chart.update()
this filter function is called.
For convenience I have set this up in a jsfiddle for you to play with.
Note: This solution was designed around the API for version 2.7.1 of ChartJS. Future versions may improve the control over dataset legend labels.
Source: stackoverflow.com
Related Query
- Is it possible in chartjs to hide certain dataset legends?
- Chartjs hide dataset legend v3
- how to hide specific dataset based on condition chartjs angular
- Hide legends in Chartjs
- chartjs - show hide specific dataset with click on element outside graph
- Chart.js v2 hide dataset labels
- Hide points in ChartJS LineGraph
- How to disable a tooltip for a specific dataset in ChartJS
- Chartjs hide data point labels
- How to fix chart Legends width-height with overflow scroll in ChartJS
- Hide labels on x-axis ChartJS
- ChartJs how to get from mulitiple dateset which dataset bar is clicked
- ChartJS hide labels on small screen sizes
- Hide gridlines in chartjs without the drawTicks
- Chartjs doesn't update dataset label on tooltips
- In ChartJS is it possible to change the line style between different points?
- Is it possible to combine two Y axes into a single tooltip with ChartJS 2?
- onClick event to Hide dataset Chart.js V2
- In chart.js, Is it possible to hide x-axis label/text of bar chart if accessing from mobile?
- Chartjs line graph dataset with offset
- Loop Dataset ChartJS Javascript
- hide dataset by default using Chart.js and a custom script
- Displaying mixed types of legends (bar and lines) with Chartjs
- How to dynamically set ChartJs line chart width based on dataset size?
- Chartjs bar dataset label not showing
- How to add ChartJS code in Html2Pdf to view image
- Unable to hide Legend in Chartjs with PrimeFaces7.0
- ChartJS Annotation Hide /Show
- Possible to hide Chart.js grid lines that extend beyond chart?
- Is it possible to change pointStyle for elements of a ChartJS bubble chart per dataset?
More Query from same tag
- Merge 2 charts into 1 chart using update button (chart.js)
- Chart.js responsive css size
- Angular Charts.js: Can't refresh chart with data
- How to iterate over array elements inside objects in javascript
- How to reformat tooltip in Chart.js?
- How to set data values as labels in Chart.js with a Radar Chart
- Usage of ChartJS v3 with TypeScript and Webpack
- How to install Chart.js in Laravel?
- How to set min max for x axis depend on data with Chartjs and Spring Boot?
- How to create a variable for chart js datalabel plugin formatter
- Label not showing in Chart.js with Grails
- Is it possible in chartjs to hide certain dataset legends?
- Angular and ChartJS to create bar chart
- Chart.js showing x-axis ticks even though set to false
- Line is rendering only vertical line instead of sloped line
- How to set single color on each bar in angular chart js
- Implement a cash flow timeline
- ChartJS 2.7.0 updating and exporting as png
- ChartJS yAxis showing not coherent data
- Using ng2-charts, how to "zoom out"?
- Calling MouseLeave chartJs Angular
- Chart.js I want to fix my xAxis to my yAxis position 0
- When I added a funnel chart to chartjs all the charts are load compressed until resize page
- Chart.js Bar Chart: How to remove space between the bars in v2.3?
- chartjs-plugin-annotation box in Django app
- Need to put border on variablepie highchart?
- Cannot read property 'reactiveProp' of undefined in vue-chartjs
- How to trigger ChartJS legend onClick with out interrupting its normal working
- Hovering over chart.js values in Meteor onRendered function causes chart axis shift
- Chartjs: Multiple data values for the same label