set dagre ranksep according to max edge label size

This commit is contained in:
Gavin Nishizawa 2022-12-20 16:05:35 -08:00
parent 2cffb51217
commit 707f5b9dd7
No known key found for this signature in database
GPG key ID: AE3B177777CE55CD

View file

@ -64,22 +64,35 @@ func Layout(ctx context.Context, g *d2graph.Graph) (err error) {
}
rootAttrs := dagreGraphAttrs{
ranksep: 100,
edgesep: 40,
nodesep: 60,
}
isHorizontal := false
switch g.Root.Attributes.Direction.Value {
case "down":
rootAttrs.rankdir = "TB"
case "right":
rootAttrs.rankdir = "LR"
isHorizontal = true
case "left":
rootAttrs.rankdir = "RL"
isHorizontal = true
case "up":
rootAttrs.rankdir = "BT"
default:
rootAttrs.rankdir = "TB"
}
maxLabelSize := 0
for _, edge := range g.Edges {
size := edge.LabelDimensions.Width
if !isHorizontal {
size = edge.LabelDimensions.Height
}
maxLabelSize = go2.Max(maxLabelSize, size)
}
rootAttrs.ranksep = go2.Max(100, maxLabelSize+40)
configJS := setGraphAttrs(rootAttrs)
if _, err := vm.RunString(configJS); err != nil {
return err