Skip to content

Commit 388a7fe

Browse files
committed
abstract - and fix - automatic axis type clearing
1 parent 040ed1b commit 388a7fe

File tree

18 files changed

+120
-56
lines changed

18 files changed

+120
-56
lines changed

src/plot_api/edit_types.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,16 @@ var isPlainObject = Lib.isPlainObject;
1515
var traceOpts = {
1616
valType: 'flaglist',
1717
extras: ['none'],
18-
flags: ['calc', 'calcIfAutorange', 'plot', 'style', 'colorbars'],
18+
flags: ['calc', 'calcIfAutorange', 'clearAxisTypes', 'plot', 'style', 'colorbars'],
1919
description: [
2020
'trace attributes should include an `editType` string matching this flaglist.',
2121
'*calc* is the most extensive: a full `Plotly.plot` starting by clearing `gd.calcdata`',
2222
'to force it to be regenerated',
2323
'*calcIfAutorange* does a full `Plotly.plot`, but only clears and redoes `gd.calcdata`',
2424
'if there is at least one autoranged axis.',
25+
'*clearAxisTypes* resets the types of the axes this trace is on, because new data could',
26+
'cause the automatic axis type detection to change. Log type will not be cleared, as that',
27+
'is never automatically chosen so must have been user-specified.',
2528
'*plot* calls `Plotly.plot` but without first clearing `gd.calcdata`.',
2629
'*style* only calls `module.style` for all trace modules and redraws the legend.',
2730
'*colorbars* only redraws colorbars.'

src/plot_api/plot_api.js

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1282,8 +1282,9 @@ Plotly.restyle = function restyle(gd, astr, val, traces) {
12821282
var specs = _restyle(gd, aobj, traces),
12831283
flags = specs.flags;
12841284

1285-
// clear calcdata if required
1285+
// clear calcdata and/or axis types if required so they get regenerated
12861286
if(flags.clearCalc) gd.calcdata = undefined;
1287+
if(flags.clearAxisTypes) clearAxisTypes(gd, traces, {});
12871288

12881289
// fill in redraw sequence
12891290
var seq = [];
@@ -1340,12 +1341,6 @@ function _restyle(gd, aobj, _traces) {
13401341
axlist,
13411342
flagAxForDelete = {};
13421343

1343-
// these ones may alter the axis type
1344-
// (at least if the first trace is involved)
1345-
var axtypeAttrs = [
1346-
'type', 'x', 'y', 'x0', 'y0', 'orientation', 'xaxis', 'yaxis'
1347-
];
1348-
13491344
// make a new empty vals array for undoit
13501345
function a0() { return traces.map(function() { return undefined; }); }
13511346

@@ -1557,11 +1552,6 @@ function _restyle(gd, aobj, _traces) {
15571552
}
15581553
}
15591554

1560-
// check if we need to call axis type
1561-
if((traces.indexOf(0) !== -1) && (axtypeAttrs.indexOf(ai) !== -1)) {
1562-
Plotly.Axes.clearTypes(gd, traces);
1563-
}
1564-
15651555
// major enough changes deserve autoscale, autobin, and
15661556
// non-reversed axes so people don't get confused
15671557
if(['orientation', 'type'].indexOf(ai) !== -1) {
@@ -2101,8 +2091,9 @@ Plotly.update = function update(gd, traceUpdate, layoutUpdate, traces) {
21012091
var relayoutSpecs = _relayout(gd, Lib.extendFlat({}, layoutUpdate)),
21022092
relayoutFlags = relayoutSpecs.flags;
21032093

2104-
// clear calcdata if required
2094+
// clear calcdata and/or axis types if required
21052095
if(restyleFlags.clearCalc || relayoutFlags.calc) gd.calcdata = undefined;
2096+
if(restyleFlags.clearAxisTypes) clearAxisTypes(gd, traces, layoutUpdate);
21062097

21072098
// fill in redraw sequence
21082099
var seq = [];
@@ -2157,6 +2148,37 @@ Plotly.update = function update(gd, traceUpdate, layoutUpdate, traces) {
21572148
});
21582149
};
21592150

2151+
// empty out types for all axes containing these traces
2152+
// so we auto-set them again
2153+
var axLetters = ['x', 'y', 'z'];
2154+
function clearAxisTypes(gd, traces, layoutUpdate) {
2155+
if(typeof traces === 'number') traces = [traces];
2156+
else if(!Array.isArray(traces) || !traces.length) {
2157+
traces = (gd._fullData).map(function(d, i) { return i; });
2158+
}
2159+
for(var i = 0; i < traces.length; i++) {
2160+
var trace = gd._fullData[i];
2161+
for(var j = 0; j < 3; j++) {
2162+
var ax = Plotly.Axes.getFromTrace(gd, trace, axLetters[j]);
2163+
2164+
// do not clear log type - that's never an auto result so must have been intentional
2165+
if(ax && ax.type !== 'log') {
2166+
var axAttr = ax._name;
2167+
var sceneName = ax._id.substr(1);
2168+
if(sceneName.substr(0, 5) === 'scene') {
2169+
if(layoutUpdate[sceneName] !== undefined) continue;
2170+
axAttr = sceneName + '.' + axAttr;
2171+
}
2172+
var typeAttr = axAttr + '.type';
2173+
2174+
if(layoutUpdate[axAttr] === undefined && layoutUpdate[typeAttr] === undefined) {
2175+
Lib.nestedProperty(gd.layout, typeAttr).set(null);
2176+
}
2177+
}
2178+
}
2179+
}
2180+
}
2181+
21602182
/**
21612183
* Animate to a frame, sequence of frame, frame group, or frame definition
21622184
*

src/plots/attributes.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ module.exports = {
1616
role: 'info',
1717
values: [], // listed dynamically
1818
dflt: 'scatter',
19-
editType: 'calc'
19+
editType: 'calc+clearAxisTypes'
2020
},
2121
visible: {
2222
valType: 'enumerated',

src/plots/cartesian/attributes.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ module.exports = {
1414
valType: 'subplotid',
1515
role: 'info',
1616
dflt: 'x',
17-
editType: 'calc',
17+
editType: 'calc+clearAxisTypes',
1818
description: [
1919
'Sets a reference between this trace\'s x coordinates and',
2020
'a 2D cartesian x axis.',
@@ -27,7 +27,7 @@ module.exports = {
2727
valType: 'subplotid',
2828
role: 'info',
2929
dflt: 'y',
30-
editType: 'calc',
30+
editType: 'calc+clearAxisTypes',
3131
description: [
3232
'Sets a reference between this trace\'s y coordinates and',
3333
'a 2D cartesian y axis.',

src/plots/cartesian/axes.js

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -175,19 +175,6 @@ axes.getDataToCoordFunc = function(gd, trace, target, targetArray) {
175175
return getDataConversions(gd, trace, target, targetArray).d2c;
176176
};
177177

178-
// empty out types for all axes containing these traces
179-
// so we auto-set them again
180-
axes.clearTypes = function(gd, traces) {
181-
if(!Array.isArray(traces) || !traces.length) {
182-
traces = (gd._fullData).map(function(d, i) { return i; });
183-
}
184-
traces.forEach(function(tracenum) {
185-
var trace = gd.data[tracenum];
186-
delete (axes.getFromId(gd, trace.xaxis) || {}).type;
187-
delete (axes.getFromId(gd, trace.yaxis) || {}).type;
188-
});
189-
};
190-
191178
// get counteraxis letter for this axis (name or id)
192179
// this can also be used as the id for default counter axis
193180
axes.counterLetter = function(id) {

src/plots/gl3d/layout/attributes.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ module.exports = {
1414
valType: 'subplotid',
1515
role: 'info',
1616
dflt: 'scene',
17-
editType: 'calc',
17+
editType: 'calc+clearAxisTypes',
1818
description: [
1919
'Sets a reference between this trace\'s 3D coordinate system and',
2020
'a 3D scene.',

src/traces/bar/attributes.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ module.exports = {
9999
valType: 'enumerated',
100100
role: 'info',
101101
values: ['v', 'h'],
102-
editType: 'calc',
102+
editType: 'calc+clearAxisTypes',
103103
description: [
104104
'Sets the orientation of the bars.',
105105
'With *v* (*h*), the value of the each bar spans',

src/traces/box/attributes.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@ var scatterMarkerAttrs = scatterAttrs.marker,
1919
module.exports = {
2020
y: {
2121
valType: 'data_array',
22-
editType: 'calc',
22+
editType: 'calc+clearAxisTypes',
2323
description: [
2424
'Sets the y sample data or coordinates.',
2525
'See overview for more info.'
2626
].join(' ')
2727
},
2828
x: {
2929
valType: 'data_array',
30-
editType: 'calc',
30+
editType: 'calc+clearAxisTypes',
3131
description: [
3232
'Sets the x sample data or coordinates.',
3333
'See overview for more info.'
@@ -36,7 +36,7 @@ module.exports = {
3636
x0: {
3737
valType: 'any',
3838
role: 'info',
39-
editType: 'calc',
39+
editType: 'calc+clearAxisTypes',
4040
description: [
4141
'Sets the x coordinate of the box.',
4242
'See overview for more info.'
@@ -45,7 +45,7 @@ module.exports = {
4545
y0: {
4646
valType: 'any',
4747
role: 'info',
48-
editType: 'calc',
48+
editType: 'calc+clearAxisTypes',
4949
description: [
5050
'Sets the y coordinate of the box.',
5151
'See overview for more info.'
@@ -54,7 +54,7 @@ module.exports = {
5454
name: {
5555
valType: 'string',
5656
role: 'info',
57-
editType: 'calc',
57+
editType: 'calc+clearAxisTypes',
5858
description: [
5959
'Sets the trace name.',
6060
'The trace name appear as the legend item and on hover.',
@@ -134,7 +134,7 @@ module.exports = {
134134
valType: 'enumerated',
135135
values: ['v', 'h'],
136136
role: 'style',
137-
editType: 'calc',
137+
editType: 'calc+clearAxisTypes',
138138
description: [
139139
'Sets the orientation of the box(es).',
140140
'If *v* (*h*), the distribution is visualized along',

src/traces/carpet/attributes.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ module.exports = {
3434
},
3535
x: {
3636
valType: 'data_array',
37-
editType: 'calc',
37+
editType: 'calc+clearAxisTypes',
3838
description: [
3939
'A two dimensional array of x coordinates at each carpet point.',
4040
'If ommitted, the plot is a cheater plot and the xaxis is hidden',
@@ -43,7 +43,7 @@ module.exports = {
4343
},
4444
y: {
4545
valType: 'data_array',
46-
editType: 'calc',
46+
editType: 'calc+clearAxisTypes',
4747
description: 'A two dimensional array of y coordinates at each carpet point.'
4848
},
4949
a: {

src/traces/heatmap/attributes.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ module.exports = extendFlat({}, {
4343
valType: 'enumerated',
4444
values: ['array', 'scaled'],
4545
role: 'info',
46-
editType: 'calc',
46+
editType: 'calc+clearAxisTypes',
4747
description: [
4848
'If *array*, the heatmap\'s x coordinates are given by *x*',
4949
'(the default behavior when `x` is provided).',
@@ -55,7 +55,7 @@ module.exports = extendFlat({}, {
5555
valType: 'enumerated',
5656
values: ['array', 'scaled'],
5757
role: 'info',
58-
editType: 'calc',
58+
editType: 'calc+clearAxisTypes',
5959
description: [
6060
'If *array*, the heatmap\'s y coordinates are given by *y*',
6161
'(the default behavior when `y` is provided)',

src/traces/histogram/attributes.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ var barAttrs = require('../bar/attributes');
1414
module.exports = {
1515
x: {
1616
valType: 'data_array',
17-
editType: 'calc',
17+
editType: 'calc+clearAxisTypes',
1818
description: [
1919
'Sets the sample data to be binned on the x axis.'
2020
].join(' ')
2121
},
2222
y: {
2323
valType: 'data_array',
24-
editType: 'calc',
24+
editType: 'calc+clearAxisTypes',
2525
description: [
2626
'Sets the sample data to be binned on the y axis.'
2727
].join(' ')

src/traces/mesh3d/attributes.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,23 @@ var extendFlat = require('../../lib/extend').extendFlat;
1919
module.exports = extendFlat(colorAttrs('', 'calc', false), {
2020
x: {
2121
valType: 'data_array',
22-
editType: 'calc',
22+
editType: 'calc+clearAxisTypes',
2323
description: [
2424
'Sets the X coordinates of the vertices. The nth element of vectors `x`, `y` and `z`',
2525
'jointly represent the X, Y and Z coordinates of the nth vertex.'
2626
].join(' ')
2727
},
2828
y: {
2929
valType: 'data_array',
30-
editType: 'calc',
30+
editType: 'calc+clearAxisTypes',
3131
description: [
3232
'Sets the Y coordinates of the vertices. The nth element of vectors `x`, `y` and `z`',
3333
'jointly represent the X, Y and Z coordinates of the nth vertex.'
3434
].join(' ')
3535
},
3636
z: {
3737
valType: 'data_array',
38-
editType: 'calc',
38+
editType: 'calc+clearAxisTypes',
3939
description: [
4040
'Sets the Z coordinates of the vertices. The nth element of vectors `x`, `y` and `z`',
4141
'jointly represent the X, Y and Z coordinates of the nth vertex.'

src/traces/ohlc/attributes.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ module.exports = {
5555

5656
x: {
5757
valType: 'data_array',
58-
editType: 'calc',
58+
editType: 'calc+clearAxisTypes',
5959
description: [
6060
'Sets the x coordinates.',
6161
'If absent, linear coordinate will be generated.'

src/traces/scatter/attributes.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ var extendFlat = require('../../lib/extend').extendFlat;
2121
module.exports = {
2222
x: {
2323
valType: 'data_array',
24-
editType: 'calc',
24+
editType: 'calc+clearAxisTypes',
2525
description: 'Sets the x coordinates.'
2626
},
2727
x0: {
2828
valType: 'any',
2929
dflt: 0,
3030
role: 'info',
31-
editType: 'calc',
31+
editType: 'calc+clearAxisTypes',
3232
description: [
3333
'Alternate to `x`.',
3434
'Builds a linear space of x coordinates.',
@@ -48,14 +48,14 @@ module.exports = {
4848
},
4949
y: {
5050
valType: 'data_array',
51-
editType: 'calc',
51+
editType: 'calc+clearAxisTypes',
5252
description: 'Sets the y coordinates.'
5353
},
5454
y0: {
5555
valType: 'any',
5656
dflt: 0,
5757
role: 'info',
58-
editType: 'calc',
58+
editType: 'calc+clearAxisTypes',
5959
description: [
6060
'Alternate to `y`.',
6161
'Builds a linear space of y coordinates.',

src/traces/scatter3d/attributes.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ function makeProjectionAttr(axLetter) {
5454
};
5555
}
5656

57-
module.exports = overrideAll({
57+
var attrs = module.exports = overrideAll({
5858
x: scatterAttrs.x,
5959
y: scatterAttrs.y,
6060
z: {
@@ -172,3 +172,5 @@ module.exports = overrideAll({
172172
error_y: errorBarAttrs,
173173
error_z: errorBarAttrs,
174174
}, 'calc', 'nested');
175+
176+
attrs.x.editType = attrs.y.editType = attrs.z.editType = 'calc+clearAxisTypes';

src/traces/scattergl/attributes.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ var scatterLineAttrs = scatterAttrs.line,
2020
scatterMarkerAttrs = scatterAttrs.marker,
2121
scatterMarkerLineAttrs = scatterMarkerAttrs.line;
2222

23-
module.exports = overrideAll({
23+
var attrs = module.exports = overrideAll({
2424
x: scatterAttrs.x,
2525
x0: scatterAttrs.x0,
2626
dx: scatterAttrs.dx,
@@ -86,3 +86,5 @@ module.exports = overrideAll({
8686
error_y: scatterAttrs.error_y,
8787
error_x: scatterAttrs.error_x
8888
}, 'calc', 'nested');
89+
90+
attrs.x.editType = attrs.y.editType = attrs.x0.editType = attrs.y0.editType = 'calc+clearAxisTypes';

src/traces/surface/attributes.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ function makeContourAttr(axLetter) {
9797
};
9898
}
9999

100-
module.exports = overrideAll({
100+
var attrs = module.exports = overrideAll({
101101
z: {
102102
valType: 'data_array',
103103
description: 'Sets the z coordinates.'
@@ -244,3 +244,5 @@ module.exports = overrideAll({
244244
})
245245
}
246246
}, 'calc', 'nested');
247+
248+
attrs.x.editType = attrs.y.editType = attrs.z.editType = 'calc+clearAxisTypes';

0 commit comments

Comments
 (0)