Skip to content

Commit fad2cb5

Browse files
use modify & supply plotProp funcs to build BarWidth and PadWidth
1 parent cb1a95a commit fad2cb5

File tree

2 files changed

+66
-16
lines changed

2 files changed

+66
-16
lines changed

src/DefaultEditor.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import {
2424
import {DEFAULT_FONTS} from './constants';
2525
import {localize, connectAxesToLayout, connectLayoutToPlot} from './lib';
2626

27-
import {BoxGap} from './shame';
27+
import {BoxWidth, BoxPad} from './shame';
2828

2929
const LayoutPanel = connectLayoutToPlot(Panel);
3030
const AxesFold = connectAxesToLayout(Fold);
@@ -148,10 +148,10 @@ class DefaultEditor extends Component {
148148
</TraceMarkerSection>
149149

150150
<Section name={_('Size and Spacing')}>
151-
<BoxGap label={_('Bar Width')} attr="bargap" />
152-
<BoxGap label={_('Box Width')} attr="boxgap" />
153-
<BoxGap label={_('Bar Padding')} attr="bargroupgap" />
154-
<BoxGap label={_('Box Padding')} attr="boxgroupgap" />
151+
<BoxWidth label={_('Bar Width')} attr="bargap" />
152+
<BoxWidth label={_('Box Width')} attr="boxgap" />
153+
<BoxPad label={_('Bar Padding')} attr="bargroupgap" />
154+
<BoxPad label={_('Box Padding')} attr="boxgroupgap" />
155155
</Section>
156156

157157
<Section name={_('Lines')}>

src/shame.js

+61-11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/*
22
* DELETE THIS FILE. EVERYTHING NEEDS TO FIND A HOME.
33
*/
4+
import isNumeric from 'fast-isnumeric';
45
import {list} from 'plotly.js/src/plots/cartesian/axis_ids';
56
import {UnconnectedNumeric} from './components/fields/Numeric';
67
import {
@@ -22,20 +23,69 @@ class NumericNoArrows extends UnconnectedNumeric {}
2223
NumericNoArrows.propTypes = UnconnectedNumeric.propTypes;
2324
NumericNoArrows.defaultProps = {
2425
showArrows: false,
26+
postfix: '%',
2527
};
2628

27-
export const BoxGap = connectLayoutToPlot(
29+
// Workaround the issue with nested layouts inside trace component.
30+
// See:
31+
// https://github.com/plotly/react-plotly.js-editor/issues/58#issuecomment-345492794
32+
const supplyLayoutPlotProps = (props, context) => {
33+
return unpackPlotProps(props, {
34+
...context,
35+
...getLayoutContext(context),
36+
});
37+
};
38+
39+
export const BoxWidth = connectLayoutToPlot(
2840
connectToContainer(NumericNoArrows, {
29-
supplyPlotProps: (props, context) => {
30-
// workaround the issue with nested layouts inside trace component.
31-
// See
32-
// https://github.com/plotly/react-plotly.js-editor/issues/58#issuecomment-345492794
33-
const plotProps = unpackPlotProps(props, {
34-
...context,
35-
...getLayoutContext(context),
36-
});
37-
38-
// Full value should multiply by percentage if number.
41+
supplyPlotProps: supplyLayoutPlotProps,
42+
modifyPlotProps: (props, context, plotProps) => {
43+
const {fullValue, updatePlot} = plotProps;
44+
plotProps.fullValue = () => {
45+
let fv = fullValue();
46+
if (isNumeric(fv)) {
47+
fv = Math.round((1 - fv) * 100);
48+
}
49+
return fv;
50+
};
51+
52+
plotProps.updatePlot = v => {
53+
if (isNumeric(v)) {
54+
updatePlot(1 - v / 100);
55+
} else {
56+
updatePlot(v);
57+
}
58+
};
59+
60+
plotProps.max = 100;
61+
62+
return plotProps;
63+
},
64+
})
65+
);
66+
67+
export const BoxPad = connectLayoutToPlot(
68+
connectToContainer(NumericNoArrows, {
69+
supplyPlotProps: supplyLayoutPlotProps,
70+
modifyPlotProps: (props, context, plotProps) => {
71+
const {fullValue, updatePlot} = plotProps;
72+
plotProps.fullValue = () => {
73+
let fv = fullValue();
74+
if (isNumeric(fv)) {
75+
fv = fv * 100;
76+
}
77+
return fv;
78+
};
79+
80+
plotProps.updatePlot = v => {
81+
if (isNumeric(v)) {
82+
updatePlot(v / 100);
83+
} else {
84+
updatePlot(v);
85+
}
86+
};
87+
88+
plotProps.max = 100;
3989

4090
return plotProps;
4191
},

0 commit comments

Comments
 (0)