d3.js:tickformat - 添加%符号而不乘以100

rol*_*fsf 25 formatting d3.js

我的数据有百分比,例如, [10.1, 3.2, 5.4]

d3.format("0f") 会给我的 [10, 3, 5]

d3.format("0%")会给我[1010%, 320%, 540%](乘以100)

我怎么得到[10%, 3%, 5%]

我无法弄清楚在第一种情况下在哪里添加+"%"或在第二种情况下消除*100

代码的相关部分:

var formatPercent = d3.format("0f");

var min = 0; 
var max = d3.max(data, function(d) { return + d[5]; });
max = Math.round(max * 1.2); // pad it

//define the x-axis
var xAxis = d3.svg.axis()
                .scale(x)
                .orient('top')
                .ticks(6)
                .tickFormat(formatPercent);
Run Code Online (Sandbox Code Playgroud)

和数据像这样:

{
    "Geography": [
    ["Midwest", 234017797, 498, 8.2, 9.0, 11.3],
    ["Northeast", 265972035, 566, 8.9, 12.1, 13.1],
    ["South", 246235593, 524, 8.1, 8.3, 10.8],
    ["West", 362774577, 772, 9.4,9.9, 11.7]
    ]
}
Run Code Online (Sandbox Code Playgroud)

这是每行中的最后三个数字,即我用于绘制范围的百分比值.我希望根据数据中的高值和低值将x轴格式化为整数+%格式.

谢谢!

Tej*_*tha 50

更新D3 v4(使用ES6):

// Can also be axisTop, axisRight, or axisBottom
d3.axisLeft()
    .tickFormat(d => d + "%")
Run Code Online (Sandbox Code Playgroud)

D3 v3的原始答案:

您可以创建自己的格式:

d3.svg.axis()
    .tickFormat(function(d) { return d + "%"; });
Run Code Online (Sandbox Code Playgroud)

如果你想摆脱小数位:

d3.svg.axis()
    .tickFormat(function(d) { return parseInt(d, 10) + "%"; });
Run Code Online (Sandbox Code Playgroud)