diff --git a/d2layouts/d2dagrelayout/layout.go b/d2layouts/d2dagrelayout/layout.go index 3ae70e35a..68d8ec0f6 100644 --- a/d2layouts/d2dagrelayout/layout.go +++ b/d2layouts/d2dagrelayout/layout.go @@ -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