-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
27bb36d
cleanup: break up annotations into files
etpinard bc4069c
cleanup: break shapes into files
etpinard 2b632e1
dry up shape test suite
etpinard 0503901
test: add relayout tests for annotations
etpinard a5dbf44
sub Plotly[module]. --> [module].
etpinard File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
|
||
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}); | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 😛