What is the best way to conceal a tooltip on a specific data label in Chart.js?

I am attempting to conceal a tooltip in Chart.js whenever the name of a clicked object is "Something". My attempt so far looks like this:

  this.doughnutChart = new Chart(this.doughnutCanvas.nativeElement, {
  type: 'doughnut',
  data: {
    datasets: [{
      label: _.map(this.dataService.AmTimeSlots, 'ProjectName'),
      data: _.map(this.dataService.AmTimeSlots, 'Duration'),
      backgroundColor: _.map(this.dataService.AmTimeSlots, 'Color'),
      hoverBackgroundColor: _.map(this.dataService.AmTimeSlots, 'HoverColor'),
      borderColor: _.map(this.dataService.AmTimeSlots, 'BorderColor'),
      borderWidth: 1.5
    },
    {
      label: _.map(this.dataService.PmTimeSlots, 'ProjectName'),
      data: _.map(this.dataService.PmTimeSlots, 'Duration'),
      backgroundColor: _.map(this.dataService.PmTimeSlots, 'Color'),
      hoverBackgroundColor: _.map(this.dataService.PmTimeSlots, 'HoverColor'),
      borderColor: _.map(this.dataService.PmTimeSlots, 'BorderColor'),
      borderWidth: 1.5
    }],
  },
  options: {
    elements: {
      arc: {
        roundedCornersFor: 0
      }
    },
    segmentShowStroke: false,
    responsive: true,
    maintainAspectRatio: true,
    legend: {
      display: false
    },
    onClick: this.chartClick.bind(this),
    cutoutPercentage: 65,
    tooltips: {
      filter: function (tooltipItem) {
        if (tooltipItem.xLabel == "Free Slot") {
          return false;
        } else {
          return true;
        }
      },
      callbacks: {
        label: function (tooltipItems, data) {
        return data.datasets[tooltipItems.datasetIndex].label[tooltipItems.index];
        },
        afterLabel: function (tooltipItems, data) {
        return Math.floor(data.datasets[tooltipItems.datasetIndex].data[tooltipItems.index] / 6) + 'h ' + data.datasets[tooltipItems.datasetIndex].data[tooltipItems.index] * 10 % 60 + 'm';
        }
      }
    }
  },
  config: {
    data: this.dataService,
    settings: this.settingsService
  }
});

The above code is functioning properly and successfully hiding the text of the tooltip. However, the issue lies in the fact that the black label/border still remains visible. How can I hide it?

Answer №1

If you want to customize tooltips, you have the option to implement a filter function:

options: {
    tooltips: {
       filter: function (tooltipItem, data) {
           var label = data.labels[tooltipItem.index];
           if (label == "Red") {
             return false;
           } else {
             return true;
           }
       }
    }
}

Check out this live example on jsfiddle: https://jsfiddle.net/beaver71/ndc2uao2/

Answer №2

Important Information for Chart.js 3:

options: {
  plugins: {
    tooltip: {
       customFilter: function (tooltipItem, data) {
           return tooltipItem.label === "Red";
       }
    }
  }
}

Answer №3

Update for Chartjs v4

New Filter Function Syntax

