11// React component code
2- import React , { useContext , useEffect , useRef , useState } from 'react' ;
2+ import React , { RefObject , useContext , useEffect , useRef , useState } from 'react' ;
33import { graphviz } from 'd3-graphviz' ;
44import {
55 generateDotString ,
@@ -125,33 +125,7 @@ const GraphvizParent: React.FC<GraphvizParentProps> = ({
125125 setFilteredDotString ( null ) ;
126126 }
127127 } , [ csvData , filter , selfLoops , minVisits , selectedSequence ] ) ;
128-
129- // Render Graphviz graphs using d3-graphviz
130- const renderGraph = (
131- dot : string | null ,
132- ref : React . RefObject < HTMLDivElement > ,
133- filename : string ,
134- numberOfGraphs : number
135- ) => {
136- if ( dot && ref . current ) {
137- // Dynamically adjust width based on the number of graphs
138- const width = numberOfGraphs === 3 ? 325 : 425 ; // Adjust the width for 3 graphs or 2 graphs
139- const height = 530 ; // Fixed height (or adjust dynamically if needed)
140-
141- graphviz ( ref . current )
142- . width ( width )
143- . height ( height )
144- . renderDot ( dot )
145- . on ( 'end' , ( ) => {
146- const svgElement = ref . current ?. querySelector ( 'svg' ) ;
147- if ( svgElement ) {
148- exportGraphAsPNG ( svgElement , filename ) ;
149- }
150- } ) ;
151- }
152- } ;
153-
154- // Export a graph as high-quality PNG
128+ // Export a graph as high-quality PNG
155129 const exportGraphAsPNG = ( graphRef : React . RefObject < HTMLDivElement > , filename : string ) => {
156130 if ( ! graphRef . current ) return ;
157131
@@ -202,17 +176,43 @@ const GraphvizParent: React.FC<GraphvizParentProps> = ({
202176
203177 const numberOfGraphs = [ topDotString , dotString , filteredDotString ] . filter ( Boolean ) . length ;
204178
179+ // Render Graphviz graphs using d3-graphviz
180+ const renderGraph = (
181+ dot : string | null ,
182+ ref : React . RefObject < HTMLDivElement > ,
183+ filename : string ,
184+ numberOfGraphs : number
185+ ) => {
186+ if ( dot && ref . current ) {
187+ // Dynamically adjust width based on the number of graphs
188+ const width = numberOfGraphs === 3 ? 325 : 425 ; // Adjust the width for 3 graphs or 2 graphs
189+ const height = 530 ; // Fixed height (or adjust dynamically if needed)
190+
191+ graphviz ( ref . current )
192+ . width ( width )
193+ . height ( height )
194+ . renderDot ( dot )
195+ . on ( 'end' , ( ) => {
196+ const svgElement :RefObject < HTMLDivElement > | SVGSVGElement | null | undefined = ref . current ?. querySelector ( 'svg' ) ;
197+ if ( svgElement ) {
198+ exportGraphAsPNG ( svgElement as unknown as RefObject < HTMLDivElement > , filename ) ;
199+ }
200+ } ) ;
201+ }
202+ } ;
203+
204+
205205 useEffect ( ( ) => {
206206 renderGraph ( filteredDotString , graphRefFiltered , 'filtered_graph' , numberOfGraphs ) ;
207- } , [ topDotString ] ) ;
207+ } , [ filteredDotString ] ) ;
208208
209209 useEffect ( ( ) => {
210210 renderGraph ( topDotString , graphRefTop , 'selected_sequence' , numberOfGraphs ) ;
211- } , [ dotString ] ) ;
211+ } , [ topDotString ] ) ;
212212
213213 useEffect ( ( ) => {
214214 renderGraph ( dotString , graphRefMain , 'all_students' , numberOfGraphs ) ;
215- } , [ filteredDotString ] ) ;
215+ } , [ dotString ] ) ;
216216
217217
218218
@@ -270,4 +270,4 @@ function ExportButton({onClick, label = "Export Image"}: ExportButtonProps) {
270270 { label }
271271 </ Button >
272272 ) ;
273- } ;
273+ }
0 commit comments