Skip to content

Commit 082e0e6

Browse files
committed
commit
1 parent dbd6c35 commit 082e0e6

2 files changed

Lines changed: 12 additions & 11 deletions

File tree

app/components/graphView.tsx

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -265,19 +265,19 @@ 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-
277268
// Setup text properties to measure background size
278269
ctx.font = '2px Arial';
279270
const padding = 0.5;
280-
const textHeight = 2; // Approximate height for 2px font
271+
// Get text width and height
272+
const label = graph.LabelsMap.get(link.label)!
273+
let { textWidth, textHeight } = label
274+
275+
if (!textWidth || !textHeight) {
276+
const { width, actualBoundingBoxAscent, actualBoundingBoxDescent } = ctx.measureText(link.label)
277+
textWidth = width
278+
textHeight = actualBoundingBoxAscent + actualBoundingBoxDescent
279+
graph.LabelsMap.set(link.label, { ...label, textWidth, textHeight })
280+
}
281281

282282
// Save the current context state
283283
ctx.save();

app/components/model.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export interface Category {
1414
export interface Label {
1515
name: string,
1616
textWidth: number,
17+
textHeight: number,
1718
}
1819

1920
export type Node = NodeObject<{
@@ -225,7 +226,7 @@ export class Graph {
225226

226227
let label = this.labelsMap.get(edgeData.relation)
227228
if (!label) {
228-
label = { name: edgeData.relation, textWidth: 0 }
229+
label = { name: edgeData.relation, textWidth: 0, textHeight: 0 }
229230
this.labelsMap.set(edgeData.relation, label)
230231
this.labels.push(label)
231232
}

0 commit comments

Comments
 (0)