1
- import {
2
- select ,
3
- scaleOrdinal ,
4
- schemeTableau10 ,
5
- hsl ,
6
- hierarchy ,
7
- partition ,
8
- arc as d3Arc ,
9
- interpolate ,
10
- HierarchyNode ,
11
- } from "d3" ;
1
+ import * as d3 from "d3" ;
12
2
import { useRef , useEffect , useState } from "react" ;
13
3
import { getData } from "./data" ;
14
4
import { TreeNode , NodeDetails } from "./NodeDetails" ;
@@ -32,19 +22,20 @@ export const SectionViz = (): JSX.Element => {
32
22
const svgNode = svgRef . current ;
33
23
34
24
// Clear any existing content in case of re-render
35
- select ( svgNode ) . selectAll ( "*" ) . remove ( ) ;
25
+ d3 . select ( svgNode ) . selectAll ( "*" ) . remove ( ) ;
36
26
37
27
// Set up dimensions and radius for the sunburst chart
38
28
const width = 800 ;
39
29
const height = width ;
40
30
const radius = ( width - 10 ) / 2 ;
41
31
42
32
// Create the main SVG container and center the group
43
- const svg = select ( svgNode )
33
+ const svg = d3
34
+ . select ( svgNode )
44
35
. attr ( "viewBox" , [ - width / 2 , - height / 2 , width , width ] )
45
36
. style ( "font" , "10px sans-serif" ) ;
46
37
// Define a color scale for base colors (root and first ring)
47
- const baseColor = scaleOrdinal ( schemeTableau10 ) ;
38
+ const baseColor = d3 . scaleOrdinal ( d3 . schemeTableau10 ) ;
48
39
49
40
// Helper function to get color for a node
50
41
function getNodeColor ( d ) : string {
@@ -55,7 +46,7 @@ export const SectionViz = (): JSX.Element => {
55
46
const ancestor = d . ancestors ( ) . find ( ( n ) => n . depth === 1 ) ;
56
47
if ( ! ancestor ) return "#ccc" ; // fallback
57
48
58
- const baseColorHsl = hsl ( baseColor ( ancestor . data . name ) ) ;
49
+ const baseColorHsl = d3 . hsl ( baseColor ( ancestor . data . name ) ) ;
59
50
60
51
// Increase lightness and decrease saturation as we go deeper
61
52
const lightnessFactor = 0.05 * ( d . depth - 1 ) ;
@@ -69,7 +60,8 @@ export const SectionViz = (): JSX.Element => {
69
60
}
70
61
71
62
// Create a tooltip for interactivity
72
- const tooltip = select ( "body" )
63
+ const tooltip = d3
64
+ . select ( "body" )
73
65
. append ( "div" )
74
66
. attr ( "class" , "tooltip" )
75
67
. style ( "opacity" , 0 )
@@ -83,11 +75,12 @@ export const SectionViz = (): JSX.Element => {
83
75
. style ( "pointer-events" , "none" ) ;
84
76
85
77
// Create a hierarchy from the sample data.
86
- const hierarchyData = hierarchy ( data )
78
+ const hierarchyData = d3
79
+ . hierarchy ( data )
87
80
. sum ( ( d ) => ( d . children . length == 0 ? 1 : 0 ) )
88
81
. sort ( ( a : unknown , b : unknown ) => ( a . data . name < b . data . name ? - 1 : 1 ) ) ;
89
82
90
- const root = partition ( ) . size ( [ 2 * Math . PI , hierarchyData . height + 1 ] ) (
83
+ const root = d3 . partition ( ) . size ( [ 2 * Math . PI , hierarchyData . height + 1 ] ) (
91
84
hierarchyData
92
85
) ;
93
86
@@ -117,8 +110,8 @@ export const SectionViz = (): JSX.Element => {
117
110
. outerRadius ( ( d ) => ( Math . min ( d . y1 , DEPTH ) * radius ) / DEPTH - 0.5 ) ;
118
111
119
112
function isVisible (
120
- d : HierarchyNode < TreeNode > ,
121
- currentRoot : HierarchyNode < TreeNode > ,
113
+ d : d3 . HierarchyNode < TreeNode > ,
114
+ currentRoot : d3 . HierarchyNode < TreeNode > ,
122
115
targetDepth : number
123
116
) : boolean {
124
117
const isDescendant = d . ancestors ( ) . indexOf ( currentRoot ) >= 0 ;
@@ -282,13 +275,12 @@ export const SectionViz = (): JSX.Element => {
282
275
} as NodeTarget ;
283
276
} ) ;
284
277
285
- const transition = svg . transition ( ) . duration ( 750 ) ;
286
-
287
278
// Transition the arcs.
288
279
path
289
- . transition ( transition )
280
+ . transition ( )
281
+ . duration ( 750 )
290
282
. tween ( "data" , ( d : TreeNode ) => {
291
- const i = interpolate ( d . current , d . target ) ;
283
+ const i = d3 . interpolate ( d . current , d . target ) ;
292
284
// eslint-disable-next-line sonarjs/no-nested-functions -- cleaner in d3 to keep this inlined
293
285
return ( transition_duration : number ) : void => {
294
286
d . current = i ( transition_duration ) ;
@@ -305,12 +297,13 @@ export const SectionViz = (): JSX.Element => {
305
297
306
298
// Transition the labels with updated visibility logic
307
299
label
308
- . transition ( svg . transition ( ) . duration ( 750 ) )
300
+ . transition ( )
301
+ . duration ( 750 )
309
302
. tween ( "data" , ( d ) => {
310
- const i = interpolate ( d . current , d . target ) ;
303
+ const i = d3 . interpolate ( d . current , d . target ) ;
311
304
// eslint-disable-next-line sonarjs/no-nested-functions -- cleaner in d3 to keep this inlined
312
- return ( transition : number ) : void => {
313
- d . current = i ( transition ) ;
305
+ return ( transition_duration : number ) : void => {
306
+ d . current = i ( transition_duration ) ;
314
307
} ;
315
308
} )
316
309
. attrTween (
@@ -339,7 +332,7 @@ export const SectionViz = (): JSX.Element => {
339
332
// Clean up on component unmount: remove tooltip and clear svg.
340
333
return ( ) : void => {
341
334
tooltip . remove ( ) ;
342
- select ( svgNode ) . selectAll ( "*" ) . remove ( ) ;
335
+ d3 . select ( svgNode ) . selectAll ( "*" ) . remove ( ) ;
343
336
} ;
344
337
} , [ ] ) ;
345
338
0 commit comments