graphvizdotneato

How to place text next to a node?


I am rendering a large-ish graph using Graphviz's neato. All of the following takes place in this environment:

digraph G {
  graph [scale="1,0.7"]

Somewhere in there, I want to display a node that is just a colored circle, with a text next to it.

I am ok with placing two nodes (one for the circle, one with the label) side by side, but I somehow fail to left-align the text:

n1 [pos="1,-8!" style=filled shape=circle width=0.25 color=green1]
n2 [pos="1,-8!" label="the node label"]

Obviously, the label (n2) is centered on the node (n1) like this. Even though I intend to move the label slightly to the right in the final graph, let's keep it like this for demonstration purposes:

centered node label

Ultimately, this is what I'm trying to achive:

properly aligned node label

But for this output, I have to adjust the position of the label manually, in such a way that the center point of the label node is just at the right spot for the left edge of the text to be next to the circle - i.e. dependent on the label's length and thus rather unwiedly.


I have tried using the "left-align line break":

n1 [pos="1,-8!" style=filled shape=circle width=0.25 color=green1]
n2 [pos="1,-8!" label="the node label\l"]

The label is still centered.


I have also tried using the labeljust attribute:

n1 [pos="1,-8!" style=filled shape=circle width=0.25 color=green1]
n2 [pos="1,-8!" label="the node label" labeljust=l]

The label is still centered.


Then, I have tried to align the text using a HTML-like label and the align attribute, as mentioned e.g. here. As Creole's <plain> tag is not recognized by Graphviz, I have used <font> as the seemingly next best alternative of having just a wrapper tag on which to place the align attribute, without any further formatting:

n1 [pos="1,-8!" style=filled shape=circle width=0.25 color=green1]
n2 [pos="1,-8!" label=<<font align="left">the node label</font>>]

For this, Graphviz outputs a warning about an illegal attribute on the <font> tag and the text is still centered.


How can I left-align my text, so it becomes easier to place the text node? Or is there another way altogether to put a text on the right-hand side of my node?


Solution

  • There is no easy way to do what you ask. xlabel should do what you ask, but you can't specify the label location (it seems to default to "near the northwest corner").
    The alignment & justification efforts do not work, because they apply inside the node boundary, not outside

    Here are three possible solutions:

    digraph L{
    
    n1 [pos="1,5!"    style=filled label="" shape=circle width=0.25 color=green1]
    n2 [pos="2.0,5!" shape=plaintext width="1.2" label="the node label1" labeljust=l]
    
    b [pos="1,1!" style=filled shape=circle fixedsize=true  height=.25 width=0.25 color=pink1
      label="                                   the node label4"]
    
    d [pos="1,3!" style=filled shape=circle fixedsize=true  height=".25" width=".25" color=lightblue
      label=<
      <table border="0" cellspacing="0" cellborder="0">
      <TR><TD width="138"></TD><TD>the node label6</TD></TR></table>> ]
    }
    

    Giving:
    enter image description here