{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE TypeFamilies #-}
module Plots.Axis.Title
( Title
, HasTitle (..)
, drawTitle
) where
import Data.Default
import Data.Typeable
import Diagrams.Prelude
import Diagrams.TwoD.Text
import Plots.Types
data Title b v n = Title
{ tVisible :: Bool
, tTxt :: String
, tTxtFun :: TextAlignment n -> String -> QDiagram b v n Any
, tStyle :: Style v n
, tPlacement :: Placement
, tAlignment :: TextAlignment n
, tGap :: n
} deriving Typeable
instance (Renderable (Text n) b, TypeableFloat n)
=> Default (Title b V2 n) where
def = Title
{ tVisible = True
, tTxt = ""
, tTxtFun = mkText
, tStyle = mempty # fontSize (output 11)
, tPlacement = midAbove
, tAlignment = BoxAlignedText 0.5 0
, tGap = 20
}
type instance V (Title b v n) = v
type instance N (Title b v n) = n
instance HasVisibility (Title b v n) where
visible = lens tVisible (\t b -> t {tVisible = b})
instance HasGap (Title b v n) where
gap = lens tGap (\t g -> t {tGap = g})
instance HasPlacement (Title b v n) where
placement = titlePlacement
class HasTitle a b | a -> b where
title :: Lens' a (Title b (V a) (N a))
titleText :: Lens' a String
titleText = title . lens tTxt (\t s -> t {tTxt = s})
titleStyle :: Lens' a (Style (V a) (N a))
titleStyle = title . lens tStyle (\t s -> t {tStyle = s})
titlePlacement :: Lens' a Placement
titlePlacement = title . lens tPlacement (\t s -> t {tPlacement = s})
titleTextFunction :: Lens' a (TextAlignment (N a) -> String -> QDiagram b (V a) (N a) Any)
titleTextFunction = title . lens tTxtFun (\t s -> t {tTxtFun = s})
titleAlignment :: Lens' a (TextAlignment (N a))
titleAlignment = title . lens tAlignment (\t s -> t {tAlignment = s})
titleGap :: Lens' a (N a)
titleGap = title . lens tGap (\t s -> t {tGap = s})
instance HasTitle (Title b v n) b where
title = id
drawTitle
:: TypeableFloat n
=> BoundingBox V2 n
-> Title b V2 n
-> QDiagram b V2 n Any
drawTitle bb t
| t ^. hidden || nullOf titleText t = mempty
| otherwise = placeAgainst
bb
(t ^. titlePlacement)
(t ^. titleGap)
tDia
where
tDia = tTxtFun t (t ^. titleAlignment) (tTxt t)
# applyStyle (tStyle t)