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
时,结果图如下所示:
但我想要实现以下目标:
最佳答案
尝试自定义您的参数。沿着这些思路的东西可能会起作用:
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/