Skip to content

Add support for (hover) 'text' in mesh3d traces #2327

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 6, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions src/traces/mesh3d/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ var colorAttrs = require('../../components/colorscale/color_attributes');
var colorscaleAttrs = require('../../components/colorscale/attributes');
var colorbarAttrs = require('../../components/colorbar/attributes');
var surfaceAtts = require('../surface/attributes');
var baseAttrs = require('../../plots/attributes');

var extendFlat = require('../../lib/extend').extendFlat;


module.exports = extendFlat(colorAttrs('', 'calc', false), {
x: {
valType: 'data_array',
Expand Down Expand Up @@ -78,6 +78,19 @@ module.exports = extendFlat(colorAttrs('', 'calc', false), {

},

text: {
valType: 'string',
role: 'info',
dflt: '',
arrayOk: true,
editType: 'calc',
description: [
'Sets the text elements associated with the vertices.',
'If trace `hoverinfo` contains a *text* flag and *hovertext* is not set,',
'these elements will be seen in the hover labels.'
].join(' ')
},

delaunayaxis: {
valType: 'enumerated',
role: 'info',
Expand Down Expand Up @@ -209,5 +222,7 @@ module.exports = extendFlat(colorAttrs('', 'calc', false), {
description: 'Epsilon for face normals calculation avoids math issues arising from degenerate geometry.'
},
editType: 'calc'
}, surfaceAtts.lighting)
}, surfaceAtts.lighting),

hoverinfo: extendFlat({}, baseAttrs.hoverinfo, {editType: 'calc'})
});
7 changes: 7 additions & 0 deletions src/traces/mesh3d/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ proto.handlePick = function(selection) {
this.data.z[selectIndex]
];

var text = this.data.text;
if(Array.isArray(text) && text[selectIndex] !== undefined) {
selection.textLabel = text[selectIndex];
} else if(text) {
selection.textLabel = text;
}

return true;
}
};
Expand Down
2 changes: 2 additions & 0 deletions src/traces/mesh3d/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,6 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout
else if('vertexcolor' in traceIn) coerce('vertexcolor');
else coerce('color', defaultColor);
}

coerce('text');
};
3 changes: 3 additions & 0 deletions src/traces/scatter3d/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
var scatterAttrs = require('../scatter/attributes');
var colorAttributes = require('../../components/colorscale/color_attributes');
var errorBarAttrs = require('../../components/errorbars/attributes');
var baseAttrs = require('../../plots/attributes');
var DASHES = require('../../constants/gl3d_dashes');

var MARKER_SYMBOLS = require('../../constants/gl3d_markers');
Expand Down Expand Up @@ -171,6 +172,8 @@ var attrs = module.exports = overrideAll({
error_x: errorBarAttrs,
error_y: errorBarAttrs,
error_z: errorBarAttrs,

hoverinfo: extendFlat({}, baseAttrs.hoverinfo, {editType: 'calc'})
}, 'calc', 'nested');

attrs.x.editType = attrs.y.editType = attrs.z.editType = 'calc+clearAxisTypes';
18 changes: 15 additions & 3 deletions src/traces/surface/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
var Color = require('../../components/color');
var colorscaleAttrs = require('../../components/colorscale/attributes');
var colorbarAttrs = require('../../components/colorbar/attributes');
var baseAttrs = require('../../plots/attributes');

