From 733f5fb8f574a84e21606dad76ab192b415fbfae Mon Sep 17 00:00:00 2001 From: Nathan Bierema Date: Sat, 31 Dec 2022 20:33:04 -0500 Subject: [PATCH] Update --- .../examples/tree/src/index.ts | 2 +- .../d3-state-visualizer/src/charts/index.ts | 3 +- .../src/charts/tree/tree.ts | 14 +++-- packages/d3-state-visualizer/src/index.ts | 3 +- packages/d3tooltip/src/index.ts | 54 ++++++++++++++----- 5 files changed, 56 insertions(+), 20 deletions(-) diff --git a/packages/d3-state-visualizer/examples/tree/src/index.ts b/packages/d3-state-visualizer/examples/tree/src/index.ts index 5e8cf5e6..8c06c327 100644 --- a/packages/d3-state-visualizer/examples/tree/src/index.ts +++ b/packages/d3-state-visualizer/examples/tree/src/index.ts @@ -28,7 +28,7 @@ const render = tree(document.getElementById('root')!, { isSorted: false, widthBetweenNodesCoeff: 1.5, heightBetweenNodesCoeff: 2, - style: { border: '1px solid black' }, + chartStyles: { border: '1px solid black' }, tooltipOptions: { offset: { left: 30, top: 10 }, indentationSize: 2 }, }); diff --git a/packages/d3-state-visualizer/src/charts/index.ts b/packages/d3-state-visualizer/src/charts/index.ts index fe78e795..39da13c3 100644 --- a/packages/d3-state-visualizer/src/charts/index.ts +++ b/packages/d3-state-visualizer/src/charts/index.ts @@ -1,2 +1,3 @@ +export type { StyleValue } from 'd3tooltip'; export { default as tree } from './tree/tree'; -export type { InputOptions, NodeWithId, Primitive } from './tree/tree'; +export type { InputOptions, Node } from './tree/tree'; diff --git a/packages/d3-state-visualizer/src/charts/tree/tree.ts b/packages/d3-state-visualizer/src/charts/tree/tree.ts index c818e806..5d2cec77 100644 --- a/packages/d3-state-visualizer/src/charts/tree/tree.ts +++ b/packages/d3-state-visualizer/src/charts/tree/tree.ts @@ -435,10 +435,16 @@ export default function ( if (!tooltipOptions.disabled) { nodeEnter.call( - tooltip>( - 'tooltip', - { ...tooltipOptions, root } - ) + tooltip< + SVGGElement, + HierarchyPointNodeWithPrivateChildren, + SVGGElement, + unknown, + HTMLElement, + unknown, + null, + undefined + >('tooltip', { ...tooltipOptions, root }) .text((d, i) => getTooltipString(d, i, tooltipOptions)) .styles(tooltipOptions.style) ); diff --git a/packages/d3-state-visualizer/src/index.ts b/packages/d3-state-visualizer/src/index.ts index 4bf5a25c..2dc3bea7 100644 --- a/packages/d3-state-visualizer/src/index.ts +++ b/packages/d3-state-visualizer/src/index.ts @@ -1,2 +1,3 @@ +export type { StyleValue } from 'd3tooltip'; export { tree } from './charts'; -export type { InputOptions, NodeWithId } from './charts'; +export type { InputOptions, Node } from './charts'; diff --git a/packages/d3tooltip/src/index.ts b/packages/d3tooltip/src/index.ts index 931d92c3..954adb5f 100644 --- a/packages/d3tooltip/src/index.ts +++ b/packages/d3tooltip/src/index.ts @@ -3,17 +3,22 @@ import type { BaseType, ContainerElement, Selection } from 'd3'; import { is } from 'ramda'; import functor from './utils/functor'; -interface Options { +interface Options< + GElement extends ContainerElement, + Datum, + PElement extends BaseType, + PDatum +> { left: number | undefined; top: number | undefined; offset: { left: number; top: number; }; - root: Selection | undefined; + root: Selection | undefined; } -const defaultOptions: Options = { +const defaultOptions: Options = { left: undefined, // mouseX top: undefined, // mouseY offset: { left: 0, top: 0 }, @@ -22,8 +27,13 @@ const defaultOptions: Options = { export type StyleValue = string | number | boolean; -interface Tip { - (selection: Selection): void; +interface Tip< + GElement extends BaseType, + Datum, + PElement extends BaseType, + PDatum +> { + (selection: Selection): void; styles: ( this: this, value: { [key: string]: StyleValue } | undefined @@ -36,11 +46,25 @@ interface Tip { ) => this; } -export function tooltip( +export function tooltip< + GElement extends BaseType, + Datum, + PElement extends BaseType, + PDatum, + RootGElement extends ContainerElement, + RootDatum, + RootPElement extends BaseType, + RootPDatum +>( className = 'tooltip', - options: Partial = {} -): Tip { - const { left, top, offset, root } = { ...defaultOptions, ...options }; + options: Partial< + Options + > = {} +): Tip { + const { left, top, offset, root } = { + ...defaultOptions, + ...options, + } as Options; let text: ( datum: Datum, @@ -49,12 +73,16 @@ export function tooltip( ) => string = () => ''; let styles: { [key: string]: StyleValue } = {}; - let el: Selection; - const anchor: Selection = - root || d3.select('body'); + let el: Selection; + const anchor: Selection< + RootGElement, + RootDatum, + RootPElement | HTMLElement, + RootPDatum + > = root || d3.select('body'); const rootNode = anchor.node()!; - function tip(selection: Selection) { + function tip(selection: Selection) { selection.on('mouseover.tip', (node) => { const [mouseX, mouseY] = d3.mouse(rootNode); const [x, y] = [left || mouseX + offset.left, top || mouseY - offset.top];