如何在chart.js中的工具提示中添加$前缀,并且没有小数点



我有一个用chart.js制作的条形图,它从谷歌中提取数据。

工具提示很好,但我想在前面添加前缀$,并且不显示小数点。

https://codepen.io/jameswill77/pen/WNzXxeJ

function handleQueryResponse(response) {
var data = response.getDataTable();
var columns = data.getNumberOfColumns();
var rows = data.getNumberOfRows();
console.log(data.toJSON());
const colors = ['#eeeeee', '#23F0C7'];
dataj = JSON.parse(data.toJSON());
console.log(dataj.cols[0].label);
const labels = [];
for (c = 1; c < dataj.cols.length; c++) {
if (dataj.cols[c].label != "") {
labels.push(dataj.cols[c].label);
}

这可以通过在setup变量中定义options.plugins.tooltip.callbacks.label函数来实现,如下所示。

var setup = {
type: 'bar',
data: chartdata,
options: {
plugins: {
title: {
display: true,
text: dataj.cols[0].label
},
tooltip: {
callbacks: {              
label: ctx => ctx.label + ': $' + parseInt(ctx.parsed.y).toLocaleString()
}
}
},
responsive: true,
}
}

请查看Chart.js文档中的Tooltip回调页面以了解更多详细信息。

最新更新