I was having some problem when trying to customize the hover tooltip for mixed chart using chart.js.
I have a bar chart which show the total profit for certain product and a line chart to show the total quantity of that certain product. When I hover over the bar chart, I wanted the tooltip to show something like:
Total profit: $ 1399.30
The price should be in two decimal format appended to the back of 'Total profit: $'. When I hover over the line chart, I wanted to show something like:
Quantity sold: 40 unit(s)
Here is my code to populate related array:
for(var i = 0 ; i < topProductList.length; i++){
labelData.push(topProductList[i].itemName);
amountData.push((topProductList[i].price * topProductList[i].quantity).toFixed(2));
quantityData.push(topProductList[i].quantity);
}
The callback where I tried to merge back the x-axis label as suggested by @GRUNT:
tooltips: {
callbacks: {
title: function(t, d) {
return d.labels[t[0].index].replace(/\n/, ' ');
}
}
}
The option for my chart:
var opt = {
type: "bar",
data: {
labels: labelData,
datasets: [{
type: "bar",
label: 'Total Profit ',
data: amountData,
},{
type: "line",
label: 'Quantity Sold ',
data: quantityData,
}]
},
options: options
};
For now, when I hover my bar chart, I am getting Total profit: 1399.3
and for line chart, Quantity sold: 40
which is not my desired output as above.
Any ideas how to override the tooltip hover customization?
Thanks!
You can use the following tooltip's label callback function, for showing different tooltip labels when hovered on different graphs :
also, your first dataset should be for
line
chart, followed bybar
, like so :ᴡᴏʀᴋɪɴɢ ᴇxᴀᴍᴘʟᴇ ⧩