Ron*_*ald 25 javascript formatting charts number-formatting chart.js
我查看了Chart.js文档并且没有找到关于数字格式的任何内容,即数字格式为"#,###.00"的1,000.02
我也做了一些基本测试,似乎图表不接受非数字文本的值
有没有人找到一种方法来获取格式化为千位分隔符和固定小数位数的值?我想将图表中的轴值和值格式化.
Yac*_*e B 22
Javascript中没有用于数字格式的内置功能.我发现最简单的解决方案是这个页面上的addCommas函数.
然后你只需要修改你的tooltipTemplate参数行,Chart.defaults.global如下所示:
tooltipTemplate: "<%= addCommas(value) %>"
Run Code Online (Sandbox Code Playgroud)
Charts.js会处理剩下的事情.
这是addCommas功能:
function addCommas(nStr)
{
nStr += '';
x = nStr.split('.');
x1 = x[0];
x2 = x.length > 1 ? '.' + x[1] : '';
var rgx = /(\d+)(\d{3})/;
while (rgx.test(x1)) {
x1 = x1.replace(rgx, '$1' + ',' + '$2');
}
return x1 + x2;
}
Run Code Online (Sandbox Code Playgroud)
Sum*_*nth 16
对于以逗号格式编号的数字,即3,443,440.您可以在tooltipTemplate中使用toLocaleString()函数.
tooltipTemplate:"<%= datasetLabel%> - <%= value.toLocaleString()%>"
and*_*ieb 12
在Chart.js v2.5中,现有的解决方案并不适用于我.我发现的解决方案:
options: {
scales: {
yAxes: [{
ticks: {
callback: function (value) {
return numeral(value).format('$ 0,0')
}
}
}]
}
}
Run Code Online (Sandbox Code Playgroud)
我使用了numeral.js,但你可以使用Yacine提出的addCommas函数,或其他任何东西.
您可以Chart.defaults.global使用函数设置tooltipTemplate值以格式化值:
tooltipTemplate : function(valueObj) {
return formatNumber(valueObj.value, 2, ',', '.');
}
Run Code Online (Sandbox Code Playgroud)
这是格式函数:
function formatNumber(number, decimalsLength, decimalSeparator, thousandSeparator) {
var n = number,
decimalsLength = isNaN(decimalsLength = Math.abs(decimalsLength)) ? 2 : decimalsLength,
decimalSeparator = decimalSeparator == undefined ? "," : decimalSeparator,
thousandSeparator = thousandSeparator == undefined ? "." : thousandSeparator,
sign = n < 0 ? "-" : "",
i = parseInt(n = Math.abs(+n || 0).toFixed(decimalsLength)) + "",
j = (j = i.length) > 3 ? j % 3 : 0;
return sign +
(j ? i.substr(0, j) + thousandSeparator : "") +
i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + thousandSeparator) +
(decimalsLength ? decimalSeparator + Math.abs(n - i).toFixed(decimalsLength).slice(2) : "");
}
Run Code Online (Sandbox Code Playgroud)
对于使用版本:2.5.0的用户,以下是@andresgottlieb解决方案的增强功能。这样,您还可以在图表的工具提示中设置金额格式,而不仅是“ yAxes”中的“ ticks”
...
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero:true,
callback: function(value, index, values) {
return '$ ' + number_format(value);
}
}
}]
},
tooltips: {
callbacks: {
label: function(tooltipItem, chart){
var datasetLabel = chart.datasets[tooltipItem.datasetIndex].label || '';
return datasetLabel + ': $ ' + number_format(tooltipItem.yLabel, 2);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是我正在使用的number_format()函数:
function number_format(number, decimals, dec_point, thousands_sep) {
// * example: number_format(1234.56, 2, ',', ' ');
// * return: '1 234,56'
number = (number + '').replace(',', '').replace(' ', '');
var n = !isFinite(+number) ? 0 : +number,
prec = !isFinite(+decimals) ? 0 : Math.abs(decimals),
sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep,
dec = (typeof dec_point === 'undefined') ? '.' : dec_point,
s = '',
toFixedFix = function (n, prec) {
var k = Math.pow(10, prec);
return '' + Math.round(n * k) / k;
};
// Fix for IE parseFloat(0.55).toFixed(0) = 0;
s = (prec ? toFixedFix(n, prec) : '' + Math.round(n)).split('.');
if (s[0].length > 3) {
s[0] = s[0].replace(/\B(?=(?:\d{3})+(?!\d))/g, sep);
}
if ((s[1] || '').length < prec) {
s[1] = s[1] || '';
s[1] += new Array(prec - s[1].length + 1).join('0');
}
return s.join(dec);
}
Run Code Online (Sandbox Code Playgroud)
小智 6
tooltips像这样放入“选项”:
options: {
tooltips: {
callbacks: {
label: function(tooltipItem, data) {
return tooltipItem.yLabel.toFixed(2).replace(/\d(?=(\d{3})+\.)/g, '$&,');
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
来自https://github.com/chartjs/Chart.js/pull/160的参考。
| 归档时间: |
|
| 查看次数: |
40289 次 |
| 最近记录: |