options: {
  plugins: {
    tooltip: {
       filter: function (item, index, items, inputData) {
           // Check if the label is "Red"
           return item.label === "Red";
       }
    }
  }
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Applying swichMap to combine observables from both debounced and non-debounced streams

So, currently I am working with 2 observables. The obseravableClickBtn observable will send a request immediately upon click. The observableInputText observable will debounce the requests by 3 seconds. Here is the code snippet: obseravableClickBtn ...

How can Python be utilized to extract specific data from a webpage in order to gather information for scraping additional pages in a sequential manner?

Looking to create a Python script that scrapes a web page to extract a number hidden behind a "Show More" button. This extracted number will then be used as a parameter to access a URL which returns a JSON containing data along with another number. This s ...

In Vue.js, utilize a contenteditable div to dynamically add an HTML element and bind it with v-model

Below is the HTML code I have written. <div id="app"> <button @click="renderHtml">Click to append html</button> <div class="flex"> <div class="message" @input="fakeVmodel" v-html="html" contenteditable="true">< ...

Adjust Javascript Code to Modify Speed of Sine Wave Live

Feeling a bit lost here, but I need some assistance with a simple task. I'm trying to adjust the speed of a sine wave from 2 to 10 when a button is clicked. It seems like a small change, but for some reason, I can't quite figure it out. If you& ...

What is the best way to represent a state with two possible fields in React using TypeScript?

There is a specific state item that can have its own value or reference another item using an ID. interface Item { itemName: string; itemValue: { valLiteral: number } | { idNumber: string }; } const indItem1: Item = { itemName: "first sample&quo ...

Attempting to activate template rendering with Meteor session

I'm currently facing an issue with Meteor sessions and how my code is triggering the rendering of a template. At the moment, I have a session that sets its ._id to whatever is clicked. Template.sidebar.events({ /* on click of current sidecat class ch ...

In ReactJS, the error message 'callback is not a function' is indicating a TypeError

I am unsure of the reason behind this response's body. "<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <script src="/charting_library/charting_library.m ...

Replacing text within a paragraph using D3.js

Is it possible to develop a D3 function that can choose a paragraph, delete its content, and replace it with new text? If so, what would be the most effective way to accomplish this? I attempted the following: d3.select("#triggerButton") .on("clic ...

Prevent scrolling by touch-dragging

Is there a way to completely disable scrolling when a button is pressed? I came across this helpful answer, but it seems like users can still scroll by selecting and dragging content on the page. The code provided resets the scroll position quickly, but t ...

Using Rust WebAssembly in Next.js

Seeking assistance with integrating a rust-generated wasm module into my NextJS project. This is how I am attempting to import the wasm: import init, { tokenize } from "@/wasm/lazyjson"; const Test = dynamic({ loader: async () => { ...

An issue arose following the utilization of "<style lang="scss" scoped>"

After using code from GitHub, I encountered an error. Any tips? I am new to JavaScript and Vue.js, and I'm constantly having issues with modules. I tried: npm uninstall webpack and then: npm install webpack@^4.0.0 --save-dev It's still not wo ...

Utilizing analytics.js independently of the react-ga library

Is there a way to integrate Google Analytics into my React app without using the react-ga package? I specifically want to access the ga function in a separate JavaScript file as well as within a React component. How can I achieve this by importing the anal ...

Can Aurelia components be designed to communicate values back to their parent elements?

Currently, I am developing an application using Aurelia. One part of the application features a grid that displays users along with their active status. To allow for editing of the active state, I have implemented a slide button control, similar to those ...

Struggling with setting up Chrome options using Selenium WebDriver in JavaScript?

Encountering an issue while attempting to access a specific website using Selenium WebDriver in JavaScript with the Chrome browser. In the provided code snippet, the browser driver is initialized; however, there seems to be a problem when utilizing setChro ...

Is it possible for Swagger to produce unique custom generic types?

Let's consider a scenario where we receive an API return model in C# public class ApiResult<T> { public T Result; public bool Success; } and send back an ApiResult<string> object instance to the client This leads us to a swagger gen ...

In Ember.js, where should I place the initialization code for a controller? I attempted to set it up in the routes

With my experience working with ember.js and highcharts, I have come across some examples that were too simplistic for me to grasp how to set up the chart objects and render them properly. I have explored initializers and understand the significance of ro ...

Utilize JavaScript to extract and exhibit various SQL records stored within a multidimensional array

How can I use JS, JQuery, PHP, and MySQLi to automatically generate an HTML table within a div on a webpage? The table should display data based on user-input search criteria. For example, the user enters a start date and end date, clicks a button, which t ...

Expanding Javascript Array Objects with Dynamic Parameters based on Conditions

I'm trying to dynamically add a parameter to the array object url based on a function's value. Unfortunately, my attempts so far have been unsuccessful. var change_lookup_number = function() { var type = document.getElementById('type_num ...

What are the best methods to safeguard an audio file and integrate it into my React Native application for playback?

What is the best method to secure my audio file from unauthorized media player apps while ensuring it can still be played on my React Native application? The backend includes Node.js and Express.js. ...

A step-by-step guide on iterating over an array and passing each element to a function that returns a promise encapsulating an additional

I'm facing an issue while looping through an array of items, each being used in an operation that returns a promise which includes another promise. The flow is not as desired and I'm unsure why. Is there a better way to handle this scenario? I&ap ...