Is it possible to use the graphviz module to draw graphs in a racket frame (GUI)? If it's possible, would anyone a tutorial that shows how to use? Thank's
3 Answers
Here is what I would do:
- generate a dot-file (the Graphviz format) from the graph
- use
system
to run grapviz on the file producing a png file - display the png in a racket frame
For actual code see: https://github.com/wangkuiyi/graphviz-server
Note that Stephen Chang's graph library has support for generating dot-files: http://pkg-build.racket-lang.org/doc/graph/index.html#%28part._.Graphviz%29
Update:
In order to make a graph editor you can save the graph data in a file, then let Graphviz output layout information in the dot-format: http://www.graphviz.org/doc/info/output.html#d:xdot1.4 Parse the output file and then redraw the graphs on screen.

- 30,661
- 4
- 57
- 106
-
1Thank you for that purpose. What I want is more along the lines to make a graph editor. A progressive construction – chsms Apr 21 '15 at 14:04
-
Updated the answer. Consider asking your question on the Racket mailing list. Chances are someone has written a graph editor - or the start of one - already. – soegaard Apr 21 '15 at 15:39
Here is 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)

- 993
- 9
- 9
-
You can avoid having to deal with temporary files, by wrapping the system call in `(with-output-to-string (lambda () (with-input-from-string dot-string (lambda () ... ))))`. We do so in https://github.com/fractalide/fractalide/blob/fe5c9d03443e8291b90ccbf1cdf8bf20ced9d233/modules/rkt/rkt-fbp/agents/hyperflow/graph/loader.rkt#L47 when we ask `dot` to layout our nodes for our graph editor. – clacke Sep 12 '18 at 07:22
You can use the graphviz package. If you want to use the dot language directly, you can use the dot->pict function. Alternatively, you can use digraph->pict, which makes dot a bit more powerful by allowing using Racket picts as node shapes.

- 846
- 7
- 12