Skip to content

Commit f23cb31

Browse files
committed
Add test for scale.quantize.
1 parent 3db7fd1 commit f23cb31

4 files changed

Lines changed: 74 additions & 5 deletions

File tree

d3.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2650,8 +2650,8 @@ d3.scale.quantize = function() {
26502650

26512651
scale.domain = function(x) {
26522652
if (!arguments.length) return [x0, x1];
2653-
x0 = x[0];
2654-
x1 = x[1];
2653+
x0 = +x[0];
2654+
x1 = +x[x.length - 1];
26552655
kx = range.length / (x1 - x0);
26562656
return scale;
26572657
};

d3.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/scale/quantize.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ d3.scale.quantize = function() {
1111

1212
scale.domain = function(x) {
1313
if (!arguments.length) return [x0, x1];
14-
x0 = x[0];
15-
x1 = x[1];
14+
x0 = +x[0];
15+
x1 = +x[x.length - 1];
1616
kx = range.length / (x1 - x0);
1717
return scale;
1818
};

test/scale/quantize-test.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
require("../env");
2+
require("../../d3");
3+
4+
var vows = require("vows"),
5+
assert = require("assert");
6+
7+
var suite = vows.describe("d3.scale.quantize");
8+
9+
suite.addBatch({
10+
"quantize": {
11+
topic: function() {
12+
return d3.scale.quantize;
13+
},
14+
"has the default domain [0, 1]": function(quantize) {
15+
var x = quantize();
16+
assert.deepEqual(x.domain(), [0, 1]);
17+
assert.equal(x(.25), 0);
18+
},
19+
"has the default range [0, 1]": function(quantize) {
20+
var x = quantize();
21+
assert.deepEqual(x.range(), [0, 1]);
22+
assert.equal(x(.75), 1);
23+
},
24+
"maps a number to a discrete value in the range": function(quantize) {
25+
var x = quantize().range([0, 1, 2]);
26+
assert.equal(x(0), 0);
27+
assert.equal(x(.2), 0);
28+
assert.equal(x(.4), 1);
29+
assert.equal(x(.6), 1);
30+
assert.equal(x(.8), 2);
31+
assert.equal(x(1), 2);
32+
},
33+
"coerces domain to numbers": function(quantize) {
34+
var x = quantize().domain(["0", "100"]);
35+
assert.strictEqual(x.domain()[0], 0);
36+
assert.strictEqual(x.domain()[1], 100);
37+
},
38+
"only considers the extent of the domain": function(quantize) {
39+
var x = quantize().domain([-1, 0, 100]);
40+
assert.deepEqual(x.domain(), [-1, 100]);
41+
},
42+
"clamps input values to the domain": function(quantize) {
43+
var a = {}, b = {}, c = {}, x = quantize().range([a, b, c]);
44+
assert.equal(x(-.5), a);
45+
assert.equal(x(1.5), c);
46+
},
47+
"range cardinality determines the degree of quantization": function(quantize) {
48+
var x = quantize();
49+
assert.inDelta(x.range(d3.range(0, 1.001, .001))(1/3), .333, 1e-6);
50+
assert.inDelta(x.range(d3.range(0, 1.01, .01))(1/3), .33, 1e-6);
51+
assert.inDelta(x.range(d3.range(0, 1.1, .1))(1/3), .3, 1e-6);
52+
assert.inDelta(x.range(d3.range(0, 1.2, .2))(1/3), .4, 1e-6);
53+
assert.inDelta(x.range(d3.range(0, 1.25, .25))(1/3), .25, 1e-6);
54+
assert.inDelta(x.range(d3.range(0, 1.5, .5))(1/3), .5, 1e-6);
55+
assert.inDelta(x.range(d3.range(1))(1/3), 0, 1e-6);
56+
},
57+
"range values are arbitrary": function(quantize) {
58+
var a = {}, b = {}, c = {}, x = quantize().range([a, b, c]);
59+
assert.equal(x(0), a);
60+
assert.equal(x(.2), a);
61+
assert.equal(x(.4), b);
62+
assert.equal(x(.6), b);
63+
assert.equal(x(.8), c);
64+
assert.equal(x(1), c);
65+
}
66+
}
67+
});
68+
69+
suite.export(module);

0 commit comments

Comments
 (0)