Skip to content
graph_visualizer โ€บ Classes โ€บ GraphVisualizer

GraphVisualizer โ€‹

class GraphVisualizer

Generate and display module dependency graphs in a browser.

Supports both static Graphviz DOT diagrams and interactive AntV G6 visualizations through the GraphRenderer selection.

Usage โ€‹

dart
// Interactive G6 visualization
await GraphVisualizer.visualize(AppModule(), renderer: GraphRenderer.g6);

// Static Graphviz DOT diagram
await GraphVisualizer.visualize(AppModule());

See also:

Constructors โ€‹

GraphVisualizer() โ€‹

GraphVisualizer()

Properties โ€‹

hashCode no setter inherited โ€‹

int get hashCode

The hash code for this object.

A hash code is a single integer which represents the state of the object that affects operator == comparisons.

All objects have hash codes. The default hash code implemented by Object represents only the identity of the object, the same way as the default operator == implementation only considers objects equal if they are identical (see identityHashCode).

If operator == is overridden to use the object state instead, the hash code must also be changed to represent that state, otherwise the object cannot be used in hash based data structures like the default Set and Map implementations.

Hash codes must be the same for objects that are equal to each other according to operator ==. The hash code of an object should only change if the object changes in a way that affects equality. There are no further requirements for the hash codes. They need not be consistent between executions of the same program and there are no distribution guarantees.

Objects that are not equal are allowed to have the same hash code. It is even technically allowed that all instances have the same hash code, but if clashes happen too often, it may reduce the efficiency of hash-based data structures like HashSet or HashMap.

If a subclass overrides hashCode, it should override the operator == operator as well to maintain consistency.

Inherited from Object.

Implementation
dart
external int get hashCode;

runtimeType no setter inherited โ€‹

Type get runtimeType

A representation of the runtime type of the object.

Inherited from Object.

Implementation
dart
external Type get runtimeType;

Methods โ€‹

noSuchMethod() inherited โ€‹

dynamic noSuchMethod(Invocation invocation)

Invoked when a nonexistent method or property is accessed.

A dynamic member invocation can attempt to call a member which doesn't exist on the receiving object. Example:

dart
dynamic object = 1;
object.add(42); // Statically allowed, run-time error

This invalid code will invoke the noSuchMethod method of the integer 1 with an Invocation representing the .add(42) call and arguments (which then throws).

Classes can override noSuchMethod to provide custom behavior for such invalid dynamic invocations.

A class with a non-default noSuchMethod invocation can also omit implementations for members of its interface. Example:

dart
class MockList<T> implements List<T> {
  noSuchMethod(Invocation invocation) {
    log(invocation);
    super.noSuchMethod(invocation); // Will throw.
  }
}
void main() {
  MockList().add(42);
}

