Skip to content

Commit

Permalink
Add a few tests for d3.scale.linear.
Browse files Browse the repository at this point in the history
  • Loading branch information
mbostock committed Mar 8, 2011
1 parent 9857dad commit 871cefc
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 1 deletion.
3 changes: 2 additions & 1 deletion Makefile
Expand Up @@ -137,7 +137,8 @@ tests: \
tests/test-append.test \
tests/test-attr.test \
tests/test-format.test \
tests/test-transition.test
tests/test-transition.test \
tests/test-scale-linear.test

%.min.js: %.js Makefile
@rm -f $@
Expand Down
52 changes: 52 additions & 0 deletions tests/test-scale-linear.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.linear();
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.linear().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.linear().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.linear().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.linear().range(["red", "blue"]).interpolate(d3.interpolateHsl);
console.log("domain([0, 1]).range([\"red\", \"blue\"]).interpolate(hsl):");
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("");
36 changes: 36 additions & 0 deletions tests/test-scale-linear.out
@@ -0,0 +1,36 @@
domain([0, 1]).range([0, 1]):
-0.5 -> −0.500
0.0 -> 0.000
0.5 -> 0.500
1.0 -> 1.000
1.5 -> 1.500

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

domain([01/01/1990, 01/01/1991]).range([0, 1]):
10/20/1989 -> −0.200
01/01/1990 -> 0.000
03/15/1990 -> 0.200
05/27/1990 -> 0.400
01/01/1991 -> 1.000
03/15/1991 -> 1.200

domain([0, 1]).range(["red", "blue"]):
-0.5 -> rgb(383,0,-127)
0.0 -> rgb(255,0,0)
0.5 -> rgb(128,0,128)
1.0 -> rgb(0,0,255)
1.5 -> rgb(-127,0,383)

domain([0, 1]).range(["red", "blue"]).interpolate(hsl):
-0.5 -> #0000ff
0.0 -> #ff0000
0.5 -> #00ff00
1.0 -> #0000ff
1.5 -> #ff0000

0 comments on commit 871cefc

Please sign in to comment.