From 94895ab693917af0476f5ec1ed80cd3e68e3a9b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BAlio=20C=C3=A9sar=20Batista?= Date: Fri, 2 Dec 2022 17:08:04 -0800 Subject: [PATCH] min object width --- d2layouts/d2sequence/constants.go | 5 +++++ d2layouts/d2sequence/layout_test.go | 2 +- d2layouts/d2sequence/sequence_diagram.go | 10 ++++++++-- 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/d2layouts/d2sequence/constants.go b/d2layouts/d2sequence/constants.go index 7dcf72d35..e39f8e2dd 100644 --- a/d2layouts/d2sequence/constants.go +++ b/d2layouts/d2sequence/constants.go @@ -8,6 +8,8 @@ const VERTICAL_PAD = 50. const MIN_ACTOR_DISTANCE = 250. +const MIN_ACTOR_WIDTH = 150. + // min vertical distance between messages const MIN_MESSAGE_DISTANCE = 80. @@ -25,3 +27,6 @@ const SPAN_MESSAGE_PAD = 16. const LIFELINE_STROKE_WIDTH int = 2 const LIFELINE_STROKE_DASH int = 8 + +// pad when the actor has the label placed OutsideMiddleBottom so that the lifeline is not so close to the text +const LIFELINE_LABEL_PAD = 5. diff --git a/d2layouts/d2sequence/layout_test.go b/d2layouts/d2sequence/layout_test.go index c54028a07..83892aada 100644 --- a/d2layouts/d2sequence/layout_test.go +++ b/d2layouts/d2sequence/layout_test.go @@ -268,7 +268,7 @@ func TestSpansSequenceDiagram(t *testing.T) { if a_t1.TopLeft.Y != b_t1.TopLeft.Y { t.Fatal("expected a.t1 and b.t1 to be placed at the same Y") } - if a_t1.TopLeft.Y-SPAN_MESSAGE_PAD != g.Edges[0].Route[0].Y { + if a_t1.TopLeft.Y+SPAN_MESSAGE_PAD != g.Edges[0].Route[0].Y { t.Fatal("expected a.t1 to be placed at the same Y of the first message") } diff --git a/d2layouts/d2sequence/sequence_diagram.go b/d2layouts/d2sequence/sequence_diagram.go index 9e5206e58..10aa7259d 100644 --- a/d2layouts/d2sequence/sequence_diagram.go +++ b/d2layouts/d2sequence/sequence_diagram.go @@ -52,6 +52,12 @@ func newSequenceDiagram(actors []*d2graph.Object, messages []*d2graph.Edge) *seq for rank, actor := range actors { sd.root = actor.Parent sd.objectRank[actor] = rank + + if actor.Width < MIN_ACTOR_WIDTH { + aspectRatio := actor.Height / actor.Width + actor.Width = MIN_ACTOR_WIDTH + actor.Height = math.Round(aspectRatio * actor.Width) + } sd.maxActorHeight = math.Max(sd.maxActorHeight, actor.Height) queue := make([]*d2graph.Object, len(actor.ChildrenArray)) @@ -138,7 +144,7 @@ func (sd *sequenceDiagram) addLifelineEdges() { actorBottom := actor.Center() actorBottom.Y = actor.TopLeft.Y + actor.Height if *actor.LabelPosition == string(label.OutsideBottomCenter) { - actorBottom.Y += float64(*actor.LabelHeight) + actorBottom.Y += float64(*actor.LabelHeight) + LIFELINE_LABEL_PAD } actorLifelineEnd := actor.Center() actorLifelineEnd.Y = endY @@ -180,7 +186,7 @@ func (sd *sequenceDiagram) placeSpans() { } // places spans from most to least nested - // the order is important because the only way a child span exists is if there'e an message to it + // the order is important because the only way a child span exists is if there's a message to it // however, the parent span might not have a message to it and then its position is based on the child position // or, there can be a message to it, but it comes after the child one meaning the top left position is still based on the child // and not on its own message