Skip to content

Commit 1746a76

Browse files
authored
fix(Charts): dynamic chart size (#154)
1 parent 2c95176 commit 1746a76

File tree

9 files changed

+43
-28
lines changed

9 files changed

+43
-28
lines changed

packages/charts/src/components/BarChart/__snapshots__/BarChart.test.tsx.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
exports[`BarChart loading placeholder 1`] = `
44
<div
5-
class="ChartContainer--chart-"
5+
class="ChartContainer--chart- ChartContainer--chart-0-"
66
style="position: relative; padding-top: 6px; width: 300px; height: 300px;"
77
>
88
<svg

packages/charts/src/components/ColumnChart/__snapshots__/ColumnChart.test.tsx.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
exports[`ColumnChart loading placeholder 1`] = `
44
<div
5-
class="ChartContainer--chart-"
5+
class="ChartContainer--chart- ChartContainer--chart-0-"
66
style="position: relative; padding-top: 6px; width: 300px; height: 300px;"
77
>
88
<svg

packages/charts/src/components/DonutChart/__snapshots__/DonutChart.test.tsx.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
exports[`DonutChart loading placeholder 1`] = `
44
<div
5-
class="ChartContainer--chart-"
5+
class="ChartContainer--chart- ChartContainer--chart-0-"
66
style="position: relative; padding-top: 6px; width: 300px; height: 300px;"
77
>
88
<svg

packages/charts/src/components/LineChart/__snapshots__/LineChart.test.tsx.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
exports[`LineChart loading placeholder 1`] = `
44
<div
5-
class="ChartContainer--chart-"
5+
class="ChartContainer--chart- ChartContainer--chart-0-"
66
style="position: relative; padding-top: 6px; width: 300px; height: 300px;"
77
>
88
<svg

packages/charts/src/components/PieChart/__snapshots__/PieChart.test.tsx.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
exports[`PieChart loading placeholder 1`] = `
44
<div
5-
class="ChartContainer--chart-"
5+
class="ChartContainer--chart- ChartContainer--chart-0-"
66
style="position: relative; padding-top: 6px; width: 300px; height: 300px;"
77
>
88
<svg

packages/charts/src/interfaces/ChartBaseProps.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ export interface ChartBaseProps extends CommonProps {
88
colors?: Array<CSSProperties['color']>;
99
height?: number | string;
1010
width?: number | string;
11+
minHeight?: number;
12+
minWidth?: number;
1113
options?: ChartOptions;
1214
categoryAxisFormatter?: (value: any, currentDataset?: object, currentContext?: object) => string | number;
1315
valueAxisFormatter?: (value: any, currentDataset?: object, currentContext?: object) => string | number;

packages/charts/src/internal/useSizeMonitor.ts

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,33 @@
11
import { useCallback, useEffect, useRef, useState } from 'react';
22
import ResizeObserver from 'resize-observer-polyfill';
33

4-
export const useSizeMonitor = (heightProp, widthProp, container) => {
4+
export const useSizeMonitor = (props, container) => {
5+
const { height: heightProp, width: widthProp, minHeight, minWidth } = props;
56
const [height, setHeight] = useState(null);
67
const [width, setWidth] = useState(null);
78

89
const enableSizeMonitor = typeof heightProp === 'string' || typeof widthProp === 'string';
910

10-
const recalculateSize = useCallback(() => {
11-
const { height: clientRectHeight, width: clientRectWidth } = container.current.getBoundingClientRect();
12-
13-
setHeight(clientRectHeight);
14-
setWidth(clientRectWidth);
15-
}, [container.current, setHeight, setWidth]);
11+
const recalculateSize = useCallback(
12+
(e?) => {
13+
let clientRectHeight;
14+
let clientRectWidth;
15+
16+
if (!e || !e[0] || !e[0].contentRect) {
17+
clientRectHeight = container.current.getBoundingClientRect().height;
18+
clientRectWidth = container.current.getBoundingClientRect().width;
19+
} else {
20+
clientRectHeight = e[0].contentRect.height;
21+
clientRectWidth = e[0].contentRect.width;
22+
}
23+
24+
// console.log(props);
25+
26+
setHeight(Math.max(minHeight, clientRectHeight));
27+
setWidth(Math.max(minWidth, clientRectWidth));
28+
},
29+
[container.current, setHeight, setWidth]
30+
);
1631

1732
const observer = useRef(new ResizeObserver(recalculateSize));
1833

packages/charts/src/internal/withChartContainer.tsx

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,15 @@ import { ChartBaseProps } from '../interfaces/ChartBaseProps';
44
import { getLoadingState } from './Placeholder';
55
import { useSizeMonitor } from './useSizeMonitor';
66

7-
// const calculateChartHeight = (props) => {
8-
// if (props.noLegend) {
9-
// return `${props.height}px`;
10-
// }
11-
// return `${props.height - 60}px`;
12-
// };
7+
const calculateChartHeight = (props) => {
8+
if (props.noLegend) {
9+
return typeof props.height === 'string' ? props.height : `${props.height}px`;
10+
}
11+
return typeof props.height === 'string' ? `calc(${props.height} - 60px)` : `${props.height - 60}px`;
12+
};
1313

1414
const styles = {
1515
chart: {
16-
// '& svg': {
17-
// width: (props) => `${props.width}px`,
18-
// height: calculateChartHeight
19-
// },
2016
'& .legend': {
2117
height: '55px',
2218
maxHeight: '55px',
@@ -26,6 +22,10 @@ const styles = {
2622
flexWrap: 'wrap',
2723
padding: '0 1rem',
2824
boxSizing: 'border-box'
25+
},
26+
'& svg': {
27+
width: (props) => `${props.width}px`,
28+
height: calculateChartHeight
2929
}
3030
}
3131
};
@@ -49,7 +49,7 @@ export const withChartContainer = (Component: ComponentType<any>) => {
4949
return getLoadingState(loading, datasets, (Component as any).LoadingPlaceholder);
5050
}, [loading, datasets, Component]);
5151

52-
const { height, width } = useSizeMonitor(props.height, props.width, outerContainer);
52+
const { height, width } = useSizeMonitor(props, outerContainer);
5353

5454
const inlineStyle: CSSProperties = useMemo(() => {
5555
return {
@@ -90,11 +90,7 @@ export const withChartContainer = (Component: ComponentType<any>) => {
9090
);
9191
});
9292

93-
ChartContainer.defaultProps = {
94-
width: 350,
95-
height: 350,
96-
...(Component.defaultProps || {})
97-
};
93+
ChartContainer.defaultProps = Component.defaultProps;
9894
ChartContainer.displayName = Component.displayName;
9995

10096
return ChartContainer;

packages/charts/src/util/ChartBaseDefaultProps.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ export const ChartBaseDefaultProps: ChartBaseProps = {
55
datasets: [],
66
colors: [],
77
height: 300,
8+
minHeight: 300,
9+
minWidth: 300,
810
width: 300,
911
options: {},
1012
categoryAxisFormatter: (d) => d,

0 commit comments

Comments
 (0)