Reputation: 4593
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:
Upvotes: 2
Views: 296
Reputation: 804
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
Upvotes: 3