Graphviz: change font for the whole graph? - graphviz

I am wondering if I can define an alternative font for the whole graph.
...
digraph script_concept {
graph [layout="dot",fontname="helvetica"];
...
According to this 1 older post the fontname atribute can be defined only separately:
Nodes and edges don't inherit the font of the graph, you need to specify
them separately
Is there any other way, how to define the font globally?

No, there is no other way.
As in the forum post you linked, you have to define the default values separately (like the other attributes) at the beginning of your graphviz file:
digraph g {
graph [fontname = "helvetica"];
node [fontname = "helvetica"];
edge [fontname = "helvetica"];
...
}

Not sure if this is a recent update, but you can change these at the command-line level using the -G, -E and -N attribute flags. That is, the following works for me:
$ dot -Tpng -Nfontname=Roboto -Nfontsize=10 \
-Efontname=Roboto -Efontsize=10 \
tree.dot > tree.png

However, there's one easy trick, if you are exporting svgs:
sed 's/Times,serif/Helvetica/g' thegraph.svg > thegraph_helvetica.svg
combine this with Make and all the horror will be hidden :)
here's an example Makefile:
all: helvetica
svg:
cat thegraph.dot | dot -Tsvg > thegraph.svg
helvetica: svg
sed 's/Times,serif/Helvetica/g' thegraph.svg > thegraph_helvetica.svg

Related

Visualize boost graph topology after layout algorithm is performed

I am using fruchterman_reingold_force_directed_layout algorithm on my graph
to get a cluster free layout. Below is code for my vertices and edge
using RectTopology = boost::rectangle_topology<>;
using point = RectTopology::point_type;
class MyVertex{
public:
MyVertex(){ myObject = NULL; }
Mybject* myObject;
point position;
std::string name;
};
class MyEdge{
public:
MyEdge(){ myLine = NULL; }
MyLine* myLine;
double weight;
};
//Boost graph defination
using graphT = boost::adjacency_list<boost::listS, boost::vecS, boost::undirectedS, MyVertex, MyEdge>;
using vertexT = boost::graph_traits<graphT>::vertex_descriptor; //Define Vertex
using vertexIt = boost::graph_traits<graphT>::vertex_iterator; //Vertex Iterator
using edgeT = boost::graph_traits<graphT>::edge_descriptor; //Define Edge
using edgeIt = boost::graph_traits<graphT>::edge_iterator; //Edge Iterator
forcedDirLay(){
boost::minstd_rand gen;
RectTopology rect_top(gen, 0, 0, 1, 1);
boost::random_graph_layout(myGraph, boost::get(&SLDVertex::position, myGraph), rect_top);
boost::fruchterman_reingold_force_directed_layout(myGraph, boost::get(&SLDVertex::position, myGraph), rect_top);
}
Imagine now I have a graph and perform my layout algorithm which all works fine
and I have position info for every vertices.
How can I visualize each vertex where they end up after layout algorithm
finished ? Is there a way to get position info to Dot file and I can visualize
Dot file ?
I have a function to convert my graph to dot file but don't know how
to get position info to dot
file. Thanks in advance.
GraphToDotFile(){
std::ofstream dot(".\\graph.dot");
boost::write_graphviz(dot, myGraph,
boost::make_label_writer(boost::get(&MyVertex::name, myGraph)));
dot.close();
}
You can use dynamic properties to add graphviz attributes, see the example:
https://www.boost.org/doc/libs/1_74_0/libs/graph/example/graphviz.cpp
The attributes that specify the position in graphviz are here:
Position of node, or spline control points.
For nodes, the position indicates the center of the node. On output,
the coordinates are in points.
In neato and fdp, pos can be used to set the initial position of a
node. By default, the coordinates are assumed to be in inches.
However, the -s command line flag can be used to specify different
units. As the output coordinates are in points, feeding the output of
a graph laid out by a Graphviz program into neato or fdp will almost
always require the -s flag.
When the -n command line flag is used with neato, it is assumed the
positions have been set by one of the layout programs, and are
therefore in points. Thus, neato -n can accept input correctly without
requiring a -s flag and, in fact, ignores any such flag.
Valid for: Edges, Nodes.
The neato -n trick works like a champ.
Adding a bit to the above,
yourProg produces graph.dot; then
neato -n -Tpng graph.dot >graph.png
You may have to fiddle with yourProg to make sure that graph.dot has all the required attributes, units are correct, etc. You can produce a tiny example by hand, then
dot -Tdot tiny.gv >tiny.dot
and compare the two .dot files

