|
| 1 | +/** |
| 2 | +* Copyright 2012-2015, 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 isNumeric = require('fast-isnumeric'); |
| 13 | + |
| 14 | +var Lib = require('../../lib'); |
| 15 | + |
| 16 | +var hasColorbar = require('../colorbar/has_colorbar'); |
| 17 | +var colorbarDefaults = require('../colorbar/defaults'); |
| 18 | +var isValidScale = require('./is_valid_scale'); |
| 19 | +var flipScale = require('./flip_scale'); |
| 20 | + |
| 21 | + |
| 22 | +module.exports = function colorScaleDefaults(traceIn, traceOut, layout, coerce, opts) { |
| 23 | + var prefix = opts.prefix, |
| 24 | + cLetter = opts.cLetter, |
| 25 | + containerStr = prefix.slice(0, prefix.length-1), |
| 26 | + containerIn = prefix ? |
| 27 | + Lib.nestedProperty(traceIn, containerStr).get() || {} : |
| 28 | + traceIn, |
| 29 | + containerOut = prefix ? |
| 30 | + Lib.nestedProperty(traceOut, containerStr).get() || {} : |
| 31 | + traceOut, |
| 32 | + minIn = containerIn[cLetter + 'min'], |
| 33 | + maxIn = containerIn[cLetter + 'max'], |
| 34 | + sclIn = containerIn.colorscale; |
| 35 | + |
| 36 | + var validMinMax = isNumeric(minIn) && isNumeric(maxIn) && (minIn < maxIn); |
| 37 | + coerce(prefix + cLetter + 'auto', !validMinMax); |
| 38 | + coerce(prefix + cLetter + 'min'); |
| 39 | + coerce(prefix + cLetter + 'max'); |
| 40 | + |
| 41 | + // handles both the trace case (autocolorscale is false by default) and |
| 42 | + // the marker and marker.line case (autocolorscale is true by default) |
| 43 | + var autoColorscaleDftl; |
| 44 | + if(sclIn!==undefined) autoColorscaleDftl = !isValidScale(sclIn); |
| 45 | + coerce(prefix + 'autocolorscale', autoColorscaleDftl); |
| 46 | + var sclOut = coerce(prefix + 'colorscale'); |
| 47 | + |
| 48 | + // reversescale is handled at the containerOut level |
| 49 | + var reverseScale = coerce(prefix + 'reversescale'); |
| 50 | + if(reverseScale) containerOut.colorscale = flipScale(sclOut); |
| 51 | + |
| 52 | + // ... until Scatter.colorbar can handle marker line colorbars |
| 53 | + if(prefix === 'marker.line.') return; |
| 54 | + |
| 55 | + // handle both the trace case where the dflt is listed in attributes and |
| 56 | + // the marker case where the dflt is determined by hasColorbar |
| 57 | + var showScaleDftl; |
| 58 | + if(prefix) showScaleDftl = hasColorbar(containerIn); |
| 59 | + var showScale = coerce(prefix + 'showscale', showScaleDftl); |
| 60 | + |
| 61 | + if(showScale) colorbarDefaults(containerIn, containerOut, layout); |
| 62 | +}; |
0 commit comments