{-# 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 n = MajorGridLines
{ magFun :: GridLineFunction n
, magStyle :: Style v n
, magVisible :: Bool
} deriving Typeable
type instance V (MajorGridLines v n) = v
type instance N (MajorGridLines v n) = n
class HasMajorGridLines f a where
majorGridLines :: LensLike' f a (MajorGridLines (V a) (N a))
majorGridLinesFunction :: Functor f => LensLike' f a (GridLineFunction (N a))
majorGridLinesFunction = majorGridLines . lens magFun (\gl maf -> gl {magFun = maf})
majorGridLinesStyle :: Functor f => LensLike' f a (Style (V a) (N a))
majorGridLinesStyle = majorGridLines . lens magStyle (\gl sty -> gl {magStyle = sty})
instance HasMajorGridLines f (MajorGridLines v n) where
majorGridLines = id
instance (Typeable n, Floating n) => Default (MajorGridLines v n) where
def = MajorGridLines
{ magFun = onTicksGridLineFunction
, magStyle = mempty # lwO 0.8
, magVisible = True
}
instance HasVisibility (MajorGridLines v n) where
visible = lens magVisible (\gl b -> gl {magVisible = b})
instance Typeable n => HasStyle (MajorGridLines v n) where
applyStyle s = majorGridLinesStyle %~ applyStyle s
data MinorGridLines v n = MinorGridLines
{ migFun :: GridLineFunction n
, migStyle :: Style v n
, migVisible :: Bool
} deriving Typeable
type instance V (MinorGridLines v n) = v
type instance N (MinorGridLines v n) = n
class HasMinorGridLines f a where
minorGridLines :: LensLike' f a (MinorGridLines (V a) (N a))
minorGridLinesFunction :: Functor f => LensLike' f a (GridLineFunction (N a))
minorGridLinesFunction = minorGridLines . lens migFun (\gl mif -> gl {migFun = mif})
minorGridLinesStyle :: Functor f => LensLike' f a (Style (V a) (N a))
minorGridLinesStyle = minorGridLines . lens migStyle (\gl sty -> gl {migStyle = sty})
instance HasMinorGridLines f (MinorGridLines v n) where
minorGridLines = id
instance (Typeable n, Floating n) => Default (MinorGridLines v n) where
def = MinorGridLines
{ migFun = onTicksGridLineFunction
, migStyle = mempty # lwO 0.5
, migVisible = False
}
instance HasVisibility (MinorGridLines v n) where
visible = lens migVisible (\gl b -> gl {migVisible = b})
instance Typeable n => HasStyle (MinorGridLines v n) where
applyStyle s = minorGridLinesStyle %~ applyStyle s
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 n) Bool
vis f a =
(\m mn -> a & majorGridLines . visible .~ m & minorGridLines . visible .~ mn)
<$> f (a ^. majorGridLines . visible) <*> f (a ^. minorGridLines . visible)
data GridLines v n = GridLines
{ majGrid :: MajorGridLines v n
, minGrid :: MinorGridLines v n
} deriving Typeable
type instance V (GridLines v n) = v
type instance N (GridLines v n) = n
class (HasMinorGridLines f a, HasMajorGridLines f a) => HasGridLines f a where
gridLines :: LensLike' f a (GridLines (V a) (N a))
instance Functor f => HasGridLines f (GridLines v n) where
gridLines = id
instance (Typeable n, Floating n) => Default (GridLines v n) where
def = GridLines
{ majGrid = def
, minGrid = def
}
instance Functor f => HasMajorGridLines f (GridLines v n) where
majorGridLines = lens majGrid (\g a -> g {majGrid = a})
instance Functor f => HasMinorGridLines f (GridLines v n) where
minorGridLines = lens minGrid (\g a -> g {minGrid = a})
instance Typeable n => HasStyle (GridLines v n) 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) (N a))
gridLinesStyle = gridLines . styles where
styles :: Traversal' (GridLines v n) (Style v n)
styles f a =
(\m mn -> a & majorGridLinesStyle .~ m & minorGridLinesStyle .~ mn)
<$> f (a ^. majorGridLinesStyle) <*> f (a ^. minorGridLinesStyle)