Skip to content

Custom trace hover labels #1582

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 43 commits into from
May 10, 2017
Merged
Show file tree
Hide file tree
Changes from 30 commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
83cd8c8
add hoverlabel attribute container for all traces
etpinard Apr 11, 2017
258f1c7
merge hoverlabel items in scatter and bar calcdata
etpinard Apr 11, 2017
fe50b6f
add hover label setting to hoverData during `cleanPoint`
etpinard Apr 11, 2017
8d7ec16
Merge branch 'master' into hoverlabel-custom
etpinard Apr 20, 2017
ffde987
break up cartesian graph_interact.js:
etpinard Apr 20, 2017
1d4930c
update Fx require statements
etpinard Apr 20, 2017
e609955
update hover constant require statements
etpinard Apr 20, 2017
12954aa
sub Fx.init by (cartesian) initIterations
etpinard Apr 20, 2017
27fb2d0
decrease max allowed circular deps to 13 :tada:
etpinard Apr 20, 2017
a96af57
mv fx attribute out of plots/ and into components/fx/
etpinard Apr 20, 2017
abcf1b0
register fx in core.js
etpinard Apr 20, 2017
03554a5
Merge pull request #1613 from plotly/fx-component
etpinard Apr 20, 2017
efcb027
revert hoverlabel in arraysToCalcdata -> add Fx.calc
etpinard Apr 20, 2017
7907140
make fx default test more robust
etpinard Apr 21, 2017
0d66e6e
lint fx constants
etpinard Apr 21, 2017
3ac4c07
lint getComponentMethod call
etpinard Apr 21, 2017
295659e
add fx supplyDefaults method
etpinard Apr 21, 2017
9d91e2c
fix mapbox hover label color for array `marker.color` values
etpinard Apr 24, 2017
add85d2
lint var declarations
etpinard Apr 24, 2017
f986fd2
add global layout defaults step for `layout.hoverlabel`
etpinard Apr 24, 2017
a8cc8b0
make common label in 'x' and 'y' hovermode adhere to layout.hoverlabel
etpinard Apr 24, 2017
9ee8877
add tests for custom hover labels cartesian, geo, mapbox & ternary
etpinard Apr 24, 2017
4ec88ef
add support for per-point `hoverlabel` setting in 2d z traces
etpinard Apr 24, 2017
b053629
add support for per-pt `hoverlabel` setting in pie traces
etpinard Apr 24, 2017
92de017
put selection coords (not hoverlabel strings) to event data
etpinard Apr 25, 2017
7332d23
set `selection.index` during gl3d trace handlePick
etpinard Apr 25, 2017
6bbfa65
add support for custom hover label in gl3d
etpinard Apr 25, 2017
28c2770
add support for custom hover label in gl2d
etpinard Apr 25, 2017
cda735b
make hoverlabel text selection query more robust
etpinard Apr 25, 2017
ce27e10
bump back max allowed circular deps to 17
etpinard Apr 25, 2017
be9e6b9
resolves #1575 - call Fx.hover with evt object containing 'xpx' & 'ypx'
etpinard Apr 26, 2017
cf61dc2
fixes #1600 - allow z cartesian traces to have a name hover label
etpinard Apr 26, 2017
9eeadcf
don't override hover label data by undefined vals
etpinard Apr 26, 2017
8321959
make sure arrayOk resyle logic works for hoverlabel attributes
etpinard Apr 27, 2017
409b771
Merge pull request #1627 from plotly/hover-misc-bugs
etpinard May 9, 2017
60bd4fc
factor out gl2d / gl3d cast hover options logic into 1 Fx method
etpinard May 9, 2017
e45bff9
perf improvements in fx helpers
etpinard May 9, 2017
e0be6cd
Merge branch 'master' into hoverlabel-custom
etpinard May 9, 2017
8d021e0
:hocho: :evergreen_tree: in sankey tests
etpinard May 10, 2017
4d3ce92
implement 'hoverlabel' settings in sankey hover routines
etpinard May 10, 2017
e50886c
fixup: don't support array value in sankey hoverlabel settings
etpinard May 10, 2017
f8258cb
add sankey hover label style tests
etpinard May 10, 2017
3f52a89
Merge branch 'master' into hoverlabel-custom
etpinard May 10, 2017
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
20 changes: 13 additions & 7 deletions src/components/annotations/annotation_defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
var Lib = require('../../lib');
var Color = require('../color');
var Axes = require('../../plots/cartesian/axes');
var constants = require('../../plots/cartesian/constants');

