Skip to content

Commit f86673c

Browse files
committed
put hoverPoint and getTraceColor in separate files
1 parent b418b5f commit f86673c

File tree

4 files changed

+123
-92
lines changed

4 files changed

+123
-92
lines changed

src/traces/scatter/get_trace_color.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* Copyright 2012-2016, 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+
10+
'use strict';
11+
12+
var Color = require('../../components/color');
13+
var subtypes = require('./subtypes');
14+
15+
16+
module.exports = function getTraceColor(trace, di) {
17+
var lc, tc;
18+
19+
// TODO: text modes
20+
21+
if(trace.mode === 'lines') {
22+
lc = trace.line.color;
23+
return (lc && Color.opacity(lc)) ?
24+
lc : trace.fillcolor;
25+
}
26+
else if(trace.mode === 'none') {
27+
return trace.fill ? trace.fillcolor : '';
28+
}
29+
else {
30+
var mc = di.mcc || (trace.marker || {}).color,
31+
mlc = di.mlcc || ((trace.marker || {}).line || {}).color;
32+
33+
tc = (mc && Color.opacity(mc)) ? mc :
34+
(mlc && Color.opacity(mlc) &&
35+
(di.mlw || ((trace.marker || {}).line || {}).width)) ? mlc : '';
36+
37+
if(tc) {
38+
// make sure the points aren't TOO transparent
39+
if(Color.opacity(tc) < 0.3) {
40+
return Color.addOpacity(tc, 0.3);
41+
}
42+
else return tc;
43+
}
44+
else {
45+
lc = (trace.line || {}).color;
46+
return (lc && Color.opacity(lc) &&
47+
subtypes.hasLines(trace) && trace.line.width) ?
48+
lc : trace.fillcolor;
49+
}
50+
}
51+
};

