Skip to content

Commit e42eb90

Browse files
committed
Add d3.scale.threshold. Fixes d3#755.
1 parent 3d4d679 commit e42eb90

5 files changed

Lines changed: 107 additions & 4 deletions

File tree

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ d3.scale.js: \
138138
src/scale/category.js \
139139
src/scale/quantile.js \
140140
src/scale/quantize.js \
141+
src/scale/threshold.js \
141142
src/scale/identity.js
142143

143144
d3.svg.js: \

d3.v2.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2496,6 +2496,28 @@
24962496
};
24972497
return rescale();
24982498
}
2499+
d3.scale.threshold = function() {
2500+
return d3_scale_threshold([ .5 ], [ 0, 1 ]);
2501+
};
2502+
function d3_scale_threshold(domain, range) {
2503+
function scale(x) {
2504+
return range[d3.bisect(domain, x)];
2505+
}
2506+
scale.domain = function(_) {
2507+
if (!arguments.length) return domain;
2508+
domain = _;
2509+
return scale;
2510+
};
2511+
scale.range = function(_) {
2512+
if (!arguments.length) return range;
2513+
range = _;
2514+
return scale;
2515+
};
2516+
scale.copy = function() {
2517+
return d3_scale_threshold(domain, range);
2518+
};
2519+
return scale;
2520+
}
24992521
d3.scale.identity = function() {
25002522
return d3_scale_identity([ 0, 1 ]);
25012523
};

d3.v2.min.js

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/scale/threshold.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
d3.scale.threshold = function() {
2+
return d3_scale_threshold([.5], [0, 1]);
3+
};
4+
5+
function d3_scale_threshold(domain, range) {
6+
7+
function scale(x) {
8+
return range[d3.bisect(domain, x)];
9+
}
10+
11+
scale.domain = function(_) {
12+
if (!arguments.length) return domain;
13+
domain = _;
14+
return scale;
15+
};
16+
17+
scale.range = function(_) {
18+
if (!arguments.length) return range;
19+
range = _;
20+
return scale;
21+
};
22+
23+
scale.copy = function() {
24+
return d3_scale_threshold(domain, range);
25+
};
26+
27+
return scale;
28+
};

test/scale/threshold-test.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
require("../env");
2+
3+
var vows = require("vows"),
4+
assert = require("assert");
5+
6+
var suite = vows.describe("d3.scale.threshold");
7+
8+
suite.addBatch({
9+
"threshold": {
10+
topic: function() {
11+
return d3.scale.threshold;
12+
},
13+
"has the default domain [.5]": function(threshold) {
14+
var x = threshold();
15+
assert.deepEqual(x.domain(), [.5]);
16+
assert.equal(x(.49), 0);
17+
},
18+
"has the default range [0, 1]": function(threshold) {
19+
var x = threshold();
20+
assert.deepEqual(x.range(), [0, 1]);
21+
assert.equal(x(.50), 1);
22+
},
23+
"maps a number to a discrete value in the range": function(threshold) {
24+
var x = threshold().domain([1/3, 2/3]).range(["a", "b", "c"]);
25+
assert.equal(x(0), "a");
26+
assert.equal(x(.2), "a");
27+
assert.equal(x(.4), "b");
28+
assert.equal(x(.6), "b");
29+
assert.equal(x(.8), "c");
30+
assert.equal(x(1), "c");
31+
},
32+
"domain values are arbitrary": function(threshold) {
33+
var x = threshold().domain(["10", "2"]).range([0, 1, 2]);
34+
assert.strictEqual(x.domain()[0], "10");
35+
assert.strictEqual(x.domain()[1], "2");
36+
assert.equal(x("0"), 0);
37+
assert.equal(x("12"), 1);
38+
assert.equal(x("3"), 2);
39+
},
40+
"range values are arbitrary": function(threshold) {
41+
var a = {}, b = {}, c = {}, x = threshold().domain([1/3, 2/3]).range([a, b, c]);
42+
assert.equal(x(0), a);
43+
assert.equal(x(.2), a);
44+
assert.equal(x(.4), b);
45+
assert.equal(x(.6), b);
46+
assert.equal(x(.8), c);
47+
assert.equal(x(1), c);
48+
}
49+
}
50+
});
51+
52+
suite.export(module);

0 commit comments

Comments
 (0)