From 48195e9d5cb9dd47ee2fa4709fda56d2e8b8b875 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BAlio=20C=C3=A9sar=20Batista?= Date: Mon, 28 Nov 2022 13:18:33 -0800 Subject: [PATCH] Distribute long labels across actors --- d2layouts/d2sequence/layout.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/d2layouts/d2sequence/layout.go b/d2layouts/d2sequence/layout.go index c98b5b3c4..5d1107a39 100644 --- a/d2layouts/d2sequence/layout.go +++ b/d2layouts/d2sequence/layout.go @@ -16,11 +16,19 @@ func Layout(ctx context.Context, g *d2graph.Graph) (err error) { actorXStep := MIN_ACTOR_DISTANCE maxActorHeight := 0. + actorRank := make(map[*d2graph.Object]int) + for rank, actor := range g.Objects { + actorRank[actor] = rank + } for _, edge := range g.Edges { edgeYStep = math.Max(edgeYStep, float64(edge.LabelDimensions.Height)+HORIZONTAL_PAD) - actorXStep = math.Max(actorXStep, float64(edge.LabelDimensions.Width)+HORIZONTAL_PAD) maxActorHeight = math.Max(maxActorHeight, edge.Src.Height+HORIZONTAL_PAD) maxActorHeight = math.Max(maxActorHeight, edge.Dst.Height+HORIZONTAL_PAD) + // ensures that long labels, spanning over multiple actors, don't make for large gaps between actors + // by distributing the label length across the actors rank difference + rankDiff := math.Abs(float64(actorRank[edge.Src]) - float64(actorRank[edge.Dst])) + distributedLabelWidth := float64(edge.LabelDimensions.Width) / rankDiff + actorXStep = math.Max(actorXStep, distributedLabelWidth+HORIZONTAL_PAD) } placeActors(g.Objects, maxActorHeight, actorXStep)