diff --git a/src/components/colorscale/calc.js b/src/components/colorscale/calc.js index b609b38d5da..e887b6820cd 100644 --- a/src/components/colorscale/calc.js +++ b/src/components/colorscale/calc.js @@ -11,11 +11,14 @@ var Lib = require('../../lib'); -var scales = require('./scales'); var flipScale = require('./flip_scale'); -module.exports = function calc(trace, vals, containerStr, cLetter) { +module.exports = function calc(gd, trace, opts) { + var fullLayout = gd._fullLayout; + var vals = opts.vals; + var containerStr = opts.containerStr; + var cLetter = opts.cLetter; var container = trace; var inputContainer = trace._input; var fullInputContainer = trace._fullInput; @@ -84,9 +87,9 @@ module.exports = function calc(trace, vals, containerStr, cLetter) { doUpdate(autoAttr, (auto !== false || (min === undefined && max === undefined))); if(container.autocolorscale) { - if(min * max < 0) scl = scales.RdBu; - else if(min >= 0) scl = scales.Reds; - else scl = scales.Blues; + if(min * max < 0) scl = fullLayout.colorscale.diverging; + else if(min >= 0) scl = fullLayout.colorscale.sequential; + else scl = gd._fullLayout.colorscale.sequentialminus; // reversescale is handled at the containerOut level doUpdate('colorscale', scl, container.reversescale ? flipScale(scl) : scl); diff --git a/src/components/colorscale/index.js b/src/components/colorscale/index.js index 33b0019df61..d17e8f2305b 100644 --- a/src/components/colorscale/index.js +++ b/src/components/colorscale/index.js @@ -9,12 +9,20 @@ 'use strict'; +exports.moduleType = 'component'; + +exports.name = 'colorscale'; + exports.scales = require('./scales'); exports.defaultScale = require('./default_scale'); exports.attributes = require('./attributes'); +exports.layoutAttributes = require('./layout_attributes'); + +exports.supplyLayoutDefaults = require('./layout_defaults'); + exports.handleDefaults = require('./defaults'); exports.calc = require('./calc'); diff --git a/src/components/colorscale/layout_attributes.js b/src/components/colorscale/layout_attributes.js new file mode 100644 index 00000000000..9285951850d --- /dev/null +++ b/src/components/colorscale/layout_attributes.js @@ -0,0 +1,46 @@ +/** +* Copyright 2012-2018, 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 scales = require('./scales'); + +var msg = 'Note that `autocolorscale` must be true for this attribute to work.'; +module.exports = { + editType: 'calc', + sequential: { + valType: 'colorscale', + dflt: scales.Reds, + role: 'style', + editType: 'calc', + description: [ + 'Sets the default sequential colorscale for positive values.', + msg + ].join(' ') + }, + sequentialminus: { + valType: 'colorscale', + dflt: scales.Blues, + role: 'style', + editType: 'calc', + description: [ + 'Sets the default sequential colorscale for negative values.', + msg + ].join(' ') + }, + diverging: { + valType: 'colorscale', + dflt: scales.RdBu, + role: 'style', + editType: 'calc', + description: [ + 'Sets the default diverging colorscale.', + msg + ].join(' ') + } +}; diff --git a/src/components/colorscale/layout_defaults.js b/src/components/colorscale/layout_defaults.js new file mode 100644 index 00000000000..bbc12580614 --- /dev/null +++ b/src/components/colorscale/layout_defaults.js @@ -0,0 +1,26 @@ +/** +* Copyright 2012-2018, 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 colorscaleAttrs = require('./layout_attributes'); +var Template = require('../../plot_api/plot_template'); + +module.exports = function supplyLayoutDefaults(layoutIn, layoutOut) { + var colorscaleIn = layoutIn.colorscale; + var colorscaleOut = Template.newContainer(layoutOut, 'colorscale'); + function coerce(attr, dflt) { + return Lib.coerce(colorscaleIn, colorscaleOut, colorscaleAttrs, attr, dflt); + } + + coerce('sequential'); + coerce('sequentialminus'); + coerce('diverging'); +}; diff --git a/src/core.js b/src/core.js index 1025c37d4a7..6a8ad04bba7 100644 --- a/src/core.js +++ b/src/core.js @@ -53,7 +53,8 @@ register([ require('./components/rangeslider'), require('./components/rangeselector'), require('./components/grid'), - require('./components/errorbars') + require('./components/errorbars'), + require('./components/colorscale') ]); // locales en and en-US are required for default behavior diff --git a/src/plots/layout_attributes.js b/src/plots/layout_attributes.js index 103a0b234f7..d8428800dba 100644 --- a/src/plots/layout_attributes.js +++ b/src/plots/layout_attributes.js @@ -10,6 +10,7 @@ var fontAttrs = require('./font_attributes'); var colorAttrs = require('../components/color/attributes'); +var colorscaleAttrs = require('../components/colorscale/layout_attributes'); var globalFont = fontAttrs({ editType: 'calc', @@ -188,6 +189,7 @@ module.exports = { editType: 'calc', description: 'Sets the default trace colors.' }, + colorscale: colorscaleAttrs, datarevision: { valType: 'any', role: 'info', diff --git a/src/traces/bar/calc.js b/src/traces/bar/calc.js index b3c5775d5e0..6c2ec1ecb19 100644 --- a/src/traces/bar/calc.js +++ b/src/traces/bar/calc.js @@ -42,10 +42,18 @@ module.exports = function calc(gd, trace) { // auto-z and autocolorscale if applicable if(hasColorscale(trace, 'marker')) { - colorscaleCalc(trace, trace.marker.color, 'marker', 'c'); + colorscaleCalc(gd, trace, { + vals: trace.marker.color, + containerStr: 'marker', + cLetter: 'c' + }); } if(hasColorscale(trace, 'marker.line')) { - colorscaleCalc(trace, trace.marker.line.color, 'marker.line', 'c'); + colorscaleCalc(gd, trace, { + vals: trace.marker.line.color, + containerStr: 'marker.line', + cLetter: 'c' + }); } arraysToCalcdata(cd, trace); diff --git a/src/traces/barpolar/calc.js b/src/traces/barpolar/calc.js index 4c9876ef5c2..7181fccca41 100644 --- a/src/traces/barpolar/calc.js +++ b/src/traces/barpolar/calc.js @@ -53,10 +53,18 @@ function calc(gd, trace) { } if(hasColorscale(trace, 'marker')) { - colorscaleCalc(trace, trace.marker.color, 'marker', 'c'); + colorscaleCalc(gd, trace, { + vals: trace.marker.color, + containerStr: 'marker', + cLetter: 'c' + }); } if(hasColorscale(trace, 'marker.line')) { - colorscaleCalc(trace, trace.marker.line.color, 'marker.line', 'c'); + colorscaleCalc(gd, trace, { + vals: trace.marker.line.color, + containerStr: 'marker.line', + cLetter: 'c' + }); } arraysToCalcdata(cd, trace); diff --git a/src/traces/choropleth/calc.js b/src/traces/choropleth/calc.js index b9a88430701..4f71dcb2886 100644 --- a/src/traces/choropleth/calc.js +++ b/src/traces/choropleth/calc.js @@ -30,7 +30,11 @@ module.exports = function calc(gd, trace) { } arraysToCalcdata(calcTrace, trace); - colorscaleCalc(trace, trace.z, '', 'z'); + colorscaleCalc(gd, trace, { + vals: trace.z, + containerStr: '', + cLetter: 'z' + }); calcSelection(calcTrace, trace); return calcTrace; diff --git a/src/traces/cone/calc.js b/src/traces/cone/calc.js index 2c0e5907326..64b36de9453 100644 --- a/src/traces/cone/calc.js +++ b/src/traces/cone/calc.js @@ -34,5 +34,9 @@ module.exports = function calc(gd, trace) { trace._len = len; trace._normMax = normMax; - colorscaleCalc(trace, [normMin, normMax], '', 'c'); + colorscaleCalc(gd, trace, { + vals: [normMin, normMax], + containerStr: '', + cLetter: 'c' + }); }; diff --git a/src/traces/contourcarpet/calc.js b/src/traces/contourcarpet/calc.js index 0e02e252ff9..1a645d5f261 100644 --- a/src/traces/contourcarpet/calc.js +++ b/src/traces/contourcarpet/calc.js @@ -101,7 +101,11 @@ function heatmappishCalc(gd, trace) { if(trace.contours.type === 'levels' && trace.contours.coloring !== 'none') { // auto-z and autocolorscale if applicable - colorscaleCalc(trace, z, '', 'z'); + colorscaleCalc(gd, trace, { + vals: z, + containerStr: '', + cLetter: 'z' + }); } return [cd0]; diff --git a/src/traces/heatmap/calc.js b/src/traces/heatmap/calc.js index 37477db0315..86ebf03aa83 100644 --- a/src/traces/heatmap/calc.js +++ b/src/traces/heatmap/calc.js @@ -146,7 +146,11 @@ module.exports = function calc(gd, trace) { // auto-z and autocolorscale if applicable if(!isContour || trace.contours.type !== 'constraint') { - colorscaleCalc(trace, z, '', 'z'); + colorscaleCalc(gd, trace, { + vals: z, + containerStr: '', + cLetter: 'z' + }); } if(isContour && trace.contours && trace.contours.coloring === 'heatmap') { diff --git a/src/traces/mesh3d/calc.js b/src/traces/mesh3d/calc.js index d5f133ee6b6..ffb18a71b38 100644 --- a/src/traces/mesh3d/calc.js +++ b/src/traces/mesh3d/calc.js @@ -12,6 +12,10 @@ var colorscaleCalc = require('../../components/colorscale/calc'); module.exports = function calc(gd, trace) { if(trace.intensity) { - colorscaleCalc(trace, trace.intensity, '', 'c'); + colorscaleCalc(gd, trace, { + vals: trace.intensity, + containerStr: '', + cLetter: 'c' + }); } }; diff --git a/src/traces/parcats/calc.js b/src/traces/parcats/calc.js index 70b8f8a277f..07ea14d3794 100644 --- a/src/traces/parcats/calc.js +++ b/src/traces/parcats/calc.js @@ -73,7 +73,11 @@ module.exports = function calc(gd, trace) { // Process colorscale if(line) { if(hasColorscale(trace, 'line')) { - colorscaleCalc(trace, trace.line.color, 'line', 'c'); + colorscaleCalc(gd, trace, { + vals: trace.line.color, + containerStr: 'line', + cLetter: 'c' + }); } markerColorscale = Drawing.tryColorscale(line); } else { diff --git a/src/traces/parcoords/calc.js b/src/traces/parcoords/calc.js index d584e66225d..c9ce1f7c7a0 100644 --- a/src/traces/parcoords/calc.js +++ b/src/traces/parcoords/calc.js @@ -19,7 +19,11 @@ module.exports = function calc(gd, trace) { var cscale = cs ? trace.line.colorscale : [[0, trace.line.color], [1, trace.line.color]]; if(hasColorscale(trace, 'line')) { - calcColorscale(trace, color, 'line', 'c'); + calcColorscale(gd, trace, { + vals: color, + containerStr: 'line', + cLetter: 'c' + }); } return wrap({ diff --git a/src/traces/scatter/calc.js b/src/traces/scatter/calc.js index d04c62ca541..d745433dbca 100644 --- a/src/traces/scatter/calc.js +++ b/src/traces/scatter/calc.js @@ -87,7 +87,7 @@ function calc(gd, trace) { } arraysToCalcdata(cd, trace); - calcColorscale(trace); + calcColorscale(gd, trace); calcSelection(cd, trace); if(stackGroupOpts) { diff --git a/src/traces/scatter/colorscale_calc.js b/src/traces/scatter/colorscale_calc.js index 20c52101a83..69d225eb7ae 100644 --- a/src/traces/scatter/colorscale_calc.js +++ b/src/traces/scatter/colorscale_calc.js @@ -15,17 +15,29 @@ var calcColorscale = require('../../components/colorscale/calc'); var subTypes = require('./subtypes'); -module.exports = function calcMarkerColorscale(trace) { +module.exports = function calcMarkerColorscale(gd, trace) { if(subTypes.hasLines(trace) && hasColorscale(trace, 'line')) { - calcColorscale(trace, trace.line.color, 'line', 'c'); + calcColorscale(gd, trace, { + vals: trace.line.color, + containerStr: 'line', + cLetter: 'c' + }); } if(subTypes.hasMarkers(trace)) { if(hasColorscale(trace, 'marker')) { - calcColorscale(trace, trace.marker.color, 'marker', 'c'); + calcColorscale(gd, trace, { + vals: trace.marker.color, + containerStr: 'marker', + cLetter: 'c' + }); } if(hasColorscale(trace, 'marker.line')) { - calcColorscale(trace, trace.marker.line.color, 'marker.line', 'c'); + calcColorscale(gd, trace, { + vals: trace.marker.line.color, + containerStr: 'marker.line', + cLetter: 'c' + }); } } }; diff --git a/src/traces/scatter3d/calc.js b/src/traces/scatter3d/calc.js index e8235d82598..9f2fa358d49 100644 --- a/src/traces/scatter3d/calc.js +++ b/src/traces/scatter3d/calc.js @@ -21,7 +21,7 @@ module.exports = function calc(gd, trace) { var cd = [{x: false, y: false, trace: trace, t: {}}]; arraysToCalcdata(cd, trace); - calcColorscales(trace); + calcColorscales(gd, trace); return cd; }; diff --git a/src/traces/scattercarpet/calc.js b/src/traces/scattercarpet/calc.js index d3ea17ff846..48c1ae81691 100644 --- a/src/traces/scattercarpet/calc.js +++ b/src/traces/scattercarpet/calc.js @@ -50,7 +50,7 @@ module.exports = function calc(gd, trace) { cd[0].trace = trace; calcMarkerSize(trace, serieslen); - calcColorscale(trace); + calcColorscale(gd, trace); arraysToCalcdata(cd, trace); calcSelection(cd, trace); diff --git a/src/traces/scattergeo/calc.js b/src/traces/scattergeo/calc.js index af3af25236b..df31edbe330 100644 --- a/src/traces/scattergeo/calc.js +++ b/src/traces/scattergeo/calc.js @@ -39,7 +39,7 @@ module.exports = function calc(gd, trace) { } arraysToCalcdata(calcTrace, trace); - calcMarkerColorscale(trace); + calcMarkerColorscale(gd, trace); calcSelection(calcTrace, trace); if(len) { diff --git a/src/traces/scattergl/index.js b/src/traces/scattergl/index.js index f6c2ba9be1b..ae16aa7e7fb 100644 --- a/src/traces/scattergl/index.js +++ b/src/traces/scattergl/index.js @@ -84,7 +84,7 @@ function calc(gd, trace) { } // create scene options and scene - calcColorscales(trace); + calcColorscales(gd, trace); var opts = sceneOptions(gd, subplot, trace, positions, x, y); var scene = sceneUpdate(gd, subplot); diff --git a/src/traces/scatterpolar/calc.js b/src/traces/scatterpolar/calc.js index 69d1cd55600..14e70964bb4 100644 --- a/src/traces/scatterpolar/calc.js +++ b/src/traces/scatterpolar/calc.js @@ -45,7 +45,7 @@ module.exports = function calc(gd, trace) { var ppad = calcMarkerSize(trace, len); trace._extremes.x = Axes.findExtremes(radialAxis, rArray, {ppad: ppad}); - calcColorscale(trace); + calcColorscale(gd, trace); arraysToCalcdata(cd, trace); calcSelection(cd, trace); diff --git a/src/traces/scatterpolargl/index.js b/src/traces/scatterpolargl/index.js index 8118e3f2622..630904b668c 100644 --- a/src/traces/scatterpolargl/index.js +++ b/src/traces/scatterpolargl/index.js @@ -38,7 +38,7 @@ function calc(gd, trace) { stash.r = rArray; stash.theta = thetaArray; - calcColorscales(trace); + calcColorscales(gd, trace); // only compute 'style' options in calc, as position options // depend on the radial range and must be set in plot diff --git a/src/traces/scatterternary/calc.js b/src/traces/scatterternary/calc.js index e7a4be1b52e..f9316651016 100644 --- a/src/traces/scatterternary/calc.js +++ b/src/traces/scatterternary/calc.js @@ -72,7 +72,7 @@ module.exports = function calc(gd, trace) { } calcMarkerSize(trace, serieslen); - calcColorscale(trace); + calcColorscale(gd, trace); arraysToCalcdata(cd, trace); calcSelection(cd, trace); diff --git a/src/traces/splom/index.js b/src/traces/splom/index.js index ab74c2a3e79..a8c8cfbeb8d 100644 --- a/src/traces/splom/index.js +++ b/src/traces/splom/index.js @@ -80,7 +80,7 @@ function calc(gd, trace) { } } - calcColorscales(trace); + calcColorscales(gd, trace); Lib.extendFlat(opts, convertMarkerStyle(trace)); var visibleLength = cdata.length; @@ -310,7 +310,7 @@ function editStyle(gd, cd0) { var scene = gd._fullLayout._splomScenes[trace.uid]; if(scene) { - calcColorscales(trace); + calcColorscales(gd, trace); Lib.extendFlat(scene.matrixOptions, convertMarkerStyle(trace)); // TODO [un]selected styles? diff --git a/src/traces/streamtube/calc.js b/src/traces/streamtube/calc.js index 1ba591d3918..c5bc6a20bcf 100644 --- a/src/traces/streamtube/calc.js +++ b/src/traces/streamtube/calc.js @@ -43,7 +43,11 @@ module.exports = function calc(gd, trace) { normMin = Math.min(normMin, norm); } - colorscaleCalc(trace, [normMin, normMax], '', 'c'); + colorscaleCalc(gd, trace, { + vals: [normMin, normMax], + containerStr: '', + cLetter: 'c' + }); var xMax = -Infinity; var xMin = Infinity; diff --git a/src/traces/surface/calc.js b/src/traces/surface/calc.js index e838f898e6d..593cd05ddc6 100644 --- a/src/traces/surface/calc.js +++ b/src/traces/surface/calc.js @@ -15,8 +15,16 @@ var colorscaleCalc = require('../../components/colorscale/calc'); // Compute auto-z and autocolorscale if applicable module.exports = function calc(gd, trace) { if(trace.surfacecolor) { - colorscaleCalc(trace, trace.surfacecolor, '', 'c'); + colorscaleCalc(gd, trace, { + vals: trace.surfacecolor, + containerStr: '', + cLetter: 'c' + }); } else { - colorscaleCalc(trace, trace.z, '', 'c'); + colorscaleCalc(gd, trace, { + vals: trace.z, + containerStr: '', + cLetter: 'c' + }); } }; diff --git a/test/image/baselines/colorscale_template.png b/test/image/baselines/colorscale_template.png new file mode 100644 index 00000000000..be36c46dba0 Binary files /dev/null and b/test/image/baselines/colorscale_template.png differ diff --git a/test/image/mocks/colorscale_template.json b/test/image/mocks/colorscale_template.json new file mode 100644 index 00000000000..e3fa4e38505 --- /dev/null +++ b/test/image/mocks/colorscale_template.json @@ -0,0 +1,161 @@ +{ + "data": [{ + "x": [ + 50076, + -42372, + -19260, + 3852, + 26964, + -65484, + -42372, + -19260, + 3852, + 26964, + -88596, + -65484, + -42372, + -19260, + 3852, + 26964, + 50076, + 73188, + -65484, + -42372, + -19260, + 3852, + 26964, + 50076, + -42372, + -19260, + 3852, + 26964, + -88596, + -65484, + -42372, + -19260, + 3852, + 26964, + 50076, + 73188, + -88596, + -65484, + -42372, + -19260, + 3852, + 26964, + 50076, + 73188 + ], + "y": [ + 51851.8, + 77841.4, + 77841.4, + 77841.4, + 77841.4, + 51851.8, + 51851.8, + 51851.8, + 51851.8, + 51851.8, + -26117, + -26117, + -26117, + -26117, + -26117, + -26117, + -26117, + -26117, + -52106.6, + -52106.6, + -52106.6, + -52106.6, + -52106.6, + -52106.6, + -78096.2, + -78096.2, + -78096.2, + -78096.2, + -127.4, + -127.4, + -127.4, + -127.4, + -127.4, + -127.4, + -127.4, + -127.4, + 25862.2, + 25862.2, + 25862.2, + 25862.2, + 25862.2, + 25862.2, + 25862.2, + 25862.2 + ], + "z": [ + 4.361856, + 4.234497, + 4.321701, + 4.450315, + 4.416136, + 4.210373, + 4.32009, + 4.246728, + 4.293992, + 4.316364, + 3.908434, + 4.433257, + 4.364234, + 4.308714, + 4.275516, + 4.126979, + 4.296483, + 4.320471, + 4.339848, + 4.39907, + 4.345006, + 4.315032, + 4.295618, + 4.262052, + 4.154291, + 4.404264, + 4.33847, + 4.270931, + 4.032226, + 4.381492, + 4.328922, + 4.24046, + 4.349151, + 4.202861, + 4.256402, + 4.28972, + 3.956225, + 4.337909, + 4.31226, + 4.259435, + 4.146854, + 4.235799, + 4.238752, + 4.299876 + ], + "type": "heatmap" + }], + "layout": { + "template": { + "data": { + "heatmap": [{ + "autocolorscale": true + }] + }, + "layout": { + "title": "heatmap template", + "colorscale": { + "sequential": [ + [0, "rgb(0,0,0)"], + [1, "rgb(255,255,255)"] + ] + } + } + } + } +} diff --git a/test/jasmine/assets/supply_defaults.js b/test/jasmine/assets/supply_defaults.js index 249b85cecf1..28bba9b870f 100644 --- a/test/jasmine/assets/supply_defaults.js +++ b/test/jasmine/assets/supply_defaults.js @@ -2,6 +2,10 @@ var Plots = require('@src/plots/plots'); +// The following is used to fill up the Registry module +/* eslint-disable-next-line */ +var Plotly = require('@lib'); + /** * supplyDefaults that fills in necessary _context */ diff --git a/test/jasmine/tests/colorscale_test.js b/test/jasmine/tests/colorscale_test.js index 2bfa115a75a..2721f5710bf 100644 --- a/test/jasmine/tests/colorscale_test.js +++ b/test/jasmine/tests/colorscale_test.js @@ -4,6 +4,18 @@ var Plots = require('@src/plots/plots'); var Heatmap = require('@src/traces/heatmap'); var Scatter = require('@src/traces/scatter'); +var supplyAllDefaults = require('../assets/supply_defaults'); + +function _supply(trace, layout) { + var gd = { + data: [trace], + layout: layout || {} + }; + + supplyAllDefaults(gd); + + return gd; +} describe('Test colorscale:', function() { 'use strict'; @@ -348,39 +360,63 @@ describe('Test colorscale:', function() { handleDefaults(traceIn, traceOut, layout, coerce, opts); expect(traceOut.marker.showscale).toBe(true); }); + }); describe('calc', function() { var calcColorscale = Colorscale.calc; var trace, z; + var gd; beforeEach(function() { trace = {}; z = {}; + gd = {}; }); it('should be RdBuNeg when autocolorscale and z <= 0', function() { trace = { type: 'heatmap', - z: [[0, -1.5], [-2, -10]], + z: [[-0, -1.5], [-2, -10]], autocolorscale: true, _input: {autocolorscale: true} }; - z = [[0, -1.5], [-2, -10]]; - calcColorscale(trace, z, '', 'z'); + gd = _supply(trace); + calcColorscale(gd, trace, {vals: trace.z, containerStr: '', cLetter: 'z'}); expect(trace.autocolorscale).toBe(true); expect(trace.colorscale[5]).toEqual([1, 'rgb(220,220,220)']); }); + it('should be layout.colorscale.sequentialminus when autocolorscale and z <= 0', function() { + var colorscale = [[0, 'rgb(0,0,0)'], [1, 'rgb(255,255,255)']]; + trace = { + type: 'heatmap', + z: [[-0, -1.5], [-2, -10]], + autocolorscale: true, + _input: {autocolorscale: true} + }; + var layout = { + colorscale: { + sequentialminus: colorscale + } + }; + gd = _supply(trace, layout); + calcColorscale(gd, trace, {vals: trace.z, containerStr: '', cLetter: 'z'}); + expect(trace.autocolorscale).toBe(true); + expect(trace.colorscale).toEqual(colorscale); + }); + it('should set autocolorscale to false if it wasn\'t explicitly set true in input', function() { trace = { type: 'heatmap', z: [[0, -1.5], [-2, -10]], + zmin: -10, + zmax: 0, autocolorscale: true, _input: {} }; - z = [[0, -1.5], [-2, -10]]; - calcColorscale(trace, z, '', 'z'); + gd = _supply(trace); + calcColorscale(gd, trace, {vals: trace.z, containerStr: '', cLetter: 'z'}); expect(trace.autocolorscale).toBe(false); expect(trace.colorscale[5]).toEqual([1, 'rgb(220,220,220)']); }); @@ -393,11 +429,32 @@ describe('Test colorscale:', function() { _input: {autocolorscale: true} }; z = [[undefined, undefined], [-0.5, undefined]]; - calcColorscale(trace, z, '', 'z'); + gd = _supply(trace); + calcColorscale(gd, trace, {vals: z, containerStr: '', cLetter: 'z'}); expect(trace.autocolorscale).toBe(true); expect(trace.colorscale[5]).toEqual([1, 'rgb(220,220,220)']); }); + it('should be layout.colorscale.sequentialminus when autocolorscale and the only numerical z <= -0.5', function() { + var colorscale = [[0, 'rgb(0,0,0)'], [1, 'rgb(255,255,255)']]; + trace = { + type: 'heatmap', + z: [['a', 'b'], [-0.5, 'd']], + autocolorscale: true, + _input: {autocolorscale: true} + }; + z = [[undefined, undefined], [-0.5, undefined]]; + var layout = { + colorscale: { + sequentialminus: colorscale + } + }; + gd = _supply(trace, layout); + calcColorscale(gd, trace, {vals: z, containerStr: '', cLetter: 'z'}); + expect(trace.autocolorscale).toBe(true); + expect(trace.colorscale).toEqual(colorscale); + }); + it('should be Reds when the only numerical z >= 0.5', function() { trace = { type: 'heatmap', @@ -406,11 +463,74 @@ describe('Test colorscale:', function() { _input: {autocolorscale: true} }; z = [[undefined, undefined], [0.5, undefined]]; - calcColorscale(trace, z, '', 'z'); + gd = _supply(trace); + calcColorscale(gd, trace, {vals: z, containerStr: '', cLetter: 'z'}); expect(trace.autocolorscale).toBe(true); expect(trace.colorscale[0]).toEqual([0, 'rgb(220,220,220)']); }); + it('should be layout.colorscale.sequential when autocolorscale and the only numerical z >= 0.5', function() { + var colorscale = [[0, 'rgb(0,0,0)'], [1, 'rgb(255,255,255)']]; + trace = { + type: 'heatmap', + z: [['a', 'b'], [0.5, 'd']], + autocolorscale: true, + _input: {autocolorscale: true} + }; + z = [[undefined, undefined], [0.5, undefined]]; + var layout = { + colorscale: { + sequential: colorscale + } + }; + gd = _supply(trace, layout); + calcColorscale(gd, trace, {vals: z, containerStr: '', cLetter: 'z'}); + expect(trace.autocolorscale).toBe(true); + expect(trace.colorscale).toEqual(colorscale); + }); + + it('should be layout.colorscale.diverging when autocolorscale and there are positive and negative values', function() { + var colorscale = [[0, 'rgb(0,0,0)'], [1, 'rgb(255,255,255)']]; + trace = { + type: 'heatmap', + z: [[-1.0, 'b'], [0.5, 'd']], + autocolorscale: true, + _input: {autocolorscale: true} + }; + z = [[-1.0, undefined], [0.5, undefined]]; + var layout = { + colorscale: { + diverging: colorscale + } + }; + gd = _supply(trace, layout); + calcColorscale(gd, trace, {vals: z, containerStr: '', cLetter: 'z'}); + expect(trace.autocolorscale).toBe(true); + expect(trace.colorscale).toEqual(colorscale); + }); + + it('should ignore layout.colorscale.diverging when colorscale is defined at trace-level', function() { + var colorscale = [[0, 'rgb(0,0,0)'], [1, 'rgb(255,255,255)']]; + var layoutColorscale = [[0, 'rgb(0,0,0)'], [1, 'rgb(255,255,255)']]; + trace = { + type: 'heatmap', + z: [[-1.0, 'b'], [0.5, 'd']], + autocolorscale: true, + _input: {autocolorscale: true}, + colorscale: colorscale + }; + z = [[-1.0, undefined], [0.5, undefined]]; + var layout = { + colorscale: { + diverging: layoutColorscale + } + }; + gd = _supply(trace, layout); + calcColorscale(gd, trace, {vals: z, containerStr: '', cLetter: 'z'}); + expect(trace.autocolorscale).toBe(true); + expect(trace.colorscale).toEqual(colorscale); + }); + it('should be reverse the auto scale when reversescale is true', function() { trace = { type: 'heatmap', @@ -420,7 +540,8 @@ describe('Test colorscale:', function() { _input: {autocolorscale: true} }; z = [[undefined, undefined], [0.5, undefined]]; - calcColorscale(trace, z, '', 'z'); + gd = _supply(trace); + calcColorscale(gd, trace, {vals: z, containerStr: '', cLetter: 'z'}); expect(trace.autocolorscale).toBe(true); expect(trace.colorscale[trace.colorscale.length - 1]) .toEqual([1, 'rgb(220,220,220)']);