8000 More hovertemplates! by etpinard · Pull Request #3530 · plotly/plotly.js · GitHub
[go: up one dir, main page]

Skip to content

More hovertemplates! #3530

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 7 commits into from
Feb 15, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
add hovertemplate to gl3d traces
- i.e. scatter3d, surface, mesh3d, cone, streamtube and isosurface!
  • Loading branch information
etpinard committed Feb 8, 2019
commit b49e13fb6c3b0d4678c849807a52250dd1cfdb2f
2 changes: 1 addition & 1 deletion src/components/fx/hovertemplate_attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ module.exports = function(opts, extra) {
role: 'info',
dflt: '',
arrayOk: true,
editType: 'none',
editType: opts.editType || 'none',
description: [
'Template string used for rendering the information that appear on hover box.',
'Note that this will override `hoverinfo`.',
Expand Down
100 changes: 58 additions & 42 deletions src/plots/gl3d/scene.js
8000
Original file line number Diff line number Diff line change
Expand Up @@ -71,18 +71,21 @@ function render(scene) {
var pdata = project(scene.glplot.cameraParams, selection.dataCoordinate);
trace = lastPicked.data;
var ptNumber = selection.index;
var hoverinfo = Fx.castHoverinfo(trace, scene.fullLayout, ptNumber);
var hoverinfoParts = hoverinfo.split('+');
var isHoverinfoAll = hoverinfo === 'all';

var xVal = formatter('xaxis', selection.traceCoordinate[0]);
var yVal = formatter('yaxis', selection.traceCoordinate[1]);
var zVal = formatter('zaxis', selection.traceCoordinate[2]);
var labels = {
xLabel: formatter('xaxis', selection.traceCoordinate[0]),
yLabel: formatter('yaxis', selection.traceCoordinate[1]),
zLabel: formatter('zaxis', selection.traceCoordinate[2])
};

var hoverinfo = Fx.castHoverinfo(trace, scene.fullLayout, ptNumber);
var hoverinfoParts = (hoverinfo || '').split('+');
var isHoverinfoAll = hoverinfo && hoverinfo === 'all';

if(!isHoverinfoAll) {
if(hoverinfoParts.indexOf('x') === -1) xVal = undefined;
if(hoverinfoParts.indexOf('y') === -1) yVal = undefined;
if(hoverinfoParts.indexOf('z') === -1) zVal = undefined;
if(!trace.hovertemplate && !isHoverinfoAll) {
if(hoverinfoParts.indexOf('x') === -1) labels.xLabel = undefined;
if(hoverinfoParts.indexOf('y') === -1) labels.yLabel = undefined;
if(hoverinfoParts.indexOf('z') === -1) labels.zLabel = undefined;
if(hoverinfoParts.indexOf('text') === -1) selection.textLabel = undefined;
if(hoverinfoParts.indexOf('name') === -1) lastPicked.name = undefined;
}
Expand All @@ -91,27 +94,38 @@ function render(scene) {
var vectorTx = [];

if(trace.type === 'cone' || trace.type === 'streamtube') {
labels.uLabel = formatter('xaxis', selection.traceCoordinate[3]);
if(isHoverinfoAll || hoverinfoParts.indexOf('u') !== -1) {
vectorTx.push('u: ' + formatter('xaxis', selection.traceCoordinate[3]));
vectorTx.push('u: ' + labels.uLabel);
}

labels.vLabel = formatter('yaxis', selection.traceCoordinate[4]);
if(isHoverinfoAll || hoverinfoParts.indexOf('v') !== -1) {
vectorTx.push('v: ' + formatter('yaxis', selection.traceCoordinate[4]));
vectorTx.push('v: ' + labels.vLabel);
}

labels.wLabel = formatter('zaxis', selection.traceCoordinate[5]);
if(isHoverinfoAll || hoverinfoParts.indexOf('w') !== -1) {
vectorTx.push('w: ' + formatter('zaxis', selection.traceCoordinate[5]));
vectorTx.push('w: ' + labels.wLabel);
}

labels.normLabel = selection.traceCoordinate[6].toPrecision(3);
if(isHoverinfoAll || hoverinfoParts.indexOf('norm') !== -1) {
vectorTx.push('norm: ' + selection.traceCoordinate[6].toPrecision(3));
vectorTx.push('norm: ' + labels.normLabel);
}
if(trace.type === 'streamtube' && (isHoverinfoAll || hoverinfoParts.indexOf('divergence') !== -1)) {
vectorTx.push('divergence: ' + selection.traceCoordinate[7].toPrecision(3));
if(trace.type === 'streamtube') {
labels.divergenceLabel = selection.traceCoordinate[7].toPrecision(3);
if(isHoverinfoAll || hoverinfoParts.indexOf('divergence') !== -1) {
vectorTx.push('divergence: ' + labels.divergenceLabel);
}
}
if(selection.textLabel) {
vectorTx.push(selection.textLabel);
}
tx = vectorTx.join('<br>');
} else if(trace.type === 'isosurface') {
vectorTx.push('value: ' + Axes.tickText(scene.mockAxis, scene.mockAxis.d2l(selection.traceCoordinate[3]), 'hover').text);
labels.valueLabel = Axes.tickText(scene.mockAxis, scene.mockAxis.d2l(selection.traceCoordinate[3]), 'hover').text;
vectorTx.push('value: ' + labels.valueLabel);
if(selection.textLabel) {
vectorTx.push(selection.textLabel);
}
Expand All @@ -120,27 +134,6 @@ function render(scene) {
tx = selection.textLabel;
}

if(scene.fullSceneLayout.hovermode) {
Fx.loneHover({
x: (0.5 + 0.5 * pdata[0] / pdata[3]) * width,
y: (0.5 - 0.5 * pdata[1] / pdata[3]) * height,
xLabel: xVal,
yLabel: yVal,
zLabel: zVal,
text: tx,
name: lastPicked.name,
color: Fx.castHoverOption(trace, ptNumber, 'bgcolor') || lastPicked.color,
borderColor: Fx.castHoverOption(trace, ptNumber, 'bordercolor'),
fontFamily: Fx.castHoverOption(trace, ptNumber, 'font.family'),
fontSize: Fx.castHoverOption(trace, ptNumber, 'font.size'),
fontColor: Fx.castHoverOption(trace, ptNumber, 'font.color')
}, {
container: svgContainer,
gd: scene.graphDiv
});
}

// TODO not sure if streamtube x/y/z should be emitted as x/y/z
var pointData = {
x: selection.traceCoordinate[0],
y: selection.traceCoordinate[1],
Expand All @@ -151,18 +144,41 @@ function render(scene) {
pointNumber: ptNumber
};

Fx.appendArrayPointValue(pointData, trace, ptNumber);

if(trace._module.eventData) {
pointData = trace._module.eventData(pointData, selection, trace, {}, ptNumber);
}

Fx.appendArrayPointValue(pointData, trace, ptNumber);

var eventData = {points: [pointData]};

if(scene.fullSceneLayout.hovermode) {
Fx.loneHover({
trace: trace,
x: (0.5 + 0.5 * pdata[0] / pdata[3]) * width,
y: (0.5 - 0.5 * pdata[1] / pdata[3]) * height,
xLabel: labels.xLabel,
yLabel: labels.yLabel,
zLabel: labels.zLabel,
text: tx,
name: lastPicked.name,
color: Fx.castHoverOption(trace, ptNumber, 'bgcolor') || lastPicked.color,
borderColor: Fx.castHoverOption(trace, ptNumber, 'bordercolor'),
fontFamily: Fx.castHoverOption(trace, ptNumber, 'font.family'),
fontSize: Fx.castHoverOption(trace, ptNumber, 'font.size'),
fontColor: Fx.castHoverOption(trace, ptNumber, 'font.color'),
hovertemplate: Lib.castOption(trace, ptNumber, 'hovertemplate'),
hovertemplateLabels: Lib.extendFlat({}, pointData, labels),
eventData: [pointData]
}, {
container: svgContainer,
gd: scene.graphDiv
});
}

if(selection.buttons && selection.distance < 5) {
scene.graphDiv.emit('plotly_click', eventData);
}
else {
} else {
scene.graphDiv.emit('plotly_hover', eventData);
}

Expand Down
4 changes: 3 additions & 1 deletion src/traces/cone/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

var colorscaleAttrs = require('../../components/colorscale/attributes');
var colorbarAttrs = require('../../components/colorbar/attributes');
var hovertemplateAttrs = require('../../components/fx/hovertemplate_attributes');
var mesh3dAttrs = require('../mesh3d/attributes');
var baseAttrs = require('../../plots/attributes');

Expand Down Expand Up @@ -157,7 +158,8 @@ var attrs = {
'If trace `hoverinfo` contains a *text* flag and *hovertext* is not set,',
'these elements will be seen in the hover labels.'
].join(' ')
}
},
hovertemplate: hovertemplateAttrs({editType: 'calc'}, {keys: ['norm']})
};

extendFlat(attrs, colorscaleAttrs('', {
Expand Down
1 change: 1 addition & 0 deletions src/traces/cone/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout
colorscaleDefaults(traceIn, traceOut, layout, coerce, {prefix: '', cLetter: 'c'});

coerce('text');
coerce('hovertemplate');

// disable 1D transforms (for now)
traceOut._length = null;
Expand Down
4 changes: 4 additions & 0 deletions src/traces/cone/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ module.exports = {
},
calc: require('./calc'),
plot: require('./convert'),
eventData: function(out, pt) {
out.norm = pt.traceCoordinate[6];
return out;
},

meta: {
description: [
Expand Down
2 changes: 2 additions & 0 deletions src/traces/isosurface/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

var colorscaleAttrs = require('../../components/colorscale/attributes');
var colorbarAttrs = require('../../components/colorbar/attributes');
var hovertemplateAttrs = require('../../components/fx/hovertemplate_attributes');
var surfaceAtts = require('../surface/attributes');
var meshAttrs = require('../mesh3d/attributes');
var baseAttrs = require('../../plots/attributes');
Expand Down Expand Up @@ -226,6 +227,7 @@ var attrs = module.exports = overrideAll( 2A0 extendFlat({
'these ele 10928 ments will be seen in the hover labels.'
].join(' ')
},
hovertemplate: hovertemplateAttrs()
},

colorscaleAttrs('', {
Expand Down
2 changes: 1 addition & 1 deletion src/traces/isosurface/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
* LICENSE file in the root directory of this source tree.
*/


'use strict';

var Registry = require('../../registry');
Expand Down Expand Up @@ -85,6 +84,7 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout
// Coerce remaining properties
[
'text',
'hovertemplate',
'lighting.ambient',
'lighting.diffuse',
'lighting.specular',
Expand Down
2 changes: 2 additions & 0 deletions src/traces/mesh3d/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

var colorscaleAttrs = require('../../components/colorscale/attributes');
var colorbarAttrs = require('../../components/colorbar/attributes');
var hovertemplateAttrs = require('../../components/fx/hovertemplate_attributes');
var surfaceAtts = require('../surface/attributes');
var baseAttrs = require('../../plots/attributes');

Expand Down Expand Up @@ -89,6 +90,7 @@ module.exports = extendFlat({
'these elements will be seen in the hover labels.'
].join(' ')
},
hovertemplate: hovertemplateAttrs({editType: 'calc'}),

delaunayaxis: {
valType: 'enumerated',
Expand Down
1 change: 1 addition & 0 deletions src/traces/mesh3d/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout
}

coerce('text');
coerce('hovertemplate');

// disable 1D transforms
// x/y/z should match lengths, and i/j/k should match as well, but
Expand Down
2 changes: 2 additions & 0 deletions src/traces/scatter3d/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

var scatterAttrs = require('../scatter/attributes');
var colorAttributes = require('../../components/colorscale/attributes');
var hovertemplateAttrs = require('../../components/fx/hovertemplate_attributes');
var baseAttrs = require('../../plots/attributes');
var DASHES = require('../../constants/gl3d_dashes');

Expand Down Expand Up @@ -94,6 +95,7 @@ var attrs = module.exports = overrideAll({
'To be seen, trace `hoverinfo` must contain a *text* flag.'
].join(' ')
}),
hovertemplate: hovertemplateAttrs(),

mode: extendFlat({}, scatterAttrs.mode, // shouldn't this be on-par with 2D?
{dflt: 'lines+markers'}),
Expand Down
1 change: 1 addition & 0 deletions src/traces/scatter3d/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout

coerce('text');
coerce('hovertext');
coerce('hovertemplate');
coerce('mode');

if(subTypes.hasLines(traceOut)) {
Expand Down
10 changes: 9 additions & 1 deletion src/traces/streamtube/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

var colorscaleAttrs = require('../../components/colorscale/attributes');
var colorbarAttrs = require('../../components/colorbar/attributes');
var hovertemplateAttrs = require('../../components/fx/hovertemplate_attributes');
var mesh3dAttrs = require('../mesh3d/attributes');
var baseAttrs = require('../../plots/attributes');

Expand Down Expand Up @@ -130,7 +131,14 @@ var attrs = {
'this text element will be seen in all hover labels.',
'Note that streamtube traces do not support array `text` values.'
].join(' ')
}
},
hovertemplate: hovertemplateAttrs({editType: 'calc'}, {
keys: [
'tubex', 'tubey', 'tubez',
'tubeu', 'tubev', 'tubew',
'norm', 'divergence'
]
})
};

extendFlat(attrs, colorscaleAttrs('', {
Expand Down
1 change: 1 addition & 0 deletions src/traces/streamtube/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout
colorscaleDefaults(traceIn, traceOut, layout, coerce, {prefix: '', cLetter: 'c'});

coerce('text');
coerce('hovertemplate');

// disable 1D transforms (for now)
// x/y/z and u/v/w have matching lengths,
Expand Down
2 changes: 2 additions & 0 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 hovertemplateAttrs = require('../../components/fx/hovertemplate_attributes');
var baseAttrs = require('../../plots/attributes');

var extendFlat = require('../../lib/extend').extendFlat;
Expand Down Expand Up @@ -123,6 +124,7 @@ var attrs = module.exports = overrideAll(extendFlat({
'these elements will be seen in the hover labels.'
].join(' ')
},
hovertemplate: hovertemplateAttrs(),

surfacecolor: {
valType: 'data_array',
Expand Down
1 change: 1 addition & 0 deletions src/traces/surface/defaults.js
7D49
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout
handleCalendarDefaults(traceIn, traceOut, ['x', 'y', 'z'], layout);

coerce('text');
coerce('hovertemplate');

// Coerce remaining properties
[
Expand Down
10 changes: 10 additions & 0 deletions test/jasmine/tests/cone_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,16 @@ describe('Test cone interactions', function() {
'norm: 3.00'
].join('\n')
});

return Plotly.restyle(gd, 'hovertemplate', 'NORM : %{norm}<br>at %{x},%{y},%{z}<extra>LOOKOUT</extra>');
})
.then(delay(20))
.then(_hover)
.then(function() {
assertHoverLabelContent({
name: 'LOOKOUT',
nums: 'NORM : 3.00\nat 2,2,2'
});
})
.catch(failTest)
.then(done);
Expand Down
16 changes: 16 additions & 0 deletions test/jasmine/tests/gl3d_plot_interact_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,11 @@ describe('Test gl3d plots', function() {
})
.then(function() {
assertHoverText(null, null, '100k');

return Plotly.restyle(gd, 'hovertemplate', 'THIS Y -- %{y}<extra></extra>');
})
.then(function() {
assertHoverText(null, null, null, 'THIS Y -- c');
})
.catch(failTest)
.then(done);
Expand Down Expand Up @@ -339,6 +344,11 @@ describe('Test gl3d plots', function() {
})
.then(function() {
assertHoverText(null, null, null, 'yo!');

return Plotly.restyle(gd, 'hovertemplate', '!!! %{z} !!!<extra></extra>');
})
.then(function() {
assertHoverText(null, null, null, '!!! 43 !!!');
})
.then(done);
});
Expand Down Expand Up @@ -418,6 +428,12 @@ describe('Test gl3d plots', function() {
.then(function() {
assertHoverText(null, null, null, 'yo!');
})
.then(function() {
return Plotly.restyle(gd, 'hovertemplate', '%{x}-%{y}-%{z}<extra></extra>');
})
.then(function() {
assertHoverText(null, null, null, '3-4-5');
})
.catch(failTest)
.then(done);
});
Expand Down
Loading
0