Skip to content

Commit f59614c

Browse files
committed
Properly initialize trace
1 parent 0685ade commit f59614c

File tree

5 files changed

+22
-11
lines changed

5 files changed

+22
-11
lines changed

examples/simple/src/App.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import createPlotComponent from 'react-plotly.js/factory';
44
import PlotlyEditor, {dereference} from 'react-plotly.js-editor';
55
import 'react-plotly.js-editor/lib/react-plotly.js-editor.css';
66

7-
87
const Plot = createPlotComponent(plotly);
98

109
class App extends Component {
@@ -24,7 +23,7 @@ class App extends Component {
2423
// overwritten with a full DOM node that contains data, layout, _fullData,
2524
// _fullLayout etc in handlePlotUpdate()
2625
const graphDiv = {
27-
data: [{type: 'scatter', xsrc: 'col1', ysrc: 'col2', mode: 'markers'}],
26+
data: [{type: 'scatter', xsrc: 'col1', ysrc: 'col2'}],
2827
layout: {title: 'Room readings'},
2928
};
3029

src/components/fields/TraceSelector.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,22 @@ class TraceSelector extends Component {
6565
this.traceOptions = [{label: 'Scatter', value: 'scatter'}];
6666
}
6767

68-
this.fullValue = plotlyTraceToCustomTrace(props.fullContainer);
68+
this.fullValue = plotlyTraceToCustomTrace(props.container);
69+
}
70+
71+
componentWillMount() {
72+
const {container} = this.props;
73+
const value = plotlyTraceToCustomTrace(container);
74+
this.updatePlot(value);
6975
}
7076

7177
componentWillReceiveProps(nextProps, nextContext) {
7278
this.setLocals(nextProps, nextContext);
7379
}
7480

7581
updatePlot(value) {
76-
const update = customTraceToPlotlyTrace(value);
82+
const {container} = this.props;
83+
const update = customTraceToPlotlyTrace(value, container);
7784

7885
if (this.props.updateContainer) {
7986
this.props.updateContainer(update);
@@ -97,7 +104,7 @@ TraceSelector.contextTypes = {
97104

98105
TraceSelector.propTypes = {
99106
getValObject: PropTypes.func,
100-
fullContainer: PropTypes.object.isRequired,
107+
container: PropTypes.object.isRequired,
101108
fullValue: PropTypes.any.isRequired,
102109
updateContainer: PropTypes.func,
103110
};

src/components/fields/__tests__/DataSelector-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ describe('DataSelector', () => {
3838
const onUpdateTraces = jest.fn();
3939
const wrapper = render({onUpdateTraces}).find(DropdownWidget);
4040
wrapper.prop('onChange')('y1');
41-
expect(onUpdateTraces.mock.calls[0][0]).toEqual({
41+
expect(onUpdateTraces.mock.calls[2][0]).toEqual({
4242
update: {xsrc: 'y1', x: [2, 3, 4]},
4343
traceIndexes: [0],
4444
});

src/components/fields/__tests__/TraceSelector-test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ describe('TraceSelector', () => {
8181
const innerDropdown = wrapper.find(Dropdown);
8282
innerDropdown.prop('onChange')('line');
8383

84-
const payload = onUpdateTraces.mock.calls[0][0];
84+
const payload = onUpdateTraces.mock.calls[1][0];
8585
expect(payload.update).toEqual({
8686
fill: 'none',
8787
mode: 'lines',
@@ -107,7 +107,7 @@ describe('TraceSelector', () => {
107107
const innerDropdown = wrapper.find(Dropdown);
108108
innerDropdown.prop('onChange')('area');
109109

110-
const payload = onUpdateTraces.mock.calls[0][0];
110+
const payload = onUpdateTraces.mock.calls[1][0];
111111
expect(payload.update).toEqual({fill: 'tozeroy', type: 'scatter'});
112112
});
113113
});

src/lib/customTraceType.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,30 @@
11
export function plotlyTraceToCustomTrace(trace) {
22
if (
33
trace.type === 'scatter' &&
4+
trace.fill &&
45
['tozeroy', 'tozerox', 'tonexty', 'tonextx', 'toself', 'tonext'].includes(
56
trace.fill
67
)
78
) {
89
return 'area';
910
} else if (
1011
trace.type === 'scatter' &&
11-
(trace.mode === 'lines' || trace.mode === 'lines+markers')
12+
(!trace.mode || trace.mode === 'lines' || trace.mode === 'lines+markers')
1213
) {
1314
return 'line';
1415
}
1516
return trace.type;
1617
}
1718

18-
export function customTraceToPlotlyTrace(customTraceType) {
19-
if (customTraceType === 'line') {
19+
export function customTraceToPlotlyTrace(customTraceType, container) {
20+
if (customTraceType === 'line' && container.mode) {
2021
return {type: 'scatter', mode: 'lines', fill: 'none'};
2122
}
2223

24+
if (customTraceType === 'line' && !container.mode) {
25+
return {type: 'scatter', mode: 'lines+markers', fill: 'none'};
26+
}
27+
2328
if (customTraceType === 'scatter') {
2429
return {type: 'scatter', mode: 'markers', fill: 'none'};
2530
}

0 commit comments

Comments
 (0)