D3:格式化刻度值。显示B(十亿)而不是G(千兆) [英] D3: Formatting tick value. To show B (Billion) instead of G (Giga)

查看:89
本文介绍了D3:格式化刻度值。显示B(十亿)而不是G(千兆)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在D3折线图中格式化Y轴数据。它是关于世界人口的,因此非常大。

I want to format Y-axis data in D3 Line chart. It's about world population so that is quite large.

我目前正在使用下面的代码,它显示的是G而不是B。

I am currently using below code, which is showing "G" instead of "B".

d3.format("s")

我研究过,发现这里的G代表Giga,有没有办法把它改成B代表Billion?

I researched and found that here "G" stands of Giga, is there a way to change it to B which denotes Billion?

谢谢。

推荐答案

如果你喜欢SI-prefix的格式,而不是实际的前缀,只需用简单的<$ c $换掉它c> .replace :

If you like the formatting of SI-prefix, but not the actual prefix, just swap it out with a simple .replace:

var f = d3.format("0.2s");

document.write(
  f(1e9).replace(/G/,"B")
);

<script src="https://d3js.org/d3.v4.min.js"></script>

这篇关于D3:格式化刻度值。显示B(十亿)而不是G(千兆)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