module Graphics.Gnuplot.Private.Plot where
import qualified Data.Monoid.State as State
import Data.Monoid (Monoid, mempty, mappend, )
import qualified Graphics.Gnuplot.Display as Display
import qualified Graphics.Gnuplot.Private.Graph as Graph
import Graphics.Gnuplot.Utility (quote, commaConcat, )
import Data.Maybe (mapMaybe, )
newtype T graph = Cons (State.T Int [File graph])
instance Monoid (T graph) where
mempty = Cons mempty
mappend (Cons s0) (Cons s1) =
Cons (mappend s0 s1)
withUniqueFile :: String -> [graph] -> T graph
withUniqueFile content graphs =
Cons (State.Cons $ \n ->
([File (tmpFileStem ++ show n ++ ".csv") (Just content) graphs],
succ n))
fromGraphs :: FilePath -> [graph] -> T graph
fromGraphs name gs =
Cons (State.pure [File name Nothing gs])
data File graph =
File {
filename_ :: FilePath,
content_ :: Maybe String,
graphs_ :: [graph]
}
writeData :: File graph -> IO ()
writeData (File fn cont _) =
maybe (return ()) (writeFile fn) cont
tmpFileStem :: FilePath
tmpFileStem = "curve"
instance Functor T where
fmap f (Cons mp) =
Cons $
fmap (map (\file -> file{graphs_ = map f $ graphs_ file}))
mp
instance Graph.C graph => Display.C (T graph) where
toScript p@(Cons mp) =
Display.Script $ State.Cons $
\(n0, opts) ->
let (blocks, n1) = State.run mp n0
files =
mapMaybe
(\blk -> fmap (Display.File (filename_ blk)) (content_ blk))
blocks
graphs =
concatMap (\blk ->
map (\gr -> quote (filename_ blk) ++ " " ++ Graph.toString gr) $ graphs_ blk) $
blocks
in (Display.Body files
[plotCmd p undefined ++ " " ++ commaConcat graphs],
(n1, opts))
plotCmd ::
Graph.C graph =>
T graph -> graph -> String
plotCmd _plot = Graph.command