Commit 14fda4e3 authored by tom's avatar tom

chart resize

parent bc2cb096
import { useToken } from '@chakra-ui/react'; import { useToken } from '@chakra-ui/react';
import _debounce from 'lodash/debounce';
import React from 'react'; import React from 'react';
import json from 'data/charts_eth_txs.json'; import json from 'data/charts_eth_txs.json';
...@@ -27,18 +28,38 @@ const EthereumDailyTxsChart = ({ margin }: Props) => { ...@@ -27,18 +28,38 @@ const EthereumDailyTxsChart = ({ margin }: Props) => {
const ref = React.useRef<SVGSVGElement>(null); const ref = React.useRef<SVGSVGElement>(null);
const overlayRef = React.useRef<SVGRectElement>(null); const overlayRef = React.useRef<SVGRectElement>(null);
const [ rect, setRect ] = React.useState<{ width: number; height: number}>(); const [ rect, setRect ] = React.useState<{ width: number; height: number}>({ width: 0, height: 0 });
React.useEffect(() => { const calculateRect = React.useCallback(() => {
const rect = ref.current?.getBoundingClientRect(); const rect = ref.current?.getBoundingClientRect();
rect && setRect({ width: rect.width, height: rect.height }); return { width: rect?.width || 0, height: rect?.height || 0 };
}, [ ]); }, []);
React.useEffect(() => {
setRect(calculateRect());
}, [ calculateRect ]);
React.useEffect(() => {
let timeoutId: number;
const resizeHandler = _debounce(() => {
setRect({ width: 0, height: 0 });
timeoutId = window.setTimeout(() => {
setRect(calculateRect());
}, 0);
}, 100);
const resizeObserver = new ResizeObserver(resizeHandler);
const width = rect?.width || 0; resizeObserver.observe(document.body);
const height = rect?.height || 0; return function cleanup() {
resizeObserver.unobserve(document.body);
window.clearTimeout(timeoutId);
};
}, [ calculateRect ]);
const innerWidth = width - (margin?.left || 0) - (margin?.right || 0); const { width, height } = rect;
const innerHeight = height - (margin?.bottom || 0) - (margin?.top || 0);
const innerWidth = Math.max(width - (margin?.left || 0) - (margin?.right || 0), 0);
const innerHeight = Math.max(height - (margin?.bottom || 0) - (margin?.top || 0), 0);
const { yTickFormat, xScale, yScale } = useTimeGraphController({ data, width: innerWidth, height: innerHeight }); const { yTickFormat, xScale, yScale } = useTimeGraphController({ data, width: innerWidth, height: innerHeight });
...@@ -46,7 +67,7 @@ const EthereumDailyTxsChart = ({ margin }: Props) => { ...@@ -46,7 +67,7 @@ const EthereumDailyTxsChart = ({ margin }: Props) => {
return ( return (
<svg width={ width || '100%' } height={ height || '100%' } ref={ ref }> <svg width={ width || '100%' } height={ height || '100%' } ref={ ref }>
<g transform={ `translate(${ margin?.left || 0 },${ margin?.top || 0 })` }> <g transform={ `translate(${ margin?.left || 0 },${ margin?.top || 0 })` } opacity={ width ? 1 : 0 }>
{ /* BASE GRID LINE */ } { /* BASE GRID LINE */ }
<GridLine <GridLine
type="horizontal" type="horizontal"
...@@ -63,12 +84,14 @@ const EthereumDailyTxsChart = ({ margin }: Props) => { ...@@ -63,12 +84,14 @@ const EthereumDailyTxsChart = ({ margin }: Props) => {
ticks={ 5 } ticks={ 5 }
size={ innerHeight } size={ innerHeight }
transform={ `translate(0, ${ innerHeight })` } transform={ `translate(0, ${ innerHeight })` }
disableAnimation
/> />
<GridLine <GridLine
type="horizontal" type="horizontal"
scale={ yScale } scale={ yScale }
ticks={ 5 } ticks={ 5 }
size={ innerWidth } size={ innerWidth }
disableAnimation
/> />
{ /* GRAPH */ } { /* GRAPH */ }
...@@ -92,6 +115,7 @@ const EthereumDailyTxsChart = ({ margin }: Props) => { ...@@ -92,6 +115,7 @@ const EthereumDailyTxsChart = ({ margin }: Props) => {
scale={ yScale } scale={ yScale }
ticks={ 5 } ticks={ 5 }
tickFormat={ yTickFormat } tickFormat={ yTickFormat }
disableAnimation
/> />
<Overlay ref={ overlayRef } width={ innerWidth } height={ innerHeight }> <Overlay ref={ overlayRef } width={ innerWidth } height={ innerHeight }>
<Axis <Axis
...@@ -100,6 +124,7 @@ const EthereumDailyTxsChart = ({ margin }: Props) => { ...@@ -100,6 +124,7 @@ const EthereumDailyTxsChart = ({ margin }: Props) => {
transform={ `translate(0, ${ innerHeight })` } transform={ `translate(0, ${ innerHeight })` }
ticks={ 5 } ticks={ 5 }
anchorEl={ overlayRef.current } anchorEl={ overlayRef.current }
disableAnimation
/> />
<Tooltip <Tooltip
anchorEl={ overlayRef.current } anchorEl={ overlayRef.current }
......
...@@ -39,18 +39,13 @@ const Line = ({ xScale, yScale, data, animation, ...props }: Props) => { ...@@ -39,18 +39,13 @@ const Line = ({ xScale, yScale, data, animation, ...props }: Props) => {
}, []); }, []);
React.useEffect(() => { React.useEffect(() => {
switch (animation) { const ANIMATIONS = {
case 'left': left: animateLeft,
animateLeft(); fadeIn: animateFadeIn,
break; none: noneAnimation,
case 'fadeIn': };
animateFadeIn(); const animationFn = ANIMATIONS[animation];
break; window.setTimeout(animationFn, 100);
case 'none':
default:
noneAnimation();
break;
}
}, [ animateLeft, animateFadeIn, noneAnimation, animation ]); }, [ animateLeft, animateFadeIn, noneAnimation, animation ]);
// Recalculate line length if scale has changed // Recalculate line length if scale has changed
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment