Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable Chart JS tooltip when there is no value?

I am using Chart JS v.1.0.2. When I have one line and missing data, the tooltip shows x-label.

Does anyone know how to disable the tooltip when the point value is null?

Thanks very much!

like image 746
greenTea2Codes Avatar asked Aug 25 '15 12:08

greenTea2Codes


People also ask

How to disable tooltip in chart js?

To disable the tooltip menu that pops up when you hover over a chart element, you must disable it in the options object of your chart configuration. The path is options. plugins. tooltip.

What is tooltip chart?

Overview. Tooltips are the labels that appear when users hover over data points on your chart. They generally appear across all ZingChart charts by default, and can display any combination of data values, text, and/or tokens.


2 Answers

If you don't mind a few console messages you can throw an error to exit out of the tooltip method for null values, like so

var myLineChart = new Chart(ctx).Line(data, {
    tooltipTemplate: function (d) {
        if (d.value === null)
            throw '';
        else
            // else return the normal tooltip text
            return d.label + ': ' + d.value;
    }
});

The alternative would be to extend the chart or write a custom tooltips function


Fiddle - http://jsfiddle.net/y4zunrx6/

like image 107
potatopeelings Avatar answered Dec 25 '22 01:12

potatopeelings


Using chart.js 2.3.0 and angular-chart.js 1.1.1, I solved the problem globally by resolving the ChartJsProvider provider in my angular.module('shared').config(...) function and specifying a custom label callback for the tooltips option:

        ChartJsProvider.setOptions({
            tooltips: {
                enabled: true,
                //mode: 'single',
                callbacks: {
                    label: function (tooltipItem, data) {
                        const tooltip = data.datasets[tooltipItem.datasetIndex];
                        const value = tooltip.data[tooltipItem.index];
                        return value === 0 ? null : tooltip.label + ': ' + value;
                    }
                }
            }
        });

By returning null the tooltip is not shown for that specific tooltipItem.

Reference: Chart.js Tooltip Configuration

like image 34
Augusto Barreto Avatar answered Dec 25 '22 02:12

Augusto Barreto