Commit e573be11 authored by Kai Zhang's avatar Kai Zhang

minor

parent d105a1c6
......@@ -132,13 +132,13 @@ mkGraph (n, vattr) (es,eattr) = runST $ do
zip' a b | length a /= length b = error "incorrect length"
| otherwise = zipWith (\(x,y) z -> (x,y,z)) a b
fromLabeledEdges :: (Graph d, Hashable v, Read v, Eq v, Show v)
=> [(v, v)] -> LGraph d v ()
fromLabeledEdges es = mkGraph (n, Just labels) (es', Nothing)
fromLabeledEdges :: (Graph d, Hashable v, Read v, Eq v, Show v, Show e)
=> [((v, v), e)] -> LGraph d v e
fromLabeledEdges es = mkGraph (n, Just labels) (es', Just $ snd $ unzip es)
where
es' = map (f *** f) es
es' = map (f *** f) $ fst $ unzip es
where f x = M.lookupDefault undefined x labelToId
labels = nub $ concat [ [a,b] | (a,b) <- es ]
labels = nub $ concat [ [a,b] | ((a,b),_) <- es ]
labelToId = M.fromList $ zip labels [0..]
n = M.size labelToId
......
......@@ -21,6 +21,7 @@ data NodeAttr = NodeAttr
, _nodeLabel :: String
, _positionX :: Double
, _positionY :: Double
, _nodeZindex :: Int
} deriving (Show, Read, Eq)
instance Hashable NodeAttr where
......@@ -33,6 +34,7 @@ defaultNodeAttributes = NodeAttr
, _nodeLabel = ""
, _positionX = 0
, _positionY = 0
, _nodeZindex = 1
}
data EdgeAttr = EdgeAttr
......@@ -40,6 +42,7 @@ data EdgeAttr = EdgeAttr
, _edgeColour :: AlphaColour Double
, _edgeWeight :: Double
, _edgeArrowLength :: Double
, _edgeZindex :: Int
} deriving (Show, Read, Eq)
instance Hashable EdgeAttr where
......@@ -51,6 +54,7 @@ defaultEdgeAttributes = EdgeAttr
, _edgeColour = opaque black
, _edgeWeight = 1.0
, _edgeArrowLength = 5.0
, _edgeZindex = 0
}
genXMLTree :: (ArrowXml a, Graph d) => LGraph d NodeAttr EdgeAttr -> a XmlTree XmlTree
......
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE GADTs #-}
module IGraph.Exporter.Graphics
( renderGraph
, graphToDiagram
) where
import Diagrams.Prelude
import Diagrams.Size (dims)
import Diagrams.Backend.Cairo
import Data.List (sortBy)
import Data.Ord (comparing)
import Diagrams.Backend.Cairo
import Diagrams.Prelude
import Diagrams.Size (dims)
import IGraph
import IGraph.Exporter.GEXF
import IGraph
import IGraph.Exporter.GEXF
renderGraph :: Graph d => FilePath -> Double -> Double -> LGraph d NodeAttr EdgeAttr -> IO ()
renderGraph out w h gr = renderCairo out (dims $ w ^& h) $ graphToDiagram gr
graphToDiagram :: Graph d => LGraph d NodeAttr EdgeAttr -> Diagram B
graphToDiagram gr = position (map drawNode (nodes gr)) <> mconcat (map drawEdge (edges gr))
graphToDiagram gr = mconcat $ fst $ unzip $ sortBy (flip (comparing snd)) $
map drawNode (nodes gr) ++ map drawEdge (edges gr)
where
drawNode x = ( _positionX nattr ^& _positionY nattr
, circle (_size nattr) # lwO 0 # fcA (_nodeColour nattr) )
drawNode x = ( moveTo (_positionX nattr ^& _positionY nattr)
(circle (_size nattr) # lwO 0 # fcA (_nodeColour nattr))
, _nodeZindex nattr )
where
nattr = nodeLab gr x
drawEdge (from, to) = {-arrowBetween'
......@@ -27,8 +31,8 @@ graphToDiagram gr = position (map drawNode (nodes gr)) <> mconcat (map drawEdge
& arrowHead .~ arrowH
& headLength .~ output (_edgeArrowLength eattr)
) start end-}
fromVertices [start, end]
# lwO (_edgeWeight eattr) # lcA (_edgeColour eattr)
( fromVertices [start, end]
# lwO (_edgeWeight eattr) # lcA (_edgeColour eattr), _edgeZindex eattr )
where
eattr = edgeLab gr (from, to)
start = _positionX nattr1 ^& _positionY nattr1
......
......@@ -84,7 +84,7 @@ eigenvectorCentrality gr ws = unsafePerformIO $ do
-- | Google's PageRank
pagerank :: Graph d
=> LGraph d v e
-> Maybe [Double]
-> Maybe [Double] -- ^ edge weights
-> Double -- ^ damping factor, usually around 0.85
-> [Double]
pagerank gr ws d = unsafePerformIO $ alloca $ \p -> do
......
......@@ -4,4 +4,4 @@ flags:
packages:
- '.'
extra-deps: []
resolver: lts-5.5
resolver: lts-6.1
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment