How to plot graph labels with Haskell's graphviz package

瘦欲@ 提交于 2021-02-08 06:24:15

问题


This question does not solve the problem that I'm facing, but my question is based on it.

What I'm trying to achieve is to plot a directed graph with labelled nodes and unlabelled edges using graphviz package. In my case nodes have labels of type String (the type definition of the graph is Gr String ()). Here's the code (where used graph is a minified version of clr486 - a perfect example of my use case):

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

It looks like the "labels" in context of graphviz haskell package don't mean what I'd assume are the actual labels of the graph - the resulting graph's labels turn out to be its "internal" indices. The output of this code is:

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

And when passed to dot resulting graph looks like this:

But what I'd like to achieve is the following:


回答1:


Try customizing your parameters. Something along these lines might work:

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


来源:https://stackoverflow.com/questions/61000454/how-to-plot-graph-labels-with-haskells-graphviz-package

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!