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
Changes from all 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
@@ -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',
@@ -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',
@@ -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
@@ -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;
}
};
2 changes: 2 additions & 0 deletions src/traces/mesh3d/defaults.js
Original file line number Diff line number Diff line change
@@ -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
@@ -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');
@@ -171,6 +172,8 @@ var attrs = module.exports = overrideAll({
error_x: errorBarAttrs,
error_y: errorBarAttrs,
error_z: errorBarAttrs,

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

attrs.x.editType = attrs.y.editType = attrs.z.editType = 'calc+clearAxisTypes';
17 changes: 14 additions & 3 deletions src/traces/surface/attributes.js
Original file line number Diff line number Diff line change
@@ -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;
@@ -112,9 +113,17 @@ 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,
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: [
@@ -242,7 +251,9 @@ var attrs = module.exports = overrideAll({
zmax: extendFlat({}, colorscaleAttrs.zmax, {
description: 'Obsolete. Use `cmax` instead.'
})
}
},

hoverinfo: extendFlat({}, baseAttrs.hoverinfo)
}, '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
@@ -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();

86 changes: 85 additions & 1 deletion test/jasmine/tests/gl3d_plot_interact_test.js
Original file line number Diff line number Diff line change
@@ -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')});
}
@@ -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);
@@ -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);
});
@@ -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 } };