Skip to content

Commit

Permalink
fix(Charts): dynamic chart size (#154)
Browse files Browse the repository at this point in the history
  • Loading branch information
vbersch authored Sep 25, 2019
1 parent 2c95176 commit 1746a76
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

exports[`BarChart loading placeholder 1`] = `
<div
class="ChartContainer--chart-"
class="ChartContainer--chart- ChartContainer--chart-0-"
style="position: relative; padding-top: 6px; width: 300px; height: 300px;"
>
<svg
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

exports[`ColumnChart loading placeholder 1`] = `
<div
class="ChartContainer--chart-"
class="ChartContainer--chart- ChartContainer--chart-0-"
style="position: relative; padding-top: 6px; width: 300px; height: 300px;"
>
<svg
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

exports[`DonutChart loading placeholder 1`] = `
<div
class="ChartContainer--chart-"
class="ChartContainer--chart- ChartContainer--chart-0-"
style="position: relative; padding-top: 6px; width: 300px; height: 300px;"
>
<svg
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

exports[`LineChart loading placeholder 1`] = `
<div
class="ChartContainer--chart-"
class="ChartContainer--chart- ChartContainer--chart-0-"
style="position: relative; padding-top: 6px; width: 300px; height: 300px;"
>
<svg
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

exports[`PieChart loading placeholder 1`] = `
<div
class="ChartContainer--chart-"
class="ChartContainer--chart- ChartContainer--chart-0-"
style="position: relative; padding-top: 6px; width: 300px; height: 300px;"
>
<svg
Expand Down
2 changes: 2 additions & 0 deletions packages/charts/src/interfaces/ChartBaseProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ export interface ChartBaseProps extends CommonProps {
colors?: Array<CSSProperties['color']>;
height?: number | string;
width?: number | string;
minHeight?: number;
minWidth?: number;
options?: ChartOptions;
categoryAxisFormatter?: (value: any, currentDataset?: object, currentContext?: object) => string | number;
valueAxisFormatter?: (value: any, currentDataset?: object, currentContext?: object) => string | number;
Expand Down
29 changes: 22 additions & 7 deletions packages/charts/src/internal/useSizeMonitor.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,33 @@
import { useCallback, useEffect, useRef, useState } from 'react';
import ResizeObserver from 'resize-observer-polyfill';

export const useSizeMonitor = (heightProp, widthProp, container) => {
export const useSizeMonitor = (props, container) => {
const { height: heightProp, width: widthProp, minHeight, minWidth } = props;
const [height, setHeight] = useState(null);
const [width, setWidth] = useState(null);

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

const recalculateSize = useCallback(() => {
const { height: clientRectHeight, width: clientRectWidth } = container.current.getBoundingClientRect();

setHeight(clientRectHeight);
setWidth(clientRectWidth);
}, [container.current, setHeight, setWidth]);
const recalculateSize = useCallback(
(e?) => {
let clientRectHeight;
let clientRectWidth;

if (!e || !e[0] || !e[0].contentRect) {
clientRectHeight = container.current.getBoundingClientRect().height;
clientRectWidth = container.current.getBoundingClientRect().width;
} else {
clientRectHeight = e[0].contentRect.height;
clientRectWidth = e[0].contentRect.width;
}

// console.log(props);

setHeight(Math.max(minHeight, clientRectHeight));
setWidth(Math.max(minWidth, clientRectWidth));
},
[container.current, setHeight, setWidth]
);

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

Expand Down
28 changes: 12 additions & 16 deletions packages/charts/src/internal/withChartContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,15 @@ import { ChartBaseProps } from '../interfaces/ChartBaseProps';
import { getLoadingState } from './Placeholder';
import { useSizeMonitor } from './useSizeMonitor';

// const calculateChartHeight = (props) => {
// if (props.noLegend) {
// return `${props.height}px`;
// }
// return `${props.height - 60}px`;
// };
const calculateChartHeight = (props) => {
if (props.noLegend) {
return typeof props.height === 'string' ? props.height : `${props.height}px`;
}
return typeof props.height === 'string' ? `calc(${props.height} - 60px)` : `${props.height - 60}px`;
};

const styles = {
chart: {
// '& svg': {
// width: (props) => `${props.width}px`,
// height: calculateChartHeight
// },
'& .legend': {
height: '55px',
maxHeight: '55px',
Expand All @@ -26,6 +22,10 @@ const styles = {
flexWrap: 'wrap',
padding: '0 1rem',
boxSizing: 'border-box'
},
'& svg': {
width: (props) => `${props.width}px`,
height: calculateChartHeight
}
}
};
Expand All @@ -49,7 +49,7 @@ export const withChartContainer = (Component: ComponentType<any>) => {
return getLoadingState(loading, datasets, (Component as any).LoadingPlaceholder);
}, [loading, datasets, Component]);

const { height, width } = useSizeMonitor(props.height, props.width, outerContainer);
const { height, width } = useSizeMonitor(props, outerContainer);

const inlineStyle: CSSProperties = useMemo(() => {
return {
Expand Down Expand Up @@ -90,11 +90,7 @@ export const withChartContainer = (Component: ComponentType<any>) => {
);
});

ChartContainer.defaultProps = {
width: 350,
height: 350,
...(Component.defaultProps || {})
};
ChartContainer.defaultProps = Component.defaultProps;
ChartContainer.displayName = Component.displayName;

return ChartContainer;
Expand Down
2 changes: 2 additions & 0 deletions packages/charts/src/util/ChartBaseDefaultProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ export const ChartBaseDefaultProps: ChartBaseProps = {
datasets: [],
colors: [],
height: 300,
minHeight: 300,
minWidth: 300,
width: 300,
options: {},
categoryAxisFormatter: (d) => d,
Expand Down

0 comments on commit 1746a76

Please sign in to comment.