Skip to content

Commit 2e7e923

Browse files
committed
Allow TextPosition component to be a data array
1 parent 31f6228 commit 2e7e923

File tree

3 files changed

+106
-28
lines changed

3 files changed

+106
-28
lines changed

src/components/fields/TextPosition.js

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
import Dropdown from './Dropdown';
2+
import RadioBlocks from '../widgets/RadioBlocks';
3+
import Field from './Field';
4+
import PropTypes from 'prop-types';
5+
import React, {Component, Fragment} from 'react';
6+
import {connectToContainer} from 'lib';
7+
import Info from './Info';
8+
import DataSelector from './DataSelector';
9+
10+
export class UnconnectedTextPosition extends Component {
11+
constructor(props) {
12+
super(props);
13+
this.state = {
14+
posType: typeof props.fullValue === 'string' ? 'simple' : 'custom',
15+
};
16+
}
17+
18+
render() {
19+
const _ = this.context.localize;
20+
const radioOptions = [
21+
{label: _('All'), value: 'simple'},
22+
{label: _('Custom'), value: 'custom'},
23+
];
24+
const control =
25+
this.state.posType === 'simple' ? (
26+
<Fragment>
27+
<Info>
28+
{_(
29+
'This will position all text values on the plot according to the selected position.'
30+
)}
31+
</Info>
32+
<Dropdown options={this.props.options} attr="textposition" />
33+
</Fragment>
34+
) : (
35+
<Fragment>
36+
<Info>
37+
<div>
38+
{_(
39+
'This will position text values individually, according to the provided data positions array. '
40+
)}
41+
</div>
42+
</Info>
43+
<DataSelector attr="textposition" />
44+
<Info>
45+
<div>{_('("Top", "Middle", "Bottom") + ("Left", "Center", "Right")')}</div>
46+
</Info>
47+
</Fragment>
48+
);
49+
50+
return (
51+
<Field {...this.props}>
52+
<RadioBlocks
53+
options={radioOptions}
54+
activeOption={this.state.posType}
55+
onOptionChange={value => {
56+
this.setState({posType: value});
57+
if (value === 'simple') {
58+
this.props.updatePlot('middle center');
59+
} else {
60+
this.props.updateContainer({textpositionsrc: null});
61+
}
62+
}}
63+
/>
64+
{control}
65+
</Field>
66+
);
67+
}
68+
}
69+
70+
UnconnectedTextPosition.propTypes = {
71+
...Field.propTypes,
72+
options: PropTypes.array,
73+
fullValue: PropTypes.oneOfType([PropTypes.array, PropTypes.string]),
74+
};
75+
76+
UnconnectedTextPosition.contextTypes = {
77+
localize: PropTypes.func,
78+
};
79+
80+
export default connectToContainer(UnconnectedTextPosition, {
81+
modifyPlotProps: (props, context, plotProps) => {
82+
const {localize: _} = context;
83+
let options = [
84+
{label: _('Top Left'), value: 'top left'},
85+
{label: _('Top Center'), value: 'top center'},
86+
{label: _('Top Right'), value: 'top right'},
87+
{label: _('Middle Left'), value: 'middle left'},
88+
{label: _('Middle Center'), value: 'middle center'},
89+
{label: _('Middle Right'), value: 'middle right'},
90+
{label: _('Bottom Left'), value: 'bottom left'},
91+
{label: _('Bottom Center'), value: 'bottom center'},
92+
{label: _('Bottom Right'), value: 'bottom right'},
93+
];
94+
if (context.container.type === 'pie' || context.container.type === 'bar') {
95+
options = [
96+
{label: _('Inside'), value: 'inside'},
97+
{label: _('Outside'), value: 'outside'},
98+
{label: _('Auto'), value: 'auto'},
99+
{label: _('None'), value: 'none'},
100+
];
101+
}
102+
plotProps.options = options;
103+
plotProps.clearable = false;
104+
},
105+
});

src/components/fields/derived.js

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -494,33 +494,6 @@ function computeAxesRefOptions(axes, propsAttr) {
494494
return options;
495495
}
496496

497-
export const TextPosition = connectToContainer(UnconnectedDropdown, {
498-
modifyPlotProps: (props, context, plotProps) => {
499-
const {localize: _} = context;
500-
let options = [
501-
{label: _('Top Left'), value: 'top left'},
502-
{label: _('Top Center'), value: 'top center'},
503-
{label: _('Top Right'), value: 'top right'},
504-
{label: _('Middle Left'), value: 'middle left'},
505-
{label: _('Middle Center'), value: 'middle center'},
506-
{label: _('Middle Right'), value: 'middle right'},
507-
{label: _('Bottom Left'), value: 'bottom left'},
508-
{label: _('Bottom Center'), value: 'bottom center'},
509-
{label: _('Bottom Right'), value: 'bottom right'},
510-
];
511-
if (context.container.type === 'pie' || context.container.type === 'bar') {
512-
options = [
513-
{label: _('Inside'), value: 'inside'},
514-
{label: _('Outside'), value: 'outside'},
515-
{label: _('Auto'), value: 'auto'},
516-
{label: _('None'), value: 'none'},
517-
];
518-
}
519-
plotProps.options = options;
520-
plotProps.clearable = false;
521-
},
522-
});
523-
524497
export const TextInfo = connectToContainer(UnconnectedFlaglist, {
525498
modifyPlotProps: (props, context, plotProps) => {
526499
const {localize: _, container} = context;

src/components/fields/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import RectanglePositioner from './RectanglePositioner';
3434
import LocationSelector from './LocationSelector';
3535
import AxisInterval from './AxisInterval';
3636
import DateTimePicker from './DateTimePicker';
37+
import TextPosition from './TextPosition';
3738

3839
import {
3940
AnnotationArrowRef,
@@ -57,7 +58,6 @@ import {
5758
TraceOrientation,
5859
AxisOverlayDropdown,
5960
AxisSide,
60-
TextPosition,
6161
ShowInLegend,
6262
HoveronDropdown,
6363
HovermodeDropdown,

0 commit comments

Comments
 (0)