Commit 58187894 authored by Mike Bostock's avatar Mike Bostock
Browse files

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.
parent b745b325
......@@ -7032,6 +7032,10 @@ d3 = function() {
scale.quantiles = function() {
return thresholds;
};
scale.invertExtent = function(y) {
y = range.indexOf(y);
return y < 0 ? [ NaN, NaN ] : [ y > 0 ? thresholds[y - 1] : domain[0], y < thresholds.length ? thresholds[y] : domain[domain.length - 1] ];
};
scale.copy = function() {
return d3_scale_quantile(domain, range);
};
......@@ -7061,14 +7065,14 @@ d3 = function() {
range = x;
return rescale();
};
scale.copy = function() {
return d3_scale_quantize(x0, x1, range);
};
scale.invertExtent = function(y) {
y = range.indexOf(y);
y = y < 0 ? NaN : y / kx + x0;
return [ y, y + 1 / kx ];
};
scale.copy = function() {
return d3_scale_quantize(x0, x1, range);
};
return rescale();
}
d3.scale.threshold = function() {
......
This source diff could not be displayed because it is too large. You can view the blob instead.
......@@ -38,6 +38,14 @@ function d3_scale_quantile(domain, range) {
return thresholds;
};
scale.invertExtent = function(y) {
y = range.indexOf(y);
return y < 0 ? [NaN, NaN] : [
y > 0 ? thresholds[y - 1] : domain[0],
y < thresholds.length ? thresholds[y] : domain[domain.length - 1]
];
};
scale.copy = function() {
return d3_scale_quantile(domain, range); // copy on write!
};
......
......@@ -30,15 +30,15 @@ function d3_scale_quantize(x0, x1, range) {
return rescale();
};
scale.copy = function() {
return d3_scale_quantize(x0, x1, range); // copy on write
};
scale.invertExtent = function(y) {
y = range.indexOf(y);
y = y < 0 ? NaN : y / kx + x0;
return [y, y + 1 / kx];
};
scale.copy = function() {
return d3_scale_quantize(x0, x1, range); // copy on write
};
return rescale();
}
......@@ -57,6 +57,33 @@ suite.addBatch({
"returns undefined if the input value is NaN": function(quantile) {
var x = quantile().domain([3, 6, 7, 8, 8, 10, 13, 15, 16, 20]).range([0, 1, 2, 3]);
assert.isUndefined(x(NaN));
},
"invertExtent": {
"maps a value in the range to a domain extent": function(quantile) {
var x = quantile().domain([3, 6, 7, 8, 8, 10, 13, 15, 16, 20]).range([0, 1, 2, 3]);
assert.deepEqual(x.invertExtent(0), [3, 7.25]);
assert.deepEqual(x.invertExtent(1), [7.25, 9]);
assert.deepEqual(x.invertExtent(2), [9, 14.5]);
assert.deepEqual(x.invertExtent(3), [14.5, 20]);
},
"allows arbitrary range values": function(quantile) {
var a = {}, b = {}, x = quantile().domain([3, 6, 7, 8, 8, 10, 13, 15, 16, 20]).range([a, b]);
assert.deepEqual(x.invertExtent(a), [3, 9]);
assert.deepEqual(x.invertExtent(b), [9, 20]);
},
"returns [NaN, NaN] when the given value is not in the range": function(quantile) {
var x = quantile().domain([3, 6, 7, 8, 8, 10, 13, 15, 16, 20]);
assert.ok(x.invertExtent(-1).every(isNaN));
assert.ok(x.invertExtent(.5).every(isNaN));
assert.ok(x.invertExtent(2).every(isNaN));
assert.ok(x.invertExtent('a').every(isNaN));
},
"returns the first match if duplicate values exist in the range": function(quantile) {
var x = quantile().domain([3, 6, 7, 8, 8, 10, 13, 15, 16, 20]).range([0, 1, 2, 0]);
assert.deepEqual(x.invertExtent(0), [3, 7.25]);
assert.deepEqual(x.invertExtent(1), [7.25, 9]);
assert.deepEqual(x.invertExtent(2), [9, 14.5]);
}
}
}
});
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment