toDot function

String toDot(
  1. ImportGraph graph, {
  2. required Iterable<String> nodes,
  3. Set<String> unreachable = const {},
  4. int featureDepth = 1,
  5. List<List<String>>? cycles,
})

graph in Graphviz DOT, for dot -Tsvg: the same drawing as toMermaid, without a size limit, with features as clusters.

Implementation

String toDot(
  ImportGraph graph, {
  required Iterable<String> nodes,
  Set<String> unreachable = const {},
  int featureDepth = 1,
  List<List<String>>? cycles,
}) {
  final layout = _Layout(graph, nodes, featureDepth, cycles ?? graph.cycles());
  final out = StringBuffer()
    ..writeln('digraph imports {')
    ..writeln('  rankdir=LR;')
    ..writeln('  node [shape=box, style=rounded, fontname="Helvetica"];');

  String node(String file, String label) {
    final dead = unreachable.contains(file)
        ? ', style="rounded,dashed", fontcolor="#888888"'
        : '';
    return '${_dot(file)} [label=${_dot(label)}$dead];';
  }

  var cluster = 0;
  for (final feature in layout.features.entries) {
    final name = feature.key;
    if (name == null) {
      for (final file in feature.value) {
        out.writeln('  ${node(file, file)}');
      }
      continue;
    }
    out
      ..writeln('  subgraph "cluster_${cluster++}" {')
      ..writeln('    label=${_dot(name)};');
    for (final file in feature.value) {
      out.writeln('    ${node(file, layout.label(file, name))}');
    }
    out.writeln('  }');
  }

  for (final edge in layout.edges) {
    final style = layout.inCycle(edge) ? ' [color="#dd3333", penwidth=2]' : '';
    out.writeln('  ${_dot(edge.$1)} -> ${_dot(edge.$2)}$style;');
  }
  out.writeln('}');
  return out.toString();
}