I have the following scenario:
x-axes: String date
y-axes: Integers
2 data sets
When i hover over a particular data point, i want to display the following:
Data collected as of 2020-06-02
33,600
33,000
Diff: 600
Here is what i get instead:
Data collected as of undefined
33,600
33,000
Diff: NaN
Here is my tooltip callbacks code:
tooltips: {
mode: 'index',
intersect: false,
callbacks: {
title: function(toolTipItem, data) {
return "Data points collected as of " + toolTipItem.xLabel;
// return "Data points collected as of " + data.labels[toolTipItem.index]; // this did not work either
},
label: function(toolTipItem, data) {
if (toolTipItem.datasetIndex === 0) {
return toolTipItem.yLabel.toString().replace(/B(?<!.d*)(?=(d{3})+(?!d))/g, ',');
} else if (toolTipItem.datasetIndex === 1) {
return toolTipItem.yLabel.toString().replace(/B(?<!.d*)(?=(d{3})+(?!d))/g, ',');
}
},
labelColor: function(toolTipItem, data){
if (toolTipItem.datasetIndex === 0) {
return {
borderColor: 'rgba(196, 196, 196, 1)'
};
} else if (toolTipItem.datasetIndex === 1) {
return {
borderColor: 'rgba(127, 231, 106, 1)'
};
}
},
footer: function(toolTipItems, data) {
let diff = 0;
diff = parseInt(data.datasets[0].data[toolTipItems.index]) - parseInt(data.datasets[1].data[toolTipItems.index]);
return 'Diff: ' + diff;
// return 'Diff: ' + diff.toString().replace(/B(?<!.d*)(?=(d{3})+(?!d))/g, ','); // this did not work either
}
}
},
I have followed the sample here:
Sample tooltip callback use case from chartjs
Having said that, would like to know why in the documentation: chartjs docs
Sometimes the argument is ToolTipItem[] and sometimes it is ToolTipItem. Am i missing something?
Thanks.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…