Skip to content

Commit c0c0ad6

Browse files
authored
Merge pull request #833 from plotly/component-break-up
Break up annotations and shapes components
2 parents fb9c5f4 + a5dbf44 commit c0c0ad6

17 files changed

+2043
-1863
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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 Lib = require('../../lib');
13+
var Color = require('../color');
14+
var Axes = require('../../plots/cartesian/axes');
15+
16+
var attributes = require('./attributes');
17+
18+
19+
module.exports = function handleAnnotationDefaults(annIn, fullLayout) {
20+
var annOut = {};
21+
22+
function coerce(attr, dflt) {
23+
return Lib.coerce(annIn, annOut, attributes, attr, dflt);
24+
}
25+
26+
coerce('opacity');
27+
coerce('align');
28+
coerce('bgcolor');
29+
30+
var borderColor = coerce('bordercolor'),
31+
borderOpacity = Color.opacity(borderColor);
32+
33+
coerce('borderpad');
34+
35+
var borderWidth = coerce('borderwidth');
36+
var showArrow = coerce('showarrow');
37+
38+
if(showArrow) {
39+
coerce('arrowcolor', borderOpacity ? annOut.bordercolor : Color.defaultLine);
40+
coerce('arrowhead');
41+
coerce('arrowsize');
42+
coerce('arrowwidth', ((borderOpacity && borderWidth) || 1) * 2);
43+
coerce('ax');
44+
coerce('ay');
45+
coerce('axref');
46+
coerce('ayref');
47+
48+
// if you have one part of arrow length you should have both
49+
Lib.noneOrAll(annIn, annOut, ['ax', 'ay']);
50+
}
51+
52+
coerce('text', showArrow ? ' ' : 'new text');
53+
coerce('textangle');
54+
Lib.coerceFont(coerce, 'font', fullLayout.font);
55+
56+
// positioning
57+
var axLetters = ['x', 'y'];
58+
for(var i = 0; i < 2; i++) {
59+
var axLetter = axLetters[i],
60+
tdMock = {_fullLayout: fullLayout};
61+
62+
// xref, yref
63+
var axRef = Axes.coerceRef(annIn, annOut, tdMock, axLetter);
64+
65+
// TODO: should be refactored in conjunction with Axes axref, ayref
66+
var aaxRef = Axes.coerceARef(annIn, annOut, tdMock, axLetter);
67+
68+
// x, y
69+
var defaultPosition = 0.5;
70+
if(axRef !== 'paper') {
71+
var ax = Axes.getFromId(tdMock, axRef);
72+
defaultPosition = ax.range[0] + defaultPosition * (ax.range[1] - ax.range[0]);
73+
74+
// convert date or category strings to numbers
75+
if(['date', 'category'].indexOf(ax.type) !== -1 &&
76+
typeof annIn[axLetter] === 'string') {
77+
var newval;
78+
if(ax.type === 'date') {
79+
newval = Lib.dateTime2ms(annIn[axLetter]);
80+
if(newval !== false) annIn[axLetter] = newval;
81+
82+
if(aaxRef === axRef) {
83+
var newvalB = Lib.dateTime2ms(annIn['a' + axLetter]);
84+
if(newvalB !== false) annIn['a' + axLetter] = newvalB;
85+
}
86+
}
87+
else if((ax._categories || []).length) {
88+
newval = ax._categories.indexOf(annIn[axLetter]);
89+
if(newval !== -1) annIn[axLetter] = newval;
90+
}
91+
}
92+
}
93+
coerce(axLetter, defaultPosition);
94+
95+
// xanchor, yanchor
96+
if(!showArrow) coerce(axLetter + 'anchor');
97+
}
98+
99+
// if you have one coordinate you should have both
100+
Lib.noneOrAll(annIn, annOut, ['x', 'y']);
101+
102+
return annOut;
103+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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 Lib = require('../../lib');
13+
var Axes = require('../../plots/cartesian/axes');
14+
15+
var draw = require('./draw').draw;
16+
17+
18+
module.exports = function calcAutorange(gd) {
19+
var fullLayout = gd._fullLayout,
20+
annotationList = fullLayout.annotations;
21+
22+
if(!annotationList.length || !gd._fullData.length) return;
23+
24+
var annotationAxes = {};
25+
annotationList.forEach(function(ann) {
26+
annotationAxes[ann.xref] = true;
27+
annotationAxes[ann.yref] = true;
28+
});
29+
30+
var autorangedAnnos = Axes.list(gd).filter(function(ax) {
31+
return ax.autorange && annotationAxes[ax._id];
32+
});
33+
if(!autorangedAnnos.length) return;
34+
35+
return Lib.syncOrAsync([
36+
draw,
37+
annAutorange
38+
], gd);
39+
};
40+
41+
function annAutorange(gd) {
42+
var fullLayout = gd._fullLayout;
43+
44+
// find the bounding boxes for each of these annotations'
45+
// relative to their anchor points
46+
// use the arrow and the text bg rectangle,
47+
// as the whole anno may include hidden text in its bbox
48+
fullLayout.annotations.forEach(function(ann) {
49+
var xa = Axes.getFromId(gd, ann.xref),
50+
ya = Axes.getFromId(gd, ann.yref);
51+
52+
if(!(xa || ya)) return;
53+
54+
var halfWidth = (ann._xsize || 0) / 2,
55+
xShift = ann._xshift || 0,
56+
halfHeight = (ann._ysize || 0) / 2,
57+
yShift = ann._yshift || 0,
58+
leftSize = halfWidth - xShift,
59+
rightSize = halfWidth + xShift,
60+
topSize = halfHeight - yShift,
61+
bottomSize = halfHeight + yShift;
62+
63+
if(ann.showarrow) {
64+
var headSize = 3 * ann.arrowsize * ann.arrowwidth;
65+
leftSize = Math.max(leftSize, headSize);
66+
rightSize = Math.max(rightSize, headSize);
67+
topSize = Math.max(topSize, headSize);
68+
bottomSize = Math.max(bottomSize, headSize);
69+
}
70+
71+
if(xa && xa.autorange) {
72+
Axes.expand(xa, [xa.l2c(ann.x)], {
73+
ppadplus: rightSize,
74+
ppadminus: leftSize
75+
});
76+
}
77+
78+
if(ya && ya.autorange) {
79+
Axes.expand(ya, [ya.l2c(ann.y)], {
80+
ppadplus: bottomSize,
81+
ppadminus: topSize
82+
});
83+
}
84+
});
85+
}
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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 handleAnnotationDefaults = require('./annotation_defaults');
13+
14+
15+
module.exports = function supplyLayoutDefaults(layoutIn, layoutOut) {
16+
var containerIn = layoutIn.annotations || [],
17+
containerOut = layoutOut.annotations = [];
18+
19+
for(var i = 0; i < containerIn.length; i++) {
20+
var annIn = containerIn[i] || {},
21+
annOut = handleAnnotationDefaults(annIn, layoutOut);
22+
23+
containerOut.push(annOut);
24+
}
25+
};

0 commit comments

Comments
 (0)