Skip to content

Commit fd51e20

Browse files
committed
image: add (hover)text attributes with test
1 parent a98ac44 commit fd51e20

File tree

5 files changed

+43
-1
lines changed

5 files changed

+43
-1
lines changed

Diff for: src/traces/image/attributes.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,18 @@ module.exports = extendFlat({
9898
editType: 'calc',
9999
description: 'Set the pixel\'s vertical size'
100100
},
101+
text: {
102+
valType: 'data_array',
103+
editType: 'plot',
104+
description: 'Sets the text elements associated with each z value.'
105+
},
106+
hovertext: {
107+
valType: 'data_array',
108+
editType: 'plot',
109+
description: 'Same as `text`.'
110+
},
101111
hoverinfo: extendFlat({}, plotAttrs.hoverinfo, {
102-
flags: ['x', 'y', 'z', 'color', 'name']
112+
flags: ['x', 'y', 'z', 'color', 'name', 'text']
103113
}),
104114
hovertemplate: hovertemplateAttrs({}, {
105115
keys: ['z', 'color', 'colormodel']

Diff for: src/traces/image/defaults.js

+2
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,7 @@ module.exports = function supplyDefaults(traceIn, traceOut) {
3232
coerce('zmin', constants.colormodel[colormodel].min);
3333
coerce('zmax', constants.colormodel[colormodel].max);
3434

35+
coerce('text');
36+
coerce('hovertext');
3537
coerce('hovertemplate');
3638
};

Diff for: src/traces/image/event_data.js

+1
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,6 @@ module.exports = function eventData(out, pt) {
1515
if(pt.ya) out.yaxis = pt.ya;
1616
out.color = pt.color;
1717
out.colormodel = pt.trace.colormodel;
18+
out.text = pt.text;
1819
return out;
1920
};

Diff for: src/traces/image/hover.js

+8
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,13 @@ module.exports = function hoverPoints(pointData, xval, yval) {
5353
pointData.extraText = colormodel.toUpperCase() + ': ' + colorstring;
5454
}
5555

56+
var text;
57+
if(Array.isArray(trace.hovertext) && Array.isArray(trace.hovertext[ny])) {
58+
text = trace.hovertext[ny][nx];
59+
} else if(Array.isArray(trace.text) && Array.isArray(trace.text[ny])) {
60+
text = trace.text[ny][nx];
61+
}
62+
5663
var py = ya.c2p(cd0.y0 + (ny + 0.5) * trace.dy);
5764
var xVal = cd0.x0 + (nx + 0.5) * trace.dx;
5865
var yVal = cd0.y0 + (ny + 0.5) * trace.dy;
@@ -69,6 +76,7 @@ module.exports = function hoverPoints(pointData, xval, yval) {
6976
yVal: yVal,
7077
yLabelVal: yVal,
7178
zLabelVal: zLabel,
79+
text: text,
7280
hovertemplateLabels: {
7381
'zLabel': zLabel,
7482
'colorLabel': colorstring,

Diff for: test/jasmine/tests/image_test.js

+21
Original file line numberDiff line numberDiff line change
@@ -420,5 +420,26 @@ describe('image hover:', function() {
420420
.then(done);
421421
});
422422
});
423+
424+
it('should support hovertemplate variable text', function(done) {
425+
var mockCopy = {data: [{
426+
type: 'image',
427+
z: [[[1, 0, 0], [0, 1, 0], [0, 0, 1]], [[0, 0, 1], [1, 0, 0], [0, 1, 0]]],
428+
zmax: [1, 1, 1],
429+
text: [['A', 'B', 'C'], ['D', 'E', 'F']],
430+
hovertemplate: '%{text}<extra></extra>'
431+
}], layout: {width: 400, height: 400}};
432+
433+
Plotly.newPlot(gd, mockCopy)
434+
.then(function() {_hover(140, 200);})
435+
.then(function() {
436+
assertHoverLabelContent({
437+
nums: 'E',
438+
name: ''
439+
}, 'variable text should be available!');
440+
})
441+
.catch(failTest)
442+
.then(done);
443+
});
423444
});
424445
});

0 commit comments

Comments
 (0)