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”?;蛘撸苍S您只想顯示特定數字的有效數字。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:
("<") Forces the field to be left-aligned within the available space.
(">") Forces the field to be right-aligned within the available space. (This is the default).
("^") Forces the field to be centered within the available space.
("<")強制字段在可用空間內左對齊。
(">")強制字段在可用空間內對齊。 (這是默認值)。
("^")強制字段以可用空間為中心。
The sign can be:
plus ("+") - a sign should be used for both positive and negative numbers.
minus ("-") - a sign should be used only for negative numbers. (This is the default.)
space (" ") - a leading space should be used on positive numbers, and a minus sign on negative numbers.
plus ("+") - 正數和負數都應該使用一個符號。
minus ("-") - 一個符號只能用于負數。 (這是默認值。)
space (" ") - 一個領先空間應該用于正數,負號上用負數。
The symbol can be:
currency ("$") - indicates that a currency symbol should be prefixed (or suffixed) per the locale. See Localization for more information on how to set the locale currency symbol.
base ("#") - for binary, octal, or hexadecimal output, prefix by "0b", "0o", or "0x", respectively.
currency ("$") - 表示貨幣符號應該根據區域設置加上(或后綴)。 有關如何設置區域設置貨幣符號的詳細信息,請參閱本地化。
base ("#") - 用于二進制,八進制或十六進制輸出,前綴分別為“0b”,“0o”或“0x”。
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:
exponent ("e") - use Number.toExponential.
general ("g") - use Number.toPrecision.
fixed ("f") - use Number.toFixed.
integer ("d") - use Number.toString, but ignore any non-integer values.
rounded ("r") - round to precision significant digits, padding with zeroes where necessary in similar fashion to fixed ("f"). If no precision is specified, falls back to general notation.
percentage ("%") - like fixed, but multiply by 100 and suffix with "%".
rounded percentage ("p") - like rounded, but multiply by 100 and suffix with "%".
binary ("b") - outputs the number in base 2.
octal ("o") - outputs the number in base 8.
hexadecimal ("x") - outputs the number in base 16, using lower-case letters for the digits above 9.
hexadecimal ("X") - outputs the number in base 16, using upper-case letters for the digits above 9.
character ("c") - converts the integer to the corresponding unicode character before printing.
SI-prefix ("s") - like rounded, but with a unit suffixed such as "9.5M" for mega, or "1.00μ" for micro.
exponent(“e”) - 使用Number.toExponential。
general(“g”) - 使用Number.toPrecision。
fixed(“f”) - 使用Number.toFixed。
integer(“d”) - 使用Number.toString,但忽略任何非整數值。
四舍五入(“r”) - 舍入到精確有效數字,必要時用零填充固定(“f”)。如果沒有指定精度,則返回到一般符號。
百分比(“%”) - 如固定,但乘以100,后綴為“%”。
舍入百分比(“p”) - 如圓形,但乘以100,后綴為“%”。
二進制(“b”) - 輸出基數2中的數字。
八進制(“o”) - 輸出基數8中的數字。
十六進制(“x”) - 輸出基數16中的數字,使用9以上數字的小寫字母。
十六進制(“X”) - 輸出基數16中的數字,使用9以上數字的大寫字母。
字符(“c”) - 在打印之前將整數轉換為相應的unicode字符。
SI前綴(“s”) - 像圓形,但單位后綴如“9.5M”為大,或“1.00μ”為微。
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進行舍入。 返回的前綴對象有兩個屬性:
symbol - the prefix symbol, such as "M" for millions.
scale - the scale function, for converting numbers to the appropriate prefixed scale.
符號 - 前綴符號,例如數百萬的“M”。
縮放 - 縮放函數,用于將數字轉換為適當的前綴標尺。
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.