Skip to content

Commit fc0a1f8

Browse files
authored
Merge pull request #2761 from plotly/plotlyjs_templating
Templates
2 parents c2457ff + 8598bc9 commit fc0a1f8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

79 files changed

+2408
-932
lines changed

devtools/test_dashboard/perf.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ window.timeit = function(f, n, nchunk, arg) {
3939

4040
var first = (times[0]).toFixed(4);
4141
var last = (times[n - 1]).toFixed(4);
42-
times.sort();
42+
times.sort(function(a, b) { return a - b; });
4343
var min = (times[0]).toFixed(4);
4444
var max = (times[n - 1]).toFixed(4);
4545
var median = (times[Math.min(Math.ceil(n / 2), n - 1)]).toFixed(4);

src/components/annotations/annotation_defaults.js

-95
This file was deleted.

src/components/annotations/attributes.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,10 @@
1111
var ARROWPATHS = require('./arrow_paths');
1212
var fontAttrs = require('../../plots/font_attributes');
1313
var cartesianConstants = require('../../plots/cartesian/constants');
14+
var templatedArray = require('../../plot_api/plot_template').templatedArray;
1415

1516

16-
module.exports = {
17-
_isLinkedToArray: 'annotation',
18-
17+
module.exports = templatedArray('annotation', {
1918
visible: {
2019
valType: 'boolean',
2120
role: 'info',
@@ -543,4 +542,4 @@ module.exports = {
543542
].join(' ')
544543
}
545544
}
546-
};
545+
});

src/components/annotations/click.js

+14-7
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88

99
'use strict';
1010

11+
var Lib = require('../../lib');
1112
var Registry = require('../../registry');
13+
var arrayEditor = require('../../plot_api/plot_template').arrayEditor;
1214

1315
module.exports = {
1416
hasClickToShow: hasClickToShow,
@@ -41,20 +43,25 @@ function hasClickToShow(gd, hoverData) {
4143
* returns: Promise that the update is complete
4244
*/
4345
function onClick(gd, hoverData) {
44-
var toggleSets = getToggleSets(gd, hoverData),
45-
onSet = toggleSets.on,
46-
offSet = toggleSets.off.concat(toggleSets.explicitOff),
47-
update = {},
48-
i;
46+
var toggleSets = getToggleSets(gd, hoverData);
47+
var onSet = toggleSets.on;
48+
var offSet = toggleSets.off.concat(toggleSets.explicitOff);
49+
var update = {};
50+
var annotationsOut = gd._fullLayout.annotations;
51+
var i, editHelpers;
4952

5053
if(!(onSet.length || offSet.length)) return;
5154

5255
for(i = 0; i < onSet.length; i++) {
53-
update['annotations[' + onSet[i] + '].visible'] = true;
56+
editHelpers = arrayEditor(gd.layout, 'annotations', annotationsOut[onSet[i]]);
57+
editHelpers.modifyItem('visible', true);
58+
Lib.extendFlat(update, editHelpers.getUpdateObj());
5459
}
5560

5661
for(i = 0; i < offSet.length; i++) {
57-
update['annotations[' + offSet[i] + '].visible'] = false;
62+
editHelpers = arrayEditor(gd.layout, 'annotations', annotationsOut[offSet[i]]);
63+
editHelpers.modifyItem('visible', false);
64+
Lib.extendFlat(update, editHelpers.getUpdateObj());
5865
}
5966

6067
return Registry.call('update', gd, {}, update);

src/components/annotations/defaults.js

+81-5
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,91 @@
99

1010
'use strict';
1111

12+
var Lib = require('../../lib');
13+
var Axes = require('../../plots/cartesian/axes');
1214
var handleArrayContainerDefaults = require('../../plots/array_container_defaults');
13-
var handleAnnotationDefaults = require('./annotation_defaults');
15+
16+
var handleAnnotationCommonDefaults = require('./common_defaults');
17+
var attributes = require('./attributes');
1418

1519

1620
module.exports = function supplyLayoutDefaults(layoutIn, layoutOut) {
17-
var opts = {
21+
handleArrayContainerDefaults(layoutIn, layoutOut, {
1822
name: 'annotations',
1923
handleItemDefaults: handleAnnotationDefaults
20-
};
21-
22-
handleArrayContainerDefaults(layoutIn, layoutOut, opts);
24+
});
2325
};
26+
27+
function handleAnnotationDefaults(annIn, annOut, fullLayout) {
28+
function coerce(attr, dflt) {
29+
return Lib.coerce(annIn, annOut, attributes, attr, dflt);
30+
}
31+
32+
var visible = coerce('visible');
33+
var clickToShow = coerce('clicktoshow');
34+
35+
if(!(visible || clickToShow)) return;
36+
37+
handleAnnotationCommonDefaults(annIn, annOut, fullLayout, coerce);
38+
39+
var showArrow = annOut.showarrow;
40+
41+
// positioning
42+
var axLetters = ['x', 'y'],
43+
arrowPosDflt = [-10, -30],
44+
gdMock = {_fullLayout: fullLayout};
45+
for(var i = 0; i < 2; i++) {
46+
var axLetter = axLetters[i];
47+
48+
// xref, yref
49+
var axRef = Axes.coerceRef(annIn, annOut, gdMock, axLetter, '', 'paper');
50+
51+
// x, y
52+
Axes.coercePosition(annOut, gdMock, coerce, axRef, axLetter, 0.5);
53+
54+
if(showArrow) {
55+
var arrowPosAttr = 'a' + axLetter,
56+
// axref, ayref
57+
aaxRef = Axes.coerceRef(annIn, annOut, gdMock, arrowPosAttr, 'pixel');
58+
59+
// for now the arrow can only be on the same axis or specified as pixels
60+
// TODO: sometime it might be interesting to allow it to be on *any* axis
61+
// but that would require updates to drawing & autorange code and maybe more
62+
if(aaxRef !== 'pixel' && aaxRef !== axRef) {
63+
aaxRef = annOut[arrowPosAttr] = 'pixel';
64+
}
65+
66+
// ax, ay
67+
var aDflt = (aaxRef === 'pixel') ? arrowPosDflt[i] : 0.4;
68+
Axes.coercePosition(annOut, gdMock, coerce, aaxRef, arrowPosAttr, aDflt);
69+
}
70+
71+
// xanchor, yanchor
72+
coerce(axLetter + 'anchor');
73+
74+
// xshift, yshift
75+
coerce(axLetter + 'shift');
76+
}
77+
78+
// if you have one coordinate you should have both
79+
Lib.noneOrAll(annIn, annOut, ['x', 'y']);
80+
81+
// if you have one part of arrow length you should have both
82+
if(showArrow) {
83+
Lib.noneOrAll(annIn, annOut, ['ax', 'ay']);
84+
}
85+
86+
if(clickToShow) {
87+
var xClick = coerce('xclick');
88+
var yClick = coerce('yclick');
89+
90+
// put the actual click data to bind to into private attributes
91+
// so we don't have to do this little bit of logic on every hover event
92+
annOut._xclick = (xClick === undefined) ?
93+
annOut.x :
94+
Axes.cleanPosition(xClick, gdMock, annOut.xref);
95+
annOut._yclick = (yClick === undefined) ?
96+
annOut.y :
97+
Axes.cleanPosition(yClick, gdMock, annOut.yref);
98+
}
99+
}

0 commit comments

Comments
 (0)