gonum: gonum.org/v1/gonum/graph/encoding/dot Index | Examples | Files

package dot

import "gonum.org/v1/gonum/graph/encoding/dot"

Package dot implements GraphViz DOT marshaling and unmarshaling of graphs.

See the GraphViz DOT Guide and the DOT grammar for more information on using specific aspects of the DOT language:

DOT Guide: https://www.graphviz.org/pdf/dotguide.pdf

DOT grammar: http://www.graphviz.org/doc/info/lang.html

Attribute quoting

Attributes and IDs are quoted if needed during marshalling, to conform with valid DOT syntax. Quoted IDs and attributes are unquoted during unmarshaling, so the data is kept in raw form. As an exception, quoted text with a leading `"<` and a trailing `>"` is not unquoted to ensure preservation of the string during a round-trip.

Index

Examples

Package Files

decode.go doc.go encode.go

func Marshal Uses

func Marshal(g graph.Graph, name, prefix, indent string) ([]byte, error)

Marshal returns the DOT encoding for the graph g, applying the prefix and indent to the encoding. Name is used to specify the graph name. If name is empty and g implements Graph, the returned string from DOTID will be used.

Graph serialization will work for a graph.Graph without modification, however, advanced GraphViz DOT features provided by Marshal depend on implementation of the Node, Attributer, Porter, Attributers, Structurer, Subgrapher and Graph interfaces.

Attributes and IDs are quoted if needed during marshalling.

func MarshalMulti Uses

func MarshalMulti(g graph.Multigraph, name, prefix, indent string) ([]byte, error)

MarshalMulti returns the DOT encoding for the multigraph g, applying the prefix and indent to the encoding. Name is used to specify the graph name. If name is empty and g implements Graph, the returned string from DOTID will be used.

Graph serialization will work for a graph.Multigraph without modification, however, advanced GraphViz DOT features provided by Marshal depend on implementation of the Node, Attributer, Porter, Attributers, Structurer, MultiSubgrapher and Multigraph interfaces.

Attributes and IDs are quoted if needed during marshalling.

func Unmarshal Uses

func Unmarshal(data []byte, dst encoding.Builder) error

Unmarshal parses the Graphviz DOT-encoded data and stores the result in dst. If the number of graphs encoded in data is not one, an error is returned and dst will hold the first graph in data.

Attributes and IDs are unquoted during unmarshalling if appropriate.

func UnmarshalMulti Uses

func UnmarshalMulti(data []byte, dst encoding.MultiBuilder) error

UnmarshalMulti parses the Graphviz DOT-encoded data as a multigraph and stores the result in dst. If the number of graphs encoded in data is not one, an error is returned and dst will hold the first graph in data.

Attributes and IDs are unquoted during unmarshalling if appropriate.

type AttributeSetters Uses

type AttributeSetters interface {
    // DOTAttributeSetters returns the global attribute setters.
    DOTAttributeSetters() (graph, node, edge encoding.AttributeSetter)
}

AttributeSetters is implemented by graph values that can set global DOT attributes.

type Attributers Uses

type Attributers interface {
    DOTAttributers() (graph, node, edge encoding.Attributer)
}

Attributers are graph.Graph values that specify top-level DOT attributes.

type DOTIDSetter Uses

type DOTIDSetter interface {
    SetDOTID(id string)
}

DOTIDSetter is implemented by types that can set a DOT ID.

type Graph Uses

type Graph interface {
    graph.Graph
    DOTID() string
}

Graph wraps named graph.Graph values.

type MultiStructurer Uses

type MultiStructurer interface {
    Structure() []Multigraph
}

MultiStructurer represents a graph.Multigraph that can define subgraphs.

type MultiSubgrapher Uses

type MultiSubgrapher interface {
    Subgraph() graph.Multigraph
}

MultiSubgrapher wraps graph.Node values that represent subgraphs.

type Multigraph Uses

type Multigraph interface {
    graph.Multigraph
    DOTID() string
}

Multigraph wraps named graph.Multigraph values.

type Node Uses

type Node interface {
    // DOTID returns a DOT node ID.
    //
    // An ID is one of the following:
    //
    //  - a string of alphabetic ([a-zA-Z\x80-\xff]) characters, underscores ('_').
    //    digits ([0-9]), not beginning with a digit.
    //  - a numeral [-]?(.[0-9]+ | [0-9]+(.[0-9]*)?).
    //  - a double-quoted string ("...") possibly containing escaped quotes (\").
    //  - an HTML string (<...>).
    DOTID() string
}

Node is a DOT graph node.

type PortSetter Uses

type PortSetter interface {
    // SetFromPort sets the From port and
    // compass direction of the receiver.
    SetFromPort(port, compass string) error

    // SetToPort sets the To port and compass
    // direction of the receiver.
    SetToPort(port, compass string) error
}

PortSetter is implemented by graph.Edge and graph.Line that can set the DOT port and compass directions of an edge.

type Porter Uses

type Porter interface {
    // FromPort returns the port and compass for
    // the From node of a graph.Edge.
    FromPort() (port, compass string)

    // ToPort returns the port and compass for
    // the To node of a graph.Edge.
    ToPort() (port, compass string)
}

Porter defines the behavior of graph.Edge values that can specify connection ports for their end points. The returned port corresponds to the DOT node port to be used by the edge, compass corresponds to DOT compass point to which the edge will be aimed.

Code:

package main

import (
    "fmt"

    "gonum.org/v1/gonum/graph"
    "gonum.org/v1/gonum/graph/encoding/dot"
    "gonum.org/v1/gonum/graph/simple"
)

type edgeWithPorts struct {
    simple.Edge
    fromPort, toPort string
}

func (e edgeWithPorts) ReversedEdge() graph.Edge {
    e.F, e.T = e.T, e.F
    e.fromPort, e.toPort = e.toPort, e.fromPort
    return e
}

func (e edgeWithPorts) FromPort() (string, string) {
    return e.fromPort, ""
}

func (e edgeWithPorts) ToPort() (string, string) {
    return e.toPort, ""
}

func main() {
    g := simple.NewUndirectedGraph()
    g.SetEdge(edgeWithPorts{
        Edge:     simple.Edge{F: simple.Node(1), T: simple.Node(0)},
        fromPort: "p1",
        toPort:   "p2",
    })

    result, _ := dot.Marshal(g, "", "", "  ")
    fmt.Print(string(result))

}

type Structurer Uses

type Structurer interface {
    Structure() []Graph
}

Structurer represents a graph.Graph that can define subgraphs.

type Subgrapher Uses

type Subgrapher interface {
    Subgraph() graph.Graph
}

Subgrapher wraps graph.Node values that represent subgraphs.

Package dot imports 13 packages (graph) and is imported by 43 packages. Updated 2019-11-17. Refresh now. Tools for package owners.