Skip to content

Commit 068f2c3

Browse files
committed
plotly - revise sizing implementation
1 parent cebfed0 commit 068f2c3

File tree

3 files changed

+55
-38
lines changed

3 files changed

+55
-38
lines changed

src/components/plotly/shared/custom-plot.js

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -54,22 +54,37 @@ const CustomPlot = ({
5454
// also, ref helps force plotly to redraw during padding transitions
5555
const { ref, width, height } = useResizeDetector()
5656

57+
const renderTitle = (title = '') => (
58+
showVizTitles &&
59+
<styles.PlotTitle
60+
x={titlePosition[0]}
61+
y={titlePosition[1]}
62+
>
63+
{title}
64+
</styles.PlotTitle>
65+
)
66+
5767
// render a dummy element with the ref from react-resize-detector.
5868
const renderDummy = (
59-
<styles.DynamicPadding size={size}>
60-
<styles.RefDummy ref={ref} />
61-
</styles.DynamicPadding>
69+
<styles.PlotContainer>
70+
<styles.DynamicSize ref={ref} size={size} >
71+
{renderTitle(' ')}
72+
<styles.Plot />
73+
</styles.DynamicSize>,
74+
</styles.PlotContainer>
6275
)
6376

6477
// set manual height and width for special cases
6578
const [manualDimensions, setManualDimensions] = useState()
6679
useLayoutEffect(() => {
6780
if (type === 'pie') {
68-
setManualDimensions(
69-
width <= height
70-
? { height: `${width}px` }
71-
: { width: `${height}px` },
72-
)
81+
if (width && height) {
82+
setManualDimensions(
83+
width <= height
84+
? { height: `${width}px` }
85+
: { width: `${height}px` },
86+
)
87+
}
7388
} else {
7489
setManualDimensions(null)
7590
}
@@ -88,18 +103,11 @@ const CustomPlot = ({
88103

89104
// render a plotly.js visualization with a title and dynamic padding
90105
const renderPlot = (data, title, key) => (
91-
<styles.DynamicPadding size={size} key={key}>
106+
<styles.PlotContainer key={key}>
92107
{
93108
applyManualDimensions(
94-
<styles.PlotContainer >
95-
{showVizTitles &&
96-
<styles.PlotTitle
97-
x={titlePosition[0]}
98-
y={titlePosition[1]}
99-
>
100-
{title}
101-
</styles.PlotTitle>
102-
}
109+
<styles.DynamicSize size={size} >
110+
{renderTitle(title)}
103111
<Plot
104112
data={data}
105113
layout={finalLayout}
@@ -108,10 +116,10 @@ const CustomPlot = ({
108116
responsive: true,
109117
}}
110118
/>
111-
</styles.PlotContainer >,
119+
</styles.DynamicSize>,
112120
)
113121
}
114-
</styles.DynamicPadding>
122+
</styles.PlotContainer>
115123
)
116124

117125
return (

src/components/plotly/shared/styles/plot.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ export default {
1414
color: '#4d4d4d',
1515
fontWeight: 'bold',
1616
zIndex: '10',
17-
width: '100%',
1817
overflow: 'visible',
18+
whiteSpace: 'nowrap',
1919
textAlign:
2020
x === 0
2121
? 'left'
@@ -34,20 +34,21 @@ export default {
3434
),
3535
})),
3636

37-
PlotContainer: styled('div')({
38-
width: 'inherit',
39-
height: 'inherit',
40-
justifyContent: 'center',
37+
PlotContainer: styled('div', forwardRef)({
38+
width: '100%',
39+
height: '100%',
4140
display: 'flex',
42-
flexDirection: 'column',
43-
overflow: 'visible',
41+
justifyContent: 'center',
42+
alignItems: 'center',
4443
}),
4544

4645
Plot: styled('div', forwardRef)({
4746
'& svg': {
4847
overflow: 'visible !important', // plotly.js override
4948
},
50-
height: 'inherit',
51-
width: 'inherit',
49+
flex: 1,
50+
minHeight: '10px',
51+
minWidth: '10px',
52+
width: '100%',
5253
}),
5354
}
Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,28 @@
11
import { forwardRef } from 'react'
22
import { styled } from 'goober'
33

4+
const MIN_SIZE = 50
5+
const MAX_PADDING = 100 - MIN_SIZE
6+
47
export default {
5-
DynamicPadding: styled('div')(({ size }) => {
6-
const padding = 1 - size
8+
DynamicSize: styled('div', forwardRef)(({ size }) => {
9+
const finalSize = `${MIN_SIZE + (size * MAX_PADDING)}%`
710
return {
811
display: 'flex',
9-
justifyContent: 'center',
10-
alignItems: 'center',
11-
width: '100%',
12-
height: '100%',
13-
transition: 'padding 0.3s ease',
14-
padding: `min(${padding * 30}%, 14rem)`,
12+
flexDirection: 'column',
13+
alignContent: 'center',
14+
position: 'relative',
15+
width: finalSize,
16+
height: finalSize,
17+
maxWidth: '100%',
18+
maxHeight: '100%',
19+
transition: 'width 0.3s, height 0.3s',
1520
}
1621
}),
1722

1823
HiddenContainer: styled('div')({
1924
pointerEvents: 'none',
20-
display: 'hidden',
25+
visibility: 'hidden',
2126
position: 'absolute',
2227
width: '100%',
2328
height: '100%',
@@ -31,5 +36,8 @@ export default {
3136
ManualDimensions: styled('div')(({ width = '100%', height = '100%' }) => ({
3237
width,
3338
height,
39+
display: 'flex',
40+
justifyContent: 'center',
41+
alignItems: 'center',
3442
})),
3543
}

0 commit comments

Comments
 (0)