|
| 1 | +/** |
| 2 | +* Copyright 2012-2017, 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 id2name = require('./axis_ids').id2name; |
| 14 | + |
| 15 | + |
| 16 | +module.exports = function handleConstraintDefaults(containerIn, containerOut, coerce, counterAxes, layoutOut) { |
| 17 | + var constraintGroups = layoutOut._axisConstraintGroups; |
| 18 | + |
| 19 | + if(!containerIn.scalewith) return; |
| 20 | + |
| 21 | + var constraintOpts = getConstraintOpts(constraintGroups, containerOut._id, counterAxes, layoutOut); |
| 22 | + |
| 23 | + var scalewith = Lib.coerce(containerIn, containerOut, { |
| 24 | + scalewith: { |
| 25 | + valType: 'enumerated', |
| 26 | + values: constraintOpts.linkableAxes |
| 27 | + } |
| 28 | + }, 'scalewith'); |
| 29 | + |
| 30 | + if(scalewith) { |
| 31 | + var scaleratio = coerce('scaleratio'); |
| 32 | + // TODO: I suppose I could do attribute.min: Number.MIN_VALUE to avoid zero, |
| 33 | + // but that seems hacky. Better way to say "must be a positive number"? |
| 34 | + // Of course if you use several super-tiny values you could eventually |
| 35 | + // force a product of these to zero and all hell would break loose... |
| 36 | + // Likewise with super-huge values. |
| 37 | + if(!scaleratio) scaleratio = containerOut.scaleratio = 1; |
| 38 | + |
| 39 | + updateConstraintGroups(constraintGroups, constraintOpts.thisGroup, |
| 40 | + containerOut._id, scalewith, scaleratio); |
| 41 | + } |
| 42 | + else if(counterAxes.indexOf(containerIn.scalewith) !== -1) { |
| 43 | + Lib.warn('ignored ' + containerOut._name + '.scalewith: "' + |
| 44 | + containerIn.scalewith + '" to avoid an infinite loop ' + |
| 45 | + 'and possibly inconsistent scaleratios.'); |
| 46 | + } |
| 47 | +}; |
| 48 | + |
| 49 | +function getConstraintOpts(constraintGroups, thisID, counterAxes, layoutOut) { |
| 50 | + // If this axis is already part of a constraint group, we can't |
| 51 | + // scalewith any other axis in that group, or we'd make a loop. |
| 52 | + // Filter counterAxes to enforce this, also matching axis types. |
| 53 | + |
| 54 | + var thisType = layoutOut[id2name(thisID)].type; |
| 55 | + |
| 56 | + var i, j, idj; |
| 57 | + for(i = 0; i < constraintGroups.length; i++) { |
| 58 | + if(constraintGroups[i][thisID]) { |
| 59 | + var thisGroup = constraintGroups[i]; |
| 60 | + |
| 61 | + var linkableAxes = []; |
| 62 | + for(j = 0; j < counterAxes.length; j++) { |
| 63 | + idj = counterAxes[j]; |
| 64 | + if(!thisGroup[idj] && layoutOut[id2name(idj)].type === thisType) { |
| 65 | + linkableAxes.push(idj); |
| 66 | + } |
| 67 | + } |
| 68 | + return {linkableAxes: linkableAxes, thisGroup: thisGroup}; |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + return {linkableAxes: counterAxes, thisGroup: null}; |
| 73 | +} |
| 74 | + |
| 75 | + |
| 76 | +/* |
| 77 | + * Add this axis to the axis constraint groups, which is the collection |
| 78 | + * of axes that are all constrained together on scale. |
| 79 | + * |
| 80 | + * constraintGroups: a list of objects. each object is |
| 81 | + * {axis_id: scale_within_group}, where scale_within_group is |
| 82 | + * only important relative to the rest of the group, and defines |
| 83 | + * the relative scales between all axes in the group |
| 84 | + * |
| 85 | + * thisGroup: the group the current axis is already in |
| 86 | + * thisID: the id if the current axis |
| 87 | + * scalewith: the id of the axis to scale it with |
| 88 | + * scaleratio: the ratio of this axis to the scalewith axis |
| 89 | + */ |
| 90 | +function updateConstraintGroups(constraintGroups, thisGroup, thisID, scalewith, scaleratio) { |
| 91 | + var i, j, groupi, keyj, thisGroupIndex; |
| 92 | + |
| 93 | + if(thisGroup === null) { |
| 94 | + thisGroup = {}; |
| 95 | + thisGroup[thisID] = 1; |
| 96 | + thisGroupIndex = constraintGroups.length; |
| 97 | + constraintGroups.push(thisGroup); |
| 98 | + } |
| 99 | + else { |
| 100 | + thisGroupIndex = constraintGroups.indexOf(thisGroup); |
| 101 | + } |
| 102 | + |
| 103 | + var thisGroupKeys = Object.keys(thisGroup); |
| 104 | + |
| 105 | + // we know that this axis isn't in any other groups, but we don't know |
| 106 | + // about the scalewith axis. If it is, we need to merge the groups. |
| 107 | + for(i = 0; i < constraintGroups.length; i++) { |
| 108 | + groupi = constraintGroups[i]; |
| 109 | + if(i !== thisGroupIndex && groupi[scalewith]) { |
| 110 | + var baseScale = groupi[scalewith]; |
| 111 | + for(j = 0; j < thisGroupKeys.length; j++) { |
| 112 | + keyj = thisGroupKeys[j]; |
| 113 | + groupi[keyj] = baseScale * scaleratio * thisGroup[keyj]; |
| 114 | + } |
| 115 | + constraintGroups.splice(thisGroupIndex, 1); |
| 116 | + return; |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + // otherwise, we insert the new scalewith axis as the base scale (1) |
| 121 | + // in its group, and scale the rest of the group to it |
| 122 | + if(scaleratio !== 1) { |
| 123 | + for(j = 0; j < thisGroupKeys.length; j++) { |
| 124 | + thisGroup[thisGroupKeys[j]] *= scaleratio; |
| 125 | + } |
| 126 | + } |
| 127 | + thisGroup[scalewith] = 1; |
| 128 | +} |
0 commit comments