Skip to content

Commit 0cba87f

Browse files
committed
Create separate functions for plot type recognition + adjust line type
1 parent b5089d3 commit 0cba87f

File tree

3 files changed

+40
-26
lines changed

3 files changed

+40
-26
lines changed

src/components/fields/TraceSelector.js

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import {UnconnectedDropdown} from './Dropdown';
22
import PropTypes from 'prop-types';
33
import React, {Component} from 'react';
4-
import nestedProperty from 'plotly.js/src/lib/nested_property';
5-
import {connectToContainer} from '../../lib';
4+
import {connectToContainer, customToPlotly, plotlyToCustom} from '../../lib';
65

76
function computeTraceOptionsFromSchema(schema) {
87
const capitalize = s => s.charAt(0).toUpperCase() + s.substring(1);
@@ -62,35 +61,15 @@ class TraceSelector extends Component {
6261
this.traceOptions = [{label: 'Scatter', value: 'scatter'}];
6362
}
6463

65-
// If we used fullData mode or fill it may be undefined if the fullTrace
66-
// is not visible and therefore does not have these values computed.
67-
const mode = nestedProperty(props.container, 'mode').get();
68-
const fill = nestedProperty(props.container, 'fill').get();
69-
const fullValue = props.fullValue;
70-
if (fullValue === 'scatter' && this.fillTypes.includes(fill)) {
71-
this.fullValue = 'area';
72-
} else if (fullValue === 'scatter' && mode === 'lines') {
73-
this.fullValue = 'line';
74-
} else {
75-
this.fullValue = fullValue;
76-
}
64+
this.fullValue = plotlyToCustom(props.fullContainer);
7765
}
7866

7967
componentWillReceiveProps(nextProps, nextContext) {
8068
this.setLocals(nextProps, nextContext);
8169
}
8270

8371
updatePlot(value) {
84-
let update;
85-
if (value === 'line') {
86-
update = {type: 'scatter', mode: 'lines', fill: 'none'};
87-
} else if (value === 'scatter') {
88-
update = {type: 'scatter', mode: 'markers', fill: 'none'};
89-
} else if (value === 'area') {
90-
update = {type: 'scatter', fill: 'tozeroy'};
91-
} else {
92-
update = {type: value};
93-
}
72+
const update = customToPlotly(value);
9473

9574
if (this.props.updateContainer) {
9675
this.props.updateContainer(update);
@@ -114,7 +93,7 @@ TraceSelector.contextTypes = {
11493

11594
TraceSelector.propTypes = {
11695
getValObject: PropTypes.func,
117-
container: PropTypes.object.isRequired,
96+
fullContainer: PropTypes.object.isRequired,
11897
fullValue: PropTypes.any.isRequired,
11998
updateContainer: PropTypes.func,
12099
};

src/lib/customTraceType.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
export function plotlyToCustom(trace) {
2+
if (
3+
trace.type === 'scatter' &&
4+
['tozeroy', 'tozerox', 'tonexty', 'tonextx', 'toself', 'tonext'].includes(
5+
trace.fill
6+
)
7+
) {
8+
return 'area';
9+
} else if (
10+
trace.type === 'scatter' &&
11+
(trace.mode === 'lines' || trace.mode === 'lines+markers')
12+
) {
13+
return 'line';
14+
}
15+
return trace.type;
16+
}
17+
18+
export function customToPlotly(customTraceType) {
19+
if (customTraceType === 'line') {
20+
return {type: 'scatter', mode: 'lines', fill: 'none'};
21+
}
22+
23+
if (customTraceType === 'scatter') {
24+
return {type: 'scatter', mode: 'markers', fill: 'none'};
25+
}
26+
27+
if (customTraceType === 'area') {
28+
return {type: 'scatter', fill: 'tozeroy'};
29+
}
30+
31+
return {type: customTraceType};
32+
}

src/lib/index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import localize, {localizeString} from './localize';
1010
import tinyColor from 'tinycolor2';
1111
import unpackPlotProps from './unpackPlotProps';
1212
import walkObject, {isPlainObject} from './walkObject';
13+
import {customToPlotly, plotlyToCustom} from './customTraceType';
1314

1415
function clamp(value, min, max) {
1516
return Math.max(min, Math.min(max, value));
@@ -33,14 +34,16 @@ export {
3334
connectLayoutToPlot,
3435
connectToContainer,
3536
connectTraceToPlot,
37+
customToPlotly,
3638
dereference,
39+
findFullTraceIndex,
3740
getDisplayName,
3841
getLayoutContext,
39-
findFullTraceIndex,
4042
icon,
4143
isPlainObject,
4244
localize,
4345
localizeString,
46+
plotlyToCustom,
4447
unpackPlotProps,
4548
walkObject,
4649
};

0 commit comments

Comments
 (0)