elk/dagre name consistency

This commit is contained in:
Alexander Wang 2022-12-30 12:25:33 -08:00
parent 85d102204f
commit 4b99bc30a7
No known key found for this signature in database
GPG key ID: D89FA31966BDBECE
2 changed files with 44 additions and 42 deletions

View file

@ -30,12 +30,12 @@ var setupJS string
//go:embed dagre.js //go:embed dagre.js
var dagreJS string var dagreJS string
type Opts struct { type ConfigurableOpts struct {
NodeSep int NodeSep int
EdgeSep int EdgeSep int
} }
var DefaultOpts = Opts{ var DefaultOpts = ConfigurableOpts{
NodeSep: 60, NodeSep: 60,
EdgeSep: 40, EdgeSep: 40,
} }
@ -52,16 +52,16 @@ type DagreEdge struct {
Points []*geo.Point `json:"points"` Points []*geo.Point `json:"points"`
} }
type dagreGraphAttrs struct { type dagreOpts struct {
// for a top to bottom graph: ranksep is y spacing, nodesep is x spacing, edgesep is x spacing // for a top to bottom graph: ranksep is y spacing, nodesep is x spacing, edgesep is x spacing
ranksep int ranksep int
edgesep int
nodesep int
// graph direction: tb (top to bottom)| bt | lr | rl // graph direction: tb (top to bottom)| bt | lr | rl
rankdir string rankdir string
ConfigurableOpts
} }
func Layout(ctx context.Context, g *d2graph.Graph, opts *Opts) (err error) { func Layout(ctx context.Context, g *d2graph.Graph, opts *ConfigurableOpts) (err error) {
if opts == nil { if opts == nil {
opts = &DefaultOpts opts = &DefaultOpts
} }
@ -76,9 +76,11 @@ func Layout(ctx context.Context, g *d2graph.Graph, opts *Opts) (err error) {
return err return err
} }
rootAttrs := dagreGraphAttrs{ rootAttrs := dagreOpts{
edgesep: opts.EdgeSep, ConfigurableOpts: ConfigurableOpts{
nodesep: opts.NodeSep, EdgeSep: opts.EdgeSep,
NodeSep: opts.NodeSep,
},
} }
isHorizontal := false isHorizontal := false
switch g.Root.Attributes.Direction.Value { switch g.Root.Attributes.Direction.Value {
@ -279,7 +281,7 @@ func Layout(ctx context.Context, g *d2graph.Graph, opts *Opts) (err error) {
return nil return nil
} }
func setGraphAttrs(attrs dagreGraphAttrs) string { func setGraphAttrs(attrs dagreOpts) string {
return fmt.Sprintf(`g.setGraph({ return fmt.Sprintf(`g.setGraph({
ranksep: %d, ranksep: %d,
edgesep: %d, edgesep: %d,
@ -288,8 +290,8 @@ func setGraphAttrs(attrs dagreGraphAttrs) string {
}); });
`, `,
attrs.ranksep, attrs.ranksep,
attrs.edgesep, attrs.ConfigurableOpts.EdgeSep,
attrs.nodesep, attrs.ConfigurableOpts.NodeSep,
attrs.rankdir, attrs.rankdir,
) )
} }

View file