var attributes = require('./attributes');

Expand Down Expand Up @@ -113,14 +112,21 @@ module.exports = function handleAnnotationDefaults(annIn, annOut, fullLayout, op
}

var hoverText = coerce('hovertext');
var globalHoverLabel = fullLayout.hoverlabel || {};

if(hoverText) {
var hoverBG = coerce('hoverlabel.bgcolor',
Color.opacity(bgColor) ? Color.rgb(bgColor) : Color.defaultLine);
var hoverBorder = coerce('hoverlabel.bordercolor', Color.contrast(hoverBG));
var hoverBG = coerce('hoverlabel.bgcolor', globalHoverLabel.bgcolor ||
(Color.opacity(bgColor) ? Color.rgb(bgColor) : Color.defaultLine)
);

var hoverBorder = coerce('hoverlabel.bordercolor', globalHoverLabel.bordercolor ||
Color.contrast(hoverBG)
);

Lib.coerceFont(coerce, 'hoverlabel.font', {
family: constants.HOVERFONT,
size: constants.HOVERFONTSIZE,
color: hoverBorder
family: globalHoverLabel.font.family,
size: globalHoverLabel.font.size,
color: globalHoverLabel.font.color || hoverBorder
});
}
coerce('captureevents', !!hoverText);
Expand Down
2 changes: 1 addition & 1 deletion src/components/annotations/draw.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ var Plotly = require('../../plotly');
var Plots = require('../../plots/plots');
var Lib = require('../../lib');
var Axes = require('../../plots/cartesian/axes');
var Fx = require('../../plots/cartesian/graph_interact');
var Color = require('../color');
var Drawing = require('../drawing');
var Fx = require('../fx');
var svgTextUtils = require('../../lib/svg_text_utils');
var setCursor = require('../../lib/setcursor');
var dragElement = require('../dragelement');
Expand Down
38 changes: 38 additions & 0 deletions src/components/fx/attributes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* 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 extendFlat = require('../../lib/extend').extendFlat;
var fontAttrs = require('../../plots/font_attributes');

module.exports = {
hoverlabel: {
bgcolor: {
valType: 'color',
role: 'style',
arrayOk: true,
description: [
'Sets the background color of the hover labels for this trace'
].join(' ')
},
bordercolor: {
valType: 'color',
role: 'style',
arrayOk: true,
description: [
'Sets the border color of the hover labels for this trace.'
].join(' ')
},
font: {
family: extendFlat({}, fontAttrs.family, { arrayOk: true }),
size: extendFlat({}, fontAttrs.size, { arrayOk: true }),
color: extendFlat({}, fontAttrs.color, { arrayOk: true })
}
}
};
37 changes: 37 additions & 0 deletions src/components/fx/calc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* 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');
var Registry = require('../../registry');

module.exports = function calc(gd) {
var calcdata = gd.calcdata;

for(var i = 0; i < calcdata.length; i++) {
var cd = calcdata[i];
var trace = cd[0].trace;

if(!trace.hoverlabel) continue;

var mergeFn = Registry.traceIs(trace, '2dMap') ? paste : Lib.mergeArray;

mergeFn(trace.hoverlabel.bgcolor, cd, 'hbg');
mergeFn(trace.hoverlabel.bordercolor, cd, 'hbc');
mergeFn(trace.hoverlabel.font.size, cd, 'hts');
mergeFn(trace.hoverlabel.font.color, cd, 'htc');
mergeFn(trace.hoverlabel.font.family, cd, 'htf');
}
};

function paste(traceAttr, cd, cdAttr) {
if(Array.isArray(traceAttr)) {
cd[0][cdAttr] = traceAttr;
}
}
27 changes: 27 additions & 0 deletions src/components/fx/click.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* 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 Registry = require('../../registry');

