用D3中的轴上的逗号替换小数点?

时间:2015-07-07 13:05:38

标签: javascript d3.js localization decimal-point

这是关于D3中轴的刻度:有没有办法用数字中的逗号替换小数点?我希望数字显示如下(在德国这是正常的,这么大的数字仅用于演示)

1.000.000,1 // This is one million and a tenth

而不是这样:

1,000,000.1

我已经阅读了关于这个主题的documentation,但似乎没有可能!?那是我迄今为止的相关代码:

localeFormatter = d3.locale({
                "decimal": ".",
                "thousands": ",",
                "grouping": [3],
                "currency": ["", "€"],
                "dateTime": "%a, %e %b %Y, %X",
                "date": "%d.%m.%Y",
                "time": "%H:%M:%S",
                "periods": ["", ""],
                "days": ["Sonntag", "Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag"],
                "shortDays": ["So", "Mo", "Di", "Mi", "Do", "Fr", "Sa"],
                "months": ["Januar", "Februar", "März", "April", "Mai", "Juni", "Juli", "August", "September", "Oktober", "November", "Dezember"],
                "shortMonths": ["Jan", "Feb", "Mär", "Apr", "Mai", "Jun", "Jul", "Aug", "Sep", "Okt", "Nov", "Dec"]
            });


numberFormat = localeFormatter.numberFormat("04.,"); // The setting is applied, but has not the desired effect...

yAxis = d3.svg.axis()
                .scale(y)
                .orient("left")
                .tickFormat(numberFormat);

1 个答案:

答案 0 :(得分:5)

使用d3.locale的以下设置:

var localeFormatter = d3.locale({
      "decimal": ",",
      "thousands": ".",
      ...
    });

请注意,我更改了.中的,,反之亦然。随着:

var numberFormat = localeFormatter.numberFormat(",.2f"); 

我使用千位分隔符,打印号码(它使用区域设置中定义的.)和逗号.2f后面的两位数字:

console.log(numberFormat(1000000.1)); // "1.000.000,10"