diff --git a/draftlogs/8186_fix.md b/draftlogs/8186_fix.md new file mode 100644 index 00000000000..142fe15f65e --- /dev/null +++ b/draftlogs/8186_fix.md @@ -0,0 +1 @@ +- Fix the `scatter3d` z range on log axes cutting off the lower error bars when the smallest point is not the first one, and keep the range in sync with the data after `Plotly.react` [[#8186](https://github.com/plotly/plotly.js/pull/8186)] diff --git a/src/plots/gl3d/scene.js b/src/plots/gl3d/scene.js index e320fe9e780..43d234f63b8 100644 --- a/src/plots/gl3d/scene.js +++ b/src/plots/gl3d/scene.js @@ -619,6 +619,12 @@ proto.plot = function(sceneData, fullLayout, layout) { // after computeTraceBounds where ax._categories are filled in scene.convertAnnotations(this); + // The traces recompute the lower log error bounds during their update, + // so drop the values from the previous plot first + for(j = 0; j < 3; ++j) { + delete fullSceneLayout[axisProperties[j]]._lowerLogErrorBound; + } + // Update traces for(i = 0; i < sceneData.length; ++i) { data = sceneData[i]; diff --git a/src/traces/scatter3d/calc_errors.js b/src/traces/scatter3d/calc_errors.js index 9406249a871..a0034238f81 100644 --- a/src/traces/scatter3d/calc_errors.js +++ b/src/traces/scatter3d/calc_errors.js @@ -24,8 +24,9 @@ function calculateAxisErrors(data, params, scaleFactor, axis) { // Keep track of the lower error bound which isn't negative! if(min > 0) { var lower = axis.c2l(min); - if(!axis._lowerLogErrorBound) axis._lowerLogErrorBound = lower; - axis._lowerErrorBound = Math.min(axis._lowerLogErrorBound, lower); + if(axis._lowerLogErrorBound === undefined || lower < axis._lowerLogErrorBound) { + axis._lowerLogErrorBound = lower; + } } } else { result[i] = [ diff --git a/test/jasmine/tests/scatter3d_test.js b/test/jasmine/tests/scatter3d_test.js index a5f1a7f8b4d..e4ab10a9c83 100644 --- a/test/jasmine/tests/scatter3d_test.js +++ b/test/jasmine/tests/scatter3d_test.js @@ -345,4 +345,73 @@ describe('Test scatter3d interactions:', function() { }) .then(done, done.fail); }); + + describe('log error bars', function() { + var samples = [ + {z: 1200, err: 300}, + {z: 410, err: 90}, + {z: 95, err: 30}, + {z: 12, err: 8} + ]; + + function makeTrace(rows) { + return { + type: 'scatter3d', + mode: 'markers', + x: rows.map(function(r, i) { return i; }), + y: rows.map(function(r, i) { return i % 2; }), + z: rows.map(function(r) { return r.z; }), + error_z: {array: rows.map(function(r) { return r.err; })} + }; + } + + var layout = {scene: {zaxis: {type: 'log'}}}; + + function zRange() { + return gd._fullLayout.scene.zaxis.range.slice(); + } + + it('@gl should start the z range below the lowest error bar whatever the point order', function(done) { + var ascending; + + Plotly.newPlot(gd, [makeTrace(samples.slice().reverse())], Lib.extendDeep({}, layout)) + .then(function() { + ascending = zRange(); + // the lower error bar of the smallest point ends at 12 - 8 = 4 + expect(ascending[0]).toBeLessThan(Math.log10(4)); + + return Plotly.newPlot(gd, [makeTrace(samples)], Lib.extendDeep({}, layout)); + }) + .then(function() { + expect(zRange()).toBeCloseToArray(ascending, 5); + }) + .then(done, done.fail); + }); + + it('@gl should recompute the lower error bound on react', function(done) { + var wide = samples.map(function(r) { return {z: r.z, err: r.err}; }); + wide[3].err = 11; + var expected; + + Plotly.newPlot(gd, [makeTrace(wide)], Lib.extendDeep({}, layout)) + .then(function() { + expected = zRange(); + + return Plotly.newPlot(gd, [makeTrace(samples)], Lib.extendDeep({}, layout)); + }) + .then(function() { + return Plotly.react(gd, [makeTrace(wide)], Lib.extendDeep({}, layout)); + }) + .then(function() { + expect(zRange()).toBeCloseToArray(expected, 5); + + return Plotly.react(gd, [makeTrace(samples)], Lib.extendDeep({}, layout)); + }) + .then(function() { + // 12 - 8 = 4 is above 12 - 11 = 1, so the range must start higher again + expect(zRange()[0]).toBeGreaterThan(expected[0]); + }) + .then(done, done.fail); + }); + }); });