如何使用GraphViz和Racket

5

是否可以使用graphviz模块在racket框架(GUI)中绘制图形? 如果可以,是否有教程展示如何使用? 谢谢。

3个回答

2
这是我会做的事情:
  • 从图形中生成一个dot-file(Graphviz格式)
  • 使用system运行grapviz文件,生成png文件
  • 在racket框架中显示png
有关实际代码,请参见:https://github.com/wangkuiyi/graphviz-server 请注意,Stephen Chang的图库支持生成dot文件:http://pkg-build.racket-lang.org/doc/graph/index.html#%28part._.Graphviz%29 更新:
为了制作图形编辑器,您可以将图形数据保存在文件中,然后让Graphviz以dot格式输出布局信息:http://www.graphviz.org/doc/info/output.html#d:xdot1.4 解析输出文件,然后在屏幕上重新绘制图形。

1
谢谢您提供这个目的。我想要的更多是制作一个图形编辑器。一个渐进式的构建。 - chsms
更新了答案。考虑在Racket邮件列表上提问你的问题。很有可能已经有人写过一个图形编辑器,或者至少有一个开头。 - soegaard

0

这里有一个例子。您必须安装点程序。

#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)

您可以通过将系统调用包装在 (with-output-to-string (lambda () (with-input-from-string dot-string (lambda () ... )))) 中来避免处理临时文件。我们在 https://github.com/fractalide/fractalide/blob/fe5c9d03443e8291b90ccbf1cdf8bf20ced9d233/modules/rkt/rkt-fbp/agents/hyperflow/graph/loader.rkt#L47 中这样做,当我们要求 dot 为我们的图形编辑器布局节点时。 - clacke

0

你可以使用graphviz包。如果你想直接使用dot语言,你可以使用dot->pict函数。另外,你也可以使用digraph->pict函数,它使得dot更加强大,允许使用Racket picts作为节点形状。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接