How to use GraphViz and Racket
3 answers
Here's what I would do:
- generate dot file (Graphviz format) from graph
- use
system
to run grapviz on file creating png file - displays png in rocket frame
For the actual code see https://github.com/wangkuiyi/graphviz-server
Note that the Stephen Chang graph library supports the creation of dot files: http://pkg-build.racket-lang.org/doc/graph/index.html#%28part._.Graphviz%29
Update:
To make a graphical editor, you can save the plot data to a file and then let Graphviz output the location information in dot format: http://www.graphviz.org/doc/info/output.html#d:xdot1.4 Do the analysis output file, and then redraw the graphics on the screen.
+2
source to share
Here's an example. You must have the dot program installed.
#lang racket
(require graph)
(require racket/gui/base)
(define dot-command "/usr/local/bin/dot -Tpng ~a >~a")
;; Given a graph, use dot to create a png and return the path to the png
(define (make-graphviz-png g)
(let ([dot-string (graphviz g #:colors (coloring/brelaz g))]
[dot-file (make-temporary-file "example~a.dot")]
[png-file (make-temporary-file "example~a.png")])
(display-to-file dot-string dot-file #:exists 'replace)
(system (format dot-command dot-file png-file))
png-file))
;; The graph
(define test-graph (unweighted-graph/directed '((hello world))))
;; Generate the png from the graph
(define bitmap (read-bitmap (make-graphviz-png test-graph)))
;; Display in a frame -- see https://stackoverflow.com/questions/5355251/bitmap-in-dr-racket
(define f (new frame% [label "Your Graph"]))
(new message% [parent f] [label bitmap])
(send f show #t)
0
source to share