Skip to content

Commit 758e07b

Browse files
committed
minor refactor
1 parent e17d153 commit 758e07b

File tree

6 files changed

+84
-55
lines changed

6 files changed

+84
-55
lines changed

src/components/containers/SubplotAccordion.js

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
connectCartesianSubplotToLayout,
88
connectNonCartesianSubplotToLayout,
99
} from 'lib';
10-
import {TRACE_TO_AXIS, AXIS_TO_ATTR} from 'lib/constants';
10+
import {TRACE_TO_AXIS, SUBPLOT_TO_ATTR} from 'lib/constants';
1111

1212
const TraceFold = connectTraceToPlot(PlotlyFold);
1313
const NonCartesianSubplotFold = connectNonCartesianSubplotToLayout(PlotlyFold);
@@ -59,24 +59,49 @@ class SubplotAccordion extends Component {
5959
))
6060
);
6161

62+
// For each key in layout, find all traces that belong to this subplot.
63+
// E.g. if layout attr is 'ternary', find all traces that are of type
64+
// that has subplot ternary, if layout attr is 'ternary2', find traces
65+
// of right type that have attr 'subplot': 'ternary' in their data.
66+
67+
/**
68+
Example:
69+
{
70+
"data": [
71+
{
72+
"type": "scatterternary",
73+
"mode": "markers",
74+
},
75+
{
76+
"type": "scatterternary",
77+
"mode": "markers",
78+
"subplot": "ternary2"
79+
}
80+
],
81+
"layout": {
82+
"ternary": {},
83+
"ternary2": {},
84+
},
85+
}
86+
*/
87+
6288
Object.keys(layout).forEach(layoutKey => {
6389
const traceIndexes = [];
6490
if (
65-
['geo', 'mapbox', 'polar', 'gl3d', 'ternary'].some(traceType => {
91+
['geo', 'mapbox', 'polar', 'gl3d', 'ternary'].some(subplotType => {
6692
const trIndex =
67-
(traceType === 'gl3d' ? 'scene' : traceType) === layoutKey
93+
SUBPLOT_TO_ATTR[subplotType].layout === layoutKey
6894
? data.findIndex(trace =>
69-
TRACE_TO_AXIS[traceType].some(tt => tt === trace.type)
95+
TRACE_TO_AXIS[subplotType].some(tt => tt === trace.type)
7096
)
7197
: data.findIndex(
72-
trace => trace[AXIS_TO_ATTR[traceType]] === layoutKey
98+
trace =>
99+
trace[SUBPLOT_TO_ATTR[subplotType].data] === layoutKey
73100
);
74101
if (trIndex !== -1) {
75102
traceIndexes.push(trIndex);
76103
}
77-
return layoutKey.startsWith(
78-
traceType === 'gl3d' ? 'scene' : traceType
79-
);
104+
return layoutKey.startsWith(SUBPLOT_TO_ATTR[subplotType].layout);
80105
})
81106
) {
82107
subplotFolds[traceIndexes[0]] = (

src/components/fields/AxesCreator.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,6 @@ class UnconnectedAxesCreator extends Component {
148148
}));
149149
}
150150

151-
// for the moment only cartesian subplots are supported
152151
if (axisType === 'cartesian') {
153152
['xaxis', 'yaxis'].forEach((type, index) => {
154153
controls.push(

src/components/fields/SubplotCreator.js

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import Dropdown from './Dropdown';
22
import Info from './Info';
33
import PropTypes from 'prop-types';
44
import React, {Component} from 'react';
5-
import {EDITOR_ACTIONS, AXIS_TO_ATTR} from 'lib/constants';
5+
import {EDITOR_ACTIONS, SUBPLOT_TO_ATTR} from 'lib/constants';
66
import Button from '../widgets/Button';
77
import {PlusIcon} from 'plotly-icons';
88
import {connectToContainer, traceTypeToAxisType, getSubplotTitle} from 'lib';
@@ -25,20 +25,18 @@ class UnconnectedSingleSubplotCreator extends Component {
2525
const lastSubplotNumber =
2626
Number(
2727
subplots[layoutAttr][subplots[layoutAttr].length - 1].split(
28-
layoutAttr === 'gl3d' ? 'scene' : layoutAttr
28+
SUBPLOT_TO_ATTR[layoutAttr].layout
2929
)[1]
3030
) || 1;
3131

3232
updateContainer({
33-
[attr]:
34-
(layoutAttr === 'gl3d' ? 'scene' : layoutAttr) +
35-
(lastSubplotNumber + 1),
33+
[attr]: SUBPLOT_TO_ATTR[layoutAttr].layout + (lastSubplotNumber + 1),
3634
});
3735
}
3836

3937
updateSubplot(update) {
4038
const currentSubplotId = this.props.fullContainer[
41-
AXIS_TO_ATTR[this.props.attr]
39+
SUBPLOT_TO_ATTR[this.props.attr].data
4240
];
4341
let subplotToBeGarbageCollected = null;
4442

@@ -47,7 +45,7 @@ class UnconnectedSingleSubplotCreator extends Component {
4745
currentSubplotId !== update &&
4846
!this.context.fullData.some(
4947
trace =>
50-
trace[AXIS_TO_ATTR[this.props.attr]] === currentSubplotId &&
48+
trace[SUBPLOT_TO_ATTR[this.props.attr].data] === currentSubplotId &&
5149
trace.index !== this.props.fullContainer.index
5250
)
5351
) {
@@ -141,9 +139,9 @@ class UnconnectedSubplotCreator extends Component {
141139
return (
142140
<PlotlySection name={_('Subplots to Use')}>
143141
<SingleSubplotCreator
144-
attr={AXIS_TO_ATTR[subplotType]}
142+
attr={SUBPLOT_TO_ATTR[subplotType].data}
145143
layoutAttr={subplotType}
146-
label={subplotType === 'gl3d' ? 'scene' : subplotType}
144+
label={SUBPLOT_TO_ATTR[subplotType].layout}
147145
options={getOptions(subplotType)}
148146
/>
149147
<Info>

src/components/widgets/TraceTypeSelector.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ class TraceTypeSelector extends Component {
8787
glAvailable(computedValue.type) &&
8888
!computedValue.type.endsWith('gl')
8989
) {
90-
computedValue.type = computedValue.type + 'gl';
90+
computedValue.type += 'gl';
9191
}
9292
updateContainer(computedValue);
9393
this.context.handleClose();

src/lib/connectTraceToPlot.js

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
traceTypeToAxisType,
99
} from '../lib';
1010
import {deepCopyPublic, setMultiValuedContainer} from './multiValues';
11-
import {EDITOR_ACTIONS, AXIS_TO_ATTR} from 'lib/constants';
11+
import {EDITOR_ACTIONS, SUBPLOT_TO_ATTR} from 'lib/constants';
1212

1313
export default function connectTraceToPlot(WrappedComponent) {
1414
class TraceConnectedComponent extends Component {
@@ -143,37 +143,40 @@ export default function connectTraceToPlot(WrappedComponent) {
143143
});
144144
return;
145145
}
146-
const subplotType = traceTypeToAxisType(currentTrace.type);
147-
const subplotNames =
148-
subplotType === 'cartesian'
149-
? [currentTrace.xaxis || 'xaxis', currentTrace.yaxis || 'yaxis']
150-
: currentTrace[AXIS_TO_ATTR[subplotType]] ||
151-
AXIS_TO_ATTR[subplotType];
152146
const axesToBeGarbageCollected = [];
153147
let subplotToBeGarbageCollected = null;
148+
const subplotType = traceTypeToAxisType(currentTrace.type);
154149

155-
const isSubplotUsedAnywhereElse = (subplotType, subplotName) =>
156-
this.context.fullData.some(
157-
trace =>
158-
(trace[AXIS_TO_ATTR[subplotType]] === subplotName ||
159-
(((subplotType === 'xaxis' || subplotType === 'yaxis') &&
160-
subplotName.charAt(1)) === '' ||
161-
(subplotName.split(subplotType)[1] === '' &&
162-
trace[AXIS_TO_ATTR[subplotType]] === null))) &&
163-
trace.index !== this.props.traceIndexes[0]
164-
);
165-
166-
// When we delete a subplot, make sure no unused axes/subplots are left
167-
if (subplotType === 'cartesian') {
168-
if (!isSubplotUsedAnywhereElse('xaxis', subplotNames[0])) {
169-
axesToBeGarbageCollected.push(subplotNames[0]);
170-
}
171-
if (!isSubplotUsedAnywhereElse('yaxis', subplotNames[1])) {
172-
axesToBeGarbageCollected.push(subplotNames[1]);
173-
}
174-
} else {
175-
if (!isSubplotUsedAnywhereElse(subplotType, subplotNames)) {
176-
subplotToBeGarbageCollected = subplotNames;
150+
if (subplotType) {
151+
const subplotNames =
152+
subplotType === 'cartesian'
153+
? [currentTrace.xaxis || 'xaxis', currentTrace.yaxis || 'yaxis']
154+
: currentTrace[SUBPLOT_TO_ATTR[subplotType].data] ||
155+
SUBPLOT_TO_ATTR[subplotType].data;
156+
157+
const isSubplotUsedAnywhereElse = (subplotType, subplotName) =>
158+
this.context.fullData.some(
159+
trace =>
160+
(trace[SUBPLOT_TO_ATTR[subplotType].data] === subplotName ||
161+
(((subplotType === 'xaxis' || subplotType === 'yaxis') &&
162+
subplotName.charAt(1)) === '' ||
163+
(subplotName.split(subplotType)[1] === '' &&
164+
trace[SUBPLOT_TO_ATTR[subplotType].data] === null))) &&
165+
trace.index !== this.props.traceIndexes[0]
166+
);
167+
168+
// When we delete a subplot, make sure no unused axes/subplots are left
169+
if (subplotType === 'cartesian') {
170+
if (!isSubplotUsedAnywhereElse('xaxis', subplotNames[0])) {
171+
axesToBeGarbageCollected.push(subplotNames[0]);
172+
}
173+
if (!isSubplotUsedAnywhereElse('yaxis', subplotNames[1])) {
174+
axesToBeGarbageCollected.push(subplotNames[1]);
175+
}
176+
} else {
177+
if (!isSubplotUsedAnywhereElse(subplotType, subplotNames)) {
178+
subplotToBeGarbageCollected = subplotNames;
179+
}
177180
}
178181
}
179182

src/lib/constants.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,17 @@ export const TRACE_TO_AXIS = {
7676
polar: ['scatterpolar', 'scatterpolargl'],
7777
};
7878

79-
export const AXIS_TO_ATTR = {
80-
cartesian: ['xaxis', 'yaxis'],
81-
ternary: 'subplot',
82-
gl3d: 'scene',
83-
geo: 'geo',
84-
mapbox: 'subplot',
85-
polar: 'subplot',
79+
// Note: scene, and xaxis/yaxis were added for convenience sake even though they're not subplot types
80+
export const SUBPLOT_TO_ATTR = {
81+
cartesian: {data: ['xaxis', 'yaxis'], layout: ['x', 'y']},
82+
xaxis: {data: ['xaxis', 'yaxis'], layout: ['x', 'y']},
83+
yaxis: {data: ['xaxis', 'yaxis'], layout: ['x', 'y']},
84+
ternary: {data: 'subplot', layout: 'ternary'},
85+
gl3d: {data: 'scene', layout: 'scene'},
86+
scene: {data: 'scene', layout: 'scene'},
87+
geo: {data: 'geo', layout: 'geo'},
88+
mapbox: {data: 'subplot', layout: 'mapbox'},
89+
polar: {data: 'subplot', layout: 'polar'},
8690
};
8791

8892
export const TRANSFORMS_LIST = ['filter', 'groupby', 'aggregate'];

0 commit comments

Comments
 (0)