|
| 1 | +import Dropdown from './Dropdown'; |
| 2 | +import Info from './Info'; |
| 3 | +import PropTypes from 'prop-types'; |
| 4 | +import React, {Component, Fragment} from 'react'; |
| 5 | +import {EDITOR_ACTIONS} from 'lib/constants'; |
| 6 | +import Button from '../widgets/Button'; |
| 7 | +import {PlusIcon} from 'plotly-icons'; |
| 8 | +import { |
| 9 | + connectToContainer, |
| 10 | + localize, |
| 11 | + traceTypeToAxisType, |
| 12 | + getAxisTitle, |
| 13 | + axisIdToAxisName, |
| 14 | +} from 'lib'; |
| 15 | + |
| 16 | +class UnconnectedNewAxisCreator extends Component { |
| 17 | + canAddAxis() { |
| 18 | + const currentAxisId = this.props.fullContainer[this.props.attr]; |
| 19 | + const currentTraceIndex = this.props.fullContainer.index; |
| 20 | + return this.context.fullData.some( |
| 21 | + d => d.index !== currentTraceIndex && d[this.props.attr] === currentAxisId |
| 22 | + ); |
| 23 | + } |
| 24 | + |
| 25 | + updateAxis() { |
| 26 | + const {attr, updateContainer} = this.props; |
| 27 | + const {onUpdate, fullLayout} = this.context; |
| 28 | + |
| 29 | + updateContainer({ |
| 30 | + [attr]: attr.charAt(0) + (fullLayout._subplots[attr].length + 1), |
| 31 | + }); |
| 32 | + |
| 33 | + if (attr === 'yaxis') { |
| 34 | + onUpdate({ |
| 35 | + type: EDITOR_ACTIONS.UPDATE_LAYOUT, |
| 36 | + payload: { |
| 37 | + update: { |
| 38 | + [`${attr + (fullLayout._subplots[attr].length + 1)}.side`]: 'right', |
| 39 | + [`${attr + |
| 40 | + (fullLayout._subplots[attr].length + 1)}.overlaying`]: 'y', |
| 41 | + }, |
| 42 | + }, |
| 43 | + }); |
| 44 | + } |
| 45 | + |
| 46 | + if (attr === 'xaxis') { |
| 47 | + onUpdate({ |
| 48 | + type: EDITOR_ACTIONS.UPDATE_LAYOUT, |
| 49 | + payload: { |
| 50 | + update: { |
| 51 | + [`${attr + (fullLayout._subplots[attr].length + 1)}.side`]: 'top', |
| 52 | + [`${attr + |
| 53 | + (fullLayout._subplots[attr].length + 1)}.overlaying`]: 'x', |
| 54 | + }, |
| 55 | + }, |
| 56 | + }); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + recalcAxes(update) { |
| 61 | + const currentAxisId = this.props.fullContainer[this.props.attr]; |
| 62 | + |
| 63 | + // When we select another axis, make sure no unused axes are left: |
| 64 | + // does any other trace have this axisID? If so, nothing needs to change |
| 65 | + if ( |
| 66 | + this.context.fullData.some( |
| 67 | + t => |
| 68 | + t[this.props.attr] === currentAxisId && |
| 69 | + t.index !== this.props.fullContainer.index |
| 70 | + ) |
| 71 | + ) { |
| 72 | + this.props.updateContainer({[this.props.attr]: update}); |
| 73 | + return; |
| 74 | + } |
| 75 | + |
| 76 | + // if not, send action to readjust axis references in trace data and layout |
| 77 | + const tracesToAdjust = this.context.fullData.filter( |
| 78 | + trace => |
| 79 | + Number(trace[this.props.attr].slice(1)) > Number(currentAxisId.slice(1)) |
| 80 | + ); |
| 81 | + |
| 82 | + this.context.onUpdate({ |
| 83 | + type: EDITOR_ACTIONS.UPDATE_AXIS_REFERENCES, |
| 84 | + payload: {tracesToAdjust, attrToAdjust: this.props.attr}, |
| 85 | + }); |
| 86 | + |
| 87 | + this.props.updateContainer({[this.props.attr]: update}); |
| 88 | + } |
| 89 | + |
| 90 | + render() { |
| 91 | + const icon = <PlusIcon />; |
| 92 | + const extraComponent = this.canAddAxis() ? ( |
| 93 | + <Button variant="no-text" icon={icon} onClick={() => this.updateAxis()} /> |
| 94 | + ) : ( |
| 95 | + <Button variant="no-text--disabled" icon={icon} onClick={() => {}} /> |
| 96 | + ); |
| 97 | + |
| 98 | + return ( |
| 99 | + <Dropdown |
| 100 | + label={this.props.label} |
| 101 | + attr={this.props.attr} |
| 102 | + clearable={false} |
| 103 | + options={this.props.options} |
| 104 | + updatePlot={u => this.recalcAxes(u)} |
| 105 | + extraComponent={extraComponent} |
| 106 | + /> |
| 107 | + ); |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +UnconnectedNewAxisCreator.propTypes = { |
| 112 | + attr: PropTypes.string, |
| 113 | + label: PropTypes.string, |
| 114 | + options: PropTypes.array, |
| 115 | + canAddAxis: PropTypes.bool, |
| 116 | + localize: PropTypes.func, |
| 117 | + container: PropTypes.object, |
| 118 | + fullContainer: PropTypes.object, |
| 119 | + updateContainer: PropTypes.func, |
| 120 | +}; |
| 121 | + |
| 122 | +UnconnectedNewAxisCreator.contextTypes = { |
| 123 | + fullLayout: PropTypes.object, |
| 124 | + data: PropTypes.array, |
| 125 | + fullData: PropTypes.array, |
| 126 | + onUpdate: PropTypes.func, |
| 127 | +}; |
| 128 | + |
| 129 | +const ConnectedNewAxisCreator = connectToContainer(UnconnectedNewAxisCreator); |
| 130 | + |
| 131 | +class AxisCreator extends Component { |
| 132 | + render() { |
| 133 | + const isFirstTraceOfType = |
| 134 | + this.context.data.filter(d => d.type === this.props.container.type) |
| 135 | + .length === 1; |
| 136 | + |
| 137 | + if (isFirstTraceOfType) { |
| 138 | + return null; |
| 139 | + } |
| 140 | + |
| 141 | + const {localize: _} = this.props; |
| 142 | + const {fullLayout} = this.context; |
| 143 | + const axisType = traceTypeToAxisType(this.props.container.type); |
| 144 | + const controls = []; |
| 145 | + |
| 146 | + function getOptions(axisType) { |
| 147 | + return fullLayout._subplots[axisType].map(axisId => ({ |
| 148 | + label: getAxisTitle(fullLayout[axisIdToAxisName(axisId)]), |
| 149 | + value: axisId, |
| 150 | + })); |
| 151 | + } |
| 152 | + |
| 153 | + // for the moment only cartesian subplots are supported |
| 154 | + if (axisType === 'cartesian') { |
| 155 | + ['xaxis', 'yaxis'].forEach((type, index) => { |
| 156 | + controls.push( |
| 157 | + <ConnectedNewAxisCreator |
| 158 | + key={index} |
| 159 | + attr={type} |
| 160 | + label={type.charAt(0).toUpperCase() + ' Axis'} |
| 161 | + options={getOptions(type)} |
| 162 | + localize={_} |
| 163 | + /> |
| 164 | + ); |
| 165 | + }); |
| 166 | + } |
| 167 | + |
| 168 | + return ( |
| 169 | + <Fragment> |
| 170 | + {controls} |
| 171 | + <Info> |
| 172 | + {_('You can style and position your axes in the Style > Axes Panel')} |
| 173 | + </Info> |
| 174 | + </Fragment> |
| 175 | + ); |
| 176 | + } |
| 177 | +} |
| 178 | + |
| 179 | +AxisCreator.propTypes = { |
| 180 | + localize: PropTypes.func, |
| 181 | + container: PropTypes.object, |
| 182 | + fullContainer: PropTypes.object, |
| 183 | +}; |
| 184 | + |
| 185 | +AxisCreator.plotly_editor_traits = { |
| 186 | + is_axis_creator: true, |
| 187 | +}; |
| 188 | + |
| 189 | +AxisCreator.contextTypes = { |
| 190 | + data: PropTypes.array, |
| 191 | + fullData: PropTypes.array, |
| 192 | + fullLayout: PropTypes.object, |
| 193 | +}; |
| 194 | + |
| 195 | +export default localize(connectToContainer(AxisCreator)); |
0 commit comments