From eed6eadd5893d4d9a55149bb4799ffb456c59a3f Mon Sep 17 00:00:00 2001 From: Gavin Nishizawa Date: Tue, 20 Dec 2022 15:31:13 -0800 Subject: [PATCH] have elk place edge labels on the edge --- d2exporter/export.go | 3 -- d2graph/d2graph.go | 1 - d2layouts/d2elklayout/layout.go | 50 ++++++++++++++++----------------- d2target/d2target.go | 8 ++---- 4 files changed, 26 insertions(+), 36 deletions(-) diff --git a/d2exporter/export.go b/d2exporter/export.go index 6724283e8..952a85662 100644 --- a/d2exporter/export.go +++ b/d2exporter/export.go @@ -242,9 +242,6 @@ func toConnection(edge *d2graph.Edge, theme *d2themes.Theme) d2target.Connection if edge.LabelPercentage != nil { connection.LabelPercentage = *edge.LabelPercentage } - if edge.LabelTL != nil { - connection.LabelTL = edge.LabelTL.Copy() - } connection.Route = edge.Route connection.IsCurve = edge.IsCurve diff --git a/d2graph/d2graph.go b/d2graph/d2graph.go index 9ef90ed4c..a6ec263f0 100644 --- a/d2graph/d2graph.go +++ b/d2graph/d2graph.go @@ -664,7 +664,6 @@ type Edge struct { LabelDimensions d2target.TextDimensions `json:"label_dimensions"` LabelPosition *string `json:"labelPosition,omitempty"` LabelPercentage *float64 `json:"labelPercentage,omitempty"` - LabelTL *geo.Point `json:"labelTL,omitempty"` IsCurve bool `json:"isCurve"` Route []*geo.Point `json:"route,omitempty"` diff --git a/d2layouts/d2elklayout/layout.go b/d2layouts/d2elklayout/layout.go index bbdf55878..be40c6bff 100644 --- a/d2layouts/d2elklayout/layout.go +++ b/d2layouts/d2elklayout/layout.go @@ -43,11 +43,12 @@ type ELKNode struct { } type ELKLabel struct { - Text string `json:"text"` - X float64 `json:"x"` - Y float64 `json:"y"` - Width float64 `json:"width"` - Height float64 `json:"height"` + Text string `json:"text"` + X float64 `json:"x"` + Y float64 `json:"y"` + Width float64 `json:"width"` + Height float64 `json:"height"` + LayoutOptions *ELKLayoutOptions `json:"layoutOptions,omitempty"` } type ELKPoint struct { @@ -78,15 +79,14 @@ type ELKGraph struct { } type ELKLayoutOptions struct { - Algorithm string `json:"elk.algorithm,omitempty"` - HierarchyHandling string `json:"elk.hierarchyHandling,omitempty"` - NodeSpacing float64 `json:"spacing.nodeNodeBetweenLayers,omitempty"` - Padding string `json:"elk.padding,omitempty"` - EdgeNodeSpacing float64 `json:"spacing.edgeNodeBetweenLayers,omitempty"` - Direction string `json:"elk.direction"` - SelfLoopSpacing float64 `json:"elk.spacing.nodeSelfLoop"` - EdgeLabelSpacing float64 `json:"spacing.edgeLabel,omitempty"` - LabelSideSelection string `json:"elk.layered.edgeLabels.sideSelection,omitempty"` + Algorithm string `json:"elk.algorithm,omitempty"` + HierarchyHandling string `json:"elk.hierarchyHandling,omitempty"` + NodeSpacing float64 `json:"spacing.nodeNodeBetweenLayers,omitempty"` + Padding string `json:"elk.padding,omitempty"` + EdgeNodeSpacing float64 `json:"spacing.edgeNodeBetweenLayers,omitempty"` + Direction string `json:"elk.direction"` + SelfLoopSpacing float64 `json:"elk.spacing.nodeSelfLoop"` + InlineEdgeLabels bool `json:"elk.edgeLabels.inline,omitempty"` } func Layout(ctx context.Context, g *d2graph.Graph) (err error) { @@ -109,13 +109,11 @@ func Layout(ctx context.Context, g *d2graph.Graph) (err error) { elkGraph := &ELKGraph{ ID: "root", LayoutOptions: &ELKLayoutOptions{ - Algorithm: "layered", - HierarchyHandling: "INCLUDE_CHILDREN", - NodeSpacing: 100.0, - EdgeNodeSpacing: 50.0, - SelfLoopSpacing: 50.0, - EdgeLabelSpacing: 5.0, - LabelSideSelection: "SMART_UP", + Algorithm: "layered", + HierarchyHandling: "INCLUDE_CHILDREN", + NodeSpacing: 100.0, + EdgeNodeSpacing: 50.0, + SelfLoopSpacing: 50.0, }, } switch g.Root.Attributes.Direction.Value { @@ -159,9 +157,7 @@ func Layout(ctx context.Context, g *d2graph.Graph) (err error) { if len(obj.ChildrenArray) > 0 { n.LayoutOptions = &ELKLayoutOptions{ - Padding: "[top=75,left=75,bottom=75,right=75]", - EdgeLabelSpacing: 5.0, - LabelSideSelection: "SMART_UP", + Padding: "[top=75,left=75,bottom=75,right=75]", } } @@ -192,6 +188,9 @@ func Layout(ctx context.Context, g *d2graph.Graph) (err error) { Text: edge.Attributes.Label.Value, Width: float64(edge.LabelDimensions.Width), Height: float64(edge.LabelDimensions.Height), + LayoutOptions: &ELKLayoutOptions{ + InlineEdgeLabels: true, + }, }) } elkGraph.Edges = append(elkGraph.Edges, e) @@ -314,8 +313,7 @@ func Layout(ctx context.Context, g *d2graph.Graph) (err error) { points[endIndex] = shape.TraceToShapeBorder(dstShape, points[endIndex], points[endIndex-1]) if edge.Attributes.Label.Value != "" { - // using the edge label position from elk layout - edge.LabelTL = geo.NewPoint(e.Labels[0].X+parentX, e.Labels[0].Y+parentY) + edge.LabelPosition = go2.Pointer(string(label.InsideMiddleCenter)) } edge.Route = points diff --git a/d2target/d2target.go b/d2target/d2target.go index 053a11687..ae098fd28 100644 --- a/d2target/d2target.go +++ b/d2target/d2target.go @@ -210,9 +210,8 @@ type Connection struct { Fill string `json:"fill,omitempty"` Text - LabelPosition string `json:"labelPosition"` - LabelPercentage float64 `json:"labelPercentage"` - LabelTL *geo.Point `json:"labelTL,omitempty"` + LabelPosition string `json:"labelPosition"` + LabelPercentage float64 `json:"labelPercentage"` Route []*geo.Point `json:"route"` IsCurve bool `json:"isCurve,omitempty"` @@ -240,9 +239,6 @@ func BaseConnection() *Connection { } func (c *Connection) GetLabelTopLeft() *geo.Point { - if c.LabelTL != nil { - return c.LabelTL - } return label.Position(c.LabelPosition).GetPointOnRoute( c.Route, float64(c.StrokeWidth),