src/traces/scatter/hover.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/**
2+
* Copyright 2012-2016, 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+
10+
'use strict';
11+
12+
var Fx = require('../../plots/cartesian/graph_interact');
13+
var ErrorBars = require('../../components/errorbars');
14+
var getTraceColor = require('./get_trace_color');
15+
16+
17+
module.exports = function hoverPoints(pointData, xval, yval, hovermode) {
18+
var cd = pointData.cd,
19+
trace = cd[0].trace,
20+
xa = pointData.xa,
21+
ya = pointData.ya,
22+
dx = function(di){
23+
// scatter points: d.mrc is the calculated marker radius
24+
// adjust the distance so if you're inside the marker it
25+
// always will show up regardless of point size, but
26+
// prioritize smaller points
27+
var rad = Math.max(3, di.mrc||0);
28+
return Math.max(Math.abs(xa.c2p(di.x)-xa.c2p(xval))-rad, 1-3/rad);
29+
},
30+
dy = function(di){
31+
var rad = Math.max(3, di.mrc||0);
32+
return Math.max(Math.abs(ya.c2p(di.y)-ya.c2p(yval))-rad, 1-3/rad);
33+
},
34+
dxy = function(di) {
35+
var rad = Math.max(3, di.mrc||0),
36+
dx = Math.abs(xa.c2p(di.x)-xa.c2p(xval)),
37+
dy = Math.abs(ya.c2p(di.y)-ya.c2p(yval));
38+
return Math.max(Math.sqrt(dx*dx + dy*dy)-rad, 1-3/rad);
39+
},
40+
distfn = Fx.getDistanceFunction(hovermode, dx, dy, dxy);
41+
42+
Fx.getClosest(cd, distfn, pointData);
43+
44+
// skip the rest (for this trace) if we didn't find a close point
45+
if(pointData.index===false) return;
46+
47+
// the closest data point
48+
var di = cd[pointData.index],
49+
xc = xa.c2p(di.x, true),
50+
yc = ya.c2p(di.y, true),
51+
rad = di.mrc||1;
52+
53+
pointData.color = getTraceColor(trace, di);
54+
55+
pointData.x0 = xc - rad;
56+
pointData.x1 = xc + rad;
57+
pointData.xLabelVal = di.x;
58+
59+
pointData.y0 = yc - rad;
60+
pointData.y1 = yc + rad;
61+
pointData.yLabelVal = di.y;
62+
63+
if(di.tx) pointData.text = di.tx;
64+
else if(trace.text) pointData.text = trace.text;
65+
66+
ErrorBars.hoverInfo(di, trace, pointData);
67+
68+
return [pointData];
69+
};

src/traces/scatter/index.js

Lines changed: 1 addition & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ scatter.name = 'scatter';
3838
scatter.categories = ['cartesian', 'symbols', 'markerColorscale', 'errorBarsOK', 'showLegend'];
3939
scatter.meta = {
4040
Scatter.supplyDefaults = require('./defaults');
41+
Scatter.hoverPoints = require('./hover');
4142
description: [
4243
'The scatter trace type encompasses line charts, scatter charts, text charts, and bubble charts.',
4344
'The data visualized as scatter point or lines is set in `x` and `y`.',
@@ -602,94 +603,3 @@ scatter.style = function(gd) {
602603
s.selectAll('g.trace path.js-fill')
603604
.call(Drawing.fillGroupStyle);
604605
};
605-
606-
scatter.getTraceColor = function(trace, di) {
607-
var lc, tc;
608-
609-
// TODO: text modes
610-
611-
if(trace.mode === 'lines') {
612-
lc = trace.line.color;
613-
return (lc && Color.opacity(lc)) ?
614-
lc : trace.fillcolor;
615-
}
616-
else if(trace.mode === 'none') {
617-
return trace.fill ? trace.fillcolor : '';
618-
}
619-
else {
620-
var mc = di.mcc || (trace.marker || {}).color,
621-
mlc = di.mlcc || ((trace.marker || {}).line || {}).color;
622-
623-
tc = (mc && Color.opacity(mc)) ? mc :
624-
(mlc && Color.opacity(mlc) &&
625-
(di.mlw || ((trace.marker || {}).line || {}).width)) ? mlc : '';
626-
627-
if(tc) {
628-
// make sure the points aren't TOO transparent
629-
if(Color.opacity(tc) < 0.3) {
630-
return Color.addOpacity(tc, 0.3);
631-
}
632-
else return tc;
633-
}
634-
else {
635-
lc = (trace.line || {}).color;
636-
return (lc && Color.opacity(lc) &&
637-
scatter.hasLines(trace) && trace.line.width) ?
638-
lc : trace.fillcolor;
639-
}
640-
}
641-
};
642-
643-
scatter.hoverPoints = function(pointData, xval, yval, hovermode) {
644-
var cd = pointData.cd,
645-
trace = cd[0].trace,
646-
xa = pointData.xa,
647-
ya = pointData.ya,
648-
dx = function(di){
649-
// scatter points: d.mrc is the calculated marker radius
650-
// adjust the distance so if you're inside the marker it
651-
// always will show up regardless of point size, but
652-
// prioritize smaller points
653-
var rad = Math.max(3, di.mrc||0);
654-
return Math.max(Math.abs(xa.c2p(di.x)-xa.c2p(xval))-rad, 1-3/rad);
655-
},
656-
dy = function(di){
657-
var rad = Math.max(3, di.mrc||0);
658-
return Math.max(Math.abs(ya.c2p(di.y)-ya.c2p(yval))-rad, 1-3/rad);
659-
},
660-
dxy = function(di) {
661-
var rad = Math.max(3, di.mrc||0),
662-
dx = Math.abs(xa.c2p(di.x)-xa.c2p(xval)),
663-
dy = Math.abs(ya.c2p(di.y)-ya.c2p(yval));
664-
return Math.max(Math.sqrt(dx*dx + dy*dy)-rad, 1-3/rad);
665-
},
666-
distfn = Fx.getDistanceFunction(hovermode, dx, dy, dxy);
667-
668-
Fx.getClosest(cd, distfn, pointData);
669-
670-
// skip the rest (for this trace) if we didn't find a close point
671-
if(pointData.index===false) return;
672-
673-
// the closest data point
674-
var di = cd[pointData.index],
675-
xc = xa.c2p(di.x, true),
676-
yc = ya.c2p(di.y, true),
677-
rad = di.mrc||1;
678-
679-
pointData.color = scatter.getTraceColor(trace, di);
680-
681-
pointData.x0 = xc - rad;
682-
pointData.x1 = xc + rad;
683-
pointData.xLabelVal = di.x;
684-
685-
pointData.y0 = yc - rad;
686-
pointData.y1 = yc + rad;
687-
pointData.yLabelVal = di.y;
688-
689-
if(di.tx) pointData.text = di.tx;
690-
else if(trace.text) pointData.text = trace.text;
691-
692-
ErrorBars.hoverInfo(di, trace, pointData);
693-
694-
return [pointData];
695-
};

src/traces/scattergl/convert.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ var Scatter = require('../scatter');
2525

2626
var ErrorBars = require('../../components/errorbars');
2727
var makeBubbleSizeFn = require('../scatter/make_bubble_size_func');
28+
var getTraceColor = require('../scatter/get_trace_color');
2829

2930
var MARKER_SYMBOLS = require('../../constants/gl_markers.json');
3031
var DASHES = require('../../constants/gl2d_dashes.json');
@@ -260,7 +261,7 @@ proto.update = function(options) {
260261

261262
// not quite on-par with 'scatter', but close enough for now
262263
// does not handle the colorscale case
263-
this.color = Scatter.getTraceColor(options, {});
264+
this.color = getTraceColor(options, {});
264265
};
265266

266267
proto.updateFast = function(options) {

0 commit comments

Comments
 (0)