Format Conversion
The@statelyai/graph library provides comprehensive support for converting graphs to and from popular graph formats. All converters follow a consistent bidirectional pattern with to* and from* functions.
Supported Formats
Cytoscape.js
JSON format for Cytoscape.js visualization
D3.js
Force-directed graph format for D3
DOT (Graphviz)
Text format for Graphviz rendering
GraphML
XML-based graph markup language
GEXF
Graph Exchange XML Format
Mermaid
Diagram-as-code with 7 diagram types
Other Formats
JGF, GML, TGF, edge lists, and more
Format Comparison
| Format | Type | Compound Graphs | Visual Data | Peer Dependency |
|---|---|---|---|---|
| Cytoscape.js | JSON | ✅ Yes | ✅ Position, color, shape | None |
| D3.js | JSON | ❌ No | ✅ Position, color | None |
| DOT | Text | ✅ Yes (subgraphs) | ✅ Color, shape, layout | dotparser |
| GraphML | XML | ✅ Yes | ✅ Full visual support | fast-xml-parser |
| GEXF | XML | ✅ Yes | ✅ Full visual + viz module | fast-xml-parser |
| JGF | JSON | ✅ Yes | ✅ Full visual support | None |
| GML | Text | ✅ Yes (nested) | ✅ Graphics block | None |
| TGF | Text | ❌ No | ❌ Labels only | None |
| Edge List | Array | ❌ No | ❌ No metadata | None |
| Adjacency List | Object | ❌ No | ❌ No metadata | None |
| XYFlow | JSON | ✅ Yes | ✅ Position, size | @xyflow/system |
| Mermaid | Text | Varies by type | ✅ Limited | None |
Creating Custom Converters
createFormatConverter()
Create a bidirectional converter from a pair of to/from functions.
Type Signature
Parameters
Function that converts a
Graph to the serialized formatFunction that parses the serialized format back into a
GraphReturns
AGraphFormatConverter<TSerial> object with to and from methods.
Converter Pattern
All format converters follow this pattern:Peer Dependencies
Some format converters require peer dependencies:Installation
Format converters are included in the main package:Error Handling
Allfrom* parsers validate input and throw descriptive errors:
Next Steps
Cytoscape.js Format
Learn about Cytoscape.js conversion
Mermaid Diagrams
Convert to/from Mermaid diagrams