d2/d2layouts/d2sequence/layout.go

111 lines
3.2 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.
2022-11-28 19:11:38 +00:00
var actorsInOrder []*d2graph.Object
seen := make(map[*d2graph.Object]struct{})
for _, edge := range g.Edges {
if _, exists := seen[edge.Src]; !exists {
seen[edge.Src] = struct{}{}
2022-11-28 19:11:38 +00:00
actorsInOrder = append(actorsInOrder, edge.Src)
}
if _, exists := seen[edge.Dst]; !exists {
seen[edge.Dst] = struct{}{}
2022-11-28 19:11:38 +00:00
actorsInOrder = append(actorsInOrder, edge.Dst)
}
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:11:38 +00:00
placeActors(actorsInOrder, maxActorHeight, actorXStep)
// edges are placed in the order users define them
2022-11-28 19:11:38 +00:00
routeEdges(g.Edges, maxActorHeight, edgeYStep)
addLifelineEdges(g, actorsInOrder, edgeYStep)
return nil
}
2022-11-28 19:11:38 +00:00
// placeActors places actors bottom aligned, side by side
func placeActors(actorsInOrder []*d2graph.Object, maxHeight, xStep float64) {
x := 0.
2022-11-28 19:11:38 +00:00
for _, actors := range actorsInOrder {
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 != "" {
// TODO: consider label right-to-left
edge.LabelPosition = go2.Pointer(string(label.OutsideTopCenter))
}
}
}
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:11:38 +00:00
func addLifelineEdges(g *d2graph.Graph, actorsInOrder []*d2graph.Object, yStep float64) {
endY := g.Edges[len(g.Edges)-1].Route[0].Y + yStep
2022-11-28 19:11:38 +00:00
for _, actor := range actorsInOrder {
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{
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},
})
}
}