Commit 307016e8 authored by Mike Bostock's avatar Mike Bostock
Browse files

Fix a rounding bug in SI-prefix format.

Also, expose d3.formatPrefix so that it's easier for callers to create a
formatter for a specific prefix (such as using the "G" prefix for all ticks).
parent 4adfae71
......@@ -74,6 +74,7 @@ d3.core.js: \
src/core/ns.js \
src/core/dispatch.js \
src/core/format.js \
src/core/formatPrefix.js \
src/core/ease.js \
src/core/event.js \
src/core/interpolate.js \
......
......@@ -10,7 +10,7 @@ try {
d3_style_setProperty.call(this, name, value + "", priority);
};
}
d3 = {version: "2.4.0"}; // semver
d3 = {version: "2.4.1"}; // semver
var d3_array = d3_arraySlice; // conversion for NodeLists
function d3_arrayCopy(pseudoarray) {
......@@ -527,9 +527,9 @@ d3.format = function(specifier) {
// Apply the scale, computing it from the value's exponent for si format.
if (scale < 0) {
var exponent = Math.max(-24, Math.min(24, d3_format_exponent(value, precision)));
value *= Math.pow(10, -exponent);
suffix = d3_format_si[8 + exponent / 3];
var prefix = d3.formatPrefix(value, precision);
value *= prefix.scale;
suffix = prefix.symbol;
} else {
value *= scale;
}
......@@ -558,8 +558,7 @@ d3.format = function(specifier) {
};
// [[fill]align][sign][#][0][width][,][.precision][type]
var d3_format_re = /(?:([^{])?([<>=^]))?([+\- ])?(#)?(0)?([0-9]+)?(,)?(\.[0-9]+)?([a-zA-Z%])?/,
d3_format_si = ["y","z","a","f","p","n","μ","m","","k","M","G","T","P","E","Z","Y"];
var d3_format_re = /(?:([^{])?([<>=^]))?([+\- ])?(#)?(0)?([0-9]+)?(,)?(\.[0-9]+)?([a-zA-Z%])?/;
var d3_format_types = {
g: function(x, p) { return x.toPrecision(p); },
......@@ -572,13 +571,6 @@ function d3_format_precision(x, p) {
return p - (x ? 1 + Math.floor(Math.log(x + Math.pow(10, 1 + Math.floor(Math.log(x) / Math.LN10) - p)) / Math.LN10) : 1);
}
function d3_format_exponent(x, p) {
if (!x) return 0;
if (p) x = d3.round(x, d3_format_precision(x, p));
var d = 1 + Math.floor(Math.log(1e-9 + x) / Math.LN10);
return Math.floor((d <= 0 ? d + 1 : d - 1) / 3) * 3;
}
function d3_format_typeDefault(x) {
return x + "";
}
......@@ -591,6 +583,26 @@ function d3_format_group(value) {
while (i > 0) t.push(value.substring(i -= 3, i + 3));
return t.reverse().join(",") + f;
}
var d3_formatPrefixes = ["y","z","a","f","p","n","μ","m","","k","M","G","T","P","E","Z","Y"].map(d3_formatPrefix);
d3.formatPrefix = function(value, precision) {
var i = 0;
if (value) {
if (value < 0) value *= -1;
if (precision) value = d3.round(value, d3_format_precision(value, precision));
i = 1 + Math.floor(1e-12 + Math.log(value) / Math.LN10);
i = Math.max(-24, Math.min(24, Math.floor((i <= 0 ? i + 1 : i - 1) / 3) * 3));
}
return d3_formatPrefixes[8 + i / 3];
};
function d3_formatPrefix(d, i) {
return {
scale: Math.pow(10, (8 - i) * 3),
symbol: d
};
}
/*
* TERMS OF USE - EASING EQUATIONS
*
......
This diff is collapsed.
{
"name": "d3",
"version": "2.4.0",
"version": "2.4.1",
"description": "A small, free JavaScript library for manipulating documents based on data.",
"keywords": [
"dom",
......
d3 = {version: "2.4.0"}; // semver
d3 = {version: "2.4.1"}; // semver
......@@ -42,9 +42,9 @@ d3.format = function(specifier) {
// Apply the scale, computing it from the value's exponent for si format.
if (scale < 0) {
var exponent = Math.max(-24, Math.min(24, d3_format_exponent(value, precision)));
value *= Math.pow(10, -exponent);
suffix = d3_format_si[8 + exponent / 3];
var prefix = d3.formatPrefix(value, precision);
value *= prefix.scale;
suffix = prefix.symbol;
} else {
value *= scale;
}
......@@ -73,8 +73,7 @@ d3.format = function(specifier) {
};
// [[fill]align][sign][#][0][width][,][.precision][type]
var d3_format_re = /(?:([^{])?([<>=^]))?([+\- ])?(#)?(0)?([0-9]+)?(,)?(\.[0-9]+)?([a-zA-Z%])?/,
d3_format_si = ["y","z","a","f","p","n","μ","m","","k","M","G","T","P","E","Z","Y"];
var d3_format_re = /(?:([^{])?([<>=^]))?([+\- ])?(#)?(0)?([0-9]+)?(,)?(\.[0-9]+)?([a-zA-Z%])?/;
var d3_format_types = {
g: function(x, p) { return x.toPrecision(p); },
......@@ -87,13 +86,6 @@ function d3_format_precision(x, p) {
return p - (x ? 1 + Math.floor(Math.log(x + Math.pow(10, 1 + Math.floor(Math.log(x) / Math.LN10) - p)) / Math.LN10) : 1);
}
function d3_format_exponent(x, p) {
if (!x) return 0;
if (p) x = d3.round(x, d3_format_precision(x, p));
var d = 1 + Math.floor(Math.log(1e-9 + x) / Math.LN10);
return Math.floor((d <= 0 ? d + 1 : d - 1) / 3) * 3;
}
function d3_format_typeDefault(x) {
return x + "";
}
......
var d3_formatPrefixes = ["y","z","a","f","p","n","μ","m","","k","M","G","T","P","E","Z","Y"].map(d3_formatPrefix);
d3.formatPrefix = function(value, precision) {
var i = 0;
if (value) {
if (value < 0) value *= -1;
if (precision) value = d3.round(value, d3_format_precision(value, precision));
i = 1 + Math.floor(1e-12 + Math.log(value) / Math.LN10);
i = Math.max(-24, Math.min(24, Math.floor((i <= 0 ? i + 1 : i - 1) / 3) * 3));
}
return d3_formatPrefixes[8 + i / 3];
};
function d3_formatPrefix(d, i) {
return {
scale: Math.pow(10, (8 - i) * 3),
symbol: d
};
}
......@@ -96,9 +96,11 @@ suite.addBatch({
assert.strictEqual(f(145999999.999999347), "146M");
assert.strictEqual(f(1e26), "100Y");
assert.strictEqual(f(.000001), "1.00μ");
assert.strictEqual(f(.009995), "0.0100");
var f = format(".4s");
assert.strictEqual(f(999.5), "999.5");
assert.strictEqual(f(999500), "999.5k");
assert.strictEqual(f(.009995), "9.995m");
},
"can output a percentage": function(format) {
var f = format("%");
......
require("../env");
require("../../d3");
var vows = require("vows"),
assert = require("assert");
var suite = vows.describe("d3.formatPrefix");
suite.addBatch({
"formatPrefix": {
topic: function() {
return d3.formatPrefix;
},
"determines the appropriate prefix for small numbers": function(prefix) {
assert.equal(prefix(0).symbol, "");
assert.equal(prefix(1e-00).symbol, "");
assert.equal(prefix(1e-01).symbol, "");
assert.equal(prefix(1e-02).symbol, "");
assert.equal(prefix(1e-03).symbol, "m");
assert.equal(prefix(1e-04).symbol, "m");
assert.equal(prefix(1e-05).symbol, "m");
assert.equal(prefix(1e-06).symbol, "μ");
assert.equal(prefix(1e-07).symbol, "μ");
assert.equal(prefix(1e-08).symbol, "μ");
assert.equal(prefix(1e-09).symbol, "n");
assert.equal(prefix(1e-10).symbol, "n");
assert.equal(prefix(1e-11).symbol, "n");
assert.equal(prefix(1e-12).symbol, "p");
assert.equal(prefix(1e-13).symbol, "p");
assert.equal(prefix(1e-14).symbol, "p");
assert.equal(prefix(1e-15).symbol, "f");
assert.equal(prefix(1e-16).symbol, "f");
assert.equal(prefix(1e-17).symbol, "f");
assert.equal(prefix(1e-18).symbol, "a");
assert.equal(prefix(1e-19).symbol, "a");
assert.equal(prefix(1e-20).symbol, "a");
assert.equal(prefix(1e-21).symbol, "z");
assert.equal(prefix(1e-22).symbol, "z");
assert.equal(prefix(1e-23).symbol, "z");
assert.equal(prefix(1e-24).symbol, "y");
assert.equal(prefix(1e-25).symbol, "y");
assert.equal(prefix(1e-26).symbol, "y");
assert.equal(prefix(1e-27).symbol, "y");
},
"determines the appropriate prefix for large numbers": function(prefix) {
assert.equal(prefix(0).symbol, "");
assert.equal(prefix(1e00).symbol, "");
assert.equal(prefix(1e01).symbol, "");
assert.equal(prefix(1e02).symbol, "");
assert.equal(prefix(1e03).symbol, "k");
assert.equal(prefix(1e04).symbol, "k");
assert.equal(prefix(1e05).symbol, "k");
assert.equal(prefix(1e06).symbol, "M");
assert.equal(prefix(1e07).symbol, "M");
assert.equal(prefix(1e08).symbol, "M");
assert.equal(prefix(1e09).symbol, "G");
assert.equal(prefix(1e10).symbol, "G");
assert.equal(prefix(1e11).symbol, "G");
assert.equal(prefix(1e12).symbol, "T");
assert.equal(prefix(1e13).symbol, "T");
assert.equal(prefix(1e14).symbol, "T");
assert.equal(prefix(1e15).symbol, "P");
assert.equal(prefix(1e16).symbol, "P");
assert.equal(prefix(1e17).symbol, "P");
assert.equal(prefix(1e18).symbol, "E");
assert.equal(prefix(1e19).symbol, "E");
assert.equal(prefix(1e20).symbol, "E");
assert.equal(prefix(1e21).symbol, "Z");
assert.equal(prefix(1e22).symbol, "Z");
assert.equal(prefix(1e23).symbol, "Z");
assert.equal(prefix(1e24).symbol, "Y");
assert.equal(prefix(1e25).symbol, "Y");
assert.equal(prefix(1e26).symbol, "Y");
assert.equal(prefix(1e27).symbol, "Y");
},
"determines the appropriate prefix for negative numbers": function(prefix) {
assert.equal(prefix(-0).symbol, "");
assert.equal(prefix(-1e-00).symbol, "");
assert.equal(prefix(-1e-03).symbol, "m");
assert.equal(prefix(-1e-06).symbol, "μ");
assert.equal(prefix(-1e-09).symbol, "n");
assert.equal(prefix(-1e-12).symbol, "p");
assert.equal(prefix(-1e-15).symbol, "f");
assert.equal(prefix(-1e-18).symbol, "a");
assert.equal(prefix(-1e-21).symbol, "z");
assert.equal(prefix(-1e-24).symbol, "y");
assert.equal(prefix(-1e-27).symbol, "y");
assert.equal(prefix(-1e00).symbol, "");
assert.equal(prefix(-1e03).symbol, "k");
assert.equal(prefix(-1e06).symbol, "M");
assert.equal(prefix(-1e09).symbol, "G");
assert.equal(prefix(-1e12).symbol, "T");
assert.equal(prefix(-1e15).symbol, "P");
assert.equal(prefix(-1e18).symbol, "E");
assert.equal(prefix(-1e21).symbol, "Z");
assert.equal(prefix(-1e24).symbol, "Y");
assert.equal(prefix(-1e27).symbol, "Y");
},
"considers the effect of rounding based on precision": function(prefix) {
assert.equal(prefix(999.5, 3).symbol, "k");
assert.equal(prefix(999.5, 4).symbol, "");
assert.equal(prefix(.009995, 3).symbol, "");
assert.equal(prefix(.009995, 4).symbol, "m");
}
}
});
suite.export(module);
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment