d2/d2layouts/d2sequence/layout.go

101 lines
2.9 KiB
Go
Raw Normal View History

package d2sequence
import (
"context"
"fmt"
"math"
"oss.terrastruct.com/d2/d2graph"
"oss.terrastruct.com/d2/lib/geo"
"oss.terrastruct.com/d2/lib/go2"
"oss.terrastruct.com/d2/lib/label"
)
func Layout(ctx context.Context, g *d2graph.Graph) (err error) {
pad := 50. // 2 * 25
edgeYStep := 100.
2022-11-28 19:11:38 +00:00
actorXStep := 200.
maxActorHeight := 0.
for _, edge := range g.Edges {
edgeYStep = math.Max(edgeYStep, float64(edge.LabelDimensions.Height)+pad)
2022-11-28 19:11:38 +00:00
actorXStep = math.Max(actorXStep, float64(edge.LabelDimensions.Width)+pad)
maxActorHeight = math.Max(maxActorHeight, edge.Src.Height+pad)
maxActorHeight = math.Max(maxActorHeight, edge.Dst.Height+pad)
}
2022-11-28 19:32:56 +00:00
placeActors(g.Objects, maxActorHeight, actorXStep)
2022-11-28 19:11:38 +00:00
routeEdges(g.Edges, maxActorHeight, edgeYStep)
2022-11-28 19:32:56 +00:00
addLifelineEdges(g, g.Objects, edgeYStep)
return nil
}
2022-11-28 19:11:38 +00:00
// placeActors places actors bottom aligned, side by side
2022-11-28 19:32:56 +00:00
func placeActors(actors []*d2graph.Object, maxHeight, xStep float64) {
x := 0.
2022-11-28 19:32:56 +00:00
for _, actors := range actors {
2022-11-28 19:11:38 +00:00
yOffset := maxHeight - actors.Height
actors.TopLeft = geo.NewPoint(x, yOffset)
x += actors.Width + xStep
actors.LabelPosition = go2.Pointer(string(label.InsideMiddleCenter))
}
}
// routeEdges routes horizontal edges from Src to Dst
func routeEdges(edgesInOrder []*d2graph.Edge, startY, yStep float64) {
edgeY := startY + yStep // in case the first edge has a tall label
for _, edge := range edgesInOrder {
start := edge.Src.Center()
start.Y = edgeY
end := edge.Dst.Center()
end.Y = edgeY
edge.Route = []*geo.Point{start, end}
edgeY += yStep
if edge.Attributes.Label.Value != "" {
2022-11-28 19:32:56 +00:00
isLeftToRight := edge.Src.TopLeft.X < edge.Dst.TopLeft.X
if isLeftToRight {
edge.LabelPosition = go2.Pointer(string(label.OutsideTopCenter))
} else {
edge.LabelPosition = go2.Pointer(string(label.OutsideBottomCenter))
}
}
}
}
2022-11-28 19:11:38 +00:00
// addLifelineEdges adds a new edge for each actor in the graph that represents the
// edge below the actor showing its lifespan
2022-11-26 06:41:11 +00:00
// ┌──────────────┐
2022-11-28 19:11:38 +00:00
// │ actor │
2022-11-26 06:41:11 +00:00
// └──────┬───────┘
// │
// │ lifeline
// │
// │
2022-11-28 19:32:56 +00:00
func addLifelineEdges(g *d2graph.Graph, actors []*d2graph.Object, yStep float64) {
endY := g.Edges[len(g.Edges)-1].Route[0].Y + yStep
2022-11-28 19:32:56 +00:00
for _, actor := range actors {
2022-11-28 19:11:38 +00:00
actorBottom := actor.Center()
actorBottom.Y = actor.TopLeft.Y + actor.Height
actorLifelineEnd := actor.Center()
actorLifelineEnd.Y = endY
g.Edges = append(g.Edges, &d2graph.Edge{
Attributes: d2graph.Attributes{
Style: d2graph.Style{
2022-11-28 19:32:56 +00:00
StrokeDash: &d2graph.Scalar{Value: "10"},
2022-11-28 19:11:38 +00:00
Stroke: actor.Attributes.Style.Stroke,
StrokeWidth: actor.Attributes.Style.StrokeWidth,
},
},
2022-11-28 19:11:38 +00:00
Src: actor,
SrcArrow: false,
Dst: &d2graph.Object{
2022-11-28 19:11:38 +00:00
ID: actor.ID + fmt.Sprintf("-lifeline-end-%d", go2.StringToIntHash(actor.ID+"-lifeline-end")),
},
DstArrow: false,
2022-11-28 19:11:38 +00:00
Route: []*geo.Point{actorBottom, actorLifelineEnd},
})
}
}