var extendFlat = require('../../lib/extend').extendFlat;
var overrideAll = require('../../plot_api/edit_types').overrideAll;
Expand Down Expand Up @@ -112,9 +113,18 @@ var attrs = module.exports = overrideAll({
},

text: {
valType: 'data_array',
description: 'Sets the text elements associated with each z value.'
valType: 'string',
role: 'info',
dflt: '',
arrayOk: true,
editType: 'calc',
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh this is in an overrideAll so you don't need editType - which I think applies to a few more of the additions in this PR too.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(I see you 🔪 some editType lines, just not the one I made the comment directly on 😅 )

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops. Fixed in -> 13a3ecf

description: [
'Sets the text elements associated with each z value.',
'If trace `hoverinfo` contains a *text* flag and *hovertext* is not set,',
'these elements will be seen in the hover labels.'
].join(' ')
},

surfacecolor: {
valType: 'data_array',
description: [
Expand Down Expand Up @@ -242,7 +252,9 @@ var attrs = module.exports = overrideAll({
zmax: extendFlat({}, colorscaleAttrs.zmax, {
description: 'Obsolete. Use `cmax` instead.'
})
}
},

hoverinfo: extendFlat({}, baseAttrs.hoverinfo, {editType: 'calc'})
}, 'calc', 'nested');

attrs.x.editType = attrs.y.editType = attrs.z.editType = 'calc+clearAxisTypes';
7 changes: 5 additions & 2 deletions src/traces/surface/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,13 @@ proto.handlePick = function(selection) {
];

var text = this.data.text;
if(text && text[selectIndex[1]] && text[selectIndex[1]][selectIndex[0]] !== undefined) {
if(Array.isArray(text) && text[selectIndex[1]] && text[selectIndex[1]][selectIndex[0]] !== undefined) {
selection.textLabel = text[selectIndex[1]][selectIndex[0]];
} else if(text) {
selection.textLabel = text;
} else {
selection.textLabel = '';
}
else selection.textLabel = '';

selection.data.dataCoordinate = selection.dataCoordinate.slice();

Expand Down
86 changes: 85 additions & 1 deletion test/jasmine/tests/gl3d_plot_interact_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ describe('Test gl3d plots', function() {
var mock3 = require('@mocks/gl3d_autocolorscale');

function assertHoverText(xLabel, yLabel, zLabel, textLabel) {
var content = [xLabel, yLabel, zLabel];
var content = [];
if(xLabel) content.push(xLabel);
if(yLabel) content.push(yLabel);
if(zLabel) content.push(zLabel);
if(textLabel) content.push(textLabel);
assertHoverLabelContent({nums: content.join('\n')});
}
Expand Down Expand Up @@ -193,6 +196,16 @@ describe('Test gl3d plots', function() {
.then(_hover)
.then(function() {
assertHoverText('x: 二 6, 2017', 'y: c', 'z: 100k', 'Clementine');

return Plotly.restyle(gd, 'hoverinfo', 'text');
})
.then(function() {
assertHoverText(null, null, null, 'Clementine');

return Plotly.restyle(gd, 'hoverinfo', 'z');
})
.then(function() {
assertHoverText(null, null, '100k');
})
.catch(fail)
.then(done);
Expand Down Expand Up @@ -273,6 +286,23 @@ describe('Test gl3d plots', function() {
'colorbar.tickvals': undefined,
'colorbar.ticktext': undefined
});

return Plotly.restyle(gd, 'hoverinfo', 'z');
})
.then(_hover)
.then(function() {
assertHoverText(null, null, '43');

return Plotly.restyle(gd, 'hoverinfo', 'text');
})
.then(_hover)
.then(function() {
assertHoverText(null, null, null, 'one two');

return Plotly.restyle(gd, 'text', 'yo!');
})
.then(function() {
assertHoverText(null, null, null, 'yo!');
})
.then(done);
});
Expand Down Expand Up @@ -302,6 +332,60 @@ describe('Test gl3d plots', function() {
.then(done);
});

it('should display correct hover labels (mesh3d case)', function(done) {
var x = [1, 1, 2, 3, 4, 2];
var y = [2, 1, 3, 4, 5, 3];
var z = [3, 7, 4, 5, 3.5, 2];
var text = x.map(function(_, i) {
return [
'ts: ' + x[i],
'hz: ' + y[i],
'ftt:' + z[i]
].join('<br>');
});

function _hover() {
mouseEvent('mouseover', 250, 250);
return delay(20)();
}

Plotly.newPlot(gd, [{
type: 'mesh3d',
x: x,
y: y,
z: z,
text: text
}], {
width: 500,
height: 500
})
.then(delay(20))
.then(_hover)
.then(function() {
assertHoverText('x: 3', 'y: 4', 'z: 5', 'ts: 3\nhz: 4\nftt:5');
})
.then(function() {
return Plotly.restyle(gd, 'hoverinfo', 'x+y');
})
.then(function() {
assertHoverText('(3, 4)');
})
.then(function() {
return Plotly.restyle(gd, 'hoverinfo', 'text');
})
.then(function() {
assertHoverText('ts: 3\nhz: 4\nftt:5');
})
.then(function() {
return Plotly.restyle(gd, 'text', 'yo!');
})
.then(function() {
assertHoverText(null, null, null, 'yo!');
})
.catch(fail)
.then(done);
});

it('should be able to reversibly change trace type', function(done) {
var _mock = Lib.extendDeep({}, mock2);
var sceneLayout = { aspectratio: { x: 1, y: 1, z: 1 } };
Expand Down