Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to automate the edge colors coming out of a node in graphviz

Tags:

graphviz

I have a huge graph with atleast 100 nodes tightly connected in graphviz. I am looking for ways that can automate the edge colors coming out of any given node such that the colors are unique. I have close to 30 edges coming out of each node. Thank you !!

like image 337
Mukesh Avatar asked Jul 15 '13 15:07

Mukesh


3 Answers

I had a similar problem, where I had loads of nodes coming out of 1 node.

I used sfdp (dot couldn't handle the number of nodes) with the -Goverlap=prism option, which spaced things out nicely.

like image 108
Neil Avatar answered Sep 27 '22 00:09

Neil


You have here a list of all colors accepted by GraphViz: http://www.graphviz.org/doc/info/colors.html

If you generate a graph from a program of your own, a simple way to have unique colors is to put all those colors in a list, and to pick one from this list when you have a new edge. Something like this pseudo-code:

List colors = {red, blue, green, ...};
for edge in edges:
    write(edge.source +
          " -> " +
          edge.sink +
          " [color=\"" +
          color.next() +
          "\"];\n"
    );

A cleaner way to do so would be to define a color from its RGB code. You can do this this way:

digraph G
{

    A -> B
    [
        color="#AABBCC"
    ]

}

Now all you have to do is to generate for all your edges a hexadecimal number between #000000 and #FFFFFF. Color formats are presented here in the documentation: http://www.graphviz.org/doc/info/attrs.html#k:color

like image 31
Bastien Pasdeloup Avatar answered Sep 25 '22 00:09

Bastien Pasdeloup


Do you need something that takes a dot file and produces a new colorized dot file? Or do you have some program or script that is generating your dot file? (In what language?)

I am a fan of picking random medium intensity colors for nodes (or clusters), and making the node background a lighter version of that color and making the exiting edges a darker version of that color.

For example, in java I have two utility methods like these.

public static Color hashColor(Object value) {
    if (value == null) {
        return Color.WHITE.darker();
    } else {
        int r = 0xff - (Math.abs(1 + value.hashCode()) % 0xce);
        int g = 0xff - (Math.abs(1 + value.hashCode()) % 0xdd);
        int b = 0xff - (Math.abs(1 + value.hashCode()) % 0xec);
        return new Color(r, g, b);
    }
}        

/**
 * @return a hex Color string in the format #rrggbb. 
 */
public static String encodeColor(Color color) {
    return "#" + String.format("%06x", color.getRGB() & 0xffffff);

}
like image 29
Rick Avatar answered Sep 26 '22 00:09

Rick