@ -38,7 +38,7 @@ type ELKNode struct {
Height float64 `json:"height"` Height float64 `json:"height"`
Children []*ELKNode `json:"children,omitempty"` Children []*ELKNode `json:"children,omitempty"`
Labels []*ELKLabel `json:"labels,omitempty"` Labels []*ELKLabel `json:"labels,omitempty"`
LayoutOptions *ELKLayoutOptions `json:"layoutOptions,omitempty"` LayoutOptions *elkOpts `json:"layoutOptions,omitempty"`
} }
type ELKLabel struct { type ELKLabel struct {
@ -47,7 +47,7 @@ type ELKLabel struct {
Y float64 `json:"y"` Y float64 `json:"y"`
Width float64 `json:"width"` Width float64 `json:"width"`
Height float64 `json:"height"` Height float64 `json:"height"`
LayoutOptions *ELKLayoutOptions `json:"layoutOptions,omitempty"` LayoutOptions *elkOpts `json:"layoutOptions,omitempty"`
} }
type ELKPoint struct { type ELKPoint struct {
@ -72,19 +72,11 @@ type ELKEdge struct {
type ELKGraph struct { type ELKGraph struct {
ID string `json:"id"` ID string `json:"id"`
LayoutOptions *ELKLayoutOptions `json:"layoutOptions"` LayoutOptions *elkOpts `json:"layoutOptions"`
Children []*ELKNode `json:"children,omitempty"` Children []*ELKNode `json:"children,omitempty"`
Edges []*ELKEdge `json:"edges,omitempty"` Edges []*ELKEdge `json:"edges,omitempty"`
} }
var DefaultOpts = ConfigurableOpts{
Algorithm: "layered",
NodeSpacing: 100.0,
Padding: "[top=75,left=75,bottom=75,right=75]",
EdgeNodeSpacing: 50.0,
SelfLoopSpacing: 50.0,
}
type ConfigurableOpts struct { type ConfigurableOpts struct {
Algorithm string `json:"elk.algorithm,omitempty"` Algorithm string `json:"elk.algorithm,omitempty"`
NodeSpacing int `json:"spacing.nodeNodeBetweenLayers,omitempty"` NodeSpacing int `json:"spacing.nodeNodeBetweenLayers,omitempty"`
@ -93,7 +85,15 @@ type ConfigurableOpts struct {
SelfLoopSpacing int `json:"elk.spacing.nodeSelfLoop"` SelfLoopSpacing int `json:"elk.spacing.nodeSelfLoop"`
} }
type ELKLayoutOptions struct { var DefaultOpts = ConfigurableOpts{
Algorithm: "layered",
NodeSpacing: 100.0,
Padding: "[top=75,left=75,bottom=75,right=75]",
EdgeNodeSpacing: 50.0,
SelfLoopSpacing: 50.0,
}
type elkOpts struct {
Direction string `json:"elk.direction"` Direction string `json:"elk.direction"`
HierarchyHandling string `json:"elk.hierarchyHandling,omitempty"` HierarchyHandling string `json:"elk.hierarchyHandling,omitempty"`
InlineEdgeLabels bool `json:"elk.edgeLabels.inline,omitempty"` InlineEdgeLabels bool `json:"elk.edgeLabels.inline,omitempty"`
@ -125,7 +125,7 @@ func Layout(ctx context.Context, g *d2graph.Graph, opts *ConfigurableOpts) (err
elkGraph := &ELKGraph{ elkGraph := &ELKGraph{
ID: "root", ID: "root",
LayoutOptions: &ELKLayoutOptions{ LayoutOptions: &elkOpts{
HierarchyHandling: "INCLUDE_CHILDREN", HierarchyHandling: "INCLUDE_CHILDREN",
ConsiderModelOrder: "NODES_AND_EDGES", ConsiderModelOrder: "NODES_AND_EDGES",
ConfigurableOpts: *opts, ConfigurableOpts: *opts,
@ -171,7 +171,7 @@ func Layout(ctx context.Context, g *d2graph.Graph, opts *ConfigurableOpts) (err
} }
if len(obj.ChildrenArray) > 0 { if len(obj.ChildrenArray) > 0 {
n.LayoutOptions = &ELKLayoutOptions{ n.LayoutOptions = &elkOpts{
ForceNodeModelOrder: true, ForceNodeModelOrder: true,
ConfigurableOpts: ConfigurableOpts{ ConfigurableOpts: ConfigurableOpts{
Padding: opts.Padding, Padding: opts.Padding,
@ -206,7 +206,7 @@ func Layout(ctx context.Context, g *d2graph.Graph, opts *ConfigurableOpts) (err
Text: edge.Attributes.Label.Value, Text: edge.Attributes.Label.Value,
Width: float64(edge.LabelDimensions.Width), Width: float64(edge.LabelDimensions.Width),
Height: float64(edge.LabelDimensions.Height), Height: float64(edge.LabelDimensions.Height),
LayoutOptions: &ELKLayoutOptions{ LayoutOptions: &elkOpts{
InlineEdgeLabels: true, InlineEdgeLabels: true,
}, },
}) })