Distribute long labels across actors

This commit is contained in:
Júlio César Batista 2022-11-28 13:18:33 -08:00
parent 918ad5d96e
commit 48195e9d5c
No known key found for this signature in database
GPG key ID: 10C4B861BF314878

View file

@ -16,11 +16,19 @@ func Layout(ctx context.Context, g *d2graph.Graph) (err error) {
actorXStep := MIN_ACTOR_DISTANCE actorXStep := MIN_ACTOR_DISTANCE
maxActorHeight := 0. maxActorHeight := 0.
actorRank := make(map[*d2graph.Object]int)
for rank, actor := range g.Objects {
actorRank[actor] = rank
}
for _, edge := range g.Edges { for _, edge := range g.Edges {
edgeYStep = math.Max(edgeYStep, float64(edge.LabelDimensions.Height)+HORIZONTAL_PAD) 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.Src.Height+HORIZONTAL_PAD)
maxActorHeight = math.Max(maxActorHeight, edge.Dst.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) placeActors(g.Objects, maxActorHeight, actorXStep)