Skip to content

Commit 8b64ccd

Browse files
authored
Merge pull request #905 from plotly/hover-namelength
Add hoverlabel.namelength widget
2 parents ee9eed9 + 0ad6665 commit 8b64ccd

File tree

5 files changed

+103
-6
lines changed

5 files changed

+103
-6
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import React, {Component} from 'react';
2+
import PropTypes from 'prop-types';
3+
import {connectToContainer} from 'lib';
4+
import Field from './Field';
5+
import RadioBlocks from '../widgets/RadioBlocks';
6+
import NumericInput from '../widgets/NumericInput';
7+
8+
export class UnconnectedHoverLabelNameLength extends Component {
9+
constructor(props) {
10+
super(props);
11+
this.state = {
12+
currentOption: this.getCurrentOption(props),
13+
};
14+
this.onOptionChange = this.onOptionChange.bind(this);
15+
}
16+
17+
getCurrentOption(props) {
18+
return props.fullValue > 0 ? 'clip' : props.fullValue === 0 ? 'hide' : 'no-clip';
19+
}
20+
21+
componentWillReceiveProps(nextProps) {
22+
if (nextProps.fullValue !== this.props.fullValue) {
23+
this.setState({
24+
currentOption: this.getCurrentOption(nextProps),
25+
});
26+
}
27+
}
28+
29+
onOptionChange(option) {
30+
if (this.state.currentOption !== 'clip' && option === 'clip') {
31+
// this allows us to go back to plotly.js default if we've
32+
// clicked away from the 'clip' option.
33+
this.props.updatePlot(15); //eslint-disable-line
34+
return;
35+
}
36+
if (option === 'no-clip') {
37+
this.props.updatePlot(-1);
38+
return;
39+
}
40+
if (option === 'hide') {
41+
this.props.updatePlot(0);
42+
return;
43+
}
44+
}
45+
46+
render() {
47+
const _ = this.context.localize;
48+
49+
return (
50+
<Field {...this.props}>
51+
<RadioBlocks
52+
activeOption={this.state.currentOption}
53+
options={[
54+
{label: _('Clip To'), value: 'clip'},
55+
{label: _('No Clip'), value: 'no-clip'},
56+
{label: _('Hide'), value: 'hide'},
57+
]}
58+
onOptionChange={this.onOptionChange}
59+
/>
60+
<div style={{height: '10px', width: '100%'}} />
61+
{this.state.currentOption === 'clip' ? (
62+
<NumericInput
63+
value={this.props.fullValue}
64+
onChange={this.props.updatePlot}
65+
onUpdate={this.props.updatePlot}
66+
units="px"
67+
/>
68+
) : null}
69+
</Field>
70+
);
71+
}
72+
}
73+
74+
UnconnectedHoverLabelNameLength.propTypes = {
75+
fullValue: PropTypes.number,
76+
updatePlot: PropTypes.func,
77+
...Field.propTypes,
78+
};
79+
80+
UnconnectedHoverLabelNameLength.contextTypes = {
81+
localize: PropTypes.func,
82+
};
83+
84+
export default connectToContainer(UnconnectedHoverLabelNameLength, {
85+
modifyPlotProps: (props, context, plotProps) => {
86+
const {container} = plotProps;
87+
plotProps.isVisible =
88+
(container.hoverinfo && container.hoverinfo.includes('name')) ||
89+
(container.hovertemplate || container.hovertemplate === ' ');
90+
},
91+
});

src/components/fields/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import LocationSelector from './LocationSelector';
3636
import AxisInterval from './AxisInterval';
3737
import DateTimePicker from './DateTimePicker';
3838
import TextPosition from './TextPosition';
39+
import HoverLabelNameLength from './HoverLabelNameLength';
3940

4041
export * from './derived';
4142
export * from './LineSelectors';
@@ -80,4 +81,5 @@ export {
8081
AxisInterval,
8182
NumericOrDate,
8283
DateTimePicker,
84+
HoverLabelNameLength,
8385
};

src/components/widgets/NumericInput.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ export default class NumericInput extends Component {
185185
/>
186186
{this.renderArrows()}
187187
{this.renderSlider()}
188+
{this.props.units ? this.props.units : null}
188189
</div>
189190
);
190191
}
@@ -203,6 +204,7 @@ NumericInput.propTypes = {
203204
step: PropTypes.number,
204205
stepmode: PropTypes.string,
205206
value: PropTypes.any,
207+
units: PropTypes.string,
206208
};
207209

208210
NumericInput.defaultProps = {

src/default_panels/StyleLayoutPanel.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -184,12 +184,12 @@ const StyleLayoutPanel = (props, {localize: _}) => (
184184
{_(
185185
'You can refer to the items in this column in any text fields of the editor like so: '
186186
)}
187-
<p>
188-
{_('Ex: ')}
189-
<span style={{letterSpacing: '1px', fontStyle: 'italic', userSelect: 'text'}}>
190-
{_('My custom title %{meta[1]}')}
191-
</span>
192-
</p>
187+
</p>
188+
<p>
189+
{_('Ex: ')}
190+
<span style={{letterSpacing: '1px', fontStyle: 'italic', userSelect: 'text'}}>
191+
{_('My custom title %{meta[1]}')}
192+
</span>
193193
</p>
194194
</Info>
195195
</PlotlyFold>

src/default_panels/StyleTracesPanel.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import {
3737
GroupCreator,
3838
NumericOrDate,
3939
AxisInterval,
40+
HoverLabelNameLength,
4041
} from '../components';
4142
import {
4243
BinningDropdown,
@@ -737,6 +738,7 @@ const StyleTracesPanel = (props, {localize: _}) => (
737738
attr="hoverlabel.split"
738739
options={[{label: _('Yes'), value: true}, {label: _('No'), value: false}]}
739740
/>
741+
<HoverLabelNameLength label={_('Trace Name')} attr="hoverlabel.namelength" />
740742
<VisibilitySelect
741743
attr="contour.show"
742744
label={_('Show Contour')}

0 commit comments

Comments
 (0)