This code has no compile-time warnings or errors even though the MockList class has no concrete implementation of any of the List interface methods. Calls to List methods are forwarded to noSuchMethod, so this code will log an invocation similar to Invocation.method(#add, [42]) and then throw.

If a value is returned from noSuchMethod, it becomes the result of the original invocation. If the value is not of a type that can be returned by the original invocation, a type error occurs at the invocation.

The default behavior is to throw a NoSuchMethodError.

Inherited from Object.

Implementation
dart
@pragma("vm:entry-point")
@pragma("wasm:entry-point")
external dynamic noSuchMethod(Invocation invocation);

toString() inherited โ€‹

String toString()

A string representation of this object.

Some classes have a default textual representation, often paired with a static parse function (like int.parse). These classes will provide the textual representation as their string representation.

Other classes have no meaningful textual representation that a program will care about. Such classes will typically override toString to provide useful information when inspecting the object, mainly for debugging or logging.

Inherited from Object.

Implementation
dart
external String toString();

Operators โ€‹

operator ==() inherited โ€‹

bool operator ==(Object other)

The equality operator.

The default behavior for all Objects is to return true if and only if this object and other are the same object.

Override this method to specify a different equality relation on a class. The overriding method must still be an equivalence relation. That is, it must be:

  • Total: It must return a boolean for all arguments. It should never throw.

  • Reflexive: For all objects o, o == o must be true.

  • Symmetric: For all objects o1 and o2, o1 == o2 and o2 == o1 must either both be true, or both be false.

  • Transitive: For all objects o1, o2, and o3, if o1 == o2 and o2 == o3 are true, then o1 == o3 must be true.

The method should also be consistent over time, so whether two objects are equal should only change if at least one of the objects was modified.

If a subclass overrides the equality operator, it should override the hashCode method as well to maintain consistency.

Inherited from Object.

Implementation
dart
external bool operator ==(Object other);

Static Methods โ€‹

buildGraphData() โ€‹

ModuleGraphData buildGraphData(dynamic rootModule)

Builds structured graph data from module tree.

Useful for CI pipelines and programmatic access to the dependency graph without rendering it in a browser.

Implementation
dart
static ModuleGraphData buildGraphData(Module rootModule) {
  final nodes = <ModuleNode>[];
  final edges = <ModuleEdge>[];
  final visited = <Type>{};
  final queue = [rootModule];
  final analyzer = ModuleBindingsAnalyzer();

  while (queue.isNotEmpty) {
    final current = queue.removeAt(0);
    final currentType = current.runtimeType;

    if (visited.contains(currentType)) continue;
    visited.add(currentType);

    final snapshot = analyzer.analyze(current);
    final nodeId = currentType.toString();

    nodes.add(
      ModuleNode(
        id: nodeId,
        name: currentType.toString(),
        isRoot: current == rootModule,
        publicDependencies: snapshot.publicDependencies,
        privateDependencies: snapshot.privateDependencies,
        expects: snapshot.expects,
        warnings: snapshot.warnings,
      ),
    );

    for (final imported in current.imports) {
      final importedType = imported.runtimeType;
      edges.add(
        ModuleEdge(
          source: nodeId,
          target: importedType.toString(),
          type: ModuleEdgeType.imports,
        ),
      );
      queue.add(imported);
    }

    try {
      for (final submodule in current.submodules) {
        final submoduleType = submodule.runtimeType;
        edges.add(
          ModuleEdge(
            source: nodeId,
            target: submoduleType.toString(),
            type: ModuleEdgeType.owns,
          ),
        );
        queue.add(submodule);
      }
    } catch (e) {
      print('Warning: Failed to read submodules of $currentType: $e');
    }
  }

  return ModuleGraphData(nodes: nodes, edges: edges);
}

generateDot() โ€‹

String generateDot(dynamic rootModule)

Generate a Graphviz DOT string representing the module tree of rootModule.

Returns a DOT-format string suitable for rendering with Graphviz or embedding in CI reports.

Implementation
dart
static String generateDot(Module rootModule) {
  final buffer = StringBuffer()
    ..writeln('digraph Modules {')
    ..writeln(
      '  node [shape=box, style="filled,rounded", fillcolor="#e3f2fd", fontname="Arial", penwidth=1.5, color="#90caf9"];',
    )
    ..writeln('  edge [fontname="Arial", fontsize=10];')
    ..writeln('  rankdir=TB;');

  final visited = <Type>{};
  final queue = [rootModule];
  final analyzer = ModuleBindingsAnalyzer();

  while (queue.isNotEmpty) {
    final current = queue.removeAt(0);
    final currentType = current.runtimeType;

    if (visited.contains(currentType)) continue;
    visited.add(currentType);

    final snapshot = analyzer.analyze(current);
    final attributes = <String>['label=${_buildNodeLabel(snapshot)}'];

    if (current == rootModule) {
      attributes
        ..add('fillcolor="#bbdefb"')
        ..add('color="#1565c0"')
        ..add('penwidth=2.5');
    }

    buffer.writeln('  "$currentType" [${attributes.join(', ')}];');

    for (final imported in current.imports) {
      final importedType = imported.runtimeType;
      buffer.writeln(
        '  "$currentType" -> "$importedType" [style=dashed, color="#616161", label="imports"];',
      );
      queue.add(imported);
    }

    try {
      for (final submodule in current.submodules) {
        final submoduleType = submodule.runtimeType;
        buffer.writeln(
          '  "$currentType" -> "$submoduleType" [dir=back, arrowtail=diamond, color="#1565c0", penwidth=1.5, label="owns"];',
        );
        queue.add(submodule);
      }
    } catch (e) {
      print('Warning: Failed to read submodules of $currentType: $e');
    }
  }

  buffer.writeln('}');
  return buffer.toString();
}

visualize() โ€‹

Future<void> visualize(dynamic rootModule, {GraphRenderer renderer = GraphRenderer.graphviz})

Generates a dependency graph for the given rootModule and opens it in the browser.

renderer controls which visualization library to use:

Implementation
dart
static Future<void> visualize(
  Module rootModule, {
  GraphRenderer renderer = GraphRenderer.graphviz,
}) async {
  final String htmlContent;

  switch (renderer) {
    case GraphRenderer.graphviz:
      final dotContent = generateDot(rootModule);
      htmlContent = HtmlGenerator.generate(dotContent);
      break;
    case GraphRenderer.g6:
      final graphData = buildGraphData(rootModule);
      htmlContent = G6HtmlGenerator.generate(graphData);
      break;
  }

  await BrowserOpener.openHtml(htmlContent);
}