graphology-dot
v0.0.1
Published
DOT parser & writer for graphology.
Maintainers
Readme
Graphology DOT Utilities
Graphviz DOT parser & writer for graphology.
For more information about the Graphviz DOT file format, see the DOT Language documentation.
Installation
npm install graphology-dotUsage
Parser
The parser takes a graphology constructor and a DOT string, returning a graphology graph instance.
var Graph = require('graphology');
// Node
var dot = require('graphology-dot');
// Browser
var dot = require('graphology-dot/browser');
// Reading a string
var graph = dot.parse(Graph, string);
// Passing options
var graph = dot.parse(Graph, string, {addMissingNodes: true});Arguments
- constructor GraphClass: graphology constructor to use.
- source string: DOT string to parse.
- options ?object: parsing options:
- addMissingNodes ?boolean [
false]: whether to add missing nodes referenced in edge statements. In DOT, nodes are commonly defined implicitly through edges, so missing nodes are always added automatically regardless of this option.
- addMissingNodes ?boolean [
Supported DOT Features
graph/digraph/strict graph/strict digraphdeclarations- Node statements with attributes:
nodeID [key=value, ...] - Edge statements:
n1 -- n2(undirected) /n1 -> n2(directed) - Edge chains:
a -> b -> c -> d - Attribute statements for defaults:
graph [...],node [...],edge [...] - Subgraphs (named and anonymous):
subgraph S { ... }/{ ... } - Comments:
// line,/* block */,# line - Quoted node IDs and attribute values:
"node name" [label="A Label"] - Numeric node IDs:
1,42 - Port/compass syntax:
node:port,node:port:compass - HTML-like labels:
<b>bold</b>
Attribute Type Inference
Attribute values are automatically cast to the appropriate JavaScript type:
true/false→boolean42,3.14,-1→number"hello"→stringbareword→string- Bare key without value (e.g.
[active]) →true
Writer
The writer takes a graphology instance and outputs a DOT string.
// Node
var dot = require('graphology-dot');
// Browser
var dot = require('graphology-dot/browser');
// Writing the graph
var dotString = dot.write(graph);
// Using custom formatting for nodes & edges
var dotString = dot.write(graph, {
formatNode: function (key, attributes) {
return {
label: attributes.label,
color: attributes.color,
size: attributes.size
};
},
formatEdge: function (key, attributes) {
return {
weight: attributes.weight,
active: attributes.active
};
}
});Arguments
- graph Graph: graphology instance to write.
- options ?object: Options:
- formatNode ?function: function receiving
(key, attributes)and returning the node's data to write. Defaults to writing all attributes. - formatEdge ?function: function receiving
(key, attributes)and returning the edge's data to write. Defaults to writing all attributes.
- formatNode ?function: function receiving
Writer Behavior
- Undirected/mixed graphs use the
graphkeyword; directed graphs usedigraph. - The graph's
idattribute becomes the graph name:graph G { ... }. - Other graph-level attributes are written as
graph [key=value, ...]. - Attribute values are formatted according to their JavaScript type:
- Booleans →
true/false - Numbers → unquoted literal (
42,3.14) - Simple string identifiers → unquoted (
red) - Strings with special characters → quoted (
"hello world")
- Booleans →