module.exports = function click(gd, evt) {
var annotationsDone = Registry.getComponentMethod('annotations', 'onClick')(gd, gd._hoverdata);

function emitClick() { gd.emit('plotly_click', {points: gd._hoverdata, event: evt}); }

if(gd._hoverdata && evt && evt.target) {
if(annotationsDone && annotationsDone.then) {
annotationsDone.then(emitClick);
}
else emitClick();

// why do we get a double event without this???
if(evt.stopImmediatePropagation) evt.stopImmediatePropagation();
}
};
30 changes: 30 additions & 0 deletions src/components/fx/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* 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';

module.exports = {
// max pixels away from mouse to allow a point to highlight
MAXDIST: 20,

// hover labels for multiple horizontal bars get tilted by this angle
YANGLE: 60,

// size and display constants for hover text

// pixel size of hover arrows
HOVERARROWSIZE: 6,
// pixels padding around text
HOVERTEXTPAD: 3,
// hover font
HOVERFONTSIZE: 13,
HOVERFONT: 'Arial, sans-serif',

// minimum time (msec) between hover calls
HOVERMINTIME: 50
};
21 changes: 21 additions & 0 deletions src/components/fx/defaults.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* 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');
var attributes = require('./attributes');
var handleHoverLabelDefaults = require('./hoverlabel_defaults');

module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout) {
function coerce(attr, dflt) {
return Lib.coerce(traceIn, traceOut, attributes, attr, dflt);
}

handleHoverLabelDefaults(traceIn, traceOut, coerce, layout.hoverlabel);
};
81 changes: 81 additions & 0 deletions src/components/fx/helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/**
* 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 constants = require('./constants');

// look for either subplot or xaxis and yaxis attributes
exports.getSubplot = function getSubplot(trace) {
return trace.subplot || (trace.xaxis + trace.yaxis) || trace.geo;
};

// convenience functions for mapping all relevant axes
exports.flat = function flat(subplots, v) {
var out = [];
Copy link
Collaborator

Choose a reason for hiding this comment

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

faster (here and in p2c below) to do var out = new Array(subplots.length) and avoid push?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done in e45bff9

for(var i = subplots.length; i > 0; i--) out.push(v);
return out;
};

exports.p2c = function p2c(axArray, v) {
var out = [];
for(var i = 0; i < axArray.length; i++) out.push(axArray[i].p2c(v));
return out;
};

exports.getDistanceFunction = function getDistanceFunction(mode, dx, dy, dxy) {
if(mode === 'closest') return dxy || quadrature(dx, dy);
return mode === 'x' ? dx : dy;
};

exports.getClosest = function getClosest(cd, distfn, pointData) {
// do we already have a point number? (array mode only)
if(pointData.index !== false) {
if(pointData.index >= 0 && pointData.index < cd.length) {
pointData.distance = 0;
}
else pointData.index = false;
}
else {
// apply the distance function to each data point
// this is the longest loop... if this bogs down, we may need
// to create pre-sorted data (by x or y), not sure how to
// do this for 'closest'
for(var i = 0; i < cd.length; i++) {
var newDistance = distfn(cd[i]);
if(newDistance <= pointData.distance) {
pointData.index = i;
pointData.distance = newDistance;
}
}
}
return pointData;
};

// for bar charts and others with finite-size objects: you must be inside
// it to see its hover info, so distance is infinite outside.
// But make distance inside be at least 1/4 MAXDIST, and a little bigger
// for bigger bars, to prioritize scatter and smaller bars over big bars
//
// note that for closest mode, two inbox's will get added in quadrature
// args are (signed) difference from the two opposite edges
// count one edge as in, so that over continuous ranges you never get a gap
exports.inbox = function inbox(v0, v1) {
if(v0 * v1 < 0 || v0 === 0) {
return constants.MAXDIST * (0.6 - 0.3 / Math.max(3, Math.abs(v0 - v1)));
}
return Infinity;
};

function quadrature(dx, dy) {
return function(di) {
var x = dx(di),
y = dy(di);
return Math.sqrt(x * x + y * y);
};
}
Loading