我正在尝试显示一个四舍五入到两位小数的工具提示值,其中包含" s" SI前缀和我自己的后缀" b"用于以Mb,kb等显示文件大小。这是一小段代码:
svg.append("rect")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")")
.attr("fill", "none")
.attr("width", width)
.attr("height", height)
.attr('pointer-events', 'all')
.on("mouseover", function() { focus.style("display", null); })
.on("mouseout", function() { focus.style("display", "none"); })
.on("mousemove", mousemove);
var bisectDate = d3.bisector(function(d) { return d.date; }).left;
function mousemove() {
var x0 = x.invert(d3.mouse(this)[0]),
i = bisectDate(data, x0, 1),
d0 = data[i - 1],
d1 = data[i],
d = x0 - d0.date > d1.date - x0 ? d1 : d0;
focus.attr("transform", "translate(" + (x(d.date) + margin.left) + "," + (y(d.value) + margin.top) + ")");
focus.select("text").text(d3.format("s")(d.value) + "b");
focus.select(".mouse-line").attr("y2", height - y(d.value));
}
有什么方法可以解决这个问题?
答案 0 :(得分:1)
您似乎无法根据需要将其格式化为两位小数。
尝试更改格式调用以包含如下位数:
...d3.format(".2s")(d.value) ...