Skip to content

Commit c227d15

Browse files
authored
Merge pull request #1417 from plotly/heatmap-xyz-text-in-hover-fix
Fix for xyz column heatmap trace 'text' on hover
2 parents 674f00f + 04bf731 commit c227d15

File tree

3 files changed

+63
-23
lines changed

3 files changed

+63
-23
lines changed

src/traces/heatmap/calc.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ module.exports = function calc(gd, trace) {
121121
Axes.expand(ya, yArray);
122122
}
123123

124-
var cd0 = {x: xArray, y: yArray, z: z};
124+
var cd0 = {x: xArray, y: yArray, z: z, text: trace.text};
125125

126126
// auto-z and autocolorscale if applicable
127127
colorscaleCalc(trace, z, '', 'z');

src/traces/heatmap/hover.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,8 @@ module.exports = function hoverPoints(pointData, xval, yval, hovermode, contour)
9696
if(zmask && !zmask[ny][nx]) zVal = undefined;
9797

9898
var text;
99-
if(Array.isArray(trace.text) && Array.isArray(trace.text[ny])) {
100-
text = trace.text[ny][nx];
99+
if(Array.isArray(cd0.text) && Array.isArray(cd0.text[ny])) {
100+
text = cd0.text[ny][nx];
101101
}
102102

103103
return [Lib.extendFlat(pointData, {

test/jasmine/tests/heatmap_test.js

Lines changed: 60 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -528,19 +528,10 @@ describe('heatmap hover', function() {
528528

529529
var gd;
530530

531-
beforeAll(function(done) {
531+
beforeAll(function() {
532532
jasmine.addMatchers(customMatchers);
533-
534-
gd = createGraphDiv();
535-
536-
var mock = require('@mocks/heatmap_multi-trace.json'),
537-
mockCopy = Lib.extendDeep({}, mock);
538-
539-
Plotly.plot(gd, mockCopy.data, mockCopy.layout).then(done);
540533
});
541534

542-
afterAll(destroyGraphDiv);
543-
544535
function _hover(gd, xval, yval) {
545536
var fullLayout = gd._fullLayout,
546537
calcData = gd.calcdata,
@@ -563,24 +554,73 @@ describe('heatmap hover', function() {
563554
return hoverData;
564555
}
565556

566-
function assertLabels(hoverPoint, xLabel, yLabel, zLabel) {
557+
function assertLabels(hoverPoint, xLabel, yLabel, zLabel, text) {
567558
expect(hoverPoint.xLabelVal).toEqual(xLabel, 'have correct x label');
568559
expect(hoverPoint.yLabelVal).toEqual(yLabel, 'have correct y label');
569560
expect(hoverPoint.zLabelVal).toEqual(zLabel, 'have correct z label');
561+
expect(hoverPoint.text).toEqual(text, 'have correct text label');
570562
}
571563

572-
it('should find closest point (case 1) and should', function() {
573-
var pt = _hover(gd, 0.5, 0.5)[0];
564+
describe('for `heatmap_multi-trace`', function() {
574565

575-
expect(pt.index).toEqual([1, 0], 'have correct index');
576-
assertLabels(pt, 1, 1, 4);
577-
});
566+
beforeAll(function(done) {
567+
gd = createGraphDiv();
568+
569+
var mock = require('@mocks/heatmap_multi-trace.json'),
570+
mockCopy = Lib.extendDeep({}, mock);
571+
572+
Plotly.plot(gd, mockCopy.data, mockCopy.layout).then(done);
573+
});
574+
575+
afterAll(destroyGraphDiv);
576+
577+
it('should find closest point (case 1) and should', function() {
578+
var pt = _hover(gd, 0.5, 0.5)[0];
579+
580+
expect(pt.index).toEqual([1, 0], 'have correct index');
581+
assertLabels(pt, 1, 1, 4);
582+
});
578583

579-
it('should find closest point (case 2) and should', function() {
580-
var pt = _hover(gd, 1.5, 0.5)[0];
584+
it('should find closest point (case 2) and should', function() {
585+
var pt = _hover(gd, 1.5, 0.5)[0];
581586

582-
expect(pt.index).toEqual([0, 0], 'have correct index');
583-
assertLabels(pt, 2, 0.2, 6);
587+
expect(pt.index).toEqual([0, 0], 'have correct index');
588+
assertLabels(pt, 2, 0.2, 6);
589+
});
584590
});
585591

592+
describe('for xyz-column traces', function() {
593+
594+
beforeAll(function(done) {
595+
gd = createGraphDiv();
596+
597+
Plotly.plot(gd, [{
598+
type: 'heatmap',
599+
x: [1, 2, 3],
600+
y: [1, 1, 1],
601+
z: [10, 4, 20],
602+
text: ['a', 'b', 'c'],
603+
hoverinfo: 'text'
604+
}])
605+
.then(done);
606+
});
607+
608+
afterAll(destroyGraphDiv);
609+
610+
it('should find closest point and should', function(done) {
611+
var pt = _hover(gd, 0.5, 0.5)[0];
612+
613+
expect(pt.index).toEqual([0, 0], 'have correct index');
614+
assertLabels(pt, 1, 1, 10, 'a');
615+
616+
Plotly.relayout(gd, 'xaxis.range', [1, 2]).then(function() {
617+
var pt2 = _hover(gd, 1.5, 0.5)[0];
618+
619+
expect(pt2.index).toEqual([0, 1], 'have correct index');
620+
assertLabels(pt2, 2, 1, 4, 'b');
621+
})
622+
.then(done);
623+
});
624+
625+
});
586626
});

0 commit comments

Comments
 (0)