Skip to content

Commit 21c8cc6

Browse files
committed
add common annotations defaults modules
- which coerces all non-position attributes for annotations and annotations3d - automatically adds supports for 'hovertext' and 'hoverlabel' for annotation3d 🎉
1 parent bd9c867 commit 21c8cc6

File tree

5 files changed

+79
-79
lines changed

5 files changed

+79
-79
lines changed

src/components/annotations/annotation_defaults.js

Lines changed: 5 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@
1010
'use strict';
1111

1212
var Lib = require('../../lib');
13-
var Color = require('../color');
1413
var Axes = require('../../plots/cartesian/axes');
15-
14+
var handleAnnotationCommonDefaults = require('./common_defaults');
1615
var attributes = require('./attributes');
1716

1817

@@ -29,26 +28,9 @@ module.exports = function handleAnnotationDefaults(annIn, annOut, fullLayout, op
2928

3029
if(!(visible || clickToShow)) return annOut;
3130

32-
coerce('opacity');
33-
var bgColor = coerce('bgcolor');
34-
35-
var borderColor = coerce('bordercolor'),
36-
borderOpacity = Color.opacity(borderColor);
37-
38-
coerce('borderpad');
39-
40-
var borderWidth = coerce('borderwidth');
41-
var showArrow = coerce('showarrow');
42-
43-
coerce('text', showArrow ? ' ' : 'new text');
44-
coerce('textangle');
45-
Lib.coerceFont(coerce, 'font', fullLayout.font);
46-
47-
coerce('width');
48-
coerce('align');
31+
handleAnnotationCommonDefaults(annIn, annOut, fullLayout, coerce);
4932

50-
var h = coerce('height');
51-
if(h) coerce('valign');
33+
var showArrow = annOut.showarrow;
5234

5335
// positioning
5436
var axLetters = ['x', 'y'],
@@ -90,14 +72,8 @@ module.exports = function handleAnnotationDefaults(annIn, annOut, fullLayout, op
9072
// if you have one coordinate you should have both
9173
Lib.noneOrAll(annIn, annOut, ['x', 'y']);
9274

75+
// if you have one part of arrow length you should have both
9376
if(showArrow) {
94-
coerce('arrowcolor', borderOpacity ? annOut.bordercolor : Color.defaultLine);
95-
coerce('arrowhead');
96-
coerce('arrowsize');
97-
coerce('arrowwidth', ((borderOpacity && borderWidth) || 1) * 2);
98-
coerce('standoff');
99-
100-
// if you have one part of arrow length you should have both
10177
Lib.noneOrAll(annIn, annOut, ['ax', 'ay']);
10278
}
10379

@@ -111,25 +87,7 @@ module.exports = function handleAnnotationDefaults(annIn, annOut, fullLayout, op
11187
annOut._yclick = (yClick === undefined) ? annOut.y : yClick;
11288
}
11389

114-
var hoverText = coerce('hovertext');
115-
var globalHoverLabel = fullLayout.hoverlabel || {};
116-
117-
if(hoverText) {
118-
var hoverBG = coerce('hoverlabel.bgcolor', globalHoverLabel.bgcolor ||
119-
(Color.opacity(bgColor) ? Color.rgb(bgColor) : Color.defaultLine)
120-
);
121-
122-
var hoverBorder = coerce('hoverlabel.bordercolor', globalHoverLabel.bordercolor ||
123-
Color.contrast(hoverBG)
124-
);
125-
126-
Lib.coerceFont(coerce, 'hoverlabel.font', {
127-
family: globalHoverLabel.font.family,
128-
size: globalHoverLabel.font.size,
129-
color: globalHoverLabel.font.color || hoverBorder
130-
});
131-
}
132-
coerce('captureevents', !!hoverText);
90+
coerce('captureevents', !!annOut.hovertext);
13391

13492
return annOut;
13593
};
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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 Lib = require('../../lib');
12+
var Color = require('../color');
13+
14+
// defaults common to 'annotations' and 'annotations3d'
15+
module.exports = function handleAnnotationCommonDefaults(annIn, annOut, fullLayout, coerce) {
16+
coerce('opacity');
17+
var bgColor = coerce('bgcolor');
18+
19+
var borderColor = coerce('bordercolor');
20+
var borderOpacity = Color.opacity(borderColor);
21+
22+
coerce('borderpad');
23+
24+
var borderWidth = coerce('borderwidth');
25+
var showArrow = coerce('showarrow');
26+
27+
coerce('text', showArrow ? ' ' : 'new text');
28+
coerce('textangle');
29+
Lib.coerceFont(coerce, 'font', fullLayout.font);
30+
31+
coerce('width');
32+
coerce('align');
33+
34+
var h = coerce('height');
35+
if(h) coerce('valign');
36+
37+
if(showArrow) {
38+
coerce('arrowcolor', borderOpacity ? annOut.bordercolor : Color.defaultLine);
39+
coerce('arrowhead');
40+
coerce('arrowsize');
41+
coerce('arrowwidth', ((borderOpacity && borderWidth) || 1) * 2);
42+
coerce('standoff');
43+
44+
}
45+
46+
var hoverText = coerce('hovertext');
47+
var globalHoverLabel = fullLayout.hoverlabel || {};
48+
49+
if(hoverText) {
50+
var hoverBG = coerce('hoverlabel.bgcolor', globalHoverLabel.bgcolor ||
51+
(Color.opacity(bgColor) ? Color.rgb(bgColor) : Color.defaultLine)
52+
);
53+
54+
var hoverBorder = coerce('hoverlabel.bordercolor', globalHoverLabel.bordercolor ||
55+
Color.contrast(hoverBG)
56+
);
57+
58+
Lib.coerceFont(coerce, 'hoverlabel.font', {
59+
family: globalHoverLabel.font.family,
60+
size: globalHoverLabel.font.size,
61+
color: globalHoverLabel.font.color || hoverBorder
62+
});
63+
}
64+
};

src/components/annotations3d/attributes.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,14 @@ module.exports = {
7474
arrowsize: annAtts.arrowsize,
7575
arrowwidth: annAtts.arrowwidth,
7676
standoff: annAtts.standoff,
77+
hovertext: annAtts.hovertext,
78+
hoverlabel: annAtts.hoverlabel
7779

78-
// maybes later
80+
// maybes later?
7981
// clicktoshow: annAtts.clicktoshow,
8082
// xclick: annAtts.xclick,
8183
// yclick: annAtts.yclick,
84+
// captureevent: annAtts.captureevent
8285

8386
// not needed!
8487
// axref: 'pixel'

src/components/annotations3d/defaults.js

Lines changed: 5 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@
99
'use strict';
1010

1111
var Lib = require('../../lib');
12-
var Color = require('../color');
1312
var handleArrayContainerDefaults = require('../../plots/array_container_defaults');
13+
var handleAnnotationCommonDefaults = require('../annotations/common_defaults');
1414
var attributes = require('./attributes');
1515

16-
module.exports = function(sceneLayoutIn, sceneLayoutOut, opts) {
16+
module.exports = function handleDefaults(sceneLayoutIn, sceneLayoutOut, opts) {
1717
handleArrayContainerDefaults(sceneLayoutIn, sceneLayoutOut, {
1818
name: 'annotations',
1919
handleItemDefaults: handleAnnotationDefaults,
20-
font: opts.font,
20+
fullLayout: opts.fullLayout,
2121
scene: opts.id
2222
});
2323
};
@@ -30,27 +30,7 @@ function handleAnnotationDefaults(annIn, annOut, sceneLayout, opts, itemOpts) {
3030
var visible = coerce('visible', !itemOpts.itemIsNotPlainObject);
3131
if(!visible) return annOut;
3232

33-
coerce('opacity');
34-
coerce('align');
35-
coerce('bgcolor');
36-
37-
var borderColor = coerce('bordercolor');
38-
var borderOpacity = Color.opacity(borderColor);
39-
40-
coerce('borderpad');
41-
42-
var borderWidth = coerce('borderwidth');
43-
var showArrow = coerce('showarrow');
44-
45-
coerce('text', showArrow ? ' ' : 'new text');
46-
coerce('textangle');
47-
Lib.coerceFont(coerce, 'font', opts.font);
48-
49-
coerce('width');
50-
coerce('align');
51-
52-
var h = coerce('height');
53-
if(h) coerce('valign');
33+
handleAnnotationCommonDefaults(annIn, annOut, opts.fullLayout, coerce);
5434

5535
// Do not use Axes.coercePosition here
5636
// as ax._categories aren't filled in at this stage,
@@ -72,7 +52,7 @@ function handleAnnotationDefaults(annIn, annOut, sceneLayout, opts, itemOpts) {
7252
coerce('xshift');
7353
coerce('yshift');
7454

75-
if(showArrow) {
55+
if(annOut.showarrow) {
7656
annOut.axref = 'pixel';
7757
annOut.ayref = 'pixel';
7858

@@ -82,12 +62,6 @@ function handleAnnotationDefaults(annIn, annOut, sceneLayout, opts, itemOpts) {
8262

8363
// if you have one part of arrow length you should have both
8464
Lib.noneOrAll(annIn, annOut, ['ax', 'ay']);
85-
86-
coerce('arrowcolor', borderOpacity ? annOut.bordercolor : Color.defaultLine);
87-
coerce('arrowhead');
88-
coerce('arrowsize');
89-
coerce('arrowwidth', ((borderOpacity && borderWidth) || 1) * 2);
90-
coerce('standoff');
9165
}
9266

9367
annOut._scene = opts.scene;

src/plots/gl3d/layout/defaults.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ module.exports = function supplyLayoutDefaults(layoutIn, layoutOut, fullData) {
3434
type: 'gl3d',
3535
attributes: layoutAttributes,
3636
handleDefaults: handleGl3dDefaults,
37+
fullLayout: layoutOut,
3738
font: layoutOut.font,
3839
fullData: fullData,
3940
getDfltFromLayout: getDfltFromLayout,

0 commit comments

Comments
 (0)