ChartJS 2 工具提示标题未定义

ChartJS 2 tooltip title is undefined

我希望将 "Ages " 这个词添加到我的工具提示标题中,以表示图表中的年龄组。

基本上,标题应该是 "Ages 11-20",例如。但是,我得到 "Ages undefined"?

有人知道我为什么会遇到这个问题吗?

代码(下方fiddle):

var ageRangedata = {type: 'bar',
data:{datasets: [
{ data : [0,1,5,2,1,0,0, 0, 0, 0], backgroundColor: 'rgba(255, 209, 240, 0.6)', borderColor: 'rgba(255, 209, 240, 1)', borderWidth: 2, label: 'Female' },
{ data : [1,0,1,1,6,1,0, 1, 0, 0], backgroundColor: 'rgba(81, 187, 245, 0.6)', borderColor: 'rgba(81, 187, 245, 1)', borderWidth: 2, label: 'Male' }],

labels:['0-10','11-20','21-30','31-40','41-50','51-60','61-70', '71-80', '81-90', '90-']},
options: {maintainAspectRatio: false,responsive: true, tooltips: { callbacks: { title: function(tooltipItem, data) { return 'Ages ' + data.labels[tooltipItem.Index]; }}},legend: { display: false }}};

var ageR = document.getElementById("ageRange").getContext("2d");
var chart_ageR = new Chart(ageR, ageRangedata);

任何知道使用 afterLabel 将文本发送到标签正下方而不是标签之后(听起来应该这样做)的人的加分(附带问题,如果你想忽略)”?

https://jsfiddle.net/unc76c7s/

这样试试:

return 'Ages ' + tooltipItem[0].xLabel;

无论如何,你的错误是非常明确的。您可以在回调中对您的 tooltipItem 执行 console.log 以查看您是否正确使用它。

希望对您有所帮助。