Skip to content

Commit

Permalink
Fix typo in d3.scale.pow. Add tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
mbostock committed Mar 11, 2011
1 parent 166f8b5 commit 996e21e
Show file tree
Hide file tree
Showing 9 changed files with 270 additions and 80 deletions.
4 changes: 3 additions & 1 deletion Makefile
Expand Up @@ -138,7 +138,9 @@ tests: \
tests/test-attr.test \
tests/test-format.test \
tests/test-transition.test \
tests/test-scale-linear.test
tests/test-scale-linear.test \
tests/test-scale-sqrt.test \
tests/test-scale-pow.test

%.min.js: %.js Makefile
@rm -f $@
Expand Down
39 changes: 22 additions & 17 deletions d3.js
@@ -1,4 +1,4 @@
(function(){d3 = {version: "1.6.0"}; // semver
(function(){d3 = {version: "1.6.0+1"}; // this branch not semver!
if (!Date.now) Date.now = function() {
return +new Date();
};
Expand Down Expand Up @@ -2086,17 +2086,9 @@ d3.scale.log = function() {
d3.scale.pow = function() {
var linear = d3.scale.linear(),
tick = d3.scale.linear(), // TODO better tick formatting...
p = 1,
b = 1 / p,
n = false;

function powp(x) {
return n ? -Math.pow(-x, p) : Math.pow(x, p);
}

function powb(x) {
return n ? -Math.pow(-x, b) : Math.pow(x, b);
}
exponent = 1,
powp = Number,
powb = powp;

function scale(x) {
return linear(powp(x));
Expand All @@ -2108,28 +2100,41 @@ d3.scale.pow = function() {

scale.domain = function(x) {
if (!arguments.length) return linear.domain().map(powb);
n = (x[0] || x[1]) < 0;
var pow = (x[0] || x[1]) < 0 ? d3_scale_pown : d3_scale_pow;
powp = pow(exponent);
powb = pow(1 / exponent);
linear.domain(x.map(powp));
tick.domain(x);
return scale;
};

scale.range = d3_rebind(scale, linear.range);
scale.rangeRound = d3_rebind(scale, linear.rangeRound);
scale.inteprolate = d3_rebind(scale, linear.interpolate);
scale.interpolate = d3_rebind(scale, linear.interpolate);
scale.ticks = tick.ticks;
scale.tickFormat = tick.tickFormat;

scale.exponent = function(x) {
if (!arguments.length) return p;
if (!arguments.length) return exponent;
var domain = scale.domain();
p = x;
b = 1 / x;
exponent = x;
return scale.domain(domain);
};

return scale;
};

function d3_scale_pow(e) {
return function(x) {
return Math.pow(x, e);
};
}

function d3_scale_pown(e) {
return function(x) {
return -Math.pow(-x, e);
};
}
d3.scale.sqrt = function() {
return d3.scale.pow().exponent(.5);
};
Expand Down
90 changes: 45 additions & 45 deletions d3.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/core/core.js
@@ -1 +1 @@
d3 = {version: "1.6.0"}; // semver
d3 = {version: "1.6.0+1"}; // this branch not semver!
37 changes: 21 additions & 16 deletions src/scale/pow.js
@@ -1,17 +1,9 @@
d3.scale.pow = function() {
var linear = d3.scale.linear(),
tick = d3.scale.linear(), // TODO better tick formatting...
p = 1,
b = 1 / p,
n = false;

function powp(x) {
return n ? -Math.pow(-x, p) : Math.pow(x, p);
}

function powb(x) {
return n ? -Math.pow(-x, b) : Math.pow(x, b);
}
exponent = 1,
powp = Number,
powb = powp;

function scale(x) {
return linear(powp(x));
Expand All @@ -23,25 +15,38 @@ d3.scale.pow = function() {

scale.domain = function(x) {
if (!arguments.length) return linear.domain().map(powb);
n = (x[0] || x[1]) < 0;
var pow = (x[0] || x[1]) < 0 ? d3_scale_pown : d3_scale_pow;
powp = pow(exponent);
powb = pow(1 / exponent);
linear.domain(x.map(powp));
tick.domain(x);
return scale;
};

scale.range = d3_rebind(scale, linear.range);
scale.rangeRound = d3_rebind(scale, linear.rangeRound);
scale.inteprolate = d3_rebind(scale, linear.interpolate);
scale.interpolate = d3_rebind(scale, linear.interpolate);
scale.ticks = tick.ticks;
scale.tickFormat = tick.tickFormat;

scale.exponent = function(x) {
if (!arguments.length) return p;
if (!arguments.length) return exponent;
var domain = scale.domain();
p = x;
b = 1 / x;
exponent = x;
return scale.domain(domain);
};

return scale;
};

function d3_scale_pow(e) {
return function(x) {
return Math.pow(x, e);
};
}

function d3_scale_pown(e) {
return function(x) {
return -Math.pow(-x, e);
};
}
53 changes: 53 additions & 0 deletions tests/test-scale-pow.js
@@ -0,0 +1,53 @@
require("./../lib/env-js/envjs/node");
require("./../lib/sizzle/sizzle");
require("./../d3");
require("./../d3.time");

var f = d3.format(" .3f");

var x = d3.scale.pow().exponent(2);
console.log("domain([0, 1]).range([0, 1]):");
console.log(" -0.5 -> ", f(x(-0.5)));
console.log(" 0.0 -> ", f(x(0.0)));
console.log(" 0.5 -> ", f(x(0.5)));
console.log(" 1.0 -> ", f(x(1.0)));
console.log(" 1.5 -> ", f(x(1.5)));
console.log("");

var x = d3.scale.pow().exponent(2).domain([1, 2]);
console.log("domain([1, 2]).range([0, 1]):");
console.log(" 0.5 -> ", f(x(0.5)));
console.log(" 1.0 -> ", f(x(1.0)));
console.log(" 1.5 -> ", f(x(1.5)));
console.log(" 2.0 -> ", f(x(2.0)));
console.log(" 2.5 -> ", f(x(2.5)));
console.log("");

var x = d3.scale.pow().exponent(2).domain([new Date(1990, 0, 1), new Date(1991, 0, 1)]);
console.log("domain([01/01/1990, 01/01/1991]).range([0, 1]):");
console.log(" 10/20/1989 -> ", f(x(new Date(1989, 09, 20))));
console.log(" 01/01/1990 -> ", f(x(new Date(1990, 00, 01))));
console.log(" 03/15/1990 -> ", f(x(new Date(1990, 02, 15))));
console.log(" 05/27/1990 -> ", f(x(new Date(1990, 04, 27))));
console.log(" 01/01/1991 -> ", f(x(new Date(1991, 00, 01))));
console.log(" 03/15/1991 -> ", f(x(new Date(1991, 02, 15))));
console.log("");

var x = d3.scale.pow().exponent(2).range(["red", "blue"]);
console.log("domain([0, 1]).range([\"red\", \"blue\"]):");
console.log(" -0.5 -> ", x(-0.5));
console.log(" 0.0 -> ", x(0.0));
console.log(" 0.5 -> ", x(0.5));
console.log(" 1.0 -> ", x(1.0));
console.log(" 1.5 -> ", x(1.5));
console.log("");

var x = d3.scale.pow().exponent(2).range(["red", "blue"]).interpolate(d3.interpolateHsl);
console.log("domain([0, 1]).range([\"red\", \"blue\"]).interpolate(hsl):");
console.log(" -0.50 -> ", x(-0.50));
console.log(" 0.00 -> ", x(0.00));
console.log(" 0.50 -> ", x(0.50));
console.log(" √0.50 -> ", x(Math.SQRT1_2));
console.log(" 1.00 -> ", x(1.00));
console.log(" 2.00 -> ", x(2.00));
console.log("");
37 changes: 37 additions & 0 deletions tests/test-scale-pow.out
@@ -0,0 +1,37 @@
domain([0, 1]).range([0, 1]):
-0.5 -> 0.250
0.0 -> 0.000
0.5 -> 0.250
1.0 -> 1.000
1.5 -> 2.250

domain([1, 2]).range([0, 1]):
0.5 -> −0.250
1.0 -> 0.000
1.5 -> 0.417
2.0 -> 1.000
2.5 -> 1.750

domain([01/01/1990, 01/01/1991]).range([0, 1]):
10/20/1989 -> −0.194
01/01/1990 -> 0.000
03/15/1990 -> 0.196
05/27/1990 -> 0.394
01/01/1991 -> 1.000
03/15/1991 -> 1.206

domain([0, 1]).range(["red", "blue"]):
-0.5 -> rgb(191,0,64)
0.0 -> rgb(255,0,0)
0.5 -> rgb(191,0,64)
1.0 -> rgb(0,0,255)
1.5 -> rgb(-319,0,574)

domain([0, 1]).range(["red", "blue"]).interpolate(hsl):
-0.50 -> #ffff00
0.00 -> #ff0000
0.50 -> #ffff00
√0.50 -> #00ff00
1.00 -> #0000ff
2.00 -> #0000ff

52 changes: 52 additions & 0 deletions tests/test-scale-sqrt.js
@@ -0,0 +1,52 @@
require("./../lib/env-js/envjs/node");
require("./../lib/sizzle/sizzle");
require("./../d3");
require("./../d3.time");

var f = d3.format(" .3f");

var x = d3.scale.sqrt();
console.log("domain([0, 1]).range([0, 1]):");
console.log(" -0.50 -> ", f(x(-0.50)));
console.log(" 0.00 -> ", f(x(0.00)));
console.log(" 0.25 -> ", f(x(0.25)));
console.log(" 0.50 -> ", f(x(0.50)));
console.log(" 1.00 -> ", f(x(1.00)));
console.log(" 4.00 -> ", f(x(4.00)));
console.log("");

var x = d3.scale.sqrt().domain([0, -1]);
console.log("domain([0, -1]).range([0, 1]):");
console.log(" 0.50 -> ", f(x(0.50)));
console.log(" 0.00 -> ", f(x(0.00)));
console.log(" -0.25 -> ", f(x(-0.25)));
console.log(" -0.50 -> ", f(x(-0.50)));
console.log(" -1.00 -> ", f(x(-1.00)));
console.log(" -4.00 -> ", f(x(-4.00)));
console.log("");

var x = d3.scale.sqrt().domain([1, 2]);
console.log("domain([1, 2]).range([0, 1]):");
console.log(" 0.5 -> ", f(x(0.5)));
console.log(" 1.0 -> ", f(x(1.0)));
console.log(" 1.5 -> ", f(x(1.5)));
console.log(" 2.0 -> ", f(x(2.0)));
console.log(" 2.5 -> ", f(x(2.5)));
console.log("");

var x = d3.scale.sqrt().range(["red", "blue"]);
console.log("domain([0, 1]).range([\"red\", \"blue\"]):");
console.log(" 0.00 -> ", x(0.00));
console.log(" 0.25 -> ", x(0.25));
console.log(" 1.00 -> ", x(1.00));
console.log(" 4.00 -> ", x(4.00));
console.log("");

var x = d3.scale.sqrt().domain([1, 0]).range(["red", "blue"]).interpolate(d3.interpolateHsl);
console.log("domain([1, 0]).range([\"red\", \"blue\"]).interpolate(hsl):");
console.log(" 0.00 -> ", x(0.00));
console.log(" 0.25 -> ", x(0.25));
console.log(" 0.50 -> ", x(0.50));
console.log(" 1.00 -> ", x(1.00));
console.log(" 4.00 -> ", x(4.00));
console.log("");
36 changes: 36 additions & 0 deletions tests/test-scale-sqrt.out
@@ -0,0 +1,36 @@
domain([0, 1]).range([0, 1]):
-0.50 -> NaN
0.00 -> 0.000
0.25 -> 0.500
0.50 -> 0.707
1.00 -> 1.000
4.00 -> 2.000

domain([0, -1]).range([0, 1]):
0.50 -> NaN
0.00 -> 0.000
-0.25 -> 0.500
-0.50 -> 0.707
-1.00 -> 1.000
-4.00 -> 2.000

domain([1, 2]).range([0, 1]):
0.5 -> −0.707
1.0 -> 0.000
1.5 -> 0.543
2.0 -> 1.000
2.5 -> 1.403

domain([0, 1]).range(["red", "blue"]):
0.00 -> rgb(255,0,0)
0.25 -> rgb(128,0,128)
1.00 -> rgb(0,0,255)
4.00 -> rgb(-255,0,510)

domain([1, 0]).range(["red", "blue"]).interpolate(hsl):
0.00 -> #0000ff
0.25 -> #00ff00
0.50 -> #d3ff00
1.00 -> #ff0000
4.00 -> #00ff00

0 comments on commit 996e21e

Please sign in to comment.