score:8

Accepted answer

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;
    }
}

Related Query