Permalink
Browse files

Merge branch 'ordinal'

  • Loading branch information...
2 parents 2c52c37 + 9cd6a46 commit 5ba1c2a06640d7ad7c39d75255bdd415b9dfa7a5 @mbostock mbostock committed Aug 1, 2011
Showing with 221 additions and 73 deletions.
  1. +4 −2 Makefile
  2. +35 −32 d3.js
  3. +2 −2 d3.min.js
  4. +1 −1 src/core/core.js
  5. +1 −0 src/core/noop.js
  6. +29 −20 src/scale/ordinal.js
  7. +4 −11 src/scale/pow.js
  8. +79 −0 test/scale/test-ordinal.js
  9. +61 −0 test/scale/test-ordinal.out
  10. +3 −3 test/scale/test-pow.out
  11. +2 −2 test/scale/test-sqrt.out
View
@@ -71,7 +71,8 @@ d3.core.js: \
src/core/hsl.js \
src/core/selection.js \
src/core/transition.js \
- src/core/timer.js
+ src/core/timer.js \
+ src/core/noop.js
d3.scale.js: \
src/scale/scale.js \
@@ -229,7 +230,8 @@ test/scale: \
test/scale/test-polylinear.test \
test/scale/test-pow.test \
test/scale/test-quantile.test \
- test/scale/test-sqrt.test
+ test/scale/test-sqrt.test \
+ test/scale/test-ordinal.test
test/svg: \
test/svg/test-arc.test \
View
67 d3.js
@@ -1,4 +1,4 @@
-(function(){d3 = {version: "1.29.0"}; // semver
+(function(){d3 = {version: "1.29.1"}; // semver
if (!Date.now) Date.now = function() {
return +new Date;
};
@@ -2178,6 +2178,7 @@ var d3_timer_frame = window.requestAnimationFrame
|| window.oRequestAnimationFrame
|| window.msRequestAnimationFrame
|| function(callback) { setTimeout(callback, 17); };
+function d3_noop() {}
d3.scale = {};
function d3_scaleExtent(domain) {
@@ -2429,9 +2430,8 @@ d3.scale.pow = function() {
scale.domain = function(x) {
if (!arguments.length) return linear.domain().map(powb);
- var pow = (x[0] || x[x.length - 1]) < 0 ? d3_scale_pown : d3_scale_pow;
- powp = pow(exponent);
- powb = pow(1 / exponent);
+ powp = d3_scale_powPow(exponent);
+ powb = d3_scale_powPow(1 / exponent);
linear.domain(x.map(powp));
return scale;
};
@@ -2458,15 +2458,9 @@ d3.scale.pow = function() {
return d3_scale_linearRebind(scale, linear);
};
-function d3_scale_pow(e) {
+function d3_scale_powPow(e) {
return function(x) {
- return Math.pow(x, e);
- };
-}
-
-function d3_scale_pown(e) {
- return function(x) {
- return -Math.pow(-x, e);
+ return x < 0 ? -Math.pow(-x, e) : Math.pow(x, e);
};
}
d3.scale.sqrt = function() {
@@ -2476,7 +2470,8 @@ d3.scale.ordinal = function() {
var domain = [],
index = {},
range = [],
- rangeBand = 0;
+ rangeBand = 0,
+ rerange = d3_noop;
function scale(x) {
var i = x in index ? index[x] : (index[x] = domain.push(x) - 1);
@@ -2491,46 +2486,54 @@ d3.scale.ordinal = function() {
x = domain[i];
if (!(x in index)) index[x] = ++j;
}
+ rerange();
return scale;
};
scale.range = function(x) {
if (!arguments.length) return range;
range = x;
+ rerange = d3_noop;
return scale;
};
scale.rangePoints = function(x, padding) {
if (arguments.length < 2) padding = 0;
- var start = x[0],
- stop = x[1],
- step = (stop - start) / (domain.length - 1 + padding);
- range = domain.length == 1
- ? [(start + stop) / 2]
- : d3.range(start + step * padding / 2, stop + step / 2, step);
- rangeBand = 0;
+ (rerange = function() {
+ var start = x[0],
+ stop = x[1],
+ step = (stop - start) / (domain.length - 1 + padding);
+ range = domain.length == 1
+ ? [(start + stop) / 2]
+ : d3.range(start + step * padding / 2, stop + step / 2, step);
+ rangeBand = 0;
+ })();
return scale;
};
scale.rangeBands = function(x, padding) {
if (arguments.length < 2) padding = 0;
- var start = x[0],
- stop = x[1],
- step = (stop - start) / (domain.length + padding);
- range = d3.range(start + step * padding, stop, step);
- rangeBand = step * (1 - padding);
+ (rerange = function() {
+ var start = x[0],
+ stop = x[1],
+ step = (stop - start) / (domain.length + padding);
+ range = d3.range(start + step * padding, stop, step);
+ rangeBand = step * (1 - padding);
+ })();
return scale;
};
scale.rangeRoundBands = function(x, padding) {
if (arguments.length < 2) padding = 0;
- var start = x[0],
- stop = x[1],
- diff = stop - start,
- step = Math.floor(diff / (domain.length + padding)),
- err = diff - (domain.length - padding) * step;
- range = d3.range(start + Math.round(err / 2), stop, step);
- rangeBand = Math.round(step * (1 - padding));
+ (rerange = function() {
+ var start = x[0],
+ stop = x[1],
+ diff = stop - start,
+ step = Math.floor(diff / (domain.length + padding)),
+ err = diff - (domain.length - padding) * step;
+ range = d3.range(start + Math.round(err / 2), stop, step);
+ rangeBand = Math.round(step * (1 - padding));
+ })();
return scale;
};
View

Large diffs are not rendered by default.

Oops, something went wrong.
View
@@ -1 +1 @@
-d3 = {version: "1.29.0"}; // semver
+d3 = {version: "1.29.1"}; // semver
View
@@ -0,0 +1 @@
+function d3_noop() {}
View
@@ -2,7 +2,8 @@ d3.scale.ordinal = function() {
var domain = [],
index = {},
range = [],
- rangeBand = 0;
+ rangeBand = 0,
+ rerange = d3_noop;
function scale(x) {
var i = x in index ? index[x] : (index[x] = domain.push(x) - 1);
@@ -17,46 +18,54 @@ d3.scale.ordinal = function() {
x = domain[i];
if (!(x in index)) index[x] = ++j;
}
+ rerange();
return scale;
};
scale.range = function(x) {
if (!arguments.length) return range;
range = x;
+ rerange = d3_noop;
return scale;
};
scale.rangePoints = function(x, padding) {
if (arguments.length < 2) padding = 0;
- var start = x[0],
- stop = x[1],
- step = (stop - start) / (domain.length - 1 + padding);
- range = domain.length == 1
- ? [(start + stop) / 2]
- : d3.range(start + step * padding / 2, stop + step / 2, step);
- rangeBand = 0;
+ (rerange = function() {
+ var start = x[0],
+ stop = x[1],
+ step = (stop - start) / (domain.length - 1 + padding);
+ range = domain.length == 1
+ ? [(start + stop) / 2]
+ : d3.range(start + step * padding / 2, stop + step / 2, step);
+ rangeBand = 0;
+ })();
return scale;
};
scale.rangeBands = function(x, padding) {
if (arguments.length < 2) padding = 0;
- var start = x[0],
- stop = x[1],
- step = (stop - start) / (domain.length + padding);
- range = d3.range(start + step * padding, stop, step);
- rangeBand = step * (1 - padding);
+ (rerange = function() {
+ var start = x[0],
+ stop = x[1],
+ step = (stop - start) / (domain.length + padding);
+ range = d3.range(start + step * padding, stop, step);
+ rangeBand = step * (1 - padding);
+ })();
return scale;
};
scale.rangeRoundBands = function(x, padding) {
if (arguments.length < 2) padding = 0;
- var start = x[0],
- stop = x[1],
- diff = stop - start,
- step = Math.floor(diff / (domain.length + padding)),
- err = diff - (domain.length - padding) * step;
- range = d3.range(start + Math.round(err / 2), stop, step);
- rangeBand = Math.round(step * (1 - padding));
+ (rerange = function() {
+ var start = x[0],
+ stop = x[1],
+ diff = stop - start,
+ step = Math.floor(diff / (domain.length + padding)),
+ err = diff - (domain.length - padding) * step;
+ range = d3.range(start + Math.round(err / 2), stop, step);
+ rangeBand = Math.round(step * (1 - padding));
+ })();
return scale;
};
View
@@ -14,9 +14,8 @@ d3.scale.pow = function() {
scale.domain = function(x) {
if (!arguments.length) return linear.domain().map(powb);
- var pow = (x[0] || x[x.length - 1]) < 0 ? d3_scale_pown : d3_scale_pow;
- powp = pow(exponent);
- powb = pow(1 / exponent);
+ powp = d3_scale_powPow(exponent);
+ powb = d3_scale_powPow(1 / exponent);
linear.domain(x.map(powp));
return scale;
};
@@ -43,14 +42,8 @@ d3.scale.pow = function() {
return d3_scale_linearRebind(scale, linear);
};
-function d3_scale_pow(e) {
+function d3_scale_powPow(e) {
return function(x) {
- return Math.pow(x, e);
- };
-}
-
-function d3_scale_pown(e) {
- return function(x) {
- return -Math.pow(-x, e);
+ return x < 0 ? -Math.pow(-x, e) : Math.pow(x, e);
};
}
View
@@ -0,0 +1,79 @@
+require("./../../lib/env-js/envjs/node");
+require("./../../d3");
+
+var x = d3.scale.ordinal().range(["a", "b", "c"]);
+console.log("range([a, b, c]):");
+console.log(" 0 -> " + x(0));
+console.log(" 1 -> " + x(1));
+console.log(" 2 -> " + x(2));
+console.log(" 3 -> " + x(3));
+console.log(" 4 -> " + x(4));
+console.log(" 5 -> " + x(5));
+console.log(" 6 -> " + x(6));
+console.log(" 0 -> " + x(0));
+console.log(" 1 -> " + x(1));
+console.log(" 2 -> " + x(2));
+console.log(" 3 -> " + x(3));
+console.log("");
+
+x.domain([]);
+console.log("domain([]).range([a, b, c]):");
+console.log(" 1 -> " + x(1));
+console.log(" 2 -> " + x(2));
+console.log(" 3 -> " + x(3));
+console.log(" 0 -> " + x(0));
+console.log(" 1 -> " + x(1));
+console.log(" 2 -> " + x(2));
+console.log("");
+
+var x = d3.scale.ordinal().domain(["a", "b", "c"]).rangePoints([0, 120]);
+console.log("domain([a, b, c]).rangePoints([0, 120]):");
+console.log(" a -> " + x("a"));
+console.log(" b -> " + x("b"));
+console.log(" c -> " + x("c"));
+console.log(" d -> " + x("d"));
+console.log("");
+
+var x = d3.scale.ordinal().rangePoints([0, 120]).domain(["a", "b", "c"]);
+console.log("rangePoints([0, 120]).domain([a, b, c]):");
+console.log(" a -> " + x("a"));
+console.log(" b -> " + x("b"));
+console.log(" c -> " + x("c"));
+console.log(" d -> " + x("d"));
+console.log("");
+
+var x = d3.scale.ordinal().domain(["a", "b", "c"]).rangeBands([0, 120]);
+console.log("domain([a, b, c]).rangeBands([0, 120]):");
+console.log(" rangeBand: " + x.rangeBand());
+console.log(" a -> " + x("a"));
+console.log(" b -> " + x("b"));
+console.log(" c -> " + x("c"));
+console.log(" d -> " + x("d"));
+console.log("");
+
+var x = d3.scale.ordinal().rangeBands([0, 120]).domain(["a", "b", "c"]);
+console.log("rangeBands([0, 120]).domain([a, b, c]):");
+console.log(" rangeBand: " + x.rangeBand());
+console.log(" a -> " + x("a"));
+console.log(" b -> " + x("b"));
+console.log(" c -> " + x("c"));
+console.log(" d -> " + x("d"));
+console.log("");
+
+var x = d3.scale.ordinal().domain(["a", "b", "c"]).rangeRoundBands([0, 119]);
+console.log("domain([a, b, c]).rangeRoundBands([0, 119]):");
+console.log(" rangeBand: " + x.rangeBand());
+console.log(" a -> " + x("a"));
+console.log(" b -> " + x("b"));
+console.log(" c -> " + x("c"));
+console.log(" d -> " + x("d"));
+console.log("");
+
+var x = d3.scale.ordinal().rangeRoundBands([0, 119]).domain(["a", "b", "c"]);
+console.log("rangeRoundBands([0, 119]).domain([a, b, c]):");
+console.log(" rangeBand: " + x.rangeBand());
+console.log(" a -> " + x("a"));
+console.log(" b -> " + x("b"));
+console.log(" c -> " + x("c"));
+console.log(" d -> " + x("d"));
+console.log("");
Oops, something went wrong.

0 comments on commit 5ba1c2a

Please sign in to comment.