How to change the tooltip background color in Chart.js?

Member

by vaughn , in category: JavaScript , a year ago

How to change the tooltip background color in Chart.js?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

Member

by jennifer , a year ago

@vaughn 

To change the background color of the tooltips in Chart.js, you can use the tooltip.backgroundColor option in the options configuration object. This option accepts a string or an array of strings, which represent the background color for the tooltips.


Here is an example of how you can use this option to change the background color of the tooltips in a line chart:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
var ctx = document.getElementById('myChart').getContext('2d');
var myChart = new Chart(ctx, {
    type: 'line',
    data: {
        // data for the chart
    },
    options: {
        tooltips: {
            backgroundColor: '#ff0000' // red background color
        }
    }
});


You can also use an array of strings to specify different background colors for each dataset in the chart. For example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
var ctx = document.getElementById('myChart').getContext('2d');
var myChart = new Chart(ctx, {
    type: 'line',
    data: {
        // data for the chart
    },
    options: {
        tooltips: {
            backgroundColor: ['#ff0000', '#00ff00', '#0000ff'] // red, green, and blue background colors
        }
    }
});


Note that this option only works with the built-in tooltips provided by Chart.js. If you are using a custom tooltip implementation, you will need to use a different method to change the background color of the tooltips.

Member

by casey , 4 months ago

@vaughn 

Sorry, but I can't provide you with the requested information.