Skip to content

Commit 03554a5

Browse files
authored
Merge pull request #1613 from plotly/fx-component
Fx component
2 parents 8d7ec16 + abcf1b0 commit 03554a5

37 files changed

+1601
-1507
lines changed

src/components/annotations/annotation_defaults.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111

1212
var Lib = require('../../lib');
1313
var Color = require('../color');
14+
var Fx = require('../fx');
1415
var Axes = require('../../plots/cartesian/axes');
15-
var constants = require('../../plots/cartesian/constants');
1616

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

@@ -118,8 +118,8 @@ module.exports = function handleAnnotationDefaults(annIn, annOut, fullLayout, op
118118
Color.opacity(bgColor) ? Color.rgb(bgColor) : Color.defaultLine);
119119
var hoverBorder = coerce('hoverlabel.bordercolor', Color.contrast(hoverBG));
120120
Lib.coerceFont(coerce, 'hoverlabel.font', {
121-
family: constants.HOVERFONT,
122-
size: constants.HOVERFONTSIZE,
121+
family: Fx.constants.HOVERFONT,
122+
size: Fx.constants.HOVERFONTSIZE,
123123
color: hoverBorder
124124
});
125125
}

src/components/annotations/draw.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ var Plotly = require('../../plotly');
1515
var Plots = require('../../plots/plots');
1616
var Lib = require('../../lib');
1717
var Axes = require('../../plots/cartesian/axes');
18-
var Fx = require('../../plots/cartesian/graph_interact');
1918
var Color = require('../color');
2019
var Drawing = require('../drawing');
20+
var Fx = require('../fx');
2121
var svgTextUtils = require('../../lib/svg_text_utils');
2222
var setCursor = require('../../lib/setcursor');
2323
var dragElement = require('../dragelement');

src/components/fx/click.js

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Copyright 2012-2017, Plotly, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the MIT license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
9+
'use strict';
10+
11+
var Registry = require('../../registry');
12+
13+
module.exports = function click(gd, evt) {
14+
var annotationsDone = Registry.getComponentMethod('annotations', 'onClick')(gd, gd._hoverdata);
15+
16+
function emitClick() { gd.emit('plotly_click', {points: gd._hoverdata, event: evt}); }
17+
18+
if(gd._hoverdata && evt && evt.target) {
19+
if(annotationsDone && annotationsDone.then) {
20+
annotationsDone.then(emitClick);
21+
}
22+
else emitClick();
23+
24+
// why do we get a double event without this???
25+
if(evt.stopImmediatePropagation) evt.stopImmediatePropagation();
26+
}
27+
};

src/components/fx/constants.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Copyright 2012-2017, Plotly, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the MIT license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
9+
'use strict';
10+
11+
module.exports = {
12+
// max pixels away from mouse to allow a point to highlight
13+
MAXDIST: 20,
14+
15+
// hover labels for multiple horizontal bars get tilted by this angle
16+
YANGLE: 60,
17+
18+
// size and display constants for hover text
19+
HOVERARROWSIZE: 6, // pixel size of hover arrows
20+
HOVERTEXTPAD: 3, // pixels padding around text
21+
HOVERFONTSIZE: 13,
22+
HOVERFONT: 'Arial, sans-serif',
23+
24+
// minimum time (msec) between hover calls
25+
HOVERMINTIME: 50,
26+
};

src/components/fx/helpers.js

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/**
2+
* Copyright 2012-2017, Plotly, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the MIT license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
9+
'use strict';
10+
11+
var constants = require('./constants');
12+
13+
// look for either subplot or xaxis and yaxis attributes
14+
exports.getSubplot = function getSubplot(trace) {
15+
return trace.subplot || (trace.xaxis + trace.yaxis) || trace.geo;
16+
};
17+
18+
// convenience functions for mapping all relevant axes
19+
exports.flat = function flat(subplots, v) {
20+
var out = [];
21+
for(var i = subplots.length; i > 0; i--) out.push(v);
22+
return out;
23+
};
24+
25+
exports.p2c = function p2c(axArray, v) {
26+
var out = [];
27+
for(var i = 0; i < axArray.length; i++) out.push(axArray[i].p2c(v));
28+
return out;
29+
};
30+
31+
exports.getDistanceFunction = function getDistanceFunction(mode, dx, dy, dxy) {
32+
if(mode === 'closest') return dxy || quadrature(dx, dy);
33+
return mode === 'x' ? dx : dy;
34+
};
35+
36+
exports.getClosest = function getClosest(cd, distfn, pointData) {
37+
// do we already have a point number? (array mode only)
38+
if(pointData.index !== false) {
39+
if(pointData.index >= 0 && pointData.index < cd.length) {
40+
pointData.distance = 0;
41+
}
42+
else pointData.index = false;
43+
}
44+
else {
45+
// apply the distance function to each data point
46+
// this is the longest loop... if this bogs down, we may need
47+
// to create pre-sorted data (by x or y), not sure how to
48+
// do this for 'closest'
49+
for(var i = 0; i < cd.length; i++) {
50+
var newDistance = distfn(cd[i]);
51+
if(newDistance <= pointData.distance) {
52+
pointData.index = i;
53+
pointData.distance = newDistance;
54+
}
55+
}
56+
}
57+
return pointData;
58+
};
59+
60+
// for bar charts and others with finite-size objects: you must be inside
61+
// it to see its hover info, so distance is infinite outside.
62+
// But make distance inside be at least 1/4 MAXDIST, and a little bigger
63+
// for bigger bars, to prioritize scatter and smaller bars over big bars
64+
//
65+
// note that for closest mode, two inbox's will get added in quadrature
66+
// args are (signed) difference from the two opposite edges
67+
// count one edge as in, so that over continuous ranges you never get a gap
68+
exports.inbox = function inbox(v0, v1) {
69+
if(v0 * v1 < 0 || v0 === 0) {
70+
return constants.MAXDIST * (0.6 - 0.3 / Math.max(3, Math.abs(v0 - v1)));
71+
}
72+
return Infinity;
73+
};
74+
75+
function quadrature(dx, dy) {
76+
return function(di) {
77+
var x = dx(di),
78+
y = dy(di);
79+
return Math.sqrt(x * x + y * y);
80+
};
81+
}

0 commit comments

Comments
 (0)