Reproduce a graph with graphviz

I want to reproduce an specific figure I saw on a book using graphviz. This is the graph in the book:
But using the following .dot:
digraph rf{
rankdir=LR;
graph[size="5,5",ratio=fill, overlap=false];
//node[height="2", width="2", fontsize="50"];
//edge[penwidth="4", fontsize="50"]
<a>[color=red];
<b>[color=red];
<s>[color=purple];
<t>[color=purple];
<s>-><a>[label="500/0"]
<s>-><b>[label="500/0"]
<a>-><b>[label="1/0"]
<a>-><t>[label="500/0"]
<b>-><t>[label="500/0"]
}
I get this:
Notice how it's really big and kind of expanded, also the labels sare overlapped with the edges.
I'd like to get a very similar graph to the one in the book.
The command I use to plot this:
circo -Tpng errornet2.dot -oerrornet2.png
Hope I can get some help, thanks!
If you use the dot engine instead of circo and constraint=falseon one edge you are near there
digraph rf{
rankdir=LR;
graph[size="5,5",ratio=fill, overlap=false];
node[height="2", width="2", fontsize="50"];
edge[penwidth="4", fontsize="50"]
<a>[color=red];
<b>[color=red];
<s>[color=purple];
<t>[color=purple];
<s>-><a>[label="500/0"]
<s>-><b>[label="500/0"]
<a>-><b>[label="1/0" constraint=false]
<a>-><t>[label="500/0"]
<b>-><t>[label="500/0"]
}
gives

Get rid of "_anonymous_0" tooltips in SVG with GraphViz

I generate SVGs using Graphviz. When embedded in HTML, the nodes, edges and arrows show an "_anonymous_0" tooltip. Can I get rid of these from within GraphViz?
Found by trial and error :-)
digraph "my title" {
tooltip=" "
node [tooltip=" "]
edge [tooltip=" "]
...
}
disables the automatic generation of tooltips.
an empty string
tooltip=""
does not(!) disable the generation.
If your dot source start something like this:
digraph {
it may help to use the following:
digraph "" {
It looks like the name of the graph (digraph mygraph { ) is transformed to a title element (<title>mygraph</title>). If you omit the name, *anonymous_0* is used instead. But if "" is used, no title element is being created.
There may be a better solution to this though...

Graphviz (DOT) Captions

I need to print a large number of graphs using Graphviz DOT. To distinguish which input each graph corresponds to, I want to also have a caption for each graph. Is there anyway to embed this into the DOT representation of the graphs.
You can use label to add a caption to the graph.
Example:
digraph {
A -> B;
label="Graph";
labelloc=top;
labeljust=left;
}
labelloc and labeljust can be used to determine top/bottom and left/right position of the graph label.
All the details and other attributes that can be used to modify the label (font etc) in the graphviz attribute reference.
Tip: Define the graph label end of your dot file, otherwise subgraphs will inherit those properties.
Graph's can have attributes just like nodes and edges do:
digraph {
graph [label="The Tale of Two Cities", labelloc=t, fontsize=30];
node [color=blue];
rankdir = LR;
London -> Paris;
Paris -> London;
}
That dot file produces this graph.
If you are looking for a way to add a caption to a Graph object of graphviz in python. Then the following code can help:
from graphviz import Graph
dot = Graph()
dot.node('1','1')
dot.node('2','2')
dot.edge('1','2', label="link")
dot.attr(label="My caption")
dot.attr(fontsize='25')
dot.render(view=True)
Output:

record nodes and rankdir in graphviz

When I changed the rankdir of my graph from LR to TD, my record nodes also changed their layout direction so they no longer look like a 'record'. I tried applying a separate rankdir to the nodes, but this had no effect.
How does one keep the record nodes with the correct layout?
digraph sample {
graph [rankdir=TD];
node [shape=record];
A [label="ShouldBeTop | ShouldBeBottom"];
B [label="Top | Bottom"];
A -> B;
}
Taking into account that rankdir effectively replaces the notion of "top" and "bottom" for the given graph, that's not surprising.
I am afraid that there is no easy remedy for this, save hacking the source (and that would not be easy at all). You can surround your labels in "{}" with some kind of mass search-replace solution to get the requested effect:
digraph sample { graph [rankdir=TD]; node [shape=record];
A [label="{ShouldBeTop | ShouldBeBottom}"];
B [label="{Top | Bottom}"]; A -> B;
}
You can use html table like labels instead of records. IIRC the table based labels do not rotate with the rank direction. See http://www.graphviz.org/doc/info/shapes.html#html

Resources