Skip to content

Commit bfc7e8a

Browse files
committed
fix(heatmap): add eventData handler for proper click event data
1 parent 0af79cc commit bfc7e8a

3 files changed

Lines changed: 61 additions & 0 deletions

File tree

src/traces/heatmap/event_data.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use strict';
2+
3+
module.exports = function eventData(out, pt) {
4+
if ('index' in pt) {
5+
out.pointNumber = pt.index;
6+
out.pointIndex = pt.index;
7+
}
8+
9+
if ('zLabelVal' in pt) {
10+
out.z = pt.zLabelVal;
11+
} else if ('z' in pt) {
12+
out.z = pt.z;
13+
}
14+
15+
return out;
16+
};

src/traces/heatmap/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ module.exports = {
88
colorbar: require('./colorbar'),
99
style: require('./style'),
1010
hoverPoints: require('./hover'),
11+
eventData: require('./event_data'),
1112

1213
moduleType: 'trace',
1314
name: 'heatmap',

test/jasmine/tests/heatmap_test.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1114,4 +1114,48 @@ describe('heatmap hover', function() {
11141114
expect(pt).toEqual(undefined);
11151115
});
11161116
});
1117+
1118+
describe('heatmap event data', function() {
1119+
var gd;
1120+
1121+
beforeEach(function() {
1122+
gd = createGraphDiv();
1123+
});
1124+
1125+
afterEach(destroyGraphDiv);
1126+
1127+
it('should include 2D pointNumber and z value in plotly_click payload', function(done) {
1128+
var mockData = [{
1129+
type: 'heatmap',
1130+
z: [[1, 2], [3, 4]],
1131+
x: ['A', 'B'],
1132+
y: ['Row1', 'Row2']
1133+
}];
1134+
1135+
Plotly.newPlot(gd, mockData).then(function() {
1136+
var clickData = null;
1137+
1138+
gd.on('plotly_click', function(data) {
1139+
clickData = data;
1140+
});
1141+
1142+
var mockClick = require('../assets/click');
1143+
var bBox = gd.getBoundingClientRect();
1144+
1145+
mockClick(bBox.left + 100, bBox.top + 300);
1146+
1147+
expect(clickData).not.toBeNull();
1148+
expect(clickData.points.length).toBe(1);
1149+
1150+
var pt = clickData.points[0];
1151+
1152+
expect(Array.isArray(pt.pointNumber)).toBe(true, 'pointNumber should be an array');
1153+
expect(pt.pointNumber).toEqual([0, 0], 'should point to the first row and col');
1154+
expect(pt.z).toBe(1, 'should extract the correct z value');
1155+
1156+
done();
1157+
}).catch(done.fail);
1158+
});
1159+
});
11171160
});
1161+

0 commit comments

Comments
 (0)