如何使用haskell graphViz绘制图形 [英] how to plot a graph using haskell graphViz

查看:192
本文介绍了如何使用haskell graphViz绘制图形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我打算使用Haskell graphViz绘制图形.我是Haskell的新手,所以这对我来说很难.有人可以给我举一个简单的例子吗?我实际上需要一个非常简单的示例,以便我可以理解它并将其用于我正在工作的场景中

I'm planning to draw a graph using Haskell graphViz. I'm new to haskell, so this is quite difficult for me. Can someone show me a simple example ? I need a very simple example actually, so that I can understand it and use it in the scenario I'm working on

在尝试安装chart-cairo时出现上述错误.我在互联网上看到了一些示例,所有示例都需要chart-cairo.知道如何解决吗?

I get the above error on trying to install chart-cairo. I saw some examples on the internet and all of them requires chart-cairo. any idea how to resolve it ?

*已编辑" 执行 https://stackoverflow.com/users/2827654/jamshidh 给定的代码后得到的输出

*EDITED" The output that I get after executing the code given by https://stackoverflow.com/users/2827654/jamshidh

推荐答案

(这解决了您的原始问题,如标题中所述,并且没有涉及安装chart-cairo或chart等问题,实际上应该是跳出不同的问题)....

(This addresses your original question, described in the title, and doesn't go into the problems installing chart-cairo or chart, etc, which really should be spun out into different questions)....

graphviz软件包在Data.Graph.Inductive.Example模块中包含一些示例图,这些示例图可用于启动和运行.您可以在 http:上查看包含的图形的列表: //hackage.haskell.org/package/fgl-5.3/docs/Data-Graph-Inductive-Example.html ....我将使用一个名为clr479

The graphviz package includes some example graphs in module Data.Graph.Inductive.Example that can be used to get you up and running. You can see the list of included graphs at http://hackage.haskell.org/package/fgl-5.3/docs/Data-Graph-Inductive-Example.html.... I will use one called clr479.

一旦有了图形,就可以使用graphToDot将其转换为表示点格式的内部结构.请注意,您将需要提供一些参数,这些参数在 http://hackage.haskell.org/package/graphviz-2999.11.0.0/docs/Data-GraphViz.html .为了启动并运行,我将使用提供的nonClusteredParams.

Once you have a graph, you can convert it to an internal structure representing the dot format using graphToDot. Note that you will need to supply some parameters, which are described in http://hackage.haskell.org/package/graphviz-2999.11.0.0/docs/Data-GraphViz.html. Just to get up and running, I will use the supplied nonClusteredParams.

let graphInDotFormat = graphToDot nonClusteredParams clr479 

然后,您需要将其转换为适合输入dot程序的文本.您可以使用renderDot . toDot

Then, you will need to convert this to text suitable for input to the dot program. You can do this with renderDot . toDot

let outputText = renderDot $ toDot graphInDotFormat

,并且像往常一样,您需要将文本转换为字符串以使用putStrLn(不要只使用show,因为它将包括引号和转义序列,而dot无法理解)

and, as usual, you need to convert text to string to use putStrLn (don't just use show, as it will include quotes and escape sequences, which dot will not understand)

putStrLn $ unpack outputText

将所有内容放在一起,最终程序createDotFile.hs将是

Putting this all together, the final program createDotFile.hs would be

import Data.Text.Lazy
import Data.GraphViz
import Data.Graph.Inductive.Example
import Data.GraphViz.Printing

main = putStrLn $ unpack $ renderDot $ toDot $ graphToDot nonClusteredParams clr479 

使用ghc createDotFile.hs进行编译(如果要对输出执行任何操作,请记住要安装必需的软件包,以及graphviz本身).在命令行上,您现在可以将该程序的输出通过管道传输到dot,它将把它转换为通常的格式....例如,在这里,我将转换为svg

Compile using ghc createDotFile.hs (remember to cabal install the required packages, as well as graphviz itself if you want to do anything with the output). On the commandline, you can now pipe the output of this program to dot, which will convert this to a usual format.... For instance, here I convert to svg

./createDotFile | dot -Tsvg > graph.svg

可以在我的Linux机器上输入以下内容

which on my linux box can be viewed by typing

eog graph.svg

编辑-

为澄清起见,haskell程序的输出需要作为GraphViz的输入提供.在此处的Windows上安装graphviz的msi文件 http://www.graphviz.org/Download_windows.php.

To clarify, the output of the haskell program needs to be provided as an input to GraphViz. The msi file to install graphviz on windows here http://www.graphviz.org/Download_windows.php.

这篇关于如何使用haskell graphViz绘制图形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