score:8
As you already concluded, there isn't a way to configure chart.js out of the box to only show tooltips of specific datasets. However, you can use the plugin interface to create a plugin that can provide this functionality.
Here is a plugin that I created for your scenario that let's you configure which datasets you want the tooltip to display for.
Chart.plugins.register({
// need to manipulate tooltip visibility before its drawn (but after update)
beforeDraw: function(chartInstance, easing) {
// check and see if the plugin is active (its active if the option exists)
if (chartInstance.config.options.tooltips.onlyShowForDatasetIndex) {
// get the plugin configuration
var tooltipsToDisplay = chartInstance.config.options.tooltips.onlyShowForDatasetIndex;
// get the active tooltip (if there is one)
var active = chartInstance.tooltip._active || [];
// only manipulate the tooltip if its just about to be drawn
if (active.length > 0) {
// first check if the tooltip relates to a dataset index we don't want to show
if (tooltipsToDisplay.indexOf(active[0]._datasetIndex) === -1) {
// we don't want to show this tooltip so set it's opacity back to 0
// which causes the tooltip draw method to do nothing
chartInstance.tooltip._model.opacity = 0;
}
}
}
}
});
With the new plugin in place, you can now use a new property in the tooltips
config called onlyShowForDatasetIndex
that accepts an array of dataset indexes that the tooltip should display for.
tooltips: {
enabled: true,
mode: 'single',
// new property from our plugin
// configure with an array of datasetIndexes that the tooltip should display for
// to get the datasetIndex, just use it's position in the dataset [] above in the data property
onlyShowForDatasetIndex: [0, 1],
callbacks: {
label: function(tooltipItems, data) {
return data.datasets[tooltipItems.datasetIndex].label + ': ' + tooltipItems.yLabel + ' %';
}
}
}
Where the index value maps to the position of the dataset in the datasets
property.
Take a look at this codepen to see this in action. Notice the tooltips only show on the bar charts but not the line (since we did not include this dataset in the new configuration property).
score:1
The only way I was able to get this to work (v3.6.1) was by setting both pointRadius
and pointHitRadius
to zero. This also removes the points from the graph for that dataset but in my case that was something I wanted as well.
datasets: [ { data: [...], pointRadius: 0, pointHitRadius: 0, }, ]
score:3
You can use this filter:
tooltips: {
filter: function (tooltipItem, data) {
var datasetLabel = data.datasets[tooltipItem.datasetIndex];
var datapointLabel = data.labels[tooltipItem.index];
var datapointValue = data.datasets[tooltipItem.datasetIndex].data[tooltipItem.index];
//you can also use tooltipItem.xlabel or ylabel to get datapoint label and value but here it depends on you chart orientation
if (datasetLabel=="production" && datapointLabel=="red" && datapointValue<30) {
return false;
} else {
return true;
}
}
}
score:9
In the dataset declaration you can pass a parameter to each of the datasets that determines the hit radius (pointHitRadius
) for the hover event. If you set this to 0 it will prevent the tooltip from being launch.
noTipDataset = {
label: "No Tooltips here",
data: ...,
pointHitRadius: 0,
...The rest of your data declaration here
}
PS: this works in chartJS V2.6 but I haven't tested versions further back
score:36
There is now a way to configure charjs to do this; simply use the filter property:
tooltips: {
filter: function (tooltipItem) {
return tooltipItem.datasetIndex === 0;
}
}
Source: stackoverflow.com
Related Query
- How to disable a tooltip for a specific dataset in ChartJS
- How to disable a custom tooltip for a dataset in Chart.js line chart?
- how to set color for each item in tooltip with ChartJS
- how to hide specific dataset based on condition chartjs angular
- How to create a custom tooltip for chartJS graph with data in JSON array in JavaScript?
- Charts JS: Doughnut chart and hiding tooltip for specific data index within each dataset
- Charts.js - How to set custom tooltip text for each dataset
- How to disable chartjs tooltip on mibile devices and small screens?
- How to set specific height for chartJs background color in terms of yAxis value
- React - how to get array from store before first render and use it as dataset for ChartJS
- How to add dataset legend in chartjs tooltip
- How to disable chartjs legendclick
- How to modify chartjs tooltip so i can add customized strings in tooltips
- Chartjs v2 - format tooltip for multiple data sets (bar and line)
- show label in tooltip but not in x axis for chartjs line chart
- How can I hide tooltip in Chart.js on a specific data label?
- How to add label for ChartJs Legend
- ChartJs how to get from mulitiple dateset which dataset bar is clicked
- How to disable Chart JS tooltip when there is no value?
- How to set a full length background color for each bar in chartjs bar
- how to change Y axis value dynamically based on user input in Chartjs for Line chart?
- How to access specific data values from tooltip - Chart.js
- Chart.JS: How to add Data to a specific dataset
- How to change grid line width for one specific line
- Chart.js how to set cutoutPercentages for each dataset
- How to get onClick Event for a Label in ChartJS and React?
- How to get rid of the white square outline or border in the tooltip for chartjs?
- How to dynamically set ChartJs line chart width based on dataset size?
- How to add ChartJS code in Html2Pdf to view image
- How to remove bars for those bars with zero value in Chartjs bar chart?
More Query from same tag
- Importing JSON to data and labels for chart.js
- Chart.js - consolidating days to month totals along the x (time) axis?
- Chart.js, increase space between bars and its labels when increasing the charts width
- Charts.js v2: always show custom (html) tooltips on chart
- Vuejs - Chartjs - turning a doughnut chart to a gauge - rotation
- ChartJS: How to set a data point to be hidden by default?
- why i have this error Utils is not defined when i want create a chart from chart.js
- Using public data for dynamic data
- first and last value is not displaying in chart.js used with django
- Grouped Bar Char in D3.js or Google Charts or Chart.js
- Import Financial chartjs
- ChartJS Custom text on certain xAxis and yAxis linesS
- In ChartJS how do I change the color of a label in the legend?
- How to display the labels in doughnut chart using ng2 charts?
- How to add area with break on line chart with fill color
- How do I persist the gridlines for ticks that are not displayed? [Chart.js]
- Am I using chart.update() correctly when trying to update the chart?
- CoreUI Chart type Bar- clickable bars to link to another page
- Chart.js stacked bar chart text on top of the stacked bars
- Changing color of specific ChartJS - AngularChartJS point
- Chartjs - first x-axis tick is not displayed
- Chart.js draw mathematical function
- Reactive charts with Meteor : d3charts , Hightcharts , ChartJS , other?
- How to use GET method to choose the right table to show ChartJS graph?
- What happened to generateLegend() in chartjs 3.0?
- y axis Formatting with Metric prefix 1000=> 1k Chart.js
- How to expand the "Y" scale of the data in chart.js?
- Chart.js - Why the chart cannot rendered in a child component but in the father component can?
- change long labels to multiline(wrap) label in ChartJs
- How to remove strikethrough from label once click on legend