Chart.js 数字格式 [英] Chart.js number format

查看:21
本文介绍了Chart.js 数字格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我查看了 Chart.js 文档,但没有找到任何关于数字格式的内容即) 1,000.02 来自数字格式 "#,###.00"

I went over the Chart.js documentation and did not find anything on number formatting ie) 1,000.02 from number format "#,###.00"

我也做了一些基本的测试,似乎图表不接受其值的非数字文本

I also did some basic tests and it seems charts do not accept non-numeric text for its values

有没有人找到将值格式化为具有千位分隔符和固定小数位数的方法?我想格式化图表中的轴值和值.

Has anyone found a way to get values formatted to have thousands separator and a fixed number of decimal places? I would like to have the axis values and values in the chart formatted.

推荐答案

Javascript 中没有用于数字格式化的内置功能.我发现最简单的解决方案是 此页面上的 addCommas 函数.

There is no built-in functionality for number formatting in Javascript. I found the easiest solution to be the addCommas function on this page.

然后你只需要将你的 tooltipTemplate 参数行从你的 Chart.defaults.global 修改成这样:

Then you just have to modify your tooltipTemplate parameter line from your Chart.defaults.global to something like this:

tooltipTemplate: "<%= addCommas(value) %>"

Charts.js 将负责其余的工作.

Charts.js will take care of the rest.

这是 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;
}

这篇关于Chart.js 数字格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