Skip to content

Commit c38673f

Browse files
committed
fix empty bin histogram2d hover labels content
- this removed the annoying `x: NaN` from histogram2d hover labels
1 parent deefe87 commit c38673f

File tree

2 files changed

+55
-5
lines changed

2 files changed

+55
-5
lines changed

src/traces/histogram2d/calc.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -205,11 +205,13 @@ function getRanges(edges, uniqueVals, gapLow, gapHigh, ax, calendar) {
205205
var i;
206206
var len = edges.length - 1;
207207
var out = new Array(len);
208-
if(uniqueVals) {
209-
for(i = 0; i < len; i++) out[i] = [uniqueVals[i], uniqueVals[i]];
210-
} else {
211-
var roundFn = getBinSpanLabelRound(gapLow, gapHigh, edges, ax, calendar);
212-
for(i = 0; i < len; i++) out[i] = [roundFn(edges[i]), roundFn(edges[i + 1], true)];
208+
var roundFn = getBinSpanLabelRound(gapLow, gapHigh, edges, ax, calendar);
209+
210+
for(i = 0; i < len; i++) {
211+
var v = (uniqueVals || [])[i];
212+
out[i] = v === undefined ?
213+
[roundFn(edges[i]), roundFn(edges[i + 1], true)] :
214+
[v, v];
213215
}
214216
return out;
215217
}

test/jasmine/tests/histogram2d_test.js

+48
Original file line numberDiff line numberDiff line change
@@ -479,5 +479,53 @@ describe('Test histogram2d hover:', function() {
479479
.catch(failTest)
480480
.then(done);
481481
});
482+
483+
it('shows the data range when bins have no value in it', function(done) {
484+
function _check(msg, xpx, ypx, lines) {
485+
_hover(xpx, ypx);
486+
assertHoverLabelContent({nums: lines.join('\n')}, msg);
487+
}
488+
489+
Plotly.newPlot('graph', [{
490+
type: 'histogram2d',
491+
x: [18.78],
492+
y: [3],
493+
xbins: {
494+
start: 0,
495+
end: 55,
496+
size: 5
497+
},
498+
ybins: {
499+
start: 0,
500+
end: 11,
501+
size: 1
502+
}
503+
}], {
504+
width: 400,
505+
height: 400,
506+
margin: {l: 0, t: 0, r: 0, b: 0}
507+
})
508+
.then(function() {
509+
expect(gd.calcdata[0][0].xRanges).toBeCloseTo2DArray([
510+
[0, 4], [5, 9], [10, 14],
511+
[18.78, 18.78],
512+
[20, 24], [25, 29], [30, 34],
513+
[35, 39], [40, 44], [45, 49], [50, 54]
514+
], 2, 'x-bins with some spread');
515+
expect(gd.calcdata[0][0].yRanges).toBeCloseTo2DArray([
516+
[0, 0], [1, 1], [2, 2], [3, 3], [4, 4], [5, 5],
517+
[6, 6], [7, 7], [8, 8], [9, 9], [10, 10]
518+
], 2, 'y-bins with single values');
519+
520+
_check('on pt (!)', 100, 275, ['x: 18.78', 'y: 3', 'z: 1']);
521+
_check('on x of pt, above it', 100, 200, ['x: 18.78', 'y: 5', 'z: 0']);
522+
_check('off left/top of pt', 50, 100, ['x: 5 - 9', 'y: 8', 'z: 0']);
523+
_check('off right/top of pt', 300, 100, ['x: 50 - 54', 'y: 8', 'z: 0']);
524+
_check('off left/bottom of pt', 50, 325, ['x: 5 - 9', 'y: 2', 'z: 0']);
525+
_check('off right/bottom of pt', 300, 325, ['x: 50 - 54', 'y: 2', 'z: 0']);
526+
})
527+
.catch(failTest)
528+
.then(done);
529+
});
482530
});
483531
});

0 commit comments

Comments
 (0)