Search code examples
graphlispracketgraphviz

How to set vertex attributes for GraphViz in Racket


I'm trying to figure out how to use vertex-attributes with racket graphviz (graph library). For example adding some style attributes. Here's what I have so far.

#lang racket
(require graph)

(define mygraph (directed-graph '(
                                  (
                                   (a b)
                                   (c d)
                                  )
                                 )))

(define-vertex-property mygraph style #:init "")
(style-set! 'b "filled, bold")

(define graphtext (graphviz mygraph
                            #:vertex-attributes [style 'mygraph]))

In the function definition for graphviz It says that #:vertex-attributes must be a list of lists and contain a symbol or procedure. But it also implies that you have to use #:vertex-attributes in conjunction with define-vertex-property but I'm not exactly sure how I connect the two.

A couple variations I've tried with no success...

(define graphtext (graphviz mygraph
  #:vertex-attributes [style 'b]))

(define graphtext (graphviz mygraph
  #:vertex-attributes [style 'b]))

(define graphtext (graphviz mygraph
  #:vertex-attributes [style (lambda () 'b)]))                          


Solution

  • Try this:

    (define graphtext
      (graphviz mygraph
                #:vertex-attributes `([style ,style])))
    

    The graph library tests were helpful in figuring it out.


    From the documentation you linked, #:vertex-attributes associates an attribute name (corresponding to symbol? in the contract) with a vertex property. The form

    (define-vertex-property graph prop-name maybe-init maybe-vs)

    defines a vertex property prop-name, which is a procedure (corresponding to procedure? in the contract). `([style ,style]) associates the attribute name style with the vertex property style.