module Web.Lightning.Plots.Graph
(
GraphPlot(..)
, Visualization (..)
, graphPlot
)
where
import Control.Monad.Reader
import Data.Aeson
import Data.Default.Class
import qualified Data.Text as T
import qualified Web.Lightning.Routes as R
import Web.Lightning.Types.Lightning
import Web.Lightning.Types.Visualization (Visualization (..))
import Web.Lightning.Utilities
data GraphPlot =
GraphPlot { gpX :: [Double]
, gpY :: [Double]
, gpConn :: [[Double]]
, gpValues :: Maybe [Int]
, gpLabels :: Maybe [T.Text]
, gpColors :: Maybe [Int]
, gpGroup :: Maybe [Int]
, gpColorMap :: Maybe T.Text
, gpSize :: Maybe [Int]
, gpToolTips :: Maybe Bool
, gpZoom :: Maybe Bool
, gpBrush :: Maybe Bool
}
deriving (Show, Eq)
instance Default GraphPlot where
def = GraphPlot [] [] [[]] Nothing Nothing Nothing Nothing Nothing
Nothing (Just True) (Just True) (Just True)
instance ToJSON GraphPlot where
toJSON (GraphPlot xs ys conn vs ls cs gs cm ss t z b) =
omitNulls [ "links" .= getLinks conn
, "nodes" .= getPoints xs ys
, "values" .= vs
, "labels" .= ls
, "color" .= cs
, "group" .= gs
, "colormap" .= cm
, "size" .= ss
, "tooltips" .= t
, "zoom" .= z
, "brush" .= b
]
instance ValidatablePlot GraphPlot where
validatePlot (GraphPlot xs ys conn vl lbl c g cm s tt z b) = do
(xs', ys') <- validateCoordinates xs ys
conn' <- validateConn conn
c' <- validateColor c
cm' <- validateColorMap cm
s' <- validateSize s
return $ GraphPlot xs' ys' conn' vl lbl c' g cm' s' tt z b
graphPlot :: Monad m => GraphPlot
-> LightningT m Visualization
graphPlot graphPlt = do
url <- ask
viz <- sendPlot "graph" graphPlt R.plot
return $ viz { vizBaseUrl = Just url }