d3.format()用法

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:

  • ("<") 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.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 193,968评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,682评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,254评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,074评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 60,964评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,055评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,484评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,170评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,433评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,512评论 2 308
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,296评论 1 325
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,184评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,545评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,880评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,150评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,437评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,630评论 2 335

推荐阅读更多精彩内容