Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[wip] lower spacing/padding #2208

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 13 additions & 14 deletions d2layouts/d2dagrelayout/layout.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,15 @@ var setupJS string
var dagreJS string

const (
MIN_RANK_SEP = 60
// Edge and spacing constants
EDGE_LABEL_GAP = 20
DEFAULT_PADDING = 30.
DEFAULT_PADDING = 20.
MIN_SPACING = 10.

// Rank separation thresholds
MIN_RANK_SEP = 70
RANK_GAP_BUFFER = 20
SPACING_THRESHOLD = 100.
)

type ConfigurableOpts struct {
Expand Down Expand Up @@ -125,14 +130,9 @@ func Layout(ctx context.Context, g *d2graph.Graph, opts *ConfigurableOpts) (err
}

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

configJS := setGraphAttrs(rootAttrs)
Expand Down Expand Up @@ -781,7 +781,6 @@ func shiftReachableDown(g *d2graph.Graph, obj *d2graph.Object, start, distance f
}

// if object below is within this distance after shifting, also shift it
threshold := 100.
checkBelow := func(curr *d2graph.Object) {
currBottom := curr.TopLeft.Y + curr.Height
currRight := curr.TopLeft.X + curr.Width
Expand All @@ -795,7 +794,7 @@ func shiftReachableDown(g *d2graph.Graph, obj *d2graph.Object, start, distance f
continue
}
if originalRight < other.TopLeft.X &&
other.TopLeft.X < originalRight+distance+threshold &&
other.TopLeft.X < originalRight+distance+SPACING_THRESHOLD &&
curr.TopLeft.Y < other.TopLeft.Y+other.Height &&
other.TopLeft.Y < currBottom {
queue(other)
Expand All @@ -811,7 +810,7 @@ func shiftReachableDown(g *d2graph.Graph, obj *d2graph.Object, start, distance f
continue
}
if originalBottom < other.TopLeft.Y &&
other.TopLeft.Y < originalBottom+distance+threshold &&
other.TopLeft.Y < originalBottom+distance+SPACING_THRESHOLD &&
curr.TopLeft.X < other.TopLeft.X+other.Width &&
other.TopLeft.X < currRight {
queue(other)
Expand Down Expand Up @@ -1052,7 +1051,7 @@ func shiftReachableDown(g *d2graph.Graph, obj *d2graph.Object, start, distance f

// above and within threshold
if other.TopLeft.X < moved.TopLeft.X &&
moved.TopLeft.X < other.TopLeft.X+other.Width+threshold {
moved.TopLeft.X < other.TopLeft.X+other.Width+SPACING_THRESHOLD {
counts = false
break
}
Expand All @@ -1065,7 +1064,7 @@ func shiftReachableDown(g *d2graph.Graph, obj *d2graph.Object, start, distance f

// above and within threshold
if other.TopLeft.Y < moved.TopLeft.Y &&
moved.TopLeft.Y < other.TopLeft.Y+other.Height+threshold {
moved.TopLeft.Y < other.TopLeft.Y+other.Height+SPACING_THRESHOLD {
counts = false
break
}
Expand Down
61 changes: 35 additions & 26 deletions d2layouts/d2elklayout/layout.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,19 @@ import (
"oss.terrastruct.com/d2/lib/shape"
)

const (
// Layout options
DEFAULT_NODE_SPACING = 30
DEFAULT_THOROUGHNESS = 8
DEFAULT_EDGE_EDGE_SPACING = 20
DEFAULT_PORT_SPACING = 20.0
DEFAULT_EDGE_NODE_SPACING = 20
DEFAULT_SELF_LOOP_SPACING = 50
CONTAINER_PADDING = 20
MIN_ENDPOINT_MARGIN = 10
MIN_SEGMENT_PADDING = 5
)

//go:embed elk.js
var elkJS string

Expand Down Expand Up @@ -119,15 +132,12 @@ type ConfigurableOpts struct {

var DefaultOpts = ConfigurableOpts{
Algorithm: "layered",
NodeSpacing: 70.0,
Padding: "[top=50,left=50,bottom=50,right=50]",
EdgeNodeSpacing: 40.0,
SelfLoopSpacing: 50.0,
NodeSpacing: DEFAULT_NODE_SPACING,
Padding: fmt.Sprintf("[top=%d,left=%d,bottom=%d,right=%d]", CONTAINER_PADDING, CONTAINER_PADDING, CONTAINER_PADDING, CONTAINER_PADDING),
EdgeNodeSpacing: DEFAULT_EDGE_NODE_SPACING,
SelfLoopSpacing: DEFAULT_SELF_LOOP_SPACING,
}

var port_spacing = 40.
var edge_node_spacing = 40

type elkOpts struct {
EdgeNode int `json:"elk.spacing.edgeNode,omitempty"`
FixedAlignment string `json:"elk.layered.nodePlacement.bk.fixedAlignment,omitempty"`
Expand Down Expand Up @@ -179,9 +189,9 @@ func Layout(ctx context.Context, g *d2graph.Graph, opts *ConfigurableOpts) (err
elkGraph := &ELKGraph{
ID: "",
LayoutOptions: &elkOpts{
Thoroughness: 8,
EdgeEdgeBetweenLayersSpacing: 50,
EdgeNode: edge_node_spacing,
Thoroughness: DEFAULT_THOROUGHNESS,
EdgeEdgeBetweenLayersSpacing: DEFAULT_EDGE_EDGE_SPACING,
EdgeNode: DEFAULT_EDGE_NODE_SPACING,
HierarchyHandling: "INCLUDE_CHILDREN",
FixedAlignment: "BALANCED",
ConsiderModelOrder: "NODES_AND_EDGES",
Expand All @@ -197,8 +207,7 @@ func Layout(ctx context.Context, g *d2graph.Graph, opts *ConfigurableOpts) (err
},
}
if elkGraph.LayoutOptions.ConfigurableOpts.SelfLoopSpacing == DefaultOpts.SelfLoopSpacing {
// +5 for a tiny bit of padding
elkGraph.LayoutOptions.ConfigurableOpts.SelfLoopSpacing = go2.Max(elkGraph.LayoutOptions.ConfigurableOpts.SelfLoopSpacing, childrenMaxSelfLoop(g.Root, g.Root.Direction.Value == "down" || g.Root.Direction.Value == "" || g.Root.Direction.Value == "up")/2+5)
elkGraph.LayoutOptions.ConfigurableOpts.SelfLoopSpacing = go2.Max(elkGraph.LayoutOptions.ConfigurableOpts.SelfLoopSpacing, childrenMaxSelfLoop(g.Root, g.Root.Direction.Value == "down" || g.Root.Direction.Value == "" || g.Root.Direction.Value == "up")/2+MIN_SEGMENT_PADDING)
}
switch g.Root.Direction.Value {
case "down":
Expand Down Expand Up @@ -248,11 +257,11 @@ func Layout(ctx context.Context, g *d2graph.Graph, opts *ConfigurableOpts) (err
switch g.Root.Direction.Value {
case "right", "left":
if obj.Attributes.HeightAttr == nil {
obj.Height = math.Max(obj.Height, math.Max(incoming, outgoing)*port_spacing)
obj.Height = math.Max(obj.Height, math.Max(incoming, outgoing)*DEFAULT_PORT_SPACING)
}
default:
if obj.Attributes.WidthAttr == nil {
obj.Width = math.Max(obj.Width, math.Max(incoming, outgoing)*port_spacing)
obj.Width = math.Max(obj.Width, math.Max(incoming, outgoing)*DEFAULT_PORT_SPACING)
}
}
}
Expand All @@ -276,11 +285,11 @@ func Layout(ctx context.Context, g *d2graph.Graph, opts *ConfigurableOpts) (err
if len(obj.ChildrenArray) > 0 {
n.LayoutOptions = &elkOpts{
ForceNodeModelOrder: true,
Thoroughness: 8,
EdgeEdgeBetweenLayersSpacing: 50,
Thoroughness: DEFAULT_THOROUGHNESS,
EdgeEdgeBetweenLayersSpacing: DEFAULT_EDGE_EDGE_SPACING,
HierarchyHandling: "INCLUDE_CHILDREN",
FixedAlignment: "BALANCED",
EdgeNode: edge_node_spacing,
EdgeNode: DEFAULT_EDGE_NODE_SPACING,
ConsiderModelOrder: "NODES_AND_EDGES",
CycleBreakingStrategy: "GREEDY_MODEL_ORDER",
NodeSizeConstraints: "MINIMUM_SIZE",
Expand Down Expand Up @@ -720,17 +729,17 @@ func deleteBends(g *d2graph.Graph) {
continue
}
case isHorizontal:
if end.Y <= endpoint.TopLeft.Y+10-dy {
if end.Y <= endpoint.TopLeft.Y+MIN_ENDPOINT_MARGIN-dy {
continue
}
if end.Y >= endpoint.TopLeft.Y+endpoint.Height-10 {
if end.Y >= endpoint.TopLeft.Y+endpoint.Height-MIN_ENDPOINT_MARGIN {
continue
}
default:
if end.X <= endpoint.TopLeft.X+10 {
if end.X <= endpoint.TopLeft.X+MIN_ENDPOINT_MARGIN {
continue
}
if end.X >= endpoint.TopLeft.X+endpoint.Width-10+dx {
if end.X >= endpoint.TopLeft.X+endpoint.Width-MIN_ENDPOINT_MARGIN+dx {
continue
}
}
Expand Down Expand Up @@ -906,7 +915,7 @@ func countObjectIntersects(g *d2graph.Graph, src, dst *d2graph.Object, s geo.Seg
if g.Objects[i] == src || g.Objects[i] == dst {
continue
}
if o.Intersects(s, float64(edge_node_spacing)-1) {
if o.Intersects(s, float64(DEFAULT_EDGE_NODE_SPACING)-1) {
count++
}
}
Expand All @@ -931,19 +940,19 @@ func countEdgeIntersects(g *d2graph.Graph, sEdge *d2graph.Edge, s geo.Segment) (
if isHorizontal == otherIsHorizontal {
if s.Overlaps(*otherS, !isHorizontal, 0.) {
if isHorizontal {
if math.Abs(s.Start.Y-otherS.Start.Y) < float64(edge_node_spacing)/2. {
if math.Abs(s.Start.Y-otherS.Start.Y) < float64(DEFAULT_EDGE_NODE_SPACING)/2. {
overlapsCount++
if math.Abs(s.Start.Y-otherS.Start.Y) < float64(edge_node_spacing)/4. {
if math.Abs(s.Start.Y-otherS.Start.Y) < float64(DEFAULT_EDGE_NODE_SPACING)/4. {
closeOverlapsCount++
if math.Abs(s.Start.Y-otherS.Start.Y) < 1. {
touchingCount++
}
}
}
} else {
if math.Abs(s.Start.X-otherS.Start.X) < float64(edge_node_spacing)/2. {
if math.Abs(s.Start.X-otherS.Start.X) < float64(DEFAULT_EDGE_NODE_SPACING)/2. {
overlapsCount++
if math.Abs(s.Start.X-otherS.Start.X) < float64(edge_node_spacing)/4. {
if math.Abs(s.Start.X-otherS.Start.X) < float64(DEFAULT_EDGE_NODE_SPACING)/4. {
closeOverlapsCount++
if math.Abs(s.Start.Y-otherS.Start.Y) < 1. {
touchingCount++
Expand Down
Loading
Loading