d2 icon indicating copy to clipboard operation
d2 copied to clipboard

Proposal: label alignment

Open exapsy opened this issue 3 years ago • 6 comments

Current issues

  1. Semantics break when you have reverse direction. The code below, will output the label on the start (source) of the arrowhead, not the target. This happens because the code logic always sees the source as the left variable not the right.
bob <- alice: {
  target-arrowhead: Lorem
}
  1. Semantics break on <-->. There is no source or target semantically speaking
  2. Semantics break on --. There is no source or target semantically speaking

Proposal

Introduce a label keyword and a align keyword.

  • label: The text that will be placed on the default spot/position unless told otherwise.
  • align: Enum value of left/right/middle. Where left is the left part of the equation (left variable), right is the right variable and when it is middle the text is placed on the middle part of the line.

Example

alice <-> bob: {
  label: Lorem ipsum
  align: right # the label is aligned on bob's side
}

exapsy avatar Dec 28 '22 17:12 exapsy

ty for the clean issue and proposal!

i've added the bug tag for the issue reported where target is wrongly identified.

the label keyword already works. as for align, you'd run into the same problem. If alice and bob are vertically aligned, due to autolayout, what does align: right mean?

we should take target to mean the right-hand-side operand when the connections are bidirectional (-- or <->)

alixander avatar Dec 28 '22 19:12 alixander

the label keyword already works. as for align, you'd run into the same problem. If alice and bob are vertically aligned, due to autolayout, what does align: right mean?

left and right are meant to be a reference to where Alice and Bob are positioned in the equation, not in the visual graphical space.

Example

alice -- bob
# alice is on the left
# bob is on the right

So essentially,

  • align: right here would point to bob
  • align: left would point to alice

exapsy avatar Jan 06 '23 09:01 exapsy

I'm also interested in being able to nudge/control the position of connection labels.

Two points:

  • The title of this issue would be more clear if it mentioned "connection".
  • I think the arrowhead and label considerations are separate issues.

@alixander Are there any mechanisms in d2 for coercing the position of a connection label? Sometimes even TALA does not make optimal label placement along connection lines. @exapsy's idea of specifying that a connection label should be closer to one of the two objects is interesting, although there are many other possible connection label position tweaks. I notice that there is already label.near: someobj when applied to shapes. Maybe that should do something reasonable when applied to connections?

murphyke avatar Aug 18 '23 20:08 murphyke

I'd also love to be able to put labels right at the head or tail of my edges. The left/right syntax is a great idea since it works for both directed and undirected edges. I hope this one is revisited some time soon!

Thanks, Darrick

pangloss avatar Mar 19 '24 03:03 pangloss

Is there a way to put a label on the head and another label on the tail? E.g., "X 1 <--> * Y". EDIT: Found an example: https://d2lang.com/tour/connections/#arrowheads

amb007 avatar Jun 19 '24 08:06 amb007

  • align: right here would point to bob
  • align: left would point to alice

Perhaps you can use keys instead: e.g., align: alice.

amb007 avatar Jun 19 '24 12:06 amb007