Skip to content

Commit 5cff9aa

Browse files
committed
Set mode according to plotly.js defaults for mode
1 parent 356d0ae commit 5cff9aa

File tree

2 files changed

+41
-19
lines changed

2 files changed

+41
-19
lines changed

src/components/fields/TraceSelector.js

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ class TraceSelector extends Component {
5252
'tonext',
5353
];
5454
}
55-
5655
this.setLocals(props, context);
5756
}
5857

@@ -64,30 +63,49 @@ class TraceSelector extends Component {
6463
} else {
6564
this.traceOptions = [{label: 'Scatter', value: 'scatter'}];
6665
}
67-
6866
this.fullValue = plotlyTraceToCustomTrace(props.container);
6967
}
7068

69+
setTraceDefaults(container, fullContainer, updateContainer) {
70+
/*
71+
* Default plotly.js mode is variable:
72+
* https://github.com/plotly/plotly.js/blob/master/src/traces/scatter/defaults.js#L32
73+
* take it from fullContainer, or if it's not present (i.e. when there's no data
74+
* in the plot yet, temporarily set it to lines + markers, it will get overriden
75+
* in componentWillReceiveProps)
76+
*/
77+
78+
// set plotly.js defaults explicitly into gd.data | non empty x & y cases
79+
// we don't want to set a mode in the case of x: [], y: [], because plotly.js
80+
// default mode changes depending on # of data points, so we wait until we
81+
// have data to set the mode.
82+
if (
83+
(container.type === 'scatter' && !container.mode && fullContainer.mode) ||
84+
(!container.type && !container.mode && fullContainer.mode)
85+
) {
86+
updateContainer({
87+
type: 'scatter',
88+
mode: fullContainer.mode,
89+
fill: 'none',
90+
});
91+
}
92+
this.fullValue = plotlyTraceToCustomTrace(container);
93+
}
94+
7195
componentWillMount() {
72-
const {container} = this.props;
73-
const value = plotlyTraceToCustomTrace(container);
74-
this.updatePlot(value);
96+
const {container, fullContainer, updateContainer} = this.props;
97+
this.setTraceDefaults(container, fullContainer, updateContainer);
7598
}
7699

77100
componentWillReceiveProps(nextProps, nextContext) {
78-
if (
79-
!nextProps.container.type ||
80-
(nextProps.container.type === 'scatter' && !nextProps.container.mode)
81-
) {
82-
this.updatePlot({type: 'scatter', mode: 'lines+markers'});
83-
}
101+
const {container, fullContainer, updateContainer} = nextProps;
102+
this.setTraceDefaults(container, fullContainer, updateContainer);
84103
this.setLocals(nextProps, nextContext);
85104
}
86105

87106
updatePlot(value) {
88107
const {container} = this.props;
89108
const update = customTraceToPlotlyTrace(value, container);
90-
91109
if (this.props.updateContainer) {
92110
this.props.updateContainer(update);
93111
}
@@ -111,6 +129,7 @@ TraceSelector.contextTypes = {
111129
TraceSelector.propTypes = {
112130
getValObject: PropTypes.func,
113131
container: PropTypes.object.isRequired,
132+
fullContainer: PropTypes.object.isRequired,
114133
fullValue: PropTypes.any.isRequired,
115134
updateContainer: PropTypes.func,
116135
};

src/lib/customTraceType.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,29 @@ export function plotlyTraceToCustomTrace(trace) {
1212
(!trace.mode || trace.mode === 'lines' || trace.mode === 'lines+markers')
1313
) {
1414
return 'line';
15-
} else if (!trace.type) {
16-
return 'line';
1715
}
1816
return trace.type;
1917
}
2018

2119
export function customTraceToPlotlyTrace(customTraceType, container) {
22-
if (customTraceType === 'line' && container.mode) {
20+
// we don't want to set a mode until there is data in the plot,
21+
// in this case TraceSelector's componentWillReceiveProps will set the mode to
22+
// the plotly.js default mode, we don't want to override this here, this is why
23+
// we always check for container.mode in our conditionals
24+
25+
if (customTraceType === 'line' && container.mode === 'markers') {
2326
return {type: 'scatter', mode: 'lines', fill: 'none'};
2427
}
2528

26-
if (customTraceType === 'line' && !container.mode) {
27-
return {type: 'scatter', mode: 'lines+markers', fill: 'none'};
29+
if (customTraceType === 'line' && container.mode) {
30+
return {type: 'scatter', mode: container.mode, fill: 'none'};
2831
}
2932

30-
if (customTraceType === 'scatter') {
33+
if (customTraceType === 'scatter' && container.mode) {
3134
return {type: 'scatter', mode: 'markers', fill: 'none'};
3235
}
3336

34-
if (customTraceType === 'area') {
37+
if (customTraceType === 'area' && container.mode) {
3538
return {type: 'scatter', fill: 'tozeroy'};
3639
}
3740

0 commit comments

Comments
 (0)