11// React component code
2- import { FC , RefObject , useContext , useEffect , useRef , useState } from 'react' ;
2+ import React , { RefObject , useContext , useEffect , useRef , useState } from 'react' ;
33import { graphviz } from 'd3-graphviz' ;
44import {
55 generateDotString ,
@@ -21,7 +21,12 @@ interface GraphvizParentProps {
2121 minVisits : number ;
2222}
2323
24- const GraphvizParent : FC < GraphvizParentProps > = ( { csvData, filter, selfLoops, minVisits} ) => {
24+ const GraphvizParent : React . FC < GraphvizParentProps > = ( {
25+ csvData,
26+ filter,
27+ selfLoops,
28+ minVisits,
29+ } ) => {
2530 const [ dotString , setDotString ] = useState < string | null > ( null ) ;
2631 const [ filteredDotString , setFilteredDotString ] = useState < string | null > ( null ) ;
2732 const [ topDotString , setTopDotString ] = useState < string | null > ( null ) ;
@@ -120,34 +125,8 @@ const GraphvizParent: FC<GraphvizParentProps> = ({csvData, filter, selfLoops, mi
120125 setFilteredDotString ( null ) ;
121126 }
122127 } , [ csvData , filter , selfLoops , minVisits , selectedSequence ] ) ;
123-
124- // Render Graphviz graphs using d3-graphviz
125- const renderGraph = (
126- dot : string | null ,
127- ref : React . RefObject < HTMLDivElement > ,
128- filename : string ,
129- numberOfGraphs : number
130- ) => {
131- if ( dot && ref . current ) {
132- // Dynamically adjust width based on the number of graphs
133- const width = numberOfGraphs === 3 ? 325 : 425 ; // Adjust the width for 3 graphs or 2 graphs
134- const height = 530 ; // Fixed height (or adjust dynamically if needed)
135-
136- graphviz ( ref . current )
137- . width ( width )
138- . height ( height )
139- . renderDot ( dot )
140- . on ( 'end' , ( ) => {
141- const svgElement = ref . current ?. querySelector ( 'svg' ) ;
142- if ( svgElement ) {
143- exportGraphAsPNG ( ref , filename ) ;
144- }
145- } ) ;
146- }
147- } ;
148-
149- // Export a graph as high-quality PNG
150- const exportGraphAsPNG = ( graphRef : RefObject < HTMLDivElement > , filename : string ) => {
128+ // Export a graph as high-quality PNG
129+ const exportGraphAsPNG = ( graphRef : React . RefObject < HTMLDivElement > , filename : string ) => {
151130 if ( ! graphRef . current ) return ;
152131
153132 const svgElement = graphRef . current . querySelector ( 'svg' ) ;
@@ -197,17 +176,43 @@ const GraphvizParent: FC<GraphvizParentProps> = ({csvData, filter, selfLoops, mi
197176
198177 const numberOfGraphs = [ topDotString , dotString , filteredDotString ] . filter ( Boolean ) . length ;
199178
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+
200205 useEffect ( ( ) => {
201206 renderGraph ( filteredDotString , graphRefFiltered , 'filtered_graph' , numberOfGraphs ) ;
202- } , [ topDotString ] ) ;
207+ } , [ filteredDotString ] ) ;
203208
204209 useEffect ( ( ) => {
205210 renderGraph ( topDotString , graphRefTop , 'selected_sequence' , numberOfGraphs ) ;
206- } , [ dotString ] ) ;
211+ } , [ topDotString ] ) ;
207212
208213 useEffect ( ( ) => {
209214 renderGraph ( dotString , graphRefMain , 'all_students' , numberOfGraphs ) ;
210- } , [ filteredDotString ] ) ;
215+ } , [ dotString ] ) ;
211216
212217
213218 return (
@@ -263,4 +268,4 @@ function ExportButton({onClick, label = "Export Image"}: ExportButtonProps) {
263268 { label }
264269 </ Button >
265270 ) ;
266- } ;
271+ }
0 commit comments