Skip to content

Commit e868a22

Browse files
aulneauVeraZab
authored andcommitted
abstract out computTraceOptions
1 parent 9e7d20d commit e868a22

File tree

4 files changed

+63
-67
lines changed

4 files changed

+63
-67
lines changed

src/components/fields/TraceSelector.js

Lines changed: 2 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -6,71 +6,10 @@ import {
66
traceTypeToPlotlyInitFigure,
77
localize,
88
plotlyTraceToCustomTrace,
9+
computeTraceOptionsFromSchema,
910
} from 'lib';
10-
import {EDITOR_ACTIONS} from 'lib/constants';
1111
import TraceTypeSelector from 'components/widgets/TraceTypeSelector';
1212

13-
function computeTraceOptionsFromSchema(schema, _, context) {
14-
// Filter out Polar "area" type as it is fairly broken and we want to present
15-
// scatter with fill as an "area" chart type for convenience.
16-
const traceTypes = Object.keys(schema.traces).filter(
17-
t => !['area', 'scattermapbox'].includes(t)
18-
);
19-
20-
// explicit map of all supported trace types (as of plotlyjs 1.32)
21-
const traceOptions = [
22-
{value: 'scatter', label: _('Scatter')},
23-
{value: 'box', label: _('Box')},
24-
{value: 'bar', label: _('Bar')},
25-
{value: 'heatmap', label: _('Heatmap')},
26-
// {value: 'histogram', label: _('Histogram')},
27-
// {value: 'histogram2d', label: _('2D Histogram')},
28-
// {value: 'histogram2dcontour', label: _('2D Contour Histogram')},
29-
{value: 'pie', label: _('Pie')},
30-
{value: 'contour', label: _('Contour')},
31-
{value: 'scatterternary', label: _('Ternary Scatter')},
32-
// {value: 'violin', label: _('Violin')},
33-
{value: 'scatter3d', label: _('3D Scatter')},
34-
{value: 'surface', label: _('Surface')},
35-
{value: 'mesh3d', label: _('3D Mesh')},
36-
{value: 'scattergeo', label: _('Atlas Map')},
37-
{value: 'choropleth', label: _('Choropleth')},
38-
// {value: 'scattergl', label: _('Scatter GL')},
39-
// {value: 'pointcloud', label: _('Point Cloud')},
40-
// {value: 'heatmapgl', label: _('Heatmap GL')},
41-
// {value: 'parcoords', label: _('Parallel Coordinates')},
42-
// {value: 'sankey', label: _('Sankey')},
43-
// {value: 'table', label: _('Table')},
44-
// {value: 'carpet', label: _('Carpet')},
45-
// {value: 'scattercarpet', label: _('Carpet Scatter')},
46-
// {value: 'contourcarpet', label: _('Carpet Contour')},
47-
{value: 'ohlc', label: _('OHLC')},
48-
{value: 'candlestick', label: _('Candlestick')},
49-
// {value: 'scatterpolar', label: _('Polar Scatter')},
50-
].filter(obj => traceTypes.indexOf(obj.value) !== -1);
51-
52-
const traceIndex = traceType =>
53-
traceOptions.findIndex(opt => opt.value === traceType);
54-
55-
traceOptions.splice(
56-
traceIndex('scatter') + 1,
57-
0,
58-
{label: _('Line'), value: 'line'},
59-
{label: _('Area'), value: 'area'}
60-
);
61-
62-
traceOptions.splice(traceIndex('scatter3d') + 1, 0, {
63-
label: _('3D Line'),
64-
value: 'line3d',
65-
});
66-
67-
if (context.config && context.config.mapboxAccessToken) {
68-
traceOptions.push({value: 'scattermapbox', label: _('Satellite Map')});
69-
}
70-
71-
return traceOptions;
72-
}
73-
7413
class TraceSelector extends Component {
7514
constructor(props, context) {
7615
super(props, context);
@@ -140,7 +79,6 @@ class TraceSelector extends Component {
14079
}
14180

14281
updatePlot(value) {
143-
14482
const {updateContainer} = this.props;
14583
if (updateContainer) {
14684
updateContainer(traceTypeToPlotlyInitFigure(value));
@@ -160,9 +98,7 @@ class TraceSelector extends Component {
16098
return (
16199
<div
162100
className="trace-type-select-dropdown__wrapper"
163-
onClick={() =>
164-
this.context.openModal(TraceTypeSelector, props)
165-
}
101+
onClick={() => this.context.openModal(TraceTypeSelector, props)}
166102
>
167103
<UnconnectedDropdown {...props} />
168104
</div>

src/components/widgets/TraceTypeSelector.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React, {Component} from 'react';
22
import PropTypes from 'prop-types';
33
import {SearchIcon, ThumnailViewIcon, GraphIcon} from 'plotly-icons';
44
import Modal, {ModalContent} from 'components/containers/Modal';
5-
import {traceTypeToPlotlyInitFigure} from 'lib';
5+
import {traceTypeToPlotlyInitFigure, computeTraceOptionsFromSchema} from 'lib';
66

77
// to be removed when icons are converted to svg
88
function slugify(text) {
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
function computeTraceOptionsFromSchema(schema, _, context) {
2+
// Filter out Polar "area" type as it is fairly broken and we want to present
3+
// scatter with fill as an "area" chart type for convenience.
4+
const traceTypes = Object.keys(schema.traces).filter(
5+
t => !['area', 'scattermapbox'].includes(t)
6+
);
7+
8+
// explicit map of all supported trace types (as of plotlyjs 1.32)
9+
const traceOptions = [
10+
{value: 'scatter', label: _('Scatter')},
11+
{value: 'box', label: _('Box')},
12+
{value: 'bar', label: _('Bar')},
13+
{value: 'heatmap', label: _('Heatmap')},
14+
{value: 'histogram', label: _('Histogram')},
15+
{value: 'histogram2d', label: _('2D Histogram')},
16+
{value: 'histogram2dcontour', label: _('2D Contour Histogram')},
17+
{value: 'pie', label: _('Pie')},
18+
{value: 'contour', label: _('Contour')},
19+
{value: 'scatterternary', label: _('Ternary Scatter')},
20+
{value: 'violin', label: _('Violin')},
21+
{value: 'scatter3d', label: _('3D Scatter')},
22+
{value: 'surface', label: _('Surface')},
23+
{value: 'mesh3d', label: _('Mesh3d')},
24+
{value: 'scattergeo', label: _('Atlas Map')},
25+
{value: 'choropleth', label: _('Choropleth')},
26+
{value: 'scattergl', label: _('Scatter GL')},
27+
{value: 'pointcloud', label: _('Point Cloud')},
28+
{value: 'heatmapgl', label: _('Heatmap GL')},
29+
{value: 'parcoords', label: _('Parallel Coordinates')},
30+
31+
{value: 'sankey', label: _('Sankey')},
32+
{value: 'table', label: _('Table')},
33+
{value: 'carpet', label: _('Carpet')},
34+
{value: 'scattercarpet', label: _('Carpet Scatter')},
35+
{value: 'contourcarpet', label: _('Carpet Contour')},
36+
{value: 'ohlc', label: _('OHLC')},
37+
{value: 'candlestick', label: _('Candlestick')},
38+
{value: 'scatterpolar', label: _('Polar Scatter')},
39+
].filter(obj => traceTypes.indexOf(obj.value) !== -1);
40+
41+
const i = traceOptions.findIndex(opt => opt.value === 'scatter');
42+
traceOptions.splice(
43+
i + 1,
44+
0,
45+
{label: _('Line'), value: 'line'},
46+
{label: _('Area'), value: 'area'},
47+
{label: _('Timeseries'), value: 'timeseries'}
48+
);
49+
50+
if (context.config && context.config.mapboxAccessToken) {
51+
traceOptions.push({value: 'scattermapbox', label: _('Satellite Map')});
52+
}
53+
54+
return traceOptions;
55+
}
56+
57+
export {computeTraceOptionsFromSchema};

src/lib/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ function renderTraceIcon(trace) {
5252
: PlotlyIcons.PlotLineIcon;
5353
}
5454

55+
import {computeTraceOptionsFromSchema} from './computeTraceOptionsFromSchema';
56+
5557
export {
5658
axisIdToAxisName,
5759
bem,
@@ -65,6 +67,7 @@ export {
6567
connectToContainer,
6668
connectTraceToPlot,
6769
containerConnectedContextTypes,
70+
computeTraceOptionsFromSchema,
6871
traceTypeToPlotlyInitFigure,
6972
dereference,
7073
findFullTraceIndex,

0 commit comments

Comments
 (0)