Skip to content

ArrayOk hoverinfo fixups #2055

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 13 commits into from
Oct 5, 2017
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
8 changes: 2 additions & 6 deletions src/traces/bar/hover.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
var Fx = require('../../components/fx');
var ErrorBars = require('../../components/errorbars');
var Color = require('../../components/color');

var fillHoverText = require('../scatter/fill_hover_text');

module.exports = function hoverPoints(pointData, xval, yval, hovermode) {
var cd = pointData.cd;
Expand Down Expand Up @@ -99,11 +99,7 @@ module.exports = function hoverPoints(pointData, xval, yval, hovermode) {
pointData.xLabelVal = di.p;
}

if(di.htx) pointData.text = di.htx;
else if(trace.hovertext) pointData.text = trace.hovertext;
else if(di.tx) pointData.text = di.tx;
else if(trace.text) pointData.text = trace.text;

fillHoverText(di, trace, pointData);
ErrorBars.hoverInfo(di, trace, pointData);

return [pointData];
Expand Down
36 changes: 36 additions & 0 deletions src/traces/scatter/fill_hover_text.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

'use strict';

var Lib = require('../../lib');

/** Fill hover 'pointData' container with 'correct' hover text value
*
* - If trace hoverinfo contains a 'text' flag and hovertext is not set,
* the text elements will be seen in the hover labels.
*
* - If trace hoverinfo contains a 'text' flag and hovertext is set,
* hovertext takes precedence over text
* i.e. the hoverinfo elements will be seen in the hover labels
*
* @param {object} calcPt
* @param {object} trace
* @param {object || array} contOut (mutated here)
*/
module.exports = function fillHoverText(calcPt, trace, contOut) {
var fill = Array.isArray(contOut) ?
function(v) { contOut.push(v); } :
function(v) { contOut.text = v; };

var htx = Lib.extractOption(calcPt, trace, 'htx', 'hovertext');
if(htx && htx !== '') return fill(htx);

var tx = Lib.extractOption(calcPt, trace, 'tx', 'text');
if(tx && tx !== '') return fill(tx);
};
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nice! 🌴 Does this really belong in scatter though? Perhaps in fx?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have no strong opinion here. This routine felt similar to

https://github.com/plotly/plotly.js/blob/master/src/traces/scatter/arrays_to_calcdata.js

that's why I put it in traces/scatter/

Copy link
Collaborator

Choose a reason for hiding this comment

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

That's fine, we certainly have a long history of traces pulling from each other, particularly from scatter, and there is something nice about it being in the traces dir. Perhaps at some point we introduce a traces/common dir for stuff like this but leave it as is for now.

8 changes: 2 additions & 6 deletions src/traces/scatter/hover.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
* LICENSE file in the root directory of this source tree.
*/


'use strict';

var Lib = require('../../lib');
var Fx = require('../../components/fx');
var ErrorBars = require('../../components/errorbars');
var getTraceColor = require('./get_trace_color');
var Color = require('../../components/color');
var fillHoverText = require('./fill_hover_text');

var MAXDIST = Fx.constants.MAXDIST;

Expand Down Expand Up @@ -73,11 +73,7 @@ module.exports = function hoverPoints(pointData, xval, yval, hovermode) {
yLabelVal: di.y
});

if(di.htx) pointData.text = di.htx;
else if(trace.hovertext) pointData.text = trace.hovertext;
else if(di.tx) pointData.text = di.tx;
else if(trace.text) pointData.text = trace.text;

fillHoverText(di, trace, pointData);
ErrorBars.hoverInfo(di, trace, pointData);

return [pointData];
Expand Down
11 changes: 2 additions & 9 deletions src/traces/scattergeo/hover.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ var Axes = require('../../plots/cartesian/axes');
var BADNUM = require('../../constants/numerical').BADNUM;

var getTraceColor = require('../scatter/get_trace_color');
var fillHoverText = require('../scatter/fill_hover_text');
var attributes = require('./attributes');


module.exports = function hoverPoints(pointData, xval, yval) {
var cd = pointData.cd;
var trace = cd[0].trace;
Expand Down Expand Up @@ -98,14 +98,7 @@ function getExtraText(trace, pt, axis) {
}

if(hasText) {
var tx;

if(pt.htx) tx = pt.htx;
else if(trace.hovertext) tx = trace.hovertext;
else if(pt.tx) tx = pt.tx;
else if(trace.text) tx = trace.text;

if(!Array.isArray(tx)) text.push(tx);
fillHoverText(pt, trace, text);
}

return text.join('<br>');
Expand Down
10 changes: 2 additions & 8 deletions src/traces/scattermapbox/hover.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

var Fx = require('../../components/fx');
var getTraceColor = require('../scatter/get_trace_color');
var fillHoverText = require('../scatter/fill_hover_text');
var BADNUM = require('../../constants/numerical').BADNUM;

module.exports = function hoverPoints(pointData, xval, yval) {
Expand Down Expand Up @@ -90,14 +91,7 @@ function getExtraText(trace, di) {
}

if(isAll || parts.indexOf('text') !== -1) {
var tx;

if(di.htx) tx = di.htx;
else if(trace.hovertext) tx = trace.hovertext;
else if(di.tx) tx = di.tx;
else if(trace.text) tx = trace.text;

if(!Array.isArray(tx)) text.push(tx);
fillHoverText(di, trace, text);
}

return text.join('<br>');
Expand Down