|
| 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 | +}; |
0 commit comments