Fix graph tooltips

This commit is contained in:
Abdo 2024-08-14 15:34:24 +03:00
parent add1248cd8
commit 887aa1c8b7
6 changed files with 28 additions and 15 deletions

View file

@ -23,7 +23,7 @@ import {
import type { GraphBounds } from "./graph-helpers";
import { GraphRange } from "./graph-helpers";
import { setDataAvailable } from "./graph-helpers";
import { hideTooltip, showTooltip } from "./tooltip";
import { hideTooltip, showTooltip } from "./tooltip-utils.svelte";
/** 4 element array */
type ButtonCounts = number[];

View file

@ -25,7 +25,7 @@ import {
import type { GraphBounds, SearchDispatch } from "./graph-helpers";
import { RevlogRange, setDataAvailable } from "./graph-helpers";
import { clickableClass } from "./graph-styles";
import { hideTooltip, showTooltip } from "./tooltip";
import { hideTooltip, showTooltip } from "./tooltip-utils.svelte";
export interface GraphData {
// indexed by day, where day is relative to today

View file

@ -12,7 +12,7 @@ import { area, axisBottom, axisLeft, axisRight, cumsum, curveBasis, max, pointer
import type { GraphBounds } from "./graph-helpers";
import { setDataAvailable } from "./graph-helpers";
import { clickableClass } from "./graph-styles";
import { hideTooltip, showTooltip } from "./tooltip";
import { hideTooltip, showTooltip } from "./tooltip-utils.svelte";
export interface HistogramData {
scale: ScaleLinear<number, number>;

View file

@ -26,7 +26,7 @@ import {
import type { GraphBounds } from "./graph-helpers";
import { GraphRange, setDataAvailable } from "./graph-helpers";
import { oddTickClass } from "./graph-styles";
import { hideTooltip, showTooltip } from "./tooltip";
import { hideTooltip, showTooltip } from "./tooltip-utils.svelte";
interface Hour {
hour: number;

View file

@ -33,7 +33,7 @@ import {
import type { GraphBounds, TableDatum } from "./graph-helpers";
import { GraphRange, numericMap, setDataAvailable } from "./graph-helpers";
import { hideTooltip, showTooltip } from "./tooltip";
import { hideTooltip, showTooltip } from "./tooltip-utils.svelte";
interface Reviews {
learn: number;

View file

@ -3,33 +3,46 @@
import type { DebouncedFunc } from "lodash-es";
import { throttle } from "lodash-es";
import { mount } from "svelte";
import Tooltip from "./Tooltip.svelte";
let tooltip: Tooltip | null = null;
type TooltipProps = {
html: string;
x: number;
y: number;
show: boolean;
};
let tooltip: Record<string, any> | null = null;
let props: TooltipProps = { html: "", x: 0, y: 0, show: false };
function getOrCreateTooltip(): Tooltip {
function getOrCreateTooltip(): TooltipProps {
if (tooltip) {
return tooltip;
return props;
}
const target = document.createElement("div");
tooltip = new Tooltip({ target });
const p = $state(props);
props = p;
tooltip = mount(Tooltip, { target, props });
document.body.appendChild(target);
return tooltip;
return props;
}
function showTooltipInner(msg: string, x: number, y: number): void {
const tooltip = getOrCreateTooltip();
tooltip.$set({ html: msg, x, y, show: true });
const props = getOrCreateTooltip();
props.html = msg;
props.x = x;
props.y = y;
props.show = true;
}
export const showTooltip: DebouncedFunc<(msg: string, x: number, y: number) => void> = throttle(showTooltipInner, 16);
export function hideTooltip(): void {
const tooltip = getOrCreateTooltip();
const props = getOrCreateTooltip();
showTooltip.cancel();
tooltip.$set({ show: false });
props.show = false;
}