D3.js 格式化y轴
如何格式化货币值我的图形栏的y轴的方向为:D3.js 格式化y轴,d3.js,nvd3.js,D3.js,Nvd3.js,如何格式化货币值我的图形栏的y轴的方向为: R$ 123.456,00 而不是: R$ 123,456.00 目前我正在使用此函数设置格式,但无法进行以下简单更改: var format = d3.format(',.2f'); // Need to change this, but don't know how chart.yAxis.tickFormat(function(d) { return "R$ " + format(d); }); 我已经在D3文档中搜索过了,但
R$ 123.456,00
而不是:
R$ 123,456.00
目前我正在使用此函数设置格式,但无法进行以下简单更改:
var format = d3.format(',.2f'); // Need to change this, but don't know how
chart.yAxis.tickFormat(function(d) {
return "R$ " + format(d);
});
我已经在D3文档中搜索过了,但是找不到任何内容。格式方法似乎不允许自定义千和十进制分隔符。我认为你应该自己替换这些符号:
var format = d3.format(',.2f');
// Format the number, adding thousands and decimal separators
var label = format(1234.00);
// Replace the . and the , symbols. The ! symbol is necessary to do the swap
// it can be other symbol though
label = label.replace('.', '!');
label = label.replace(',', '.');
label = label.replace('!', ',');
// The result is 'R$ 1.234,00'
d3.select('#chart').append('p').text('R$ ' + label);
这有替换代码。使用D35.5,您可以创建自定义区域设置 另请注意,在说明符(arg传递给.format)中,我现在包含了一个
$
,这将自动在格式化字符串中包含货币前缀
const customD3Locale = d3.formatLocale({
decimal: ",",
thousands: ".",
grouping: [3],
currency: ["R$",""]
})
const format = customD3Locale.format('$,.2f');