Skip to content

Commit 739224f

Browse files
committed
Merge branch 'mobile-version' of https://github.com/FalkorDB/code-graph into mobile-version
2 parents 1090871 + d196e59 commit 739224f

2 files changed

Lines changed: 85 additions & 22 deletions

File tree

app/components/graphView.tsx

Lines changed: 57 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -273,38 +273,77 @@ export default function GraphView({
273273

274274
if (!start.x || !start.y || !end.x || !end.y) return
275275

276+
let textX, textY, angle;
277+
276278
if (start.id === end.id) {
277279
const radius = NODE_SIZE * link.curve * 6.2;
278280
const angleOffset = -Math.PI / 4; // 45 degrees offset for text alignment
279-
const textX = start.x + radius * Math.cos(angleOffset);
280-
const textY = start.y + radius * Math.sin(angleOffset);
281-
282-
ctx.save();
283-
ctx.translate(textX, textY);
284-
ctx.rotate(-angleOffset);
281+
textX = start.x + radius * Math.cos(angleOffset);
282+
textY = start.y + radius * Math.sin(angleOffset);
283+
angle = -angleOffset;
285284
} else {
286-
const midX = (start.x + end.x) / 2 + (end.y - start.y) * (link.curve / 2);
287-
const midY = (start.y + end.y) / 2 + (start.x - end.x) * (link.curve / 2);
285+
const midX = (start.x + end.x) / 2;
286+
const midY = (start.y + end.y) / 2;
287+
const offset = link.curve / 2;
288288

289-
let textAngle = Math.atan2(end.y - start.y, end.x - start.x)
289+
angle = Math.atan2(end.y - start.y, end.x - start.x);
290290

291291
// maintain label vertical orientation for legibility
292-
if (textAngle > Math.PI / 2) textAngle = -(Math.PI - textAngle);
293-
if (textAngle < -Math.PI / 2) textAngle = -(-Math.PI - textAngle);
292+
if (angle > Math.PI / 2) angle = -(Math.PI - angle);
293+
if (angle < -Math.PI / 2) angle = -(-Math.PI - angle);
294+
295+
// Calculate perpendicular offset
296+
const perpX = -Math.sin(angle) * offset;
297+
const perpY = Math.cos(angle) * offset;
298+
299+
// Adjust position to compensate for rotation around origin
300+
const cos = Math.cos(angle);
301+
const sin = Math.sin(angle);
302+
textX = midX + perpX;
303+
textY = midY + perpY;
304+
const rotatedX = textX * cos + textY * sin;
305+
const rotatedY = -textX * sin + textY * cos;
306+
textX = rotatedX;
307+
textY = rotatedY;
308+
}
294309

295-
ctx.save();
296-
ctx.translate(midX, midY);
297-
ctx.rotate(textAngle);
310+
// Setup text properties to measure background size
311+
ctx.font = '2px Arial';
312+
const padding = 0.5;
313+
// Get text width and height
314+
const label = graph.LabelsMap.get(link.label)!
315+
let { textWidth, textHeight } = label
316+
317+
if (!textWidth || !textHeight) {
318+
const { width, actualBoundingBoxAscent, actualBoundingBoxDescent } = ctx.measureText(link.label)
319+
textWidth = width
320+
textHeight = actualBoundingBoxAscent + actualBoundingBoxDescent
321+
graph.LabelsMap.set(link.label, { ...label, textWidth, textHeight })
298322
}
299323

300-
// add label
324+
// Save the current context state
325+
ctx.save();
326+
327+
// add label with background and rotation
328+
ctx.rotate(angle);
329+
330+
// Draw background
331+
ctx.fillStyle = 'white';
332+
ctx.fillRect(
333+
textX - textWidth / 2 - padding,
334+
textY - textHeight / 2 - padding,
335+
textWidth + padding * 2,
336+
textHeight + padding * 2
337+
);
338+
339+
// Draw text
301340
ctx.globalAlpha = 1;
302341
ctx.fillStyle = 'black';
303342
ctx.textAlign = 'center';
304343
ctx.textBaseline = 'middle';
305-
ctx.font = '2px Arial';
306-
ctx.fillText(link.label, 0, 0);
307-
ctx.restore()
344+
ctx.fillText(link.label, textX, textY);
345+
346+
ctx.restore(); // reset rotation
308347
}}
309348
onNodeClick={screenSize > Number(process.env.NEXT_PUBLIC_MOBILE_BREAKPOINT) || isShowPath ? handleNodeClick : handleRightClick}
310349
onNodeRightClick={handleRightClick}

app/components/model.ts

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

14+
export interface Label {
15+
name: string,
16+
textWidth: number,
17+
textHeight: number,
18+
}
19+
1420
export type Node = NodeObject<{
1521
id: number,
1622
name: string,
@@ -62,18 +68,21 @@ export class Graph {
6268

6369
private id: string;
6470
private categories: Category[];
71+
private labels: Label[];
6572
private elements: GraphData;
66-
6773
private categoriesMap: Map<string, Category>;
74+
private labelsMap: Map<string, Label>;
6875
private nodesMap: Map<number, Node>;
6976
private linksMap: Map<number, Link>;
7077

71-
private constructor(id: string, categories: Category[], elements: GraphData,
72-
categoriesMap: Map<string, Category>, nodesMap: Map<number, Node>, edgesMap: Map<number, Link>) {
78+
private constructor(id: string, categories: Category[], labels: Label[], elements: GraphData,
79+
categoriesMap: Map<string, Category>, labelsMap: Map<string, Label>, nodesMap: Map<number, Node>, edgesMap: Map<number, Link>) {
7380
this.id = id;
7481
this.categories = categories;
82+
this.labels = labels;
7583
this.elements = elements;
7684
this.categoriesMap = categoriesMap;
85+
this.labelsMap = labelsMap;
7786
this.nodesMap = nodesMap;
7887
this.linksMap = edgesMap;
7988
}
@@ -90,6 +99,14 @@ export class Graph {
9099
return this.categoriesMap;
91100
}
92101

102+
get Labels(): Label[] {
103+
return this.labels;
104+
}
105+
106+
get LabelsMap(): Map<string, Label> {
107+
return this.labelsMap;
108+
}
109+
93110
get Elements(): GraphData {
94111
return this.elements;
95112
}
@@ -111,7 +128,7 @@ export class Graph {
111128
}
112129

113130
public static empty(): Graph {
114-
return new Graph("", [], { nodes: [], links: [] }, new Map<string, Category>(), new Map<number, Node>(), new Map<number, Link>())
131+
return new Graph("", [], [], { nodes: [], links: [] }, new Map<string, Category>(), new Map<string, Label>(), new Map<number, Node>(), new Map<number, Link>())
115132
}
116133

117134
public static create(results: any, graphName: string): Graph {
@@ -207,6 +224,13 @@ export class Graph {
207224
this.nodesMap.set(edgeData.dest_node, target)
208225
}
209226

227+
let label = this.labelsMap.get(edgeData.relation)
228+
if (!label) {
229+
label = { name: edgeData.relation, textWidth: 0, textHeight: 0 }
230+
this.labelsMap.set(edgeData.relation, label)
231+
this.labels.push(label)
232+
}
233+
210234
link = {
211235
id: edgeData.id,
212236
source,

0 commit comments

Comments
 (0)