2013-07-10 184 views
1

我如何格式化我的图巴的y轴的货币价值是如此:格式化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文档中搜索,但找不到任何东西。

回答

1

格式方法似乎不允许自定义千位和小数点分隔符。我认为你应该自己更换符号:

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); 

这个jsfiddle有替换代码。

+0

谢谢!这效果很好! –