Skip to content

Commit

Permalink
More efficient degenerate bin check.
Browse files Browse the repository at this point in the history
  • Loading branch information
mbostock committed Mar 30, 2012
1 parent 8062823 commit 39a74f4
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 15 deletions.
14 changes: 8 additions & 6 deletions d3.v2.js
Original file line number Diff line number Diff line change
Expand Up @@ -5894,12 +5894,14 @@ d3.layout.histogram = function() {
}

// Fill the bins, ignoring values outside the range.
i = -1; while(++i < n) {
x = values[i];
if ((x >= range[0]) && (x <= range[1])) {
bin = bins[d3.bisect(thresholds, x, 1, m) - 1];
bin.y += k;
bin.push(data[i]);
if (m > 0) {
i = -1; while(++i < n) {
x = values[i];
if ((x >= range[0]) && (x <= range[1])) {
bin = bins[d3.bisect(thresholds, x, 1, m) - 1];
bin.y += k;
bin.push(data[i]);
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions d3.v2.min.js

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions src/layout/histogram.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ d3.layout.histogram = function() {
}

// Fill the bins, ignoring values outside the range.
i = -1; while(++i < n) {
x = values[i];
if ((x >= range[0]) && (x <= range[1])) {
bin = bins[d3.bisect(thresholds, x, 1, m) - 1];
if (bin) {
if (m > 0) {
i = -1; while(++i < n) {
x = values[i];
if ((x >= range[0]) && (x <= range[1])) {
bin = bins[d3.bisect(thresholds, x, 1, m) - 1];
bin.y += k;
bin.push(data[i]);
}
Expand Down
6 changes: 4 additions & 2 deletions test/layout/histogram-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,10 @@ suite.addBatch({
{x: 2, y: 2, dx: 1}
]);
},
"can handle degenerate domain": function(histogram) {
var h = histogram().bins(d3.scale.linear().domain([0,0]).ticks(3));
"returns the empty array with fewer than two bins": function(histogram) {
var h = histogram().bins([1]);
assert.deepEqual(h([0]), []);
var h = histogram().bins([]);
assert.deepEqual(h([0]), []);
}
}
Expand Down

0 comments on commit 39a74f4

Please sign in to comment.