|
| 1 | +import PropTypes from 'prop-types'; |
| 2 | +import React, {Component, Fragment} from 'react'; |
| 3 | +import {DataSelector, Radio, Numeric} from '../index'; |
| 4 | +import RadioBlocks from '../widgets/RadioBlocks'; |
| 5 | +import Field from './Field'; |
| 6 | +import {connectToContainer} from 'lib'; |
| 7 | + |
| 8 | +class ErrorBars extends Component { |
| 9 | + constructor(props, context) { |
| 10 | + super(props, context); |
| 11 | + this.updatePlot = this.updatePlot.bind(this); |
| 12 | + } |
| 13 | + |
| 14 | + updatePlot(value) { |
| 15 | + if (value === 'symmetric') { |
| 16 | + this.props.updatePlot({ |
| 17 | + ...this.props.fullValue, |
| 18 | + visible: true, |
| 19 | + symmetric: true, |
| 20 | + }); |
| 21 | + } |
| 22 | + |
| 23 | + if (value === 'asymmetric') { |
| 24 | + this.props.updatePlot({ |
| 25 | + ...this.props.fullValue, |
| 26 | + visible: true, |
| 27 | + symmetric: false, |
| 28 | + }); |
| 29 | + } |
| 30 | + |
| 31 | + if (value === 'hidden') { |
| 32 | + this.props.updatePlot({ |
| 33 | + ...this.props.fullValue, |
| 34 | + visible: false, |
| 35 | + }); |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + getMode() { |
| 40 | + let mode; |
| 41 | + |
| 42 | + if (!this.props.fullValue.visible) { |
| 43 | + mode = 'hidden'; |
| 44 | + } |
| 45 | + |
| 46 | + if ( |
| 47 | + this.props.fullValue.visible && |
| 48 | + (this.props.fullValue.symmetric || |
| 49 | + typeof this.props.fullValue.symmetric === 'undefined') |
| 50 | + ) { |
| 51 | + // when this.props.fullValue.type === 'sqrt', |
| 52 | + // then this.props.fullValue.symmetric is undefined, but 'sqrt' is only |
| 53 | + // applicable when we want symmetric error bars |
| 54 | + // https://github.com/plotly/plotly.js/issues/2359 |
| 55 | + mode = 'symmetric'; |
| 56 | + } |
| 57 | + |
| 58 | + if ( |
| 59 | + this.props.fullValue.visible && |
| 60 | + this.props.fullValue.symmetric === false |
| 61 | + ) { |
| 62 | + // it has to be explicitly set to false, because we don't want it to catch |
| 63 | + // cases when it's undefined |
| 64 | + mode = 'asymmetric'; |
| 65 | + } |
| 66 | + |
| 67 | + return mode; |
| 68 | + } |
| 69 | + |
| 70 | + renderModeSelector() { |
| 71 | + const {localize: _} = this.props; |
| 72 | + |
| 73 | + return ( |
| 74 | + <Field> |
| 75 | + <RadioBlocks |
| 76 | + alignment="center" |
| 77 | + onOptionChange={this.updatePlot} |
| 78 | + activeOption={this.getMode()} |
| 79 | + options={[ |
| 80 | + {label: _('Symmetric'), value: 'symmetric'}, |
| 81 | + {label: _('Asymmetric'), value: 'asymmetric'}, |
| 82 | + {label: _('Hidden'), value: 'hidden'}, |
| 83 | + ]} |
| 84 | + /> |
| 85 | + </Field> |
| 86 | + ); |
| 87 | + } |
| 88 | + |
| 89 | + renderErrorBarControls() { |
| 90 | + const {localize: _} = this.props; |
| 91 | + const mode = this.getMode(); |
| 92 | + const showCustomDataControl = this.props.fullValue.type === 'data'; |
| 93 | + |
| 94 | + if (mode === 'symmetric') { |
| 95 | + return ( |
| 96 | + <Fragment> |
| 97 | + <Radio |
| 98 | + label={_('Error Type')} |
| 99 | + attr={`${this.props.attr}.type`} |
| 100 | + options={[ |
| 101 | + {label: _('%'), value: 'percent'}, |
| 102 | + {label: _('Constant'), value: 'constant'}, |
| 103 | + {label: _('√'), value: 'sqrt'}, |
| 104 | + {label: _('Data'), value: 'data'}, |
| 105 | + ]} |
| 106 | + /> |
| 107 | + <Numeric label={_('Value')} attr={`${this.props.attr}.value`} /> |
| 108 | + {showCustomDataControl ? ( |
| 109 | + <DataSelector |
| 110 | + label={_(`Custom Data`)} |
| 111 | + attr={`${this.props.attr}.array`} |
| 112 | + /> |
| 113 | + ) : null} |
| 114 | + </Fragment> |
| 115 | + ); |
| 116 | + } |
| 117 | + |
| 118 | + if (mode === 'asymmetric') { |
| 119 | + return ( |
| 120 | + <Fragment> |
| 121 | + <Radio |
| 122 | + label={_('Error Type')} |
| 123 | + attr={`${this.props.attr}.type`} |
| 124 | + options={[ |
| 125 | + {label: _('%'), value: 'percent'}, |
| 126 | + {label: _('Constant'), value: 'constant'}, |
| 127 | + {label: _('Data'), value: 'data'}, |
| 128 | + ]} |
| 129 | + /> |
| 130 | + <Numeric label={_('Value')} attr={`${this.props.attr}.value`} /> |
| 131 | + <Numeric |
| 132 | + label={_('Value (-)')} |
| 133 | + attr={`${this.props.attr}.valueminus`} |
| 134 | + /> |
| 135 | + {showCustomDataControl ? ( |
| 136 | + <Fragment> |
| 137 | + <DataSelector |
| 138 | + label={_(`Error (+)`)} |
| 139 | + attr={`${this.props.attr}.array`} |
| 140 | + /> |
| 141 | + <DataSelector |
| 142 | + label={_(`Error (-)`)} |
| 143 | + attr={`${this.props.attr}.arrayminus`} |
| 144 | + /> |
| 145 | + </Fragment> |
| 146 | + ) : null} |
| 147 | + </Fragment> |
| 148 | + ); |
| 149 | + } |
| 150 | + |
| 151 | + return null; |
| 152 | + } |
| 153 | + |
| 154 | + render() { |
| 155 | + return ( |
| 156 | + <Fragment> |
| 157 | + {this.renderModeSelector()} |
| 158 | + {this.renderErrorBarControls()} |
| 159 | + </Fragment> |
| 160 | + ); |
| 161 | + } |
| 162 | +} |
| 163 | + |
| 164 | +ErrorBars.propTypes = { |
| 165 | + attr: PropTypes.string, |
| 166 | + localize: PropTypes.func, |
| 167 | + fullValue: PropTypes.object, |
| 168 | + updatePlot: PropTypes.func, |
| 169 | +}; |
| 170 | + |
| 171 | +export default connectToContainer(ErrorBars); |
0 commit comments