Skip to content

Commit aac607c

Browse files
authored
Upgrade D3 to v6 (#1310)
* Update packages * Updates * Update changeset
1 parent 9994ea6 commit aac607c

File tree

6 files changed

+287
-282
lines changed

6 files changed

+287
-282
lines changed

.changeset/spicy-olives-compete.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
---
44

55
- Remove UMD build.
6-
- Upgrade d3 peer dependency from v3 to v5.
6+
- Upgrade d3 peer dependency from v3 to v6.
77
- Remove `attr` configuration method.
88
- Rename `style` configuration method to `styles` and move to options.
99
- Move `text` configuration method to options.

packages/d3-state-visualizer/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@
4141
},
4242
"dependencies": {
4343
"@babel/runtime": "^7.20.6",
44-
"@types/d3": "^5.16.4",
45-
"d3": "^5.16.0",
44+
"@types/d3": "^6.7.5",
45+
"d3": "^6.7.0",
4646
"d3tooltip": "^2.1.0",
4747
"deepmerge": "^4.2.2",
4848
"map2tree": "^2.1.0",

packages/d3-state-visualizer/src/charts/tree/tree.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,8 @@ export default function (DOMNode: HTMLElement, options: Partial<Options> = {}) {
186186
// eslint-disable-next-line @typescript-eslint/unbound-method
187187
.call(zoom.scaleTo, initialZoom)
188188
.call(
189-
zoom.on('zoom', () => {
190-
const { transform } = d3.event as D3ZoomEvent<SVGSVGElement, unknown>;
189+
zoom.on('zoom', (event) => {
190+
const { transform } = event as D3ZoomEvent<SVGSVGElement, unknown>;
191191
vis.attr('transform', transform.toString());
192192
})
193193
)
@@ -369,8 +369,8 @@ export default function (DOMNode: HTMLElement, options: Partial<Options> = {}) {
369369
.append('circle')
370370
.attr('class', 'nodeCircle')
371371
.attr('r', 0)
372-
.on('click', (clickedNode) => {
373-
if (d3.event.defaultPrevented) return;
372+
.on('click', (event, clickedNode) => {
373+
if ((event as Event).defaultPrevented) return;
374374
toggleChildren(clickedNode.data);
375375
update();
376376
});
@@ -383,7 +383,9 @@ export default function (DOMNode: HTMLElement, options: Partial<Options> = {}) {
383383
.attr('dy', '.35em')
384384
.style('fill-opacity', 0)
385385
.text((d) => d.data.name)
386-
.on('click', onClickText);
386+
.on('click', (_, datum) => {
387+
onClickText(datum);
388+
});
387389

388390
const nodeEnterAndUpdate = nodeEnter.merge(node);
389391

packages/d3tooltip/package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,17 @@
4444
"@babel/eslint-parser": "^7.19.1",
4545
"@babel/preset-env": "^7.20.2",
4646
"@babel/preset-typescript": "^7.18.6",
47-
"@types/d3": "^5.16.4",
47+
"@types/d3": "^6.7.5",
4848
"@typescript-eslint/eslint-plugin": "^5.47.0",
4949
"@typescript-eslint/parser": "^5.47.0",
50-
"d3": "^5.16.0",
50+
"d3": "^6.7.0",
5151
"eslint": "^8.30.0",
5252
"eslint-config-prettier": "^8.5.0",
5353
"rimraf": "^3.0.2",
5454
"typescript": "~4.9.4"
5555
},
5656
"peerDependencies": {
57-
"@types/d3": "^5.16.4",
58-
"d3": "^5.16.0"
57+
"@types/d3": "^6.7.5",
58+
"d3": "^6.7.0"
5959
}
6060
}

packages/d3tooltip/src/index.ts

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import * as d3 from 'd3';
2-
import type { BaseType, ContainerElement, Selection } from 'd3';
2+
import type { BaseType, Selection } from 'd3';
33

44
export type StyleValue = string | number | boolean;
55

66
interface Options<
77
Datum,
8-
RootGElement extends ContainerElement,
8+
RootGElement extends BaseType,
99
RootDatum,
1010
RootPElement extends BaseType,
1111
RootPDatum
@@ -23,13 +23,7 @@ interface Options<
2323
text: string | ((datum: Datum) => string);
2424
}
2525

26-
const defaultOptions: Options<
27-
unknown,
28-
ContainerElement,
29-
unknown,
30-
BaseType,
31-
unknown
32-
> = {
26+
const defaultOptions: Options<unknown, BaseType, unknown, BaseType, unknown> = {
3327
left: undefined, // mouseX
3428
top: undefined, // mouseY
3529
offset: { left: 0, top: 0 },
@@ -43,7 +37,7 @@ export function tooltip<
4337
Datum,
4438
PElement extends BaseType,
4539
PDatum,
46-
RootGElement extends ContainerElement,
40+
RootGElement extends BaseType,
4741
RootDatum,
4842
RootPElement extends BaseType,
4943
RootPDatum
@@ -68,9 +62,12 @@ export function tooltip<
6862
const rootNode = anchor.node()!;
6963

7064
return function tip(selection: Selection<GElement, Datum, PElement, PDatum>) {
71-
selection.on('mouseover.tip', (node) => {
72-
const [mouseX, mouseY] = d3.mouse(rootNode);
73-
const [x, y] = [left || mouseX + offset.left, top || mouseY - offset.top];
65+
selection.on('mouseover.tip', (event, datum) => {
66+
const [pointerX, pointerY] = d3.pointer(event, rootNode);
67+
const [x, y] = [
68+
left || pointerX + offset.left,
69+
top || pointerY - offset.top,
70+
];
7471

7572
anchor.selectAll(`div.${className}`).remove();
7673

@@ -81,20 +78,23 @@ export function tooltip<
8178
.style('z-index', 1001)
8279
.style('left', `${x}px`)
8380
.style('top', `${y}px`)
84-
.html(typeof text === 'function' ? () => text(node) : () => text);
81+
.html(typeof text === 'function' ? () => text(datum) : () => text);
8582

8683
for (const [key, value] of Object.entries(styles)) {
8784
el.style(key, value);
8885
}
8986
});
9087

91-
selection.on('mousemove.tip', (node) => {
92-
const [mouseX, mouseY] = d3.mouse(rootNode);
93-
const [x, y] = [left || mouseX + offset.left, top || mouseY - offset.top];
88+
selection.on('mousemove.tip', (event, datum) => {
89+
const [pointerX, pointerY] = d3.pointer(event, rootNode);
90+
const [x, y] = [
91+
left || pointerX + offset.left,
92+
top || pointerY - offset.top,
93+
];
9494

9595
el.style('left', `${x}px`)
9696
.style('top', `${y}px`)
97-
.html(typeof text === 'function' ? () => text(node) : () => text);
97+
.html(typeof text === 'function' ? () => text(datum) : () => text);
9898
});
9999

100100
selection.on('mouseout.tip', () => el.remove());

0 commit comments

Comments
 (0)