diagrams-graphviz : edge labels position

1,100 views
Skip to first unread message

Jean Anthelme

unread,
Nov 10, 2015, 7:46:35 AM11/10/15
to diagrams-discuss
I am trying to add nicely-positioned edge labels to the graph produced in the 2nd example here.

For this, I tried to change the 2nd argument of the original drawGraph function (\_ _ _ _ _ p -> stroke p) into :

 (\v1 p1 v2 p2 e p -> (text ("Edge " ++ show v1 ++ " " ++ show v2) # fontSize 5 # moveTo (centroid [p1, p2])) <> (arrowBetween' (opts p) p1 p2)) (*) but using the middle point (centroid) between two vertices does not produce a visually pleasant result.

What would be the right way to position the edge labels?

Thanks in advance.

(*) where opts p = with & gaps .~ 16 & arrowShaft .~ (unLoc . head $ pathTrails p)







Brent Yorgey

unread,
Nov 11, 2015, 11:27:26 PM11/11/15
to Jean Anthelme, diagrams-discuss
Hi Jean,

Developing some general tools for doing this sort of labelling is high on our list, but so far no one has tackled it.  In the meantime there are various levels of doing it manually.  Note your example is especially tricky because you are using primitive text objects, for which we cannot know the size.   So for now let's assume that all the edge labels have about the same size.

First, we can do a little better by putting the labels near the actual midpoint of the edges instead of at the centroid of their endpoints (for curved edges the centroid can be quite far from the edge).  To do this, simply replace "centroid [p1,p2]" by something like "head (pathTrails p) `atParam` 0.5".

Next, perhaps we don't want the labels right on top of the edges, but want to move them off to the side a bit.  The idea would be to find the normal vector at the edge midpoint, and move the label some distance along the normal.  Replace "centroid [p1,p2]" with "labelPt p" where

labelPt p = ltr `atParam` 0.5 .+^ 20 *^ n
  where
    ltr = head (pathTrails p)
    n = ltr `normalAtParam` 0.5

This is a bit better, but it does not take into account labels overlapping with other edges, etc, nor does it take into account that if the labels are not square, we may want to move them a different distance depending on the angle of the normal.  And perhaps you want to sometimes negate the normal depending on the concavity of the edge, etc. etc. --- as you can see there are an infinite number of variations of varying difficulty.  But in any case I hope this is enough to get you started!

-Brent

--
You received this message because you are subscribed to the Google Groups "diagrams-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to diagrams-discu...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jean Anthelme

unread,
Nov 14, 2015, 12:57:38 AM11/14/15
to diagrams-discuss, jant...@gmail.com
Thanks Brent, that definitely got me started. My graph is simple so I did not need a solution that also works for tricky edge cases.
Reply all
Reply to author
Forward
0 new messages