Skip to content

Commit

Permalink
Merge pull request #973 from gavin-ts/dagre-direction-right-padding
Browse files Browse the repository at this point in the history
dagre: direction right padding
  • Loading branch information
gavin-ts authored Mar 4, 2023
2 parents 366ebb5 + 5120c49 commit 531b8be
Show file tree
Hide file tree
Showing 14 changed files with 852 additions and 252 deletions.
1 change: 1 addition & 0 deletions ci/release/changelogs/next.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@
- Fixes a regression where PNG backgrounds could be cut off in the appendix. [#941](https://github.com/terrastruct/d2/pull/941)
- Fixes zooming not working in watch mode. [#944](https://github.com/terrastruct/d2/pull/944)
- [API] Fixes `DeleteIDDeltas` giving duplicate deltas in rare cases. [#957](https://github.com/terrastruct/d2/pull/957)
- Fixes insufficient vertical padding in dagre with direction: right/left. [#973](https://github.com/terrastruct/d2/pull/973)
27 changes: 19 additions & 8 deletions d2layouts/d2dagrelayout/layout.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ func Layout(ctx context.Context, g *d2graph.Graph, opts *ConfigurableOpts) (err

maxContainerLabelHeight := 0
for _, obj := range g.Objects {
// TODO count root level container label sizes for ranksep
if len(obj.ChildrenArray) == 0 || obj.Parent == g.Root {
continue
}
Expand All @@ -126,15 +127,25 @@ func Layout(ctx context.Context, g *d2graph.Graph, opts *ConfigurableOpts) (err
}
}

maxLabelSize := 0
maxLabelWidth := 0
maxLabelHeight := 0
for _, edge := range g.Edges {
size := edge.LabelDimensions.Width
if !isHorizontal {
size = edge.LabelDimensions.Height
}
maxLabelSize = go2.Max(maxLabelSize, size)
width := edge.LabelDimensions.Width
height := edge.LabelDimensions.Height
maxLabelWidth = go2.Max(maxLabelWidth, width)
maxLabelHeight = go2.Max(maxLabelHeight, height)
}

if !isHorizontal {
rootAttrs.ranksep = go2.Max(go2.Max(100, maxLabelHeight+40), maxContainerLabelHeight)
} else {
rootAttrs.ranksep = go2.Max(100, maxLabelWidth+40)
// use existing config
rootAttrs.NodeSep = rootAttrs.EdgeSep
// configure vertical padding
rootAttrs.EdgeSep = go2.Max(maxLabelHeight+40, maxContainerLabelHeight)
// Note: non-containers have both of these as padding (rootAttrs.NodeSep + rootAttrs.EdgeSep)
}
rootAttrs.ranksep = go2.Max(go2.Max(100, maxLabelSize+40), maxContainerLabelHeight)

configJS := setGraphAttrs(rootAttrs)
if _, err := vm.RunString(configJS); err != nil {
Expand Down Expand Up @@ -286,7 +297,7 @@ func Layout(ctx context.Context, g *d2graph.Graph, opts *ConfigurableOpts) (err
}

for _, obj := range g.Objects {
if obj.LabelHeight == nil || len(obj.ChildrenArray) <= 0 {
if obj.LabelHeight == nil || len(obj.ChildrenArray) == 0 {
continue
}

Expand Down
Binary file modified e2etests-cli/testdata/TestCLI_E2E/internal_linked_pdf.exp.pdf
Binary file not shown.
22 changes: 22 additions & 0 deletions e2etests/stable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,28 @@ x -> hey -> y
{
name: "font_sizes_containers_large",
script: `
ninety nine: {
style.font-size: 99
sixty four: {
style.font-size: 64
thirty two:{
style.font-size: 32
sixteen: {
style.font-size: 16
eight: {
style.font-size: 8
}
}
}
}
}
`,
},
{
name: "font_sizes_containers_large_right",
script: `
direction: right
ninety nine: {
style.font-size: 99
sixty four: {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 531b8be

Please sign in to comment.