user254694
user254694

Reputation: 1612

Graphviz drawing - make a box that starts at one node and goes to the end of another one

I have a timeline and would like a box to be drawn next to a particular number of years

digraph timeline {

    node [fontsize=24, shape = plaintext];

    1940 -> 1950;
    1950 -> 1955;
    1955 -> 1960;
 
 
    node [fontsize=20, shape = box];
    { rank=same;  1940 test; }

}

This places a timeline on the left hand side going from 1940 to 1950 and so forth. I would like draw a box next to the numbers that starts at 1940 - which is what I do now with { rank=same; 1940 test; } and that ends with 1955.

Here is an example of the box drawn at 1940

enter image description here

Here is an example of the box drawn at 1955 with code { rank=same; 1955 test; }

enter image description here

I would like to have the box drawn from the start of 1940 position to the end of 1955 position, so encompassing these two boxes right now.

Upvotes: 1

Views: 184

Answers (2)

kirogasa
kirogasa

Reputation: 949

Tricky solution is to draw a cluster and place invisible nodes inside it using style=invis. Then align the cluster with the timeline using newrank.
Script:

digraph timeline {
    newrank=true;

    node [fontsize=24, shape = plaintext];

    1940 -> 1950 -> 1955 -> 1960;
    
    subgraph cluster_1 {
        test [fontsize=20]
        cl_start [shape=none style=invis]
        cl_end [shape=none style=invis]
        cl_start -> test -> cl_end [style=invis]
    }
    
    {rank=same;1940;cl_start}
    {rank=same;1955;cl_end}
}

Result:
timeline made with GraphViz

Upvotes: 2

sroush
sroush

Reputation: 6773

No easy way to do this with Graphviz.

  • dot does not allow nodes to span ranks. It centers all nodes in the same rank on the same line (horizontal, with rankdir=TB).
  • if you set rankdir=LR, you can get nodes to span years, but positioning & node sizing become difficult
  • you could put a node on every year you want the final node to span and the post-process the result to overwrite the several nodes with a single large node.
  • you can us the neato -n (https://graphviz.org/faq/#FaqDotWithNodeCoords) capability and position & size all the nodes yourself
  • or you could try another language entirely (maybe pikchr https://pikchr.org/home/doc/trunk/homepage.md)

Upvotes: 1

Related Questions