[英]ChartJS scatterplot multi-line tooltip, one box
I am trying to customize the text in a ChartJS scatterplot tooltip.我正在尝试自定义 ChartJS 散点图工具提示中的文本。 The expected result is a combination of the following:预期结果是以下各项的组合:
image 1:图 1:
image2:图像2:
I would obviously like the data to be displayed appropriately as in image 1 and the title and data to be displayed separately with a body and footer with only one colored square as in image 2.我显然希望数据像图 1 一样正确显示,标题和数据分别显示,主体和页脚只有一个彩色方块,如图 2 所示。
I followed the answers given here Chartjs Tooltip Line Breaks .我按照此处给出的答案Chartjs Tooltip Line Breaks 。
The verified answer (Alexey Pavlov) produces image 1 like so:经过验证的答案 (Alexey Pavlov) 生成图像 1,如下所示:
callbacks: {
label: function(tooltipItem, data) {
let tt = [`${data.labels[tooltipItem.index]}`];
tt.push(`${xstat}: ${tooltipItem.xLabel}`);
tt.push(`${ystat}: ${tooltipItem.yLabel}`);
return tt;
}
},
Another answer from ghanshyam shah is much closer to what I would like to achieve, producing image 2 which instead uses a footer, however it seems as though x and y values of the data point are not accessible through tooltipItem.x/yLabel in the footer callback: ghanshyam shah 的另一个答案更接近我想要实现的目标,生成的图像 2 使用页脚,但似乎无法通过页脚中的 tooltipItem.x/yLabel 访问数据点的 x 和 y 值打回来:
callbacks: {
label: function(tooltipItem, data) {
return data.labels[tooltipItem.index];
},
footer: function(tooltipItem, data) {
let tt = [`${xstat}: ${tooltipItem.xLabel}`];
tt.push(`${ystat}: ${tooltipItem.yLabel}`);
return tt;
}
},
TL;DR: How can I access datapoint values in a ChartJS tooltip footer callback? TL;DR:如何在 ChartJS 工具提示页脚回调中访问数据点值?
What you need is a combination of label
and afterLabel
callback functions as follows:您需要的是label
和afterLabel
回调函数的组合,如下所示:
callbacks: {
label: (tooltipItem, data) => `${data.labels[tooltipItem.index]}`,
afterLabel: (tooltipItem, data) => [`${xstat}: ${tooltipItem.xLabel}`, `${ystat}: ${tooltipItem.yLabel}`]
},
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.