From cae7d92a16ebaf0a2524cd73c225b7c6c7771eba Mon Sep 17 00:00:00 2001 From: saml33 Date: Sun, 11 Jul 2021 10:39:20 +1000 Subject: [PATCH] filter stats chart by time range --- components/Chart.tsx | 97 +++++++++++++++++++++++++++++++------------- 1 file changed, 68 insertions(+), 29 deletions(-) diff --git a/components/Chart.tsx b/components/Chart.tsx index 1f84627..ff5fdae 100644 --- a/components/Chart.tsx +++ b/components/Chart.tsx @@ -4,6 +4,7 @@ import useDimensions from 'react-cool-dimensions' const Chart = ({ title, xAxis, yAxis, data, labelFormat, type }) => { const [mouseData, setMouseData] = useState(null) + const [filterData, setFilterData] = useState(null) // @ts-ignore const { observe, width, height } = useDimensions() @@ -17,40 +18,78 @@ const Chart = ({ title, xAxis, yAxis, data, labelFormat, type }) => { setMouseData(null) } + const handleFilterData = (time) => { + const startFrom = time + ? new Date(Date.now() - time * 24 * 60 * 60 * 1000).getTime() + : null + + return startFrom + ? data.filter((d) => new Date(d.time).getTime() > startFrom) + : data + } + return ( -
-
-
{title}
- {mouseData ? ( - <> -
- {labelFormat(mouseData[yAxis])} -
-
- {new Date(mouseData[xAxis]).toDateString()} -
- - ) : data.length > 0 && data[data.length - 1][yAxis] ? ( - <> -
- {labelFormat(data[data.length - 1][yAxis])} -
-
- {new Date(data[data.length - 1][xAxis]).toDateString()} -
- - ) : ( - <> -
-
- - )} +
+
+
+
{title}
+ {mouseData ? ( + <> +
+ {labelFormat(mouseData[yAxis])} +
+
+ {new Date(mouseData[xAxis]).toDateString()} +
+ + ) : data.length > 0 && data[data.length - 1][yAxis] ? ( + <> +
+ {labelFormat(data[data.length - 1][yAxis])} +
+
+ {new Date(data[data.length - 1][xAxis]).toDateString()} +
+ + ) : ( + <> +
+
+ + )} +
+
+ + + +
{width > 0 && type === 'area' ? ( @@ -81,7 +120,7 @@ const Chart = ({ title, xAxis, yAxis, data, labelFormat, type }) => {