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
var dagreJS string
type Opts struct {
type ConfigurableOpts struct {
NodeSep int
EdgeSep int
}
var DefaultOpts = Opts{
var DefaultOpts = ConfigurableOpts{
NodeSep: 60,
EdgeSep: 40,
}
@ -52,16 +52,16 @@ type DagreEdge struct {
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
ranksep int
edgesep int
nodesep int
// graph direction: tb (top to bottom)| bt | lr | rl
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 {
opts = &DefaultOpts
}
@ -76,9 +76,11 @@ func Layout(ctx context.Context, g *d2graph.Graph, opts *Opts) (err error) {
return err
}
rootAttrs := dagreGraphAttrs{
edgesep: opts.EdgeSep,
nodesep: opts.NodeSep,
rootAttrs := dagreOpts{
ConfigurableOpts: ConfigurableOpts{
EdgeSep: opts.EdgeSep,
NodeSep: opts.NodeSep,
},
}
isHorizontal := false
switch g.Root.Attributes.Direction.Value {
@ -279,7 +281,7 @@ func Layout(ctx context.Context, g *d2graph.Graph, opts *Opts) (err error) {
return nil
}
func setGraphAttrs(attrs dagreGraphAttrs) string {
func setGraphAttrs(attrs dagreOpts) string {
return fmt.Sprintf(`g.setGraph({
ranksep: %d,
edgesep: %d,
@ -288,8 +290,8 @@ func setGraphAttrs(attrs dagreGraphAttrs) string {
});
`,
attrs.ranksep,
attrs.edgesep,
attrs.nodesep,
attrs.ConfigurableOpts.EdgeSep,
attrs.ConfigurableOpts.NodeSep,
attrs.rankdir,
)
}

View file

@ -38,7 +38,7 @@ type ELKNode struct {
Height float64 `json:"height"`
Children []*ELKNode `json:"children,omitempty"`
Labels []*ELKLabel `json:"labels,omitempty"`
LayoutOptions *ELKLayoutOptions `json:"layoutOptions,omitempty"`
LayoutOptions *elkOpts `json:"layoutOptions,omitempty"`
}
type ELKLabel struct {
@ -47,7 +47,7 @@ type ELKLabel struct {
Y float64 `json:"y"`
Width float64 `json:"width"`
Height float64 `json:"height"`
LayoutOptions *ELKLayoutOptions `json:"layoutOptions,omitempty"`
LayoutOptions *elkOpts `json:"layoutOptions,omitempty"`
}
type ELKPoint struct {
@ -72,19 +72,11 @@ type ELKEdge struct {
type ELKGraph struct {
ID string `json:"id"`
LayoutOptions *ELKLayoutOptions `json:"layoutOptions"`
LayoutOptions *elkOpts `json:"layoutOptions"`
Children []*ELKNode `json:"children,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 {
Algorithm string `json:"elk.algorithm,omitempty"`
NodeSpacing int `json:"spacing.nodeNodeBetweenLayers,omitempty"`
@ -93,7 +85,15 @@ type ConfigurableOpts struct {
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"`
HierarchyHandling string `json:"elk.hierarchyHandling,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{
ID: "root",
LayoutOptions: &ELKLayoutOptions{
LayoutOptions: &elkOpts{
HierarchyHandling: "INCLUDE_CHILDREN",
ConsiderModelOrder: "NODES_AND_EDGES",
ConfigurableOpts: *opts,
@ -171,7 +171,7 @@ func Layout(ctx context.Context, g *d2graph.Graph, opts *ConfigurableOpts) (err
}
if len(obj.ChildrenArray) > 0 {
n.LayoutOptions = &ELKLayoutOptions{
n.LayoutOptions = &elkOpts{
ForceNodeModelOrder: true,
ConfigurableOpts: ConfigurableOpts{
Padding: opts.Padding,
@ -206,7 +206,7 @@ func Layout(ctx context.Context, g *d2graph.Graph, opts *ConfigurableOpts) (err
Text: edge.Attributes.Label.Value,
Width: float64(edge.LabelDimensions.Width),
Height: float64(edge.LabelDimensions.Height),
LayoutOptions: &ELKLayoutOptions{
LayoutOptions: &elkOpts{
InlineEdgeLabels: true,
},
})