Skip to content

Commit dbd6c35

Browse files
committed
commit
1 parent 41b72f1 commit dbd6c35

2 files changed

Lines changed: 47 additions & 13 deletions

File tree

app/components/graphView.tsx

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ export default function GraphView({
232232
if (!start.x || !start.y || !end.x || !end.y) return
233233

234234
let textX, textY, angle;
235-
235+
236236
if (start.id === end.id) {
237237
const radius = NODE_SIZE * link.curve * 6.2;
238238
const angleOffset = -Math.PI / 4; // 45 degrees offset for text alignment
@@ -243,9 +243,9 @@ export default function GraphView({
243243
const midX = (start.x + end.x) / 2;
244244
const midY = (start.y + end.y) / 2;
245245
const offset = link.curve / 2;
246-
246+
247247
angle = Math.atan2(end.y - start.y, end.x - start.x);
248-
248+
249249
// maintain label vertical orientation for legibility
250250
if (angle > Math.PI / 2) angle = -(Math.PI - angle);
251251
if (angle < -Math.PI / 2) angle = -(-Math.PI - angle);
@@ -265,20 +265,31 @@ export default function GraphView({
265265
textY = rotatedY;
266266
}
267267

268+
// Get text width
269+
const label = graph.LabelsMap.get(link.label)!
270+
let { textWidth } = label
271+
272+
if (!textWidth) {
273+
textWidth = ctx.measureText(link.label).width;
274+
graph.LabelsMap.set(link.label, { ...label, textWidth })
275+
}
276+
268277
// Setup text properties to measure background size
269278
ctx.font = '2px Arial';
270279
const padding = 0.5;
271-
const textWidth = ctx.measureText(link.label).width;
272280
const textHeight = 2; // Approximate height for 2px font
273281

282+
// Save the current context state
283+
ctx.save();
284+
274285
// add label with background and rotation
275286
ctx.rotate(angle);
276-
287+
277288
// Draw background
278289
ctx.fillStyle = 'white';
279290
ctx.fillRect(
280-
textX - textWidth/2 - padding,
281-
textY - textHeight/2 - padding,
291+
textX - textWidth / 2 - padding,
292+
textY - textHeight / 2 - padding,
282293
textWidth + padding * 2,
283294
textHeight + padding * 2
284295
);
@@ -289,8 +300,8 @@ export default function GraphView({
289300
ctx.textAlign = 'center';
290301
ctx.textBaseline = 'middle';
291302
ctx.fillText(link.label, textX, textY);
292-
293-
ctx.rotate(-angle); // reset rotation
303+
304+
ctx.restore(); // reset rotation
294305
}}
295306
onNodeClick={handleNodeClick}
296307
onNodeDragEnd={(n, translate) => setPosition(prev => {

app/components/model.ts

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ export interface Category {
1111
show: boolean,
1212
}
1313

14+
export interface Label {
15+
name: string,
16+
textWidth: number,
17+
}
18+
1419
export type Node = NodeObject<{
1520
id: number,
1621
name: string,
@@ -62,18 +67,21 @@ export class Graph {
6267

6368
private id: string;
6469
private categories: Category[];
70+
private labels: Label[];
6571
private elements: GraphData;
66-
6772
private categoriesMap: Map<string, Category>;
73+
private labelsMap: Map<string, Label>;
6874
private nodesMap: Map<number, Node>;
6975
private linksMap: Map<number, Link>;
7076

71-
private constructor(id: string, categories: Category[], elements: GraphData,
72-
categoriesMap: Map<string, Category>, nodesMap: Map<number, Node>, edgesMap: Map<number, Link>) {
77+
private constructor(id: string, categories: Category[], labels: Label[], elements: GraphData,
78+
categoriesMap: Map<string, Category>, labelsMap: Map<string, Label>, nodesMap: Map<number, Node>, edgesMap: Map<number, Link>) {
7379
this.id = id;
7480
this.categories = categories;
81+
this.labels = labels;
7582
this.elements = elements;
7683
this.categoriesMap = categoriesMap;
84+
this.labelsMap = labelsMap;
7785
this.nodesMap = nodesMap;
7886
this.linksMap = edgesMap;
7987
}
@@ -90,6 +98,14 @@ export class Graph {
9098
return this.categoriesMap;
9199
}
92100

101+
get Labels(): Label[] {
102+
return this.labels;
103+
}
104+
105+
get LabelsMap(): Map<string, Label> {
106+
return this.labelsMap;
107+
}
108+
93109
get Elements(): GraphData {
94110
return this.elements;
95111
}
@@ -111,7 +127,7 @@ export class Graph {
111127
}
112128

113129
public static empty(): Graph {
114-
return new Graph("", [], { nodes: [], links: [] }, new Map<string, Category>(), new Map<number, Node>(), new Map<number, Link>())
130+
return new Graph("", [], [], { nodes: [], links: [] }, new Map<string, Category>(), new Map<string, Label>(), new Map<number, Node>(), new Map<number, Link>())
115131
}
116132

117133
public static create(results: any, graphName: string): Graph {
@@ -207,6 +223,13 @@ export class Graph {
207223
this.nodesMap.set(edgeData.dest_node, target)
208224
}
209225

226+
let label = this.labelsMap.get(edgeData.relation)
227+
if (!label) {
228+
label = { name: edgeData.relation, textWidth: 0 }
229+
this.labelsMap.set(edgeData.relation, label)
230+
this.labels.push(label)
231+
}
232+
210233
link = {
211234
id: edgeData.id,
212235
source,

0 commit comments

Comments
 (0)