haskell - 如何使用 Haskell 的 graphviz 包绘制图形标签

标签 haskell graphviz

This问题并没有解决我面临的问题,但我的问题是基于它的。

我想要实现的是使用 graphviz 绘制带有标记节点和未标记边的有向图。包裹。在我的例子中,节点具有 String 类型的标签(图形的类型定义是 Gr String ())。这是代码(其中使用的图表是 clr486 的缩小版本 - 我的用例的完美示例):

module Main where

import              Data.Graph.Inductive.Graph          (mkGraph)
import              Data.Graph.Inductive.PatriciaTree   (Gr)
import              Data.GraphViz                       (graphToDot, nonClusteredParams)
import              Data.GraphViz.Printing              (renderDot, toDot)
import              Data.Text.Lazy                      (unpack)

main :: IO ()
main = do
    let exampleGraph = mkGraph (zip [1..3] ["shorts", "socks", "watch"]) [(1,2,()),(2,3,())] :: Gr String ()
    putStrLn $ unpack $ renderDot $ toDot $ graphToDot nonClusteredParams exampleGraph

看起来graphviz haskell包上下文中的“标签”并不意味着我所假设的图表的实际标签 - 结果图表的标签是它的“内部”指数。这段代码的输出是:

digraph {
    1;
    2;
    3;
    1 -> 2;
    2 -> 3;
}

当传递给 dot 时,结果图如下所示:

resulting graph

但我想要实现以下目标:

expected result

最佳答案

尝试自定义您的参数。沿着这些思路的东西可能会起作用:

module Main where

import              Data.Functor                        ((<&>))
import qualified    Data.Text.Lazy as L                 (pack)
import qualified    Data.Text.Lazy.IO as IO             (putStrLn)
import              Data.Graph.Inductive.Graph          (mkGraph)
import              Data.Graph.Inductive.PatriciaTree   (Gr)
import              Data.GraphViz                       (graphToDot, nonClusteredParams, fmtNode)
import              Data.GraphViz.Attributes.Complete   (Label(StrLabel), Attribute(Label))
import              Data.GraphViz.Printing              (renderDot, toDot)

exampleGraph :: Gr String ()
exampleGraph = mkGraph (zip [1..3] ["shorts", "socks", "watch"]) [(1,2,()),(2,3,())]

labelledNodesParams = nonClusteredParams { fmtNode= \(_,label)-> [Label (StrLabel (L.pack label))] }

putGraph :: Gr String () -> IO ()
putGraph = graphToDot labelledNodesParams <&> toDot <&> renderDot <&> IO.putStrLn

main = putGraph exampleGraph

关于haskell - 如何使用 Haskell 的 graphviz 包绘制图形标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61000454/

相关文章:

haskell - 对集合使用不同的排序

Haskell:TVar 是如何工作的?

Haskell - 编码 'maximum' 而没有实际使用函数 [初学者]

command-line - Graphviz Dot由于缺少库而无法删除重叠

graphviz - 你能在 GraphViz 中构建可重用的样式吗?

graphviz - 将子图簇形状更改为圆角矩形

Graphviz 的替代品?

Haskell 从字符串或文本创建 UUID

android - JNI native 代码中的段错误 (Android)

graphviz - 在 Graphviz 中对齐子图、排序节点和重新定位边