Skip to content

Commit

Permalink
Merge pull request #2761 from plotly/plotlyjs_templating
Browse files Browse the repository at this point in the history
Templates
  • Loading branch information
alexcjohnson committed Jul 3, 2018
2 parents c2457ff + 8598bc9 commit fc0a1f8
Show file tree
Hide file tree
Showing 79 changed files with 2,408 additions and 932 deletions.
2 changes: 1 addition & 1 deletion devtools/test_dashboard/perf.js
Expand Up @@ -39,7 +39,7 @@ window.timeit = function(f, n, nchunk, arg) {

var first = (times[0]).toFixed(4);
var last = (times[n - 1]).toFixed(4);
times.sort();
times.sort(function(a, b) { return a - b; });
var min = (times[0]).toFixed(4);
var max = (times[n - 1]).toFixed(4);
var median = (times[Math.min(Math.ceil(n / 2), n - 1)]).toFixed(4);
Expand Down
95 changes: 0 additions & 95 deletions src/components/annotations/annotation_defaults.js

This file was deleted.

7 changes: 3 additions & 4 deletions src/components/annotations/attributes.js
Expand Up @@ -11,11 +11,10 @@
var ARROWPATHS = require('./arrow_paths');
var fontAttrs = require('../../plots/font_attributes');
var cartesianConstants = require('../../plots/cartesian/constants');
var templatedArray = require('../../plot_api/plot_template').templatedArray;


module.exports = {
_isLinkedToArray: 'annotation',

module.exports = templatedArray('annotation', {
visible: {
valType: 'boolean',
role: 'info',
Expand Down Expand Up @@ -543,4 +542,4 @@ module.exports = {
].join(' ')
}
}
};
});
21 changes: 14 additions & 7 deletions src/components/annotations/click.js
Expand Up @@ -8,7 +8,9 @@

'use strict';

var Lib = require('../../lib');
var Registry = require('../../registry');
var arrayEditor = require('../../plot_api/plot_template').arrayEditor;

module.exports = {
hasClickToShow: hasClickToShow,
Expand Down Expand Up @@ -41,20 +43,25 @@ function hasClickToShow(gd, hoverData) {
* returns: Promise that the update is complete
*/
function onClick(gd, hoverData) {
var toggleSets = getToggleSets(gd, hoverData),
onSet = toggleSets.on,
offSet = toggleSets.off.concat(toggleSets.explicitOff),
update = {},
i;
var toggleSets = getToggleSets(gd, hoverData);
var onSet = toggleSets.on;
var offSet = toggleSets.off.concat(toggleSets.explicitOff);
var update = {};
var annotationsOut = gd._fullLayout.annotations;
var i, editHelpers;

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

for(i = 0; i < onSet.length; i++) {
update['annotations[' + onSet[i] + '].visible'] = true;
editHelpers = arrayEditor(gd.layout, 'annotations', annotationsOut[onSet[i]]);
editHelpers.modifyItem('visible', true);
Lib.extendFlat(update, editHelpers.getUpdateObj());
}

for(i = 0; i < offSet.length; i++) {
update['annotations[' + offSet[i] + '].visible'] = false;
editHelpers = arrayEditor(gd.layout, 'annotations', annotationsOut[offSet[i]]);
editHelpers.modifyItem('visible', false);
Lib.extendFlat(update, editHelpers.getUpdateObj());
}

return Registry.call('update', gd, {}, update);
Expand Down
86 changes: 81 additions & 5 deletions src/components/annotations/defaults.js
Expand Up @@ -9,15 +9,91 @@

'use strict';

var Lib = require('../../lib');
var Axes = require('../../plots/cartesian/axes');
var handleArrayContainerDefaults = require('../../plots/array_container_defaults');
var handleAnnotationDefaults = require('./annotation_defaults');

var handleAnnotationCommonDefaults = require('./common_defaults');
var attributes = require('./attributes');


module.exports = function supplyLayoutDefaults(layoutIn, layoutOut) {
var opts = {
handleArrayContainerDefaults(layoutIn, layoutOut, {
name: 'annotations',
handleItemDefaults: handleAnnotationDefaults
};

handleArrayContainerDefaults(layoutIn, layoutOut, opts);
});
};

function handleAnnotationDefaults(annIn, annOut, fullLayout) {
function coerce(attr, dflt) {
return Lib.coerce(annIn, annOut, attributes, attr, dflt);
}

var visible = coerce('visible');
var clickToShow = coerce('clicktoshow');

if(!(visible || clickToShow)) return;

handleAnnotationCommonDefaults(annIn, annOut, fullLayout, coerce);

var showArrow = annOut.showarrow;

// positioning
var axLetters = ['x', 'y'],
arrowPosDflt = [-10, -30],
gdMock = {_fullLayout: fullLayout};
for(var i = 0; i < 2; i++) {
var axLetter = axLetters[i];

// xref, yref
var axRef = Axes.coerceRef(annIn, annOut, gdMock, axLetter, '', 'paper');

// x, y
Axes.coercePosition(annOut, gdMock, coerce, axRef, axLetter, 0.5);

if(showArrow) {
var arrowPosAttr = 'a' + axLetter,
// axref, ayref
aaxRef = Axes.coerceRef(annIn, annOut, gdMock, arrowPosAttr, 'pixel');

// for now the arrow can only be on the same axis or specified as pixels
// TODO: sometime it might be interesting to allow it to be on *any* axis
// but that would require updates to drawing & autorange code and maybe more
if(aaxRef !== 'pixel' && aaxRef !== axRef) {
aaxRef = annOut[arrowPosAttr] = 'pixel';
}

// ax, ay
var aDflt = (aaxRef === 'pixel') ? arrowPosDflt[i] : 0.4;
Axes.coercePosition(annOut, gdMock, coerce, aaxRef, arrowPosAttr, aDflt);
}

// xanchor, yanchor
coerce(axLetter + 'anchor');

// xshift, yshift
coerce(axLetter + 'shift');
}

// if you have one coordinate you should have both
Lib.noneOrAll(annIn, annOut, ['x', 'y']);

// if you have one part of arrow length you should have both
if(showArrow) {
Lib.noneOrAll(annIn, annOut, ['ax', 'ay']);
}

if(clickToShow) {
var xClick = coerce('xclick');
var yClick = coerce('yclick');

// put the actual click data to bind to into private attributes
// so we don't have to do this little bit of logic on every hover event
annOut._xclick = (xClick === undefined) ?
annOut.x :
Axes.cleanPosition(xClick, gdMock, annOut.xref);
annOut._yclick = (yClick === undefined) ?
annOut.y :
Axes.cleanPosition(yClick, gdMock, annOut.yref);
}
}

0 comments on commit fc0a1f8

Please sign in to comment.