module Graphics.Rendering.Chart.State(
plot,
plotLeft,
plotRight,
takeColor,
takeShape,
CState,
colors,
shapes,
EC,
execEC,
liftEC,
liftCState,
) where
import Control.Lens
import Control.Monad.State
import Data.Default.Class
import Data.Colour
import Data.Colour.Names
import Graphics.Rendering.Chart.Layout
import Graphics.Rendering.Chart.Plot
import Graphics.Rendering.Chart.Drawing
import Graphics.Rendering.Chart.Renderable
data CState = CState {
_colors :: [AlphaColour Double],
_shapes :: [PointShape]
}
$( makeLenses ''CState )
type EC l a = StateT l (State CState) a
instance Default CState where
def = CState defColors defShapes
where
defColors = cycle (map opaque [blue,green,red,orange,yellow,violet])
defShapes = cycle [PointShapeCircle,PointShapePlus,PointShapeCross,PointShapeStar]
instance (Default a,ToRenderable a) => ToRenderable (EC a b) where
toRenderable = toRenderable . execEC
execEC :: (Default l) => EC l a -> l
execEC ec = evalState (execStateT ec def) def
liftEC :: (Default l1) => EC l1 a -> EC l2 l1
liftEC ec = do
cs <- lift get
let (l,cs') = runState (execStateT ec def) cs
lift (put cs')
return l
liftCState :: State CState a -> EC l a
liftCState = lift
plot :: (ToPlot p) => EC (Layout x y) (p x y) -> EC (Layout x y) ()
plot pm = do
p <- pm
layout_plots %= (++[toPlot p])
plotLeft :: (ToPlot p) => EC (LayoutLR x y1 y2) (p x y1) -> EC (LayoutLR x y1 y2) ()
plotLeft pm = do
p <- pm
layoutlr_plots %= (++[Left (toPlot p)])
plotRight :: (ToPlot p) => EC (LayoutLR x y1 y2) (p x y2) -> EC (LayoutLR x y1 y2) ()
plotRight pm = do
p <- pm
layoutlr_plots %= (++[Right (toPlot p)])
takeColor :: EC l (AlphaColour Double)
takeColor = liftCState $ do
(c:cs) <- use colors
colors .= cs
return c
takeShape :: EC l PointShape
takeShape = liftCState $ do
(c:cs) <- use shapes
shapes .= cs
return c