Skip to content

Commit 9b63f1e

Browse files
committed
Fixed svg type error and graph size issue
1 parent 409a81c commit 9b63f1e

4 files changed

Lines changed: 36 additions & 34 deletions

File tree

bun.lockb

0 Bytes
Binary file not shown.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
"@types/lodash": "^4.17.1",
3333
"@types/papaparse": "^5.3.14",
3434
"@types/timsort": "^0.3.3",
35-
"caniuse-lite": "^1.0.30001688",
35+
"caniuse-lite": "^1.0.30001699",
3636
"class-variance-authority": "^0.7.0",
3737
"clsx": "^2.1.1",
3838
"d3": "^7.9.0",

src/components/GraphvizParent.tsx

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// React component code
2-
import React, {useContext, useEffect, useRef, useState} from 'react';
2+
import React, {RefObject, useContext, useEffect, useRef, useState} from 'react';
33
import {graphviz} from 'd3-graphviz';
44
import {
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+
}

src/components/GraphvizProcessing.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import Papa from 'papaparse';
22
import {SequenceCount} from "@/Context";
3+
// import {autoType} from "d3";
34

45
interface CSVRow {
56
'Session Id': string;
@@ -20,7 +21,8 @@ interface CSVRow {
2021
export const loadAndSortData = (csvData: string): CSVRow[] => {
2122
const parsedData = Papa.parse<CSVRow>(csvData, {
2223
header: true,
23-
skipEmptyLines: true
24+
skipEmptyLines: true,
25+
delimiter: ',',
2426
}).data;
2527

2628
const transformedData = parsedData.map(row => ({

0 commit comments

Comments
 (0)