Skip to content

Commit f3ac4cc

Browse files
authored
Merge pull request #191 from plotly/trace-name-fix
Trace name fix
2 parents 7aa85e9 + 8f85a6c commit f3ac4cc

File tree

7 files changed

+67
-40
lines changed

7 files changed

+67
-40
lines changed

examples/simple/src/App.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class App extends Component {
2424
// overwritten with a full DOM node that contains data, layout, _fullData,
2525
// _fullLayout etc in handlePlotUpdate()
2626
const graphDiv = {
27-
data: [{type: 'scatter', xsrc: 'col1', ysrc: 'col2'}],
27+
data: [{type: 'scatter', xsrc: 'col1', ysrc: 'col2', mode: 'markers'}],
2828
layout: {title: 'Room readings'},
2929
};
3030

src/PlotlyEditor.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ class PlotlyEditor extends Component {
9393
if (this.props.onAddTrace) {
9494
this.props.onAddTrace(payload);
9595
}
96-
graphDiv.data.push({x: [], y: []});
96+
graphDiv.data.push({x: [], y: [], type: 'scatter', mode: 'markers'});
9797
if (this.props.onUpdate) {
9898
this.props.onUpdate();
9999
}

src/components/fields/Field.js

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,6 @@ import {bem, localize} from 'lib';
66
import {getMultiValueText} from 'lib/constants';
77

88
class Field extends Component {
9-
renderPostfix() {
10-
const {units} = this.props;
11-
if (!units) {
12-
return null;
13-
}
14-
return (
15-
<div className={bem('field', 'units')}>
16-
<div className={bem('field', 'units-text')}>{units}</div>
17-
</div>
18-
);
19-
}
20-
219
render() {
2210
const {
2311
center,

src/components/fields/TraceSelector.js

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
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 {
5+
connectToContainer,
6+
customTraceToPlotlyTrace,
7+
plotlyTraceToCustomTrace,
8+
} from '../../lib';
69

710
function computeTraceOptionsFromSchema(schema) {
811
const capitalize = s => s.charAt(0).toUpperCase() + s.substring(1);
@@ -62,35 +65,15 @@ class TraceSelector extends Component {
6265
this.traceOptions = [{label: 'Scatter', value: 'scatter'}];
6366
}
6467

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-
}
68+
this.fullValue = plotlyTraceToCustomTrace(props.fullContainer);
7769
}
7870

7971
componentWillReceiveProps(nextProps, nextContext) {
8072
this.setLocals(nextProps, nextContext);
8173
}
8274

8375
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-
}
76+
const update = customTraceToPlotlyTrace(value);
9477

9578
if (this.props.updateContainer) {
9679
this.props.updateContainer(update);
@@ -114,7 +97,7 @@ TraceSelector.contextTypes = {
11497

11598
TraceSelector.propTypes = {
11699
getValObject: PropTypes.func,
117-
container: PropTypes.object.isRequired,
100+
fullContainer: PropTypes.object.isRequired,
118101
fullValue: PropTypes.any.isRequired,
119102
updateContainer: PropTypes.func,
120103
};

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,24 @@ describe('TraceSelector', () => {
4545
expect(innerDropdown.prop('value')).toEqual('line');
4646
});
4747

48+
it('interprets scatter + mode=lines+markers as type=line', () => {
49+
const editorProps = {
50+
...fixtures.scatter({data: [{mode: 'lines+markers'}]}),
51+
onUpdate: jest.fn(),
52+
};
53+
const wrapper = mount(
54+
<TestEditor {...editorProps} plotly={plotly}>
55+
<TraceSection traceIndex={0}>
56+
<TraceSelector attr="type" />
57+
</TraceSection>
58+
</TestEditor>
59+
).find(TraceSelector);
60+
61+
const innerDropdown = wrapper.find(Dropdown);
62+
63+
expect(innerDropdown.prop('value')).toEqual('line');
64+
});
65+
4866
it('updates type=scatter mode=lines when type=line', () => {
4967
const onUpdateTraces = jest.fn();
5068
const editorProps = {

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 plotlyTraceToCustomTrace(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 customTraceToPlotlyTrace(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: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ import localize, {localizeString} from './localize';
1010
import tinyColor from 'tinycolor2';
1111
import unpackPlotProps from './unpackPlotProps';
1212
import walkObject, {isPlainObject} from './walkObject';
13+
import {
14+
customTraceToPlotlyTrace,
15+
plotlyTraceToCustomTrace,
16+
} from './customTraceType';
1317

1418
function clamp(value, min, max) {
1519
return Math.max(min, Math.min(max, value));
@@ -33,14 +37,16 @@ export {
3337
connectLayoutToPlot,
3438
connectToContainer,
3539
connectTraceToPlot,
40+
customTraceToPlotlyTrace,
3641
dereference,
42+
findFullTraceIndex,
3743
getDisplayName,
3844
getLayoutContext,
39-
findFullTraceIndex,
4045
icon,
4146
isPlainObject,
4247
localize,
4348
localizeString,
49+
plotlyTraceToCustomTrace,
4450
unpackPlotProps,
4551
walkObject,
4652
};

0 commit comments

Comments
 (0)