From 472bf120b92aaab3eacccf306c7697a7f66d90a6 Mon Sep 17 00:00:00 2001 From: Rebecca-Chou Date: Thu, 18 Aug 2022 11:10:08 -0400 Subject: [PATCH] add tooltip --- components/LineGraph.module.css | 15 +++++++ components/LineGraph.tsx | 78 ++++++++++----------------------- 2 files changed, 38 insertions(+), 55 deletions(-) diff --git a/components/LineGraph.module.css b/components/LineGraph.module.css index 5238f3e..0eebd8c 100644 --- a/components/LineGraph.module.css +++ b/components/LineGraph.module.css @@ -17,4 +17,19 @@ font-family: "Inconsolata", monospace; font-weight: 800; fill: var(--label); +} + +.tooltip { + font-family: "Inconsolata", monospace; + font-weight: bold; + top: 0; + left: 0; + position: absolute; + background-color: var(--label); + color: var(--primary-background); + box-shadow: 0px calc(1rem / 16) calc(2rem / 16) var(--card-background); + pointer-events: none; + padding: calc(10rem / 16); + font-size: calc(18rem / 16); + border-radius: calc(10rem / 16); } \ No newline at end of file diff --git a/components/LineGraph.tsx b/components/LineGraph.tsx index d60bae2..61c560d 100644 --- a/components/LineGraph.tsx +++ b/components/LineGraph.tsx @@ -1,14 +1,9 @@ -import { localPoint } from "@visx/event"; -import { - useTooltip, - useTooltipInPortal, - TooltipWithBounds, -} from "@visx/tooltip"; import { Axis, Grid, buildChartTheme, LineSeries, + Tooltip, XYChart, } from "@visx/xychart"; import React from "react"; @@ -35,46 +30,18 @@ interface LineGraphProps { yLabelSize?: number; } -interface TooltipData { - name?: string; -} - const DEFAULT_LABEL_SIZE = 16; -let tooltipTimeout: number; -export function LineGraph({ +const lineKey = ["Java", "C++"]; + +export const LineGraph = ({ data, width, height, margin, xLabelSize = DEFAULT_LABEL_SIZE, yLabelSize = DEFAULT_LABEL_SIZE, -}: LineGraphProps) { - const { - tooltipData, - tooltipLeft, - tooltipTop, - tooltipOpen, - showTooltip, - hideTooltip, - } = useTooltip(); - - const { containerRef, TooltipInPortal } = useTooltipInPortal({ - // use TooltipWithBounds - detectBounds: true, - // when tooltip containers are scrolled, this will correctly update the Tooltip position - scroll: true, - }); - - const handleMouseOver = (event: React.MouseEvent, datum) => { - const coords = localPoint(event); - showTooltip({ - tooltipLeft: coords.x, - tooltipTop: coords.y, - tooltipData: datum, - }); - }; - +}: LineGraphProps) => { // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-call const customTheme = buildChartTheme({ // colors @@ -97,7 +64,6 @@ export function LineGraph({ {data.map((d, i) => ( ))} + + ( +
+
{tooltipData?.nearestDatum?.key}
+
+ {/* {xAccessor(tooltipData.nearestDatum.datum)} + {": "} */} + {yAccessor(tooltipData.nearestDatum.datum)} +
+ )} + />
- - {tooltipOpen && ( - - Data value {tooltipData} - - )} ); -} +};