Commit 087575e8 authored by tom's avatar tom

adaptive chart

parent b16ab20b
...@@ -8,33 +8,48 @@ import GridLine from 'ui/shared/graphs/GridLine'; ...@@ -8,33 +8,48 @@ import GridLine from 'ui/shared/graphs/GridLine';
import Line from 'ui/shared/graphs/Line'; import Line from 'ui/shared/graphs/Line';
import useTimeGraphController from 'ui/shared/graphs/useTimeGraphController'; import useTimeGraphController from 'ui/shared/graphs/useTimeGraphController';
const dimensions = {
width: 600,
height: 300,
margin: { top: 0, right: 0, bottom: 20, left: 65 },
};
const data = { const data = {
items: json.map((d) => ({ ...d, date: new Date(d.date) })), items: json.map((d) => ({ ...d, date: new Date(d.date) })),
}; };
const EthereumDailyTxsChart = () => { interface Props {
const { width, height, margin } = dimensions; margin?: {
const svgWidth = width + margin.left + margin.right; top?: number;
const svgHeight = height + margin.top + margin.bottom; right?: number;
const controller = useTimeGraphController({ data, width, height }); bottom?: number;
const { yTickFormat, xScale, yScale } = controller; left?: number;
};
}
const EthereumDailyTxsChart = ({ margin }: Props) => {
const ref = React.useRef<SVGSVGElement>(null);
const [ rect, setRect ] = React.useState<{ width: number; height: number}>();
React.useEffect(() => {
const rect = ref.current?.getBoundingClientRect();
rect && setRect({ width: rect.width, height: rect.height });
}, [ ]);
const width = rect?.width || 0;
const height = rect?.height || 0;
const innerWidth = width - (margin?.left || 0) - (margin?.right || 0);
const innerHeight = height - (margin?.bottom || 0) - (margin?.top || 0);
const { yTickFormat, xScale, yScale } = useTimeGraphController({ data, width: innerWidth, height: innerHeight });
const lineColor = useToken('colors', 'blue.500'); const lineColor = useToken('colors', 'blue.500');
return ( return (
<svg width={ svgWidth } height={ svgHeight }> <svg width={ width || '100%' } height={ height || '100%' } ref={ ref }>
<g transform={ `translate(${ margin.left },${ margin.top })` }> { width > 0 && (
<g transform={ `translate(${ margin?.left || 0 },${ margin?.top || 0 })` }>
{ /* BASE GRID LINE */ } { /* BASE GRID LINE */ }
<GridLine <GridLine
type="horizontal" type="horizontal"
scale={ yScale } scale={ yScale }
ticks={ 1 } ticks={ 1 }
size={ width } size={ innerWidth }
disableAnimation disableAnimation
/> />
...@@ -43,14 +58,14 @@ const EthereumDailyTxsChart = () => { ...@@ -43,14 +58,14 @@ const EthereumDailyTxsChart = () => {
type="vertical" type="vertical"
scale={ xScale } scale={ xScale }
ticks={ 5 } ticks={ 5 }
size={ height } size={ innerHeight }
transform={ `translate(0, ${ height })` } transform={ `translate(0, ${ innerHeight })` }
/> />
<GridLine <GridLine
type="horizontal" type="horizontal"
scale={ yScale } scale={ yScale }
ticks={ 5 } ticks={ 5 }
size={ width } size={ innerWidth }
/> />
{ /* GRAPH */ } { /* GRAPH */ }
...@@ -78,12 +93,13 @@ const EthereumDailyTxsChart = () => { ...@@ -78,12 +93,13 @@ const EthereumDailyTxsChart = () => {
<Axis <Axis
type="bottom" type="bottom"
scale={ xScale } scale={ xScale }
transform={ `translate(0, ${ height })` } transform={ `translate(0, ${ innerHeight })` }
ticks={ 5 } ticks={ 5 }
/> />
</g> </g>
) }
</svg> </svg>
); );
}; };
export default EthereumDailyTxsChart; export default React.memo(EthereumDailyTxsChart);
import { Box } from '@chakra-ui/react';
import React from 'react'; import React from 'react';
import EthereumDailyTxsChart from 'ui/charts/EthereumDailyTxsChart'; import EthereumDailyTxsChart from 'ui/charts/EthereumDailyTxsChart';
import Page from 'ui/shared/Page/Page'; import Page from 'ui/shared/Page/Page';
import PageTitle from 'ui/shared/Page/PageTitle'; import PageTitle from 'ui/shared/Page/PageTitle';
const CHART_MARGIN = { bottom: 20, left: 65 };
const Graph = () => { const Graph = () => {
return ( return (
<Page> <Page>
<PageTitle text="Ethereum Daily Transactions Chart"/> <PageTitle text="Ethereum Daily Transactions Chart"/>
<EthereumDailyTxsChart/> <Box w="100%" h="400px">
<EthereumDailyTxsChart margin={ CHART_MARGIN }/>
</Box>
</Page> </Page>
); );
}; };
......
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