Skip to content

Commit 5818789

Browse files
committed
Add missing quantile.invertExtent.
Like the quantile and threshold scales, the quantile scale represents a lossy mapping where a span of values in the domain maps to a discrete range value.
1 parent b745b32 commit 5818789

5 files changed

Lines changed: 51 additions & 12 deletions

File tree

d3.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7032,6 +7032,10 @@ d3 = function() {
70327032
scale.quantiles = function() {
70337033
return thresholds;
70347034
};
7035+
scale.invertExtent = function(y) {
7036+
y = range.indexOf(y);
7037+
return y < 0 ? [ NaN, NaN ] : [ y > 0 ? thresholds[y - 1] : domain[0], y < thresholds.length ? thresholds[y] : domain[domain.length - 1] ];
7038+
};
70357039
scale.copy = function() {
70367040
return d3_scale_quantile(domain, range);
70377041
};
@@ -7061,14 +7065,14 @@ d3 = function() {
70617065
range = x;
70627066
return rescale();
70637067
};
7064-
scale.copy = function() {
7065-
return d3_scale_quantize(x0, x1, range);
7066-
};
70677068
scale.invertExtent = function(y) {
70687069
y = range.indexOf(y);
70697070
y = y < 0 ? NaN : y / kx + x0;
70707071
return [ y, y + 1 / kx ];
70717072
};
7073+
scale.copy = function() {
7074+
return d3_scale_quantize(x0, x1, range);
7075+
};
70727076
return rescale();
70737077
}
70747078
d3.scale.threshold = function() {

d3.min.js

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

src/scale/quantile.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@ function d3_scale_quantile(domain, range) {
3838
return thresholds;
3939
};
4040

41+
scale.invertExtent = function(y) {
42+
y = range.indexOf(y);
43+
return y < 0 ? [NaN, NaN] : [
44+
y > 0 ? thresholds[y - 1] : domain[0],
45+
y < thresholds.length ? thresholds[y] : domain[domain.length - 1]
46+
];
47+
};
48+
4149
scale.copy = function() {
4250
return d3_scale_quantile(domain, range); // copy on write!
4351
};

src/scale/quantize.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,15 @@ function d3_scale_quantize(x0, x1, range) {
3030
return rescale();
3131
};
3232

33-
scale.copy = function() {
34-
return d3_scale_quantize(x0, x1, range); // copy on write
35-
};
36-
3733
scale.invertExtent = function(y) {
3834
y = range.indexOf(y);
3935
y = y < 0 ? NaN : y / kx + x0;
4036
return [y, y + 1 / kx];
4137
};
4238

39+
scale.copy = function() {
40+
return d3_scale_quantize(x0, x1, range); // copy on write
41+
};
42+
4343
return rescale();
4444
}

test/scale/quantile-test.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,33 @@ suite.addBatch({
5757
"returns undefined if the input value is NaN": function(quantile) {
5858
var x = quantile().domain([3, 6, 7, 8, 8, 10, 13, 15, 16, 20]).range([0, 1, 2, 3]);
5959
assert.isUndefined(x(NaN));
60+
},
61+
"invertExtent": {
62+
"maps a value in the range to a domain extent": function(quantile) {
63+
var x = quantile().domain([3, 6, 7, 8, 8, 10, 13, 15, 16, 20]).range([0, 1, 2, 3]);
64+
assert.deepEqual(x.invertExtent(0), [3, 7.25]);
65+
assert.deepEqual(x.invertExtent(1), [7.25, 9]);
66+
assert.deepEqual(x.invertExtent(2), [9, 14.5]);
67+
assert.deepEqual(x.invertExtent(3), [14.5, 20]);
68+
},
69+
"allows arbitrary range values": function(quantile) {
70+
var a = {}, b = {}, x = quantile().domain([3, 6, 7, 8, 8, 10, 13, 15, 16, 20]).range([a, b]);
71+
assert.deepEqual(x.invertExtent(a), [3, 9]);
72+
assert.deepEqual(x.invertExtent(b), [9, 20]);
73+
},
74+
"returns [NaN, NaN] when the given value is not in the range": function(quantile) {
75+
var x = quantile().domain([3, 6, 7, 8, 8, 10, 13, 15, 16, 20]);
76+
assert.ok(x.invertExtent(-1).every(isNaN));
77+
assert.ok(x.invertExtent(.5).every(isNaN));
78+
assert.ok(x.invertExtent(2).every(isNaN));
79+
assert.ok(x.invertExtent('a').every(isNaN));
80+
},
81+
"returns the first match if duplicate values exist in the range": function(quantile) {
82+
var x = quantile().domain([3, 6, 7, 8, 8, 10, 13, 15, 16, 20]).range([0, 1, 2, 0]);
83+
assert.deepEqual(x.invertExtent(0), [3, 7.25]);
84+
assert.deepEqual(x.invertExtent(1), [7.25, 9]);
85+
assert.deepEqual(x.invertExtent(2), [9, 14.5]);
86+
}
6087
}
6188
}
6289
});

0 commit comments

Comments
 (0)