Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Break up annotations and shapes components #833

Merged
merged 5 commits into from
Aug 10, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 103 additions & 0 deletions src/components/annotations/annotation_defaults.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/**
* Copyright 2012-2016, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/


'use strict';

var Lib = require('../../lib');
var Color = require('../color');
var Axes = require('../../plots/cartesian/axes');

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


module.exports = function handleAnnotationDefaults(annIn, fullLayout) {
var annOut = {};

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

coerce('opacity');
coerce('align');
coerce('bgcolor');

var borderColor = coerce('bordercolor'),
borderOpacity = Color.opacity(borderColor);

coerce('borderpad');

var borderWidth = coerce('borderwidth');
var showArrow = coerce('showarrow');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🐄 var. One or the other 😛


if(showArrow) {
coerce('arrowcolor', borderOpacity ? annOut.bordercolor : Color.defaultLine);
coerce('arrowhead');
coerce('arrowsize');
coerce('arrowwidth', ((borderOpacity && borderWidth) || 1) * 2);
coerce('ax');
coerce('ay');
coerce('axref');
coerce('ayref');

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

coerce('text', showArrow ? ' ' : 'new text');
coerce('textangle');
Lib.coerceFont(coerce, 'font', fullLayout.font);

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

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

// TODO: should be refactored in conjunction with Axes axref, ayref
var aaxRef = Axes.coerceARef(annIn, annOut, tdMock, axLetter);

// x, y
var defaultPosition = 0.5;
if(axRef !== 'paper') {
var ax = Axes.getFromId(tdMock, axRef);
defaultPosition = ax.range[0] + defaultPosition * (ax.range[1] - ax.range[0]);

// convert date or category strings to numbers
if(['date', 'category'].indexOf(ax.type) !== -1 &&
typeof annIn[axLetter] === 'string') {
var newval;
if(ax.type === 'date') {
newval = Lib.dateTime2ms(annIn[axLetter]);
if(newval !== false) annIn[axLetter] = newval;

if(aaxRef === axRef) {
var newvalB = Lib.dateTime2ms(annIn['a' + axLetter]);
if(newvalB !== false) annIn['a' + axLetter] = newvalB;
}
}
else if((ax._categories || []).length) {
newval = ax._categories.indexOf(annIn[axLetter]);
if(newval !== -1) annIn[axLetter] = newval;
}
}
}
coerce(axLetter, defaultPosition);

// xanchor, yanchor
if(!showArrow) coerce(axLetter + 'anchor');
}

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

return annOut;
};
85 changes: 85 additions & 0 deletions src/components/annotations/calc_autorange.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/**
* Copyright 2012-2016, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/


'use strict';

var Lib = require('../../lib');
var Axes = require('../../plots/cartesian/axes');

var draw = require('./draw').draw;


module.exports = function calcAutorange(gd) {
var fullLayout = gd._fullLayout,
annotationList = fullLayout.annotations;

if(!annotationList.length || !gd._fullData.length) return;

var annotationAxes = {};
annotationList.forEach(function(ann) {
annotationAxes[ann.xref] = true;
annotationAxes[ann.yref] = true;
});

var autorangedAnnos = Axes.list(gd).filter(function(ax) {
return ax.autorange && annotationAxes[ax._id];
});
if(!autorangedAnnos.length) return;

return Lib.syncOrAsync([
draw,
annAutorange
], gd);
};

function annAutorange(gd) {
var fullLayout = gd._fullLayout;

// find the bounding boxes for each of these annotations'
// relative to their anchor points
// use the arrow and the text bg rectangle,
// as the whole anno may include hidden text in its bbox
fullLayout.annotations.forEach(function(ann) {
var xa = Axes.getFromId(gd, ann.xref),
ya = Axes.getFromId(gd, ann.yref);

if(!(xa || ya)) return;

var halfWidth = (ann._xsize || 0) / 2,
xShift = ann._xshift || 0,
halfHeight = (ann._ysize || 0) / 2,
yShift = ann._yshift || 0,
leftSize = halfWidth - xShift,
rightSize = halfWidth + xShift,
topSize = halfHeight - yShift,
bottomSize = halfHeight + yShift;

if(ann.showarrow) {
var headSize = 3 * ann.arrowsize * ann.arrowwidth;
leftSize = Math.max(leftSize, headSize);
rightSize = Math.max(rightSize, headSize);
topSize = Math.max(topSize, headSize);
bottomSize = Math.max(bottomSize, headSize);
}

if(xa && xa.autorange) {
Axes.expand(xa, [xa.l2c(ann.x)], {
ppadplus: rightSize,
ppadminus: leftSize
});
}

if(ya && ya.autorange) {
Axes.expand(ya, [ya.l2c(ann.y)], {
ppadplus: bottomSize,
ppadminus: topSize
});
}
});
}
25 changes: 25 additions & 0 deletions src/components/annotations/defaults.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Copyright 2012-2016, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/


'use strict';

var handleAnnotationDefaults = require('./annotation_defaults');


module.exports = function supplyLayoutDefaults(layoutIn, layoutOut) {
var containerIn = layoutIn.annotations || [],
containerOut = layoutOut.annotations = [];

for(var i = 0; i < containerIn.length; i++) {
var annIn = containerIn[i] || {},
annOut = handleAnnotationDefaults(annIn, layoutOut);

containerOut.push(annOut);
}
};
Loading