Add support for F, r and p format types.

`F` is an alias for `f`. `r` is a new format type that rounds the input number,
treating the precision field as the desired number of significant digits. This
is similar to `g`, except it never uses exponent notation. `p` is equivalent to
`r`, except it outputs a percentage, as with `%`.
This commit is contained in:
Mike Bostock 2011-05-20 15:38:09 -07:00
Родитель 01fdc1d93d
Коммит 85a6feec66
5 изменённых файлов: 134 добавлений и 56 удалений

49
d3.js поставляемый
Просмотреть файл

@ -350,7 +350,7 @@ function d3_dispatch(type) {
return dispatch;
};
// TODO align, type
// TODO align
d3.format = function(specifier) {
var match = d3_format_re.exec(specifier),
fill = match[1] || " ",
@ -360,36 +360,35 @@ d3.format = function(specifier) {
comma = match[7],
precision = match[8],
type = match[9],
percentage = false;
percentage = false,
integer = false;
if (type === "n") {
comma = true;
type = "g";
} else if (type === "%") {
percentage = true;
type = "f";
}
if (precision) precision = precision.substring(1);
if (zfill) {
fill = "0"; // TODO align = "=";
if (comma) width -= Math.floor((width - 1) / 4);
}
if (type === "d") precision = "0";
switch (type) {
case "F": type = "f"; break;
case "n": comma = true; type = "g"; break;
case "%": percentage = true; type = "f"; break;
case "p": percentage = true; type = "r"; break;
case "d": integer = true; precision = "0"; break;
}
type = d3_format_types[type] || d3_format_typeDefault;
return function(value) {
var number = percentage ? value * 100 : +value,
negative = (number < 0) && (number = -number) ? "\u2212" : sign;
// Return the empty string for floats formatted as ints.
if ((type === "d") && (number % 1)) return "";
if (integer && (number % 1)) return "";
// Convert the input value to the desired precision.
value = type === "g" ? number.toPrecision(precision).replace(/\.0+$/, "")
: type === "G" ? number.toPrecision(precision).replace(/\.0+$/, "").replace("e", "E")
: type === "e" ? number.toExponential(precision)
: type === "E" ? number.toExponential(precision).replace("e", "E")
: type === "f" ? number.toFixed(precision)
: "" + number;
value = type(number, precision);
// If the fill character is 0, the sign and group is applied after the fill.
if (zfill) {
@ -415,6 +414,22 @@ d3.format = function(specifier) {
// [[fill]align][sign][#][0][width][,][.precision][type]
var d3_format_re = /(?:([^{])?([<>=^]))?([+\- ])?(#)?(0)?([0-9]+)?(,)?(\.[0-9]+)?([a-zA-Z%])?/;
var d3_format_types = {
g: function(x, p) { return x.toPrecision(p).replace(/\.0+$/, ""); },
G: function(x, p) { return d3_format_types.g(x, p).replace("e", "E"); },
e: function(x, p) { return x.toExponential(p); },
E: function(x, p) { return d3_format_types.e(x, p).replace("e", "E"); },
f: function(x, p) { return x.toFixed(p); },
r: function(x, p) {
var n = 1 + Math.floor(1e-15 + Math.log(x) / Math.LN10);
return d3.round(x, p - n).toFixed(Math.max(0, p - n));
}
};
function d3_format_typeDefault(x) {
return x + "";
}
// Apply comma grouping for thousands.
function d3_format_group(value) {
var i = value.lastIndexOf("."),

4
d3.min.js поставляемый

Различия файлов скрыты, потому что одна или несколько строк слишком длинны

Просмотреть файл

@ -1,4 +1,4 @@
// TODO align, type
// TODO align
d3.format = function(specifier) {
var match = d3_format_re.exec(specifier),
fill = match[1] || " ",
@ -8,36 +8,35 @@ d3.format = function(specifier) {
comma = match[7],
precision = match[8],
type = match[9],
percentage = false;
percentage = false,
integer = false;
if (type === "n") {
comma = true;
type = "g";
} else if (type === "%") {
percentage = true;
type = "f";
}
if (precision) precision = precision.substring(1);
if (zfill) {
fill = "0"; // TODO align = "=";
if (comma) width -= Math.floor((width - 1) / 4);
}
if (type === "d") precision = "0";
switch (type) {
case "F": type = "f"; break;
case "n": comma = true; type = "g"; break;
case "%": percentage = true; type = "f"; break;
case "p": percentage = true; type = "r"; break;
case "d": integer = true; precision = "0"; break;
}
type = d3_format_types[type] || d3_format_typeDefault;
return function(value) {
var number = percentage ? value * 100 : +value,
negative = (number < 0) && (number = -number) ? "\u2212" : sign;
// Return the empty string for floats formatted as ints.
if ((type === "d") && (number % 1)) return "";
if (integer && (number % 1)) return "";
// Convert the input value to the desired precision.
value = type === "g" ? number.toPrecision(precision).replace(/\.0+$/, "")
: type === "G" ? number.toPrecision(precision).replace(/\.0+$/, "").replace("e", "E")
: type === "e" ? number.toExponential(precision)
: type === "E" ? number.toExponential(precision).replace("e", "E")
: type === "f" ? number.toFixed(precision)
: "" + number;
value = type(number, precision);
// If the fill character is 0, the sign and group is applied after the fill.
if (zfill) {
@ -63,6 +62,22 @@ d3.format = function(specifier) {
// [[fill]align][sign][#][0][width][,][.precision][type]
var d3_format_re = /(?:([^{])?([<>=^]))?([+\- ])?(#)?(0)?([0-9]+)?(,)?(\.[0-9]+)?([a-zA-Z%])?/;
var d3_format_types = {
g: function(x, p) { return x.toPrecision(p).replace(/\.0+$/, ""); },
G: function(x, p) { return d3_format_types.g(x, p).replace("e", "E"); },
e: function(x, p) { return x.toExponential(p); },
E: function(x, p) { return d3_format_types.e(x, p).replace("e", "E"); },
f: function(x, p) { return x.toFixed(p); },
r: function(x, p) {
var n = 1 + Math.floor(1e-15 + Math.log(x) / Math.LN10);
return d3.round(x, p - n).toFixed(Math.max(0, p - n));
}
};
function d3_format_typeDefault(x) {
return x + "";
}
// Apply comma grouping for thousands.
function d3_format_group(value) {
var i = value.lastIndexOf("."),

Просмотреть файл

@ -94,10 +94,10 @@ console.log(" ", d3.format(".1f")(0.49));
console.log(" ", d3.format(".2f")(0.449));
console.log(" ", d3.format(".3f")(0.4449));
console.log(" ", d3.format(".5f")(0.444449));
console.log(" ", d3.format(".1f")(100));
console.log(" ", d3.format(".2f")(100));
console.log(" ", d3.format(".3f")(100));
console.log(" ", d3.format(".5f")(100));
console.log(" ", d3.format(".1F")(100));
console.log(" ", d3.format(".2F")(100));
console.log(" ", d3.format(".3F")(100));
console.log(" ", d3.format(".5F")(100));
console.log("");
console.log("precision (significant digits):");
@ -112,6 +112,21 @@ console.log(" ", d3.format(".3g")(100));
console.log(" ", d3.format(".5g")(100));
console.log("");
console.log("precision and rounding (significant digits):");
console.log(" ", d3.format(".1r")(0.049));
console.log(" ", d3.format(".1r")(0.49));
console.log(" ", d3.format(".2r")(0.449));
console.log(" ", d3.format(".3r")(0.4449));
console.log(" ", d3.format(".5r")(0.444449));
console.log(" ", d3.format("r")(123.45));
console.log(" ", d3.format(".1r")(123.45));
console.log(" ", d3.format(".2r")(123.45));
console.log(" ", d3.format(".3r")(123.45));
console.log(" ", d3.format(".4r")(123.45));
console.log(" ", d3.format(".5r")(123.45));
console.log(" ", d3.format(".6r")(123.45));
console.log("");
console.log("precision and grouping with space fill:");
console.log(" ", d3.format("10,.1f")(123456.49));
console.log(" ", d3.format("10,.2f")(1234567.449));
@ -128,7 +143,7 @@ console.log(" ", d3.format("f")(42));
console.log("");
console.log("int type passed float:");
console.log(" ", d3.format("d")(4.2));
console.log(d3.format("d")(4.2));
console.log("");
console.log("number:");
@ -146,13 +161,23 @@ console.log("");
console.log("percentage:");
console.log(" ", d3.format("%")(0));
console.log(" ", d3.format("%")(42));
console.log(" ", d3.format("%")(42000000));
console.log(" ", d3.format("%")(420000000));
console.log(" ", d3.format("%")(-4));
console.log(" ", d3.format("%")(-42));
console.log(" ", d3.format("%")(-4200000));
console.log(" ", d3.format("%")(-42000000));
console.log(" ", d3.format("%")(.042));
console.log(" ", d3.format("%")(.42));
console.log(" ", d3.format("%")(4.2));
console.log(" ", d3.format("%")(-.042));
console.log(" ", d3.format("%")(-.42));
console.log(" ", d3.format("%")(-4.2));
console.log("");
console.log("percentage with rounding and sign:");
console.log(" ", d3.format("+.2p")(.00123));
console.log(" ", d3.format("+.2p")(.0123));
console.log(" ", d3.format("+.2p")(.123));
console.log(" ", d3.format("+.2p")(1.23));
console.log(" ", d3.format("+.2p")(-.00123));
console.log(" ", d3.format("+.2p")(-.0123));
console.log(" ", d3.format("+.2p")(-.123));
console.log(" ", d3.format("+.2p")(-1.23));
console.log("");
console.log("exponent:");

Просмотреть файл

@ -97,6 +97,20 @@ precision (significant digits):
100
100
precision and rounding (significant digits):
0.05
0.5
0.45
0.445
0.44445
123
100
120
123
123.5
123.45
123.450
precision and grouping with space fill:
123,456.5
1,234,567.45
@ -111,7 +125,7 @@ float type passed int:
42
int type passed float:
number:
0.0042
@ -127,13 +141,22 @@ number:
percentage:
0%
4200%
4200000000%
42000000000%
400%
4200%
420000000%
4200000000%
4%
42%
420%
4%
42%
420%
percentage with rounding and sign:
+0.12%
+1.2%
+12%
+120%
0.12%
1.2%
12%
120%
exponent:
0e+0