d2/d2layouts/d2sequence/layout.go

108 lines
3.4 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) {
2022-11-28 19:41:44 +00:00
edgeYStep := MIN_EDGE_DISTANCE
actorXStep := MIN_ACTOR_DISTANCE
2022-11-28 19:11:38 +00:00
maxActorHeight := 0.
2022-11-28 21:18:33 +00:00
actorRank := make(map[*d2graph.Object]int)
for rank, actor := range g.Objects {
actorRank[actor] = rank
}
for _, edge := range g.Edges {
2022-11-28 21:08:52 +00:00
edgeYStep = math.Max(edgeYStep, float64(edge.LabelDimensions.Height)+HORIZONTAL_PAD)
maxActorHeight = math.Max(maxActorHeight, edge.Src.Height+HORIZONTAL_PAD)
maxActorHeight = math.Max(maxActorHeight, edge.Dst.Height+HORIZONTAL_PAD)
2022-11-28 21:18:33 +00:00
// 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)
}
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},
})
}
}