toDot method
Export this graph to DOT Language typically used to describe Graphviz graphs.
Nodes and edges are labelled with their standard print-string, unless a
custom vertexLabel
or edgeLabel
callback is provided.
The display of vertices can be customized by providing vertexAttributes
as described in Node Attributes.
Similarly edges can be customized by providing edgeAttributes
as
described in Edge Attributes.
Implementation
String toDot({
Map<String, String>? graphAttributes,
String Function(V vertex)? vertexLabel,
Map<String, String> Function(V vertex)? vertexAttributes,
String Function(Edge<V, E> edge)? edgeLabel,
Map<String, String> Function(Edge<V, E> edge)? edgeAttributes,
}) {
final buffer = StringBuffer();
buffer.writeln('${isDirected ? 'digraph' : 'graph'} {');
for (final attribute in _encodeAttributes(graphAttributes ?? {})) {
buffer.writeln(' $attribute;');
}
final ids = vertexStrategy.createMap<int>();
for (final vertex in vertices) {
buffer.write(' ${ids[vertex] = ids.length}');
buffer.write(
_encodeNodeEdgeAttributes({
'label': vertexLabel?.call(vertex) ?? vertex.toString(),
if (vertexAttributes != null) ...vertexAttributes(vertex),
}),
);
buffer.writeln(';');
}
final arrow = isDirected ? '->' : '--';
for (final edge in edges.unique()) {
buffer.write(' ${ids[edge.source]} $arrow ${ids[edge.target]}');
buffer.write(
_encodeNodeEdgeAttributes({
'label': edgeLabel?.call(edge) ?? edge.value?.toString() ?? '',
if (edgeAttributes != null) ...edgeAttributes(edge),
}),
);
buffer.writeln(';');
}
buffer.write('}');
return buffer.toString();
}