作者:蕶ok薍 | 来源:互联网 | 2023-02-07 20:52
我正在使用Chart.js绘制饼图.
画我用的图
var ctx = document.getElementById("myChart").getContext('2d');
var myChart = new Chart(ctx, {
type: 'pie',
data: {
labels: #{raw @labels.to_json},
datasets: [{
backgroundColor: #{raw @colors.to_json},
data: #{@followers}
}]
}
});
澄清这个数据的事情是这样的
data: {
labels: ["FAISAL","muhammadfaisali","faisaliqbal3699"],
datasets: [{
backgroundColor: ["#00b638","#efaa30","#50c8ea"],
data: [500000, 75000, 100000]
}]
}
我需要将这些显示data: [500000, 75000, 100000]
为千分隔符["500,000", "75,000", "100,000"]
我尝试了不同的
包括写这种方法的东西
function separator(numbers)
{ data = []
for (i = 0; i
并试图像这样使用它
data: separator(#{@followers})
它按我的要求格式化数据但是给出了错误 Cannot read property 'custom' of undefined
在这里以千位分隔符显示数据的方法是什么

1> jordanwillis..:
要在chart.js中执行此操作,您需要使用tooltips.callbacks.label
callback属性.从此回调返回的值是用于生成工具提示文本的值.
以下是使用工具提示回调配置的图表,该回调使用数据值的本地字符串表示形式.
var ctx = document.getElementById("chart-area").getContext("2d");
var myPie = new Chart(ctx, {
type: 'pie',
data: {
labels: ["FAISAL","muhammadfaisali","faisaliqbal3699"],
datasets: [{
backgroundColor: ["#00b638","#efaa30","#50c8ea"],
data: [500000, 75000, 100000]
}],
},
options: {
title: {
display: true,
text: 'Employee Overview',
fontStyle: 'bold',
fontSize: 20
},
tooltips: {
callbacks: {
// this callback is used to create the tooltip label
label: function(tooltipItem, data) {
// get the data label and data value to display
// convert the data value to local string so it uses a comma seperated number
var dataLabel = data.labels[tooltipItem.index];
var value = ': ' + data.datasets[tooltipItem.datasetIndex].data[tooltipItem.index].toLocaleString();
// make this isn't a multi-line label (e.g. [["label 1 - line 1, "line 2, ], [etc...]])
if (Chart.helpers.isArray(dataLabel)) {
// show value on first line of multiline label
// need to clone because we are changing the value
dataLabel = dataLabel.slice();
dataLabel[0] += value;
} else {
dataLabel += value;
}
// return the text to display on the tooltip
return dataLabel;
}
}
}
}
});
您可以在此codepen中看到它的运行情况.