Skip to content

Commit

Permalink
Merge pull request #2992 from plotly/2962-scatter3d-log-error-bars
Browse files Browse the repository at this point in the history
scatter3d now performs axis transformation on error bars
  • Loading branch information
antoinerg authored Sep 17, 2018
2 parents b6d5283 + 15100f9 commit c422725
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 11 deletions.
6 changes: 5 additions & 1 deletion src/plots/gl3d/scene.js
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,11 @@ proto.plot = function(sceneData, fullLayout, layout) {
var objBounds = obj.bounds;
var pad = obj._trace.data._pad || 0;

sceneBounds[0][i] = Math.min(sceneBounds[0][i], objBounds[0][i] / dataScale[i] - pad);
if(obj.constructor.name === 'ErrorBars' && axis._lowerLogErrorBound) {
sceneBounds[0][i] = Math.min(sceneBounds[0][i], axis._lowerLogErrorBound);
} else {
sceneBounds[0][i] = Math.min(sceneBounds[0][i], objBounds[0][i] / dataScale[i] - pad);
}
sceneBounds[1][i] = Math.max(sceneBounds[1][i], objBounds[1][i] / dataScale[i] + pad);
}

Expand Down
36 changes: 27 additions & 9 deletions src/traces/scatter3d/calc_errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

var Registry = require('../../registry');

function calculateAxisErrors(data, params, scaleFactor) {
function calculateAxisErrors(data, params, scaleFactor, axis) {
if(!params || !params.visible) return null;

var computeError = Registry.getComponentMethod('errorbars', 'makeComputeError')(params);
Expand All @@ -19,10 +19,28 @@ function calculateAxisErrors(data, params, scaleFactor) {
for(var i = 0; i < data.length; i++) {
var errors = computeError(+data[i], i);

result[i] = [
-errors[0] * scaleFactor,
errors[1] * scaleFactor
];
if(axis.type === 'log') {
var point = axis.c2l(data[i]);
var min = data[i] - errors[0],
max = data[i] + errors[1];

result[i] = [
(axis.c2l(min, true) - point) * scaleFactor,
(axis.c2l(max, true) - point) * scaleFactor
];

// 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);
}
} else {
result[i] = [
-errors[0] * scaleFactor,
errors[1] * scaleFactor
];
}
}

return result;
Expand All @@ -35,11 +53,11 @@ function dataLength(array) {
return 0;
}

function calculateErrors(data, scaleFactor) {
function calculateErrors(data, scaleFactor, sceneLayout) {
var errors = [
calculateAxisErrors(data.x, data.error_x, scaleFactor[0]),
calculateAxisErrors(data.y, data.error_y, scaleFactor[1]),
calculateAxisErrors(data.z, data.error_z, scaleFactor[2])
calculateAxisErrors(data.x, data.error_x, scaleFactor[0], sceneLayout.xaxis),
calculateAxisErrors(data.y, data.error_y, scaleFactor[1], sceneLayout.yaxis),
calculateAxisErrors(data.z, data.error_z, scaleFactor[2], sceneLayout.zaxis)
];

var n = dataLength(errors);
Expand Down
2 changes: 1 addition & 1 deletion src/traces/scatter3d/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ function convertPlotlyOptions(scene, data) {
}
}

params.errorBounds = calculateError(data, scaleFactor);
params.errorBounds = calculateError(data, scaleFactor, sceneLayout);

var errorParams = calculateErrorParams([data.error_x, data.error_y, data.error_z]);
params.errorColor = errorParams.color;
Expand Down
Binary file added test/image/baselines/gl3d_error_bars_log.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/image/baselines/gl3d_error_bars_log_2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
26 changes: 26 additions & 0 deletions test/image/mocks/gl3d_error_bars_log.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"data": [{
"x": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
"y": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
"z": [1e00, 1e+01, 1e+02, 1e+03, 1e+04,
1e+05, 1e+06, 1e+07, 1e+08, 1e+09
],
"type": "scatter3d",
"error_z": {
"array": [1e-01, 1e+00, 1e+02, 9e+03, 5e+03,
0.9e+05, 1e+05, 1e+07, 9e+08, 2e+09
],
"type": "data",
"visible": true
}
}],
"layout": {
"scene": {
"zaxis": {
"type": "log"
}
},
"width": 800,
"height": 800
}
}
22 changes: 22 additions & 0 deletions test/image/mocks/gl3d_error_bars_log_2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"data": [{
"x": [0],
"y": [0],
"z": [1e-08],
"type": "scatter3d",
"error_z": {
"array": [0.9e-08],
"type": "data",
"visible": true
}
}],
"layout": {
"scene": {
"zaxis": {
"type": "log"
}
},
"width": 800,
"height": 800
}
}

0 comments on commit c422725

Please sign in to comment.