From dc0e952a66d099193201427f4056436ac42f7522 Mon Sep 17 00:00:00 2001 From: bbm Date: Wed, 8 Jul 2026 16:31:29 -0400 Subject: [PATCH 01/15] add support for 'type' attribute on colorbar, with enum values (default) 'linear' and 'log' --- src/components/colorbar/attributes.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/components/colorbar/attributes.js b/src/components/colorbar/attributes.js index f05909b244d..6d755fc1da9 100644 --- a/src/components/colorbar/attributes.js +++ b/src/components/colorbar/attributes.js @@ -33,6 +33,16 @@ module.exports = overrideAll( 'This measure excludes the size of the padding, ticks and labels.' ].join(' ') }, + type: { + valType: 'enumerated', + values: ['linear', 'log'], + dflt: 'linear', + editType: 'calc', + description: [ + 'Sets the colorbar axis type.', + 'If `"log"`, the colorscale and colorbar will be configured for logarithmic data.' + ].join(' ') + }, lenmode: { valType: 'enumerated', values: ['fraction', 'pixels'], From b6d212fdbee706bf06d8190c60cfa71241cc5747 Mon Sep 17 00:00:00 2001 From: bbm Date: Wed, 8 Jul 2026 16:31:50 -0400 Subject: [PATCH 02/15] coerce new 'type' attribute --- src/components/colorbar/defaults.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/components/colorbar/defaults.js b/src/components/colorbar/defaults.js index 2fab3cd5209..f703509894a 100644 --- a/src/components/colorbar/defaults.js +++ b/src/components/colorbar/defaults.js @@ -22,6 +22,7 @@ module.exports = function colorbarDefaults(containerIn, containerOut, layout) { var w = layout.width - margin.l - margin.r; var h = layout.height - margin.t - margin.b; + coerce('type'); var orientation = coerce('orientation'); var isVertical = orientation === 'v'; From d6a49b7ad01c442f672ab16da1e04d34bde5c4f8 Mon Sep 17 00:00:00 2001 From: bbm Date: Wed, 8 Jul 2026 16:32:38 -0400 Subject: [PATCH 03/15] transform the range for 'log' colorbar --- src/components/colorbar/draw.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/components/colorbar/draw.js b/src/components/colorbar/draw.js index 356bcdda698..3a3096c46b6 100644 --- a/src/components/colorbar/draw.js +++ b/src/components/colorbar/draw.js @@ -955,8 +955,9 @@ function mockColorBarAxis(gd, opts, zrange) { var isVertical = opts.orientation === 'v'; var cbAxisIn = { - type: 'linear', - range: zrange, + type: opts.type || 'linear', + range: opts.type === 'log' ? + [Math.log10(zrange[0]), Math.log10(zrange[1])] : zrange, tickmode: opts.tickmode, nticks: opts.nticks, tick0: opts.tick0, @@ -993,7 +994,7 @@ function mockColorBarAxis(gd, opts, zrange) { var letter = isVertical ? 'y' : 'x'; var cbAxisOut = { - type: 'linear', + type: cbAxisIn.type, _id: letter + opts._id }; From 4709876f0f2f9dab64d0de051ff90c0ddc93b410 Mon Sep 17 00:00:00 2001 From: bbm Date: Wed, 8 Jul 2026 16:34:14 -0400 Subject: [PATCH 04/15] check for log type in makeColorScaleFuncFromTrac and extractScale --- src/components/colorscale/helpers.js | 36 ++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/src/components/colorscale/helpers.js b/src/components/colorscale/helpers.js index 9b6c31eb699..90f7039d9b8 100644 --- a/src/components/colorscale/helpers.js +++ b/src/components/colorscale/helpers.js @@ -116,6 +116,15 @@ function extractScale(cont) { var cmin = cOpts.min; var cmax = cOpts.max; + // Check if a log type is defined on the colorbar + var type = (cOpts.colorbar && cOpts.colorbar.type) || 'linear'; + + // Convert domain boundaries to base-10 log if required + if(type === 'log') { + if(cmin > 0) cmin = Math.log10(cmin); + if(cmax > 0) cmax = Math.log10(cmax); + } + var scl = cOpts.reversescale ? flipScale(cOpts.colorscale) : cOpts.colorscale; @@ -206,7 +215,30 @@ function makeColorScaleFunc(specs, opts) { } function makeColorScaleFuncFromTrace(trace, opts) { - return makeColorScaleFunc(extractScale(trace), opts); + var baseColorFn = makeColorScaleFunc(extractScale(trace), opts); + var cOpts = extractOpts(trace); + var type = (cOpts.colorbar && cOpts.colorbar.type) || 'linear'; + + // Wrap the base generator if we need to dynamically apply log transformations + if (type === 'log') { + var wrappedFunc = function(v) { + if (isNumeric(v)) { + if (v <= 0) return 'rgba(0,0,0,0)'; // Logarithm of zero/negative is undefined + return baseColorFn(Math.log10(v)); + } else if (tinycolor(v).isValid()) { + return v; + } else { + return Color.defaultLine; + } + }; + + // Preserve native domain and range methods for the draw components + wrappedFunc.domain = baseColorFn.domain; + wrappedFunc.range = baseColorFn.range; + return wrappedFunc; + } + + return baseColorFn; } module.exports = { @@ -216,4 +248,4 @@ module.exports = { flipScale: flipScale, makeColorScaleFunc: makeColorScaleFunc, makeColorScaleFuncFromTrace: makeColorScaleFuncFromTrace -}; +}; \ No newline at end of file From 81d81b66904b5b99923cc4e1bbd4426c87a97c55 Mon Sep 17 00:00:00 2001 From: bbm Date: Wed, 8 Jul 2026 16:35:08 -0400 Subject: [PATCH 05/15] check that new 'type' attribute on colorbar is properly validated --- test/jasmine/tests/validate_test.js | 30 +++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/test/jasmine/tests/validate_test.js b/test/jasmine/tests/validate_test.js index b8577e07efe..473eccdbec7 100644 --- a/test/jasmine/tests/validate_test.js +++ b/test/jasmine/tests/validate_test.js @@ -820,4 +820,34 @@ describe('Plotly.validate', function () { ]); expect(out).toBeUndefined(); }); + + describe('colorbar type validation', function() { + it('should accept valid colorbar types', function() { + var trace = { + type: 'heatmap', + z: [[1, 10], [100, 1000]], + colorbar: { + type: 'log' + } + }; + + // validate returns an array of errors. An empty array means success. + var out = Plotly.validate([trace]); + expect(out).toBeUndefined(); + }); + + it('should reject invalid colorbar types', function() { + var trace = { + type: 'heatmap', + z: [[1, 2], [3, 4]], + colorbar: { + type: 'exponential' // not in our enumerated list + } + }; + + var out = Plotly.validate([trace]); + expect(out.length).toEqual(1); + expect(out[0].msg).toEqual('In data trace 0, key colorbar.type is set to an invalid value (exponential)'); + }); + }); }); From 2a85b72601bc3477c7ad2b3dd234dd9d556503f2 Mon Sep 17 00:00:00 2001 From: bbm Date: Wed, 8 Jul 2026 16:35:52 -0400 Subject: [PATCH 06/15] check color mappings of new log colorbar --- test/jasmine/tests/colorscale_test.js | 68 +++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/test/jasmine/tests/colorscale_test.js b/test/jasmine/tests/colorscale_test.js index 4957690b7c6..54661e38509 100644 --- a/test/jasmine/tests/colorscale_test.js +++ b/test/jasmine/tests/colorscale_test.js @@ -1226,4 +1226,72 @@ describe('Test colorscale restyle calls:', function() { }) .then(done, done.fail); }); + + describe('helpers log mapping', function() { + var makeColorScaleFuncFromTrace = Colorscale.makeColorScaleFuncFromTrace; + + it('should map domain values logarithmically to the color gradient when type is log', function() { + var trace = { + cmin: 1, + cmax: 100, + colorscale: [ + [0, 'rgb(0, 0, 0)'], // 0% + [1, 'rgb(100, 100, 100)'] // 100% + ], + colorbar: { + type: 'log' + } + }; + + var colorFn = makeColorScaleFuncFromTrace(trace); + + // log10(1) = 0 -> maps to 0% of the scale + expect(colorFn(1)).toEqual('rgb(0, 0, 0)'); + + // log10(10) = 1 -> maps to 50% of the scale (halfway between log10(1) and log10(100)) + expect(colorFn(10)).toEqual('rgb(50, 50, 50)'); + + // log10(100) = 2 -> maps to 100% of the scale + expect(colorFn(100)).toEqual('rgb(100, 100, 100)'); + }); + + it('should return a transparent color for zero or negative values on a log scale', function() { + var trace = { + cmin: 1, + cmax: 100, + colorscale: [ + [0, 'rgb(0, 0, 0)'], + [1, 'rgb(255, 255, 255)'] + ], + colorbar: { + type: 'log' + } + }; + + var colorFn = makeColorScaleFuncFromTrace(trace); + + // The logarithm of zero or a negative number is undefined, + // so our wrapper should catch it and return transparent black. + expect(colorFn(0)).toEqual('rgba(0,0,0,0)'); + expect(colorFn(-10)).toEqual('rgba(0,0,0,0)'); + }); + + it('should safely fall back to linear if type is undefined or linear', function() { + var trace = { + cmin: 1, + cmax: 100, + colorscale: [ + [0, 'rgb(0, 0, 0)'], + [1, 'rgb(100, 100, 100)'] + ], + // Omitting colorbar.type to simulate default behavior + }; + + var colorFn = makeColorScaleFuncFromTrace(trace); + + // Linear midpoint between 1 and 100 is 50.5 + expect(colorFn(50.5)).toEqual('rgb(50, 50, 50)'); + }); + }); + }); From a6c3865f5070f264811c47b6681587dbf3c4628d Mon Sep 17 00:00:00 2001 From: bbm Date: Wed, 8 Jul 2026 16:36:52 -0400 Subject: [PATCH 07/15] check that proper tick labels are generated for colorbar log type --- test/jasmine/tests/colorbar_test.js | 40 +++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/test/jasmine/tests/colorbar_test.js b/test/jasmine/tests/colorbar_test.js index 1785f8cb21c..5d699770f4f 100644 --- a/test/jasmine/tests/colorbar_test.js +++ b/test/jasmine/tests/colorbar_test.js @@ -620,4 +620,44 @@ describe('Test colorbar:', function() { .then(done, done.fail); }); }); + + describe('log ticks', function() { + var gd; + + beforeEach(function() { + gd = createGraphDiv(); + }); + + afterEach(destroyGraphDiv); + + it('should generate logarithmic ticks when type is log', function() { + Plotly.newPlot(gd, [{ + type: 'heatmap', + z: [[1, 10, 100], [10,100,1000]], + cmin: 1, + cmax: 1000, + colorbar: { + type: 'log', + tickmode: 'auto' + } + }]); + + // Extract the tick text elements + var tickTexts = []; + d3Select(gd).selectAll('.cbaxis text').each(function(t) { + tickTexts.push(t.text); + }); + + // The mock axis should have automatically generated exponents for 1, 10, 100, 1000 + expect(tickTexts).toContain('1'); + expect(tickTexts).toContain('2'); + expect(tickTexts).toContain('5'); + expect(tickTexts).toContain('10'); + expect(tickTexts).toContain('100'); + expect(tickTexts).toContain('1000'); + + // // Verify a linear tick like '500' was NOT generated + expect(tickTexts).not.toContain('500'); + }); + }); }); From 4fded95a4acb11a3055e396c9768ba7cf49d61e4 Mon Sep 17 00:00:00 2001 From: bbm Date: Wed, 8 Jul 2026 17:14:32 -0400 Subject: [PATCH 08/15] adding draft log entry --- draftlogs/7894_add.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 draftlogs/7894_add.md diff --git a/draftlogs/7894_add.md b/draftlogs/7894_add.md new file mode 100644 index 00000000000..f9b91f738c5 --- /dev/null +++ b/draftlogs/7894_add.md @@ -0,0 +1 @@ + - Add native log-scale colorbars, with new 'type': 'log' attribute [[#7894](https://github.com/plotly/plotly.js/pull/7894)] \ No newline at end of file From 3e7f60bbf4c789e635c6e94e48b56825b3ab07f1 Mon Sep 17 00:00:00 2001 From: bbm Date: Thu, 9 Jul 2026 18:18:50 -0400 Subject: [PATCH 09/15] properly handle possibly negative z values (mask) and negative cmin/cmax (fall back to linear scale) --- src/components/colorbar/draw.js | 2 ++ src/components/colorscale/calc.js | 42 ++++++++++++++++++++++-- src/components/colorscale/helpers.js | 49 +++++++++++++++------------- 3 files changed, 69 insertions(+), 24 deletions(-) diff --git a/src/components/colorbar/draw.js b/src/components/colorbar/draw.js index 3a3096c46b6..7be496a4c18 100644 --- a/src/components/colorbar/draw.js +++ b/src/components/colorbar/draw.js @@ -956,6 +956,8 @@ function mockColorBarAxis(gd, opts, zrange) { var cbAxisIn = { type: opts.type || 'linear', + // Colorscale.calc guarantees zrange is strictly positive whenever + // opts.type is still 'log' by the time we get here range: opts.type === 'log' ? [Math.log10(zrange[0]), Math.log10(zrange[1])] : zrange, tickmode: opts.tickmode, diff --git a/src/components/colorscale/calc.js b/src/components/colorscale/calc.js index 0dd7800d431..d0df9d0c883 100644 --- a/src/components/colorscale/calc.js +++ b/src/components/colorscale/calc.js @@ -5,9 +5,24 @@ var isNumeric = require('fast-isnumeric'); var Lib = require('../../lib'); var extractOpts = require('./helpers').extractOpts; +// mirrors Lib.aggNums's own recursion into nested (eg heatmap z) arrays, but +// masks out non-positive values first - matching how cartesian log axes +// exclude non-positive data from autorange (see findExtremes in +// plots/cartesian/autorange.js) +function maskNonPositive(vals) { + if(Lib.isArrayOrTypedArray(vals[0])) return vals.map(maskNonPositive); + + var out = new Array(vals.length); + for(var i = 0; i < vals.length; i++) { + var v = vals[i]; + out[i] = (isNumeric(v) && v > 0) ? v : undefined; + } + return out; +} + module.exports = function calc(gd, trace, opts) { var fullLayout = gd._fullLayout; - var vals = opts.vals; + var rawVals = opts.vals; var containerStr = opts.containerStr; var container = containerStr ? @@ -20,6 +35,27 @@ module.exports = function calc(gd, trace, opts) { var max = cOpts.max; var mid = cOpts.mid; + var colorbar = cOpts.colorbar; + var isLog = !!(colorbar && colorbar.type === 'log'); + + if(isLog) { + // a log colorbar needs a strictly positive domain: an explicit + // non-positive cmin/cmax can never be logged, and if this trace's + // data is going to be scanned for an auto min/max but has no + // positive values at all, there's nothing to scale from. Either way, + // fall back to a linear colorbar instead of an undefined/negative range. + var fullyPinned = !auto && isNumeric(min) && isNumeric(max); + var hasExplicitNonPositive = (isNumeric(min) && min <= 0) || (isNumeric(max) && max <= 0); + var hasPositiveData = fullyPinned || isNumeric(Lib.aggNums(Math.max, null, maskNonPositive(rawVals))); + + if(hasExplicitNonPositive || !hasPositiveData) { + isLog = false; + colorbar.type = 'linear'; + } + } + + var vals = isLog ? maskNonPositive(rawVals) : rawVals; + var minVal = function() { return Lib.aggNums(Math.min, null, vals); }; var maxVal = function() { return Lib.aggNums(Math.max, null, vals); }; @@ -43,7 +79,9 @@ module.exports = function calc(gd, trace, opts) { } } - if(auto && mid !== undefined) { + // a symmetric-around-cmid range is a linear-domain concept - skip it for + // log colorbars rather than risk pushing a positive min/max non-positive + if(auto && mid !== undefined && !isLog) { if(max - mid > mid - min) { min = mid - (max - mid); } else if(max - mid < mid - min) { diff --git a/src/components/colorscale/helpers.js b/src/components/colorscale/helpers.js index 90f7039d9b8..579eb8c09e6 100644 --- a/src/components/colorscale/helpers.js +++ b/src/components/colorscale/helpers.js @@ -117,12 +117,14 @@ function extractScale(cont) { var cmax = cOpts.max; // Check if a log type is defined on the colorbar + // (Colorscale.calc guarantees cmin/cmax are strictly positive whenever + // type is still 'log' by the time we get here - see maskNonPositive) var type = (cOpts.colorbar && cOpts.colorbar.type) || 'linear'; // Convert domain boundaries to base-10 log if required if(type === 'log') { - if(cmin > 0) cmin = Math.log10(cmin); - if(cmax > 0) cmax = Math.log10(cmax); + cmin = Math.log10(cmin); + cmax = Math.log10(cmax); } var scl = cOpts.reversescale ? @@ -215,30 +217,33 @@ function makeColorScaleFunc(specs, opts) { } function makeColorScaleFuncFromTrace(trace, opts) { - var baseColorFn = makeColorScaleFunc(extractScale(trace), opts); var cOpts = extractOpts(trace); var type = (cOpts.colorbar && cOpts.colorbar.type) || 'linear'; + var baseColorFn = makeColorScaleFunc(extractScale(trace), opts); - // Wrap the base generator if we need to dynamically apply log transformations - if (type === 'log') { - var wrappedFunc = function(v) { - if (isNumeric(v)) { - if (v <= 0) return 'rgba(0,0,0,0)'; // Logarithm of zero/negative is undefined - return baseColorFn(Math.log10(v)); - } else if (tinycolor(v).isValid()) { - return v; - } else { - return Color.defaultLine; - } - }; + if(type !== 'log') return baseColorFn; - // Preserve native domain and range methods for the draw components - wrappedFunc.domain = baseColorFn.domain; - wrappedFunc.range = baseColorFn.range; - return wrappedFunc; - } + opts = opts || {}; + var noNumericCheck = opts.noNumericCheck; + var returnArray = opts.returnArray; + + // Wrap the base generator to dynamically apply log transformations, + // matching the array/string return shape makeColorScaleFunc would have used + var wrappedFunc = function(v) { + if(noNumericCheck || isNumeric(v)) { + // log of zero/negative is undefined - treat as out-of-range + if(v > 0) return baseColorFn(Math.log10(v)); + return returnArray ? [0, 0, 0, 0] : 'rgba(0,0,0,0)'; + } else if(tinycolor(v).isValid()) { + return v; + } + return Color.defaultLine; + }; - return baseColorFn; + // Preserve native domain and range methods for the draw components + wrappedFunc.domain = baseColorFn.domain; + wrappedFunc.range = baseColorFn.range; + return wrappedFunc; } module.exports = { @@ -248,4 +253,4 @@ module.exports = { flipScale: flipScale, makeColorScaleFunc: makeColorScaleFunc, makeColorScaleFuncFromTrace: makeColorScaleFuncFromTrace -}; \ No newline at end of file +}; From 051636ad03084dbd48ce29556e6db054a518aba1 Mon Sep 17 00:00:00 2001 From: bbm Date: Thu, 9 Jul 2026 18:20:29 -0400 Subject: [PATCH 10/15] test handling of cmin, cmax and z that are zero or negative --- test/jasmine/tests/colorbar_test.js | 51 ++++++++++++++++ test/jasmine/tests/colorscale_test.js | 84 ++++++++++++++++++++++++++- 2 files changed, 134 insertions(+), 1 deletion(-) diff --git a/test/jasmine/tests/colorbar_test.js b/test/jasmine/tests/colorbar_test.js index 5d699770f4f..408e667c6e4 100644 --- a/test/jasmine/tests/colorbar_test.js +++ b/test/jasmine/tests/colorbar_test.js @@ -659,5 +659,56 @@ describe('Test colorbar:', function() { // // Verify a linear tick like '500' was NOT generated expect(tickTexts).not.toContain('500'); }); + + it('should fall back to a linear colorbar when zmin is explicitly non-positive, instead of a NaN-derived range', function(done) { + Plotly.newPlot(gd, [{ + type: 'heatmap', + z: [[-5, 10, 100], [10, 100, 1000]], + zmin: -5, + zmax: 1000, + colorbar: { + type: 'log', + tickmode: 'auto' + } + }]) + .then(function() { + var ax; + gd._fullLayout._infolayer.selectAll('g.colorbar').each(function(opts) { + ax = opts._axis; + }); + + // a log colorbar can't represent a non-positive zmin, so the + // whole colorbar (and its mocked axis) falls back to linear + // instead of producing a NaN/folded log range + expect(gd._fullData[0].colorbar.type).toBe('linear'); + expect(ax.type).toBe('linear'); + expect(ax.range).toEqual([-5, 1000]); + }) + .then(done, done.fail); + }); + + it('should mask out non-positive z values and stay log when autoscaling with mixed-sign data', function(done) { + Plotly.newPlot(gd, [{ + type: 'heatmap', + z: [[-5, 2, 100], [10, 100, 1000]], + colorbar: { + type: 'log', + tickmode: 'auto' + } + }]) + .then(function() { + var ax; + gd._fullLayout._infolayer.selectAll('g.colorbar').each(function(opts) { + ax = opts._axis; + }); + + expect(gd._fullData[0].colorbar.type).toBe('log'); + expect(ax.type).toBe('log'); + // -5 is masked out of autorange; smallest positive value (2) is zmin + expect(ax.range[0]).toBeCloseTo(Math.log10(2), 5); + expect(ax.range[1]).toBeCloseTo(Math.log10(1000), 5); + }) + .then(done, done.fail); + }); }); }); diff --git a/test/jasmine/tests/colorscale_test.js b/test/jasmine/tests/colorscale_test.js index 54661e38509..e4e45a35063 100644 --- a/test/jasmine/tests/colorscale_test.js +++ b/test/jasmine/tests/colorscale_test.js @@ -654,6 +654,53 @@ describe('Test colorscale:', function() { expect(fullLayout.coloraxis.cmax).toBe(4); expect(fullLayout.coloraxis._cmax).toBe(4); }); + + it('should fall back to a linear colorbar when zmin/zmax is explicitly non-positive for a log colorbar', function() { + trace = { + type: 'heatmap', + z: [[-5, 10, 100], [10, 100, 1000]], + zmin: -5, + zmax: 1000, + zauto: false, + colorbar: {type: 'log'} + }; + gd = _supply(trace); + calcColorscale(gd, trace, {vals: trace.z, containerStr: '', cLetter: 'z'}); + + expect(trace.colorbar.type).toBe('linear'); + expect(trace._zmin).toBe(-5); + expect(trace._zmax).toBe(1000); + }); + + it('should fall back to a linear colorbar when autoscaling finds no positive values for a log colorbar', function() { + trace = { + type: 'heatmap', + z: [[-5, -10], [-100, -1]], + colorbar: {type: 'log'} + }; + gd = _supply(trace); + calcColorscale(gd, trace, {vals: trace.z, containerStr: '', cLetter: 'z'}); + + expect(trace.colorbar.type).toBe('linear'); + expect(trace._zmin).toBe(-100); + expect(trace._zmax).toBe(-1); + }); + + it('should mask out non-positive values when autoscaling a log colorbar, like log cartesian axes do', function() { + trace = { + type: 'heatmap', + z: [[-5, 2, 100], [10, 100, 1000]], + colorbar: {type: 'log'} + }; + gd = _supply(trace); + calcColorscale(gd, trace, {vals: trace.z, containerStr: '', cLetter: 'z'}); + + // stays log - there is positive data to scale from + expect(trace.colorbar.type).toBe('log'); + // -5 is masked out; the smallest positive value (2) becomes zmin + expect(trace._zmin).toBe(2); + expect(trace._zmax).toBe(1000); + }); }); describe('extractScale + makeColorScaleFunc', function() { @@ -1275,7 +1322,42 @@ describe('Test colorscale restyle calls:', function() { expect(colorFn(0)).toEqual('rgba(0,0,0,0)'); expect(colorFn(-10)).toEqual('rgba(0,0,0,0)'); }); - + + it('should return numeric rgba arrays (not strings) when called with returnArray/noNumericCheck, as heatmap rendering does', function() { + var trace = { + cmin: 1, + cmax: 100, + colorscale: [ + [0, 'rgb(0, 0, 0)'], + [1, 'rgb(200, 100, 50)'] + ], + colorbar: { + type: 'log' + } + }; + + var colorFn = makeColorScaleFuncFromTrace(trace, {noNumericCheck: true, returnArray: true}); + + // log10(1) = 0 -> maps to 0% of the scale + var cLow = colorFn(1); + expect(cLow).toEqual([0, 0, 0, 1]); + expect(typeof cLow[0]).toBe('number'); + + // log10(100) = 2 -> maps to 100% of the scale + var cHigh = colorFn(100); + expect(cHigh).toEqual([200, 100, 50, 1]); + + // zero/negative must clamp to a numeric transparent array, not a css string, + // since callers like heatmap/plot.js index directly into the result (c[0], c[1], c[2]) + var cZero = colorFn(0); + expect(cZero).toEqual([0, 0, 0, 0]); + expect(typeof cZero[0]).toBe('number'); + + var cNeg = colorFn(-10); + expect(cNeg).toEqual([0, 0, 0, 0]); + expect(typeof cNeg[0]).toBe('number'); + }); + it('should safely fall back to linear if type is undefined or linear', function() { var trace = { cmin: 1, From 7ebd393988ee1ca01245fe834e5926c3f99f5219 Mon Sep 17 00:00:00 2001 From: Brian Benjamin Maranville Date: Wed, 2 Dec 2026 09:17:10 -0500 Subject: [PATCH 11/15] remove 'type' attribute from colorbar (will replace with colorscaletype) --- src/components/colorbar/attributes.js | 10 ---------- src/components/colorbar/defaults.js | 1 - 2 files changed, 11 deletions(-) diff --git a/src/components/colorbar/attributes.js b/src/components/colorbar/attributes.js index 6d755fc1da9..f05909b244d 100644 --- a/src/components/colorbar/attributes.js +++ b/src/components/colorbar/attributes.js @@ -33,16 +33,6 @@ module.exports = overrideAll( 'This measure excludes the size of the padding, ticks and labels.' ].join(' ') }, - type: { - valType: 'enumerated', - values: ['linear', 'log'], - dflt: 'linear', - editType: 'calc', - description: [ - 'Sets the colorbar axis type.', - 'If `"log"`, the colorscale and colorbar will be configured for logarithmic data.' - ].join(' ') - }, lenmode: { valType: 'enumerated', values: ['fraction', 'pixels'], diff --git a/src/components/colorbar/defaults.js b/src/components/colorbar/defaults.js index f703509894a..2fab3cd5209 100644 --- a/src/components/colorbar/defaults.js +++ b/src/components/colorbar/defaults.js @@ -22,7 +22,6 @@ module.exports = function colorbarDefaults(containerIn, containerOut, layout) { var w = layout.width - margin.l - margin.r; var h = layout.height - margin.t - margin.b; - coerce('type'); var orientation = coerce('orientation'); var isVertical = orientation === 'v'; From 0c489bed66389524352e213b391cbb9b619c081b Mon Sep 17 00:00:00 2001 From: Brian Benjamin Maranville Date: Wed, 2 Dec 2026 09:18:14 -0500 Subject: [PATCH 12/15] add colorscaletype attribute --- src/components/colorscale/attributes.js | 11 +++++++++++ src/components/colorscale/calc.js | 6 +++--- src/components/colorscale/defaults.js | 1 + src/components/colorscale/helpers.js | 8 ++++---- 4 files changed, 19 insertions(+), 7 deletions(-) diff --git a/src/components/colorscale/attributes.js b/src/components/colorscale/attributes.js index cd41e6ecbe1..88dd1f803ba 100644 --- a/src/components/colorscale/attributes.js +++ b/src/components/colorscale/attributes.js @@ -231,6 +231,17 @@ module.exports = function colorScaleAttrs(context, opts) { ].join(' ') }; + attrs.colorscaletype = { + valType: 'enumerated', + values: ['linear', 'log'], + dflt: 'linear', + editType: 'calc', + description: [ + 'Sets the colorscale type.' + effectDesc, + 'If `"log"`, the colorscale and colorbar will be configured for logarithmic data.' + ].join(' ') + }; + if (!noScale) { attrs.showscale = { valType: 'boolean', diff --git a/src/components/colorscale/calc.js b/src/components/colorscale/calc.js index d0df9d0c883..090c426ccec 100644 --- a/src/components/colorscale/calc.js +++ b/src/components/colorscale/calc.js @@ -35,8 +35,8 @@ module.exports = function calc(gd, trace, opts) { var max = cOpts.max; var mid = cOpts.mid; - var colorbar = cOpts.colorbar; - var isLog = !!(colorbar && colorbar.type === 'log'); + var scaleType = cOpts.colorscaletype || 'linear'; + var isLog = scaleType === 'log'; if(isLog) { // a log colorbar needs a strictly positive domain: an explicit @@ -50,7 +50,7 @@ module.exports = function calc(gd, trace, opts) { if(hasExplicitNonPositive || !hasPositiveData) { isLog = false; - colorbar.type = 'linear'; + if(cOpts.colorscaletype) cOpts._sync('colorscaletype', 'linear'); } } diff --git a/src/components/colorscale/defaults.js b/src/components/colorscale/defaults.js index 1e1151357c8..71af062f7fc 100644 --- a/src/components/colorscale/defaults.js +++ b/src/components/colorscale/defaults.js @@ -99,6 +99,7 @@ module.exports = function colorScaleDefaults(parentContIn, parentContOut, layout coerce(prefix + 'colorscale'); coerce(prefix + 'reversescale'); + coerce(prefix + 'colorscaletype'); if(prefix !== 'marker.line.') { // handles both the trace case where the dflt is listed in attributes and diff --git a/src/components/colorscale/helpers.js b/src/components/colorscale/helpers.js index 579eb8c09e6..558736bc374 100644 --- a/src/components/colorscale/helpers.js +++ b/src/components/colorscale/helpers.js @@ -37,7 +37,7 @@ function hasColorscale(trace, containerStr, colorKey) { ); } -var constantAttrs = ['showscale', 'autocolorscale', 'colorscale', 'reversescale', 'colorbar']; +var constantAttrs = ['showscale', 'autocolorscale', 'colorscale', 'reversescale', 'colorbar', 'colorscaletype']; var letterAttrs = ['min', 'max', 'mid', 'auto']; /** @@ -116,10 +116,10 @@ function extractScale(cont) { var cmin = cOpts.min; var cmax = cOpts.max; - // Check if a log type is defined on the colorbar + // Check if a colorscale type is 'log' // (Colorscale.calc guarantees cmin/cmax are strictly positive whenever // type is still 'log' by the time we get here - see maskNonPositive) - var type = (cOpts.colorbar && cOpts.colorbar.type) || 'linear'; + var type = cOpts.colorscaletype || 'linear'; // Convert domain boundaries to base-10 log if required if(type === 'log') { @@ -218,7 +218,7 @@ function makeColorScaleFunc(specs, opts) { function makeColorScaleFuncFromTrace(trace, opts) { var cOpts = extractOpts(trace); - var type = (cOpts.colorbar && cOpts.colorbar.type) || 'linear'; + var type = cOpts.colorscaletype || 'linear'; var baseColorFn = makeColorScaleFunc(extractScale(trace), opts); if(type !== 'log') return baseColorFn; From 364ad2ec15891ee88d560683f28a82a34da77115 Mon Sep 17 00:00:00 2001 From: Brian Benjamin Maranville Date: Wed, 2 Dec 2026 09:18:47 -0500 Subject: [PATCH 13/15] pull in colorscaletype attribute to options for drawing colorbar --- src/components/colorbar/draw.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/components/colorbar/draw.js b/src/components/colorbar/draw.js index 7be496a4c18..e90ef54712e 100644 --- a/src/components/colorbar/draw.js +++ b/src/components/colorbar/draw.js @@ -134,6 +134,7 @@ function makeColorBarData(gd) { opts._traceIndex = trace.index; opts._propPrefix = (contName ? contName + '.' : '') + 'colorbar.'; opts._meta = trace._meta; + opts.type = cont.colorscaletype || 'linear'; calcOpts(); out.push(opts); } @@ -151,6 +152,7 @@ function makeColorBarData(gd) { opts._id = 'cb' + k; opts._propPrefix = k + '.colorbar.'; opts._meta = fullLayout._meta; + opts.type = cont.colorscaletype || 'linear'; cbOpt = {min: 'cmin', max: 'cmax'}; if(colorAxOpts[0] !== 'heatmap') { From 859f021fe63dee183dcdaa3541c26cd4be120c35 Mon Sep 17 00:00:00 2001 From: Brian Benjamin Maranville Date: Wed, 2 Dec 2026 09:19:31 -0500 Subject: [PATCH 14/15] update tests to use colorscaletype instead of colorbar.type --- test/jasmine/tests/colorbar_test.js | 6 +++--- test/jasmine/tests/colorscale_test.js | 24 +++++++++--------------- test/jasmine/tests/validate_test.js | 16 ++++++---------- 3 files changed, 18 insertions(+), 28 deletions(-) diff --git a/test/jasmine/tests/colorbar_test.js b/test/jasmine/tests/colorbar_test.js index 408e667c6e4..9ba9c447ddf 100644 --- a/test/jasmine/tests/colorbar_test.js +++ b/test/jasmine/tests/colorbar_test.js @@ -636,8 +636,8 @@ describe('Test colorbar:', function() { z: [[1, 10, 100], [10,100,1000]], cmin: 1, cmax: 1000, + colorscaletype: 'log', colorbar: { - type: 'log', tickmode: 'auto' } }]); @@ -666,8 +666,8 @@ describe('Test colorbar:', function() { z: [[-5, 10, 100], [10, 100, 1000]], zmin: -5, zmax: 1000, + colorscaletype: 'log', colorbar: { - type: 'log', tickmode: 'auto' } }]) @@ -691,8 +691,8 @@ describe('Test colorbar:', function() { Plotly.newPlot(gd, [{ type: 'heatmap', z: [[-5, 2, 100], [10, 100, 1000]], + colorscaletype: 'log', colorbar: { - type: 'log', tickmode: 'auto' } }]) diff --git a/test/jasmine/tests/colorscale_test.js b/test/jasmine/tests/colorscale_test.js index e4e45a35063..12c121539fb 100644 --- a/test/jasmine/tests/colorscale_test.js +++ b/test/jasmine/tests/colorscale_test.js @@ -662,12 +662,12 @@ describe('Test colorscale:', function() { zmin: -5, zmax: 1000, zauto: false, - colorbar: {type: 'log'} + colorscaletype: 'log' }; gd = _supply(trace); calcColorscale(gd, trace, {vals: trace.z, containerStr: '', cLetter: 'z'}); - expect(trace.colorbar.type).toBe('linear'); + expect(trace.colorscaletype).toBe('linear'); expect(trace._zmin).toBe(-5); expect(trace._zmax).toBe(1000); }); @@ -676,12 +676,12 @@ describe('Test colorscale:', function() { trace = { type: 'heatmap', z: [[-5, -10], [-100, -1]], - colorbar: {type: 'log'} + colorscaletype: 'log' }; gd = _supply(trace); calcColorscale(gd, trace, {vals: trace.z, containerStr: '', cLetter: 'z'}); - expect(trace.colorbar.type).toBe('linear'); + expect(trace.colorscaletype).toBe('linear'); expect(trace._zmin).toBe(-100); expect(trace._zmax).toBe(-1); }); @@ -690,13 +690,13 @@ describe('Test colorscale:', function() { trace = { type: 'heatmap', z: [[-5, 2, 100], [10, 100, 1000]], - colorbar: {type: 'log'} + colorscaletype: 'log' }; gd = _supply(trace); calcColorscale(gd, trace, {vals: trace.z, containerStr: '', cLetter: 'z'}); // stays log - there is positive data to scale from - expect(trace.colorbar.type).toBe('log'); + expect(trace.colorscaletype).toBe('log'); // -5 is masked out; the smallest positive value (2) becomes zmin expect(trace._zmin).toBe(2); expect(trace._zmax).toBe(1000); @@ -1285,9 +1285,7 @@ describe('Test colorscale restyle calls:', function() { [0, 'rgb(0, 0, 0)'], // 0% [1, 'rgb(100, 100, 100)'] // 100% ], - colorbar: { - type: 'log' - } + colorscaletype: 'log' }; var colorFn = makeColorScaleFuncFromTrace(trace); @@ -1310,9 +1308,7 @@ describe('Test colorscale restyle calls:', function() { [0, 'rgb(0, 0, 0)'], [1, 'rgb(255, 255, 255)'] ], - colorbar: { - type: 'log' - } + colorscaletype: 'log' }; var colorFn = makeColorScaleFuncFromTrace(trace); @@ -1331,9 +1327,7 @@ describe('Test colorscale restyle calls:', function() { [0, 'rgb(0, 0, 0)'], [1, 'rgb(200, 100, 50)'] ], - colorbar: { - type: 'log' - } + colorscaletype: 'log' }; var colorFn = makeColorScaleFuncFromTrace(trace, {noNumericCheck: true, returnArray: true}); diff --git a/test/jasmine/tests/validate_test.js b/test/jasmine/tests/validate_test.js index 473eccdbec7..0ed375497c9 100644 --- a/test/jasmine/tests/validate_test.js +++ b/test/jasmine/tests/validate_test.js @@ -821,14 +821,12 @@ describe('Plotly.validate', function () { expect(out).toBeUndefined(); }); - describe('colorbar type validation', function() { - it('should accept valid colorbar types', function() { + describe('colorscaletype validation', function() { + it('should accept valid colorscaletype', function() { var trace = { type: 'heatmap', z: [[1, 10], [100, 1000]], - colorbar: { - type: 'log' - } + colorscaletype: 'log' }; // validate returns an array of errors. An empty array means success. @@ -836,18 +834,16 @@ describe('Plotly.validate', function () { expect(out).toBeUndefined(); }); - it('should reject invalid colorbar types', function() { + it('should reject invalid colorscaletype', function() { var trace = { type: 'heatmap', z: [[1, 2], [3, 4]], - colorbar: { - type: 'exponential' // not in our enumerated list - } + colorscaletype: 'exponential' // not in our enumerated list }; var out = Plotly.validate([trace]); expect(out.length).toEqual(1); - expect(out[0].msg).toEqual('In data trace 0, key colorbar.type is set to an invalid value (exponential)'); + expect(out[0].msg).toEqual('In data trace 0, key colorscaletype is set to an invalid value (exponential)'); }); }); }); From 9615c3f978110bf87b4729c2d84f09ed6f36891f Mon Sep 17 00:00:00 2001 From: Brian Benjamin Maranville Date: Wed, 2 Dec 2026 09:23:35 -0500 Subject: [PATCH 15/15] update draftlog for this PR --- draftlogs/7894_add.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/draftlogs/7894_add.md b/draftlogs/7894_add.md index f9b91f738c5..d1f6a89fe31 100644 --- a/draftlogs/7894_add.md +++ b/draftlogs/7894_add.md @@ -1 +1 @@ - - Add native log-scale colorbars, with new 'type': 'log' attribute [[#7894](https://github.com/plotly/plotly.js/pull/7894)] \ No newline at end of file +- Add native log-scale colorscale, with new 'colorscaletype': 'log' attribute [[#7894](https://github.com/plotly/plotly.js/pull/7894)] \ No newline at end of file