d3.format()用法

2017-09-18  本文已影响0人  heylight崖夕

This page describes the D3 3.x API.

Formatting numbers is one of those things you don't normally think about until an ugly "0.30000000000000004" appears on your axis labels. Also, maybe you want to group thousands to improve readability, and use fixed precision, such as "$1,240.10". Or, maybe you want to display only the significant digits of a particular number. D3 makes this easy using a standard number format. For example, to create a function that zero-fills to four digits, say:

格式化数字是您通常不会考虑的事情之一,直到您的轴标签上出现一个丑陋的“0.30000000000000004”,另外,也许你想组合成千上万以提高可读性,并使用固定的精度,如“$ 1,240.10”。或者,也许您只想显示特定数字的有效数字。D3使用标准数字格式使其变得容易。 例如,创建一个零来填充到四位数的函数。

var zero = d3.format("04d");

Now you can conveniently format numbers:

现在您可以方便地格式化数字:

zero(2); // "0002"
zero(123); // "0123"

In addition to numbers, D3 also supports formatting and parsing dates, and comma-separated values.

除了数字,D3还支持格式化和解析日期和逗号分隔值。

Numbers

<a name="d3_format" href="#d3_format">#</a> d3.<b>format</b>(<i>specifier</i>)

Returns a new format function with the given string specifier. (Equivalent to locale.numberFormat for the default U.S. English locale.) A format function takes a number as the only argument, and returns a string representing the formatted number. The format specifier is modeled after Python 3.1's built-in format specification mini-language. The general form of a specifier is:

使用给定的字符串说明符返回一个新的格式函数。 (相当于默认的美国英语语言环境的locale.numberFormat。)格式函数使用数字作为唯一的参数,并返回一个表示格式化数字的字符串。格式说明符是在Python 3.1的内置格式规范迷你语言之后建立的。说明符的一般形式是:

 [​[fill]align][sign][symbol][0][width][,][.precision][type]

The fill can be any character other than "{" or "}". The presence of a fill character is signaled by the character following it, which must be one of the align options.

填充可以是除“{”或“}”之外的任何字符。 填充字符的存在由跟随它的字符表示,它必须是对齐选项之一。

The align can be:

The sign can be:

The symbol can be:

The "0" option enables zero-padding.

“0”选项启用零填充。

The width defines the minimum field width. If not specified, then the width will be determined by the content.

width定义最小字段宽度。 如果未指定,则宽度将由内容确定。

The comma (",") option enables the use of a comma for a thousands separator.

comma (",")选项可以为千位分隔符使用逗号。

The precision indicates how many digits should be displayed after the decimal point for a value formatted with types "f" and "%", or before and after the decimal point for a value formatted with types "g", "r" and "p".

precision表示在格式为"f"和"%"类型的格式的小数点后,小数点后面应显示多少个数字,或格式为"g","r"和"p"。

The available type values are:

The type "n" is also supported as shorthand for ",g".

类型“n”也被支持作为“,g”的缩写。

<a name="d3_formatPrefix" href="#d3_formatPrefix">#</a> d3.<b>formatPrefix</b>(<i>value</i>[, <i>precision</i>])

Returns the SI prefix for the specified value. If an optional precision is specified, the value is rounded accordingly using d3.round before computing the prefix. The returned prefix object has two properties:

返回指定值的SI前缀。 如果指定了可选精度,则在计算前缀之前,相应地使用d3.round进行舍入。 返回的前缀对象有两个属性:

For example:

var prefix = d3.formatPrefix(1.21e9);
console.log(prefix.symbol); // "G"
console.log(prefix.scale(1.21e9)); // 1.21

This method is used by d3.format for the s format.

<a name="d3_round" href="#d3_round">#</a> d3.<b>round</b>(<i>x</i>[, <i>n</i>])

Returns the value x rounded to n digits after the decimal point. If n is omitted, it defaults to zero. The result is a number. Values are rounded to the closest multiple of 10 to the power minus n; if two multiples are equally close, the value is rounded up in accordance with the built-in round function. For example:

返回小数点后舍入为n位数的值x。 如果省略n,则默认为零。 结果是一个数字。 值被四舍五入到功率减去n的10的最接近的倍数; 如果两个倍数相等,则该值根据内置的回合函数进行四舍五入。 例如:

d3.round(1.23); // 1
d3.round(1.23, 1); // 1.2
d3.round(1.25, 1); // 1.3
d3.round(12.5, 0); // 13
d3.round(12, -1); // 10

Note that the resulting number when converted to a string may be imprecise due to IEEE floating point precision; to format a number to a string with a fixed number of decimal points, use d3.format instead.

Strings

<a name="d3_requote" href="#d3_requote">#</a> d3.<b>requote</b>(<i>string</i>)

Returns a quoted (escaped) version of the specified string such that the string may be embedded in a regular expression as a string literal.

请注意,由于IEEE浮点精度,转换为字符串时的结果数可能不精确; 要将数字格式化为具有固定小数点数的字符串,请改用d3.format。

d3.requote("[]"); // "\[\]"

Dates

See the d3.time module.

上一篇 下一篇

猜你喜欢

热点阅读