Skip to content

Commit 9527741

Browse files
committed
#820 Updated Custom tooltip to recharts v3.0+
1 parent 0786b7c commit 9527741

2 files changed

Lines changed: 92 additions & 63 deletions

File tree

src/frontend/src/components/Graph/CustomTooltip.tsx

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,36 @@
1-
import { useState, useEffect } from "react"
1+
import { useState, useEffect } from "react";
22

33
import { Box, Divider, Text } from "@chakra-ui/layout";
4-
import { TooltipProps } from "recharts";
4+
import { TooltipContentProps } from "recharts";
55
import { Icon } from "@chakra-ui/react";
66

7-
// for recharts v2.1 and above
8-
import {
9-
ValueType,
10-
NameType,
11-
} from "recharts/types/component/DefaultTooltipContent";
127
import { capitalizeFirstLetter } from "../../hooks/utils";
138
import { BiError } from "react-icons/bi";
14-
import {
15-
getHoursSinceLaunch,
16-
} from "../../hooks/graphing/utils";
9+
import { getHoursSinceLaunch } from "../../hooks/graphing/utils";
1710
import { setupHourlyUpdate } from "../../utils";
1811

19-
interface CustomTooltipProps extends TooltipProps<ValueType, NameType> {
12+
interface CustomTooltipProps extends TooltipContentProps<number, number> {
2013
upperBoundry: number;
2114
fetchedLeagues: string[];
22-
colors: string[]
15+
colors: string[];
2316
}
2417

2518
export const CustomTooltip = (props: CustomTooltipProps) => {
2619
const [currentTime, setCurrentTime] = useState(new Date());
2720

28-
const hoursAgo = getHoursSinceLaunch(currentTime) - props.label
29-
30-
const daysToDisplay = Math.floor(hoursAgo / 24)
31-
const hoursToDisplay = hoursAgo % 24
21+
let hoursAgo = -1;
22+
if (typeof props.label == "number") {
23+
hoursAgo = getHoursSinceLaunch(currentTime) - props.label;
24+
}
3225

26+
const daysToDisplay = Math.floor(hoursAgo / 24);
27+
const hoursToDisplay = hoursAgo % 24;
3328

3429
useEffect(() => {
35-
return setupHourlyUpdate(setCurrentTime)
30+
return setupHourlyUpdate(setCurrentTime);
3631
}, []);
3732

38-
39-
4033
if (props.active && props.payload && props.payload.length) {
41-
4234
const confidence = props.payload[0].payload.confidence;
4335
const isLowConfidence = confidence === "low";
4436
const isMediumConfidence = confidence === "medium";
@@ -61,8 +53,12 @@ export const CustomTooltip = (props: CustomTooltipProps) => {
6153
flexDirection="column"
6254
>
6355
<Text my="5px">
64-
{hoursAgo !== 0 && daysToDisplay !== 0 && ` ${daysToDisplay} days ago`}
65-
{hoursAgo !== 0 && hoursToDisplay !== 0 && ` ${hoursToDisplay} hours ago`}
56+
{hoursAgo !== 0 &&
57+
daysToDisplay !== 0 &&
58+
` ${daysToDisplay} days ago`}
59+
{hoursAgo !== 0 &&
60+
hoursToDisplay !== 0 &&
61+
` ${hoursToDisplay} hours ago`}
6662
{hoursAgo === 0 && ` now`}
6763
</Text>
6864
<Divider />
@@ -115,7 +111,18 @@ export const CustomTooltip = (props: CustomTooltipProps) => {
115111

116112
<Divider mb="5px" />
117113
{props.payload.map((value, index) => (
118-
<Box display="flex" flexDirection="row" key={`value-${index}`} color={props.colors[props.fetchedLeagues.indexOf((value.name as string).split(" - ")[0])]}>
114+
<Box
115+
display="flex"
116+
flexDirection="row"
117+
key={`value-${index}`}
118+
color={
119+
props.colors[
120+
props.fetchedLeagues.indexOf(
121+
(value.name as string).split(" - ")[0],
122+
)
123+
]
124+
}
125+
>
119126
<Box>{`${value.name}: `}</Box>
120127
<Box
121128
textAlign="right"

src/frontend/src/components/Graph/GraphComponent.tsx

Lines changed: 62 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,14 @@ import {
77
Tooltip,
88
Legend,
99
ResponsiveContainer,
10+
TooltipContentProps,
1011
} from "recharts";
1112
import { Box, BoxProps, Center, Spinner } from "@chakra-ui/react";
1213
import { useGraphInputStore } from "../../store/GraphInputStore";
1314
import { usePlotSettingsStore } from "../../store/PlotSettingsStore";
1415
import { capitalizeFirstLetter } from "../../hooks/utils";
1516
import { CustomTooltip } from "./CustomTooltip";
16-
import {
17-
formatHoursSinceLaunch,
18-
} from "../../hooks/graphing/utils";
17+
import { formatHoursSinceLaunch } from "../../hooks/graphing/utils";
1918
import { BiError } from "react-icons/bi";
2019
import { ErrorMessage } from "../Input/StandardLayoutInput/ErrorMessage";
2120
import useGetPlotData from "../../hooks/graphing/processPlottingData";
@@ -30,14 +29,15 @@ import PlotCustomizationButtons from "../Common/PlotCustomizationButtons";
3029
*/
3130
function GraphComponent(props: BoxProps) {
3231
const { plotQuery, leagues } = useGraphInputStore();
33-
const { result: plotData,
32+
const {
33+
result: plotData,
3434
mostCommonCurrencyUsed,
3535
confidenceRating,
3636
upperBoundryChaos,
3737
upperBoundrySecondary,
3838
fetchStatus,
39-
error } = useGetPlotData(plotQuery);
40-
39+
error,
40+
} = useGetPlotData(plotQuery);
4141

4242
const mostCommonCurrencyUsedName = `${capitalizeFirstLetter(mostCommonCurrencyUsed ?? "")} value`;
4343
const renderGraph =
@@ -57,9 +57,17 @@ function GraphComponent(props: BoxProps) {
5757
: "ui.input";
5858

5959
const colors = [
60-
"#1f77b4", "#ff7f0e", "#2ca02c", "#d62728", "#9467bd", "#8c564b", "#e377c2", "#7f7f7f", "#bcbd22", "#17becf"
61-
]
62-
60+
"#1f77b4",
61+
"#ff7f0e",
62+
"#2ca02c",
63+
"#d62728",
64+
"#9467bd",
65+
"#8c564b",
66+
"#e377c2",
67+
"#7f7f7f",
68+
"#bcbd22",
69+
"#17becf",
70+
];
6371

6472
if (fetchStatus === "fetching") {
6573
return (
@@ -145,25 +153,35 @@ function GraphComponent(props: BoxProps) {
145153
allowDataOverflow
146154
/>
147155
<Tooltip
148-
content={<CustomTooltip upperBoundry={upperBoundryChaos} fetchedLeagues={fetchedLeagues} colors={colors} />}
156+
content={(props: TooltipContentProps<number, number>) => (
157+
<CustomTooltip
158+
{...props}
159+
upperBoundry={upperBoundryChaos}
160+
fetchedLeagues={fetchedLeagues}
161+
colors={colors}
162+
/>
163+
)}
149164
isAnimationActive={false}
150165
/>
151166
<Legend verticalAlign="top" height={36} />
152-
{plotData.data.map((series, idx) => (
153-
leagues.includes(series.name) && (<Line
154-
type="monotone"
155-
data={series.data}
156-
dataKey={"valueInChaos"}
157-
key={series.name}
158-
name={series.name + " - " + "Chaos Value"}
159-
stroke={colors[idx]}
160-
yAxisId={0}
161-
hide={!showChaos}
162-
isAnimationActive={false}
163-
dot={{ fill: colors[idx] }}
164-
legendType={showChaos ? "line" : "none"}
165-
/>)
166-
))}
167+
{plotData.data.map(
168+
(series, idx) =>
169+
leagues.includes(series.name) && (
170+
<Line
171+
type="monotone"
172+
data={series.data}
173+
dataKey={"valueInChaos"}
174+
key={series.name}
175+
name={series.name + " - " + "Chaos Value"}
176+
stroke={colors[idx]}
177+
yAxisId={0}
178+
hide={!showChaos}
179+
isAnimationActive={false}
180+
dot={{ fill: colors[idx] }}
181+
legendType={showChaos ? "line" : "none"}
182+
/>
183+
),
184+
)}
167185
<YAxis
168186
label={{
169187
value: mostCommonCurrencyUsedName,
@@ -178,21 +196,25 @@ function GraphComponent(props: BoxProps) {
178196
domain={[0, upperBoundrySecondary]}
179197
allowDataOverflow
180198
/>
181-
{mostCommonCurrencyUsed !== "chaos" && plotData.data.map((series, idx) => (
182-
leagues.includes(series.name) && (<Line
183-
type="monotone"
184-
data={series.data}
185-
dataKey={"valueInMostCommonCurrencyUsed"}
186-
key={series.name}
187-
name={series.name + " - " + mostCommonCurrencyUsedName}
188-
stroke={colors[idx]}
189-
yAxisId={1}
190-
hide={!showSecondary}
191-
isAnimationActive={false}
192-
dot={{ fill: colors[idx] }}
193-
legendType={showSecondary ? "line" : "none"}
194-
/>)
195-
))}
199+
{mostCommonCurrencyUsed !== "chaos" &&
200+
plotData.data.map(
201+
(series, idx) =>
202+
leagues.includes(series.name) && (
203+
<Line
204+
type="monotone"
205+
data={series.data}
206+
dataKey={"valueInMostCommonCurrencyUsed"}
207+
key={series.name}
208+
name={series.name + " - " + mostCommonCurrencyUsedName}
209+
stroke={colors[idx]}
210+
yAxisId={1}
211+
hide={!showSecondary}
212+
isAnimationActive={false}
213+
dot={{ fill: colors[idx] }}
214+
legendType={showSecondary ? "line" : "none"}
215+
/>
216+
),
217+
)}
196218
</LineChart>
197219
</ResponsiveContainer>
198220
</Box>

0 commit comments

Comments
 (0)