{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeFamilies #-}
module Plots.Axis.Grid
(
GridLines
, HasGridLines (..)
, MajorGridLines
, HasMajorGridLines (..)
, MinorGridLines
, HasMinorGridLines (..)
, gridLinesStyle
, gridLinesVisible
, hideGridLines
, showGridLines
, GridLineFunction
, onTicksGridLineFunction
, emptyGridLineFunction
) where
import Control.Lens hiding (( # ))
import Data.Data
import Data.Default
import Control.Monad.State
import Diagrams.Prelude
import Plots.Types
type GridLineFunction n = [n] -> (n, n) -> [n]
data MajorGridLines v = MajorGridLines
{ magFun :: GridLineFunction Double
, magStyle :: Style v Double
, magVisible :: Bool
} deriving Typeable
type instance V (MajorGridLines v) = v
type instance N (MajorGridLines v) = Double
class HasMajorGridLines f a where
majorGridLines :: LensLike' f a (MajorGridLines (V a))
majorGridLinesFunction :: Functor f => LensLike' f a (GridLineFunction Double)
majorGridLinesFunction = majorGridLines . lens magFun (\gl maf -> gl {magFun = maf})
majorGridLinesStyle :: Functor f => LensLike' f a (Style (V a) Double)
majorGridLinesStyle = majorGridLines . lens magStyle (\gl sty -> gl {magStyle = sty})
instance HasMajorGridLines f (MajorGridLines v) where
majorGridLines = id
instance Default (MajorGridLines v) where
def = MajorGridLines
{ magFun = onTicksGridLineFunction
, magStyle = mempty # lwO 0.8
, magVisible = True
}
instance HasVisibility (MajorGridLines v) where
visible = lens magVisible (\gl b -> gl {magVisible = b})
instance ApplyStyle (MajorGridLines v)
instance HasStyle (MajorGridLines v) where
style = majorGridLinesStyle
data MinorGridLines v = MinorGridLines
{ migFun :: GridLineFunction Double
, migStyle :: Style v Double
, migVisible :: Bool
} deriving Typeable
type instance V (MinorGridLines v) = v
type instance N (MinorGridLines v) = Double
class HasMinorGridLines f a where
minorGridLines :: LensLike' f a (MinorGridLines (V a))
minorGridLinesFunction :: Functor f => LensLike' f a (GridLineFunction Double)
minorGridLinesFunction = minorGridLines . lens migFun (\gl mif -> gl {migFun = mif})
minorGridLinesStyle :: Functor f => LensLike' f a (Style (V a) Double)
minorGridLinesStyle = minorGridLines . lens migStyle (\gl sty -> gl {migStyle = sty})
instance HasMinorGridLines f (MinorGridLines v) where
minorGridLines = id
instance Default (MinorGridLines v) where
def = MinorGridLines
{ migFun = onTicksGridLineFunction
, migStyle = mempty # lwO 0.5
, migVisible = False
}
instance HasVisibility (MinorGridLines v) where
visible = lens migVisible (\gl b -> gl {migVisible = b})
instance ApplyStyle (MinorGridLines v)
instance HasStyle (MinorGridLines v) where
style = minorGridLinesStyle
onTicksGridLineFunction :: GridLineFunction n
onTicksGridLineFunction = const
emptyGridLineFunction :: GridLineFunction n
emptyGridLineFunction _ _ = []
gridLinesVisible :: (HasGridLines f a, Applicative f) => LensLike' f a Bool
gridLinesVisible = gridLines . vis where
vis :: Traversal' (GridLines v) Bool
vis f a =
(\m mn -> a & majorGridLines . visible .~ m & minorGridLines . visible .~ mn)
<$> f (a ^. majorGridLines . visible) <*> f (a ^. minorGridLines . visible)
data GridLines v = GridLines
{ majGrid :: MajorGridLines v
, minGrid :: MinorGridLines v
} deriving Typeable
type instance V (GridLines v) = v
type instance N (GridLines v) = Double
class (HasMinorGridLines f a, HasMajorGridLines f a) => HasGridLines f a where
gridLines :: LensLike' f a (GridLines (V a))
instance Functor f => HasGridLines f (GridLines v) where
gridLines = id
instance Default (GridLines v) where
def = GridLines
{ majGrid = def
, minGrid = def
}
instance Functor f => HasMajorGridLines f (GridLines v) where
majorGridLines = lens majGrid (\g a -> g {majGrid = a})
instance Functor f => HasMinorGridLines f (GridLines v) where
minorGridLines = lens minGrid (\g a -> g {minGrid = a})
instance ApplyStyle (GridLines v) where
applyStyle s = (majorGridLines %~ applyStyle s) . (minorGridLines %~ applyStyle s)
hideGridLines :: (HasGridLines Identity a, MonadState a m) => m ()
hideGridLines = do
minorGridLines . visible .= False
majorGridLines . visible .= False
showGridLines :: (HasGridLines Identity a, MonadState a m) => m ()
showGridLines = do
minorGridLines . visible .= True
majorGridLines . visible .= True
gridLinesStyle :: (HasGridLines f a, Applicative f) => LensLike' f a (Style (V a) Double)
gridLinesStyle = gridLines . styles where
styles :: Traversal' (GridLines v) (Style v Double)
styles f a =
(\m mn -> a & majorGridLinesStyle .~ m & minorGridLinesStyle .~ mn)
<$> f (a ^. majorGridLinesStyle) <*> f (a ^. minorGridLinesStyle)