cga/Graphics/Diagram/Plotter.hs

116 lines
4.1 KiB
Haskell
Raw Normal View History

{-# OPTIONS_HADDOCK ignore-exports #-}
2014-10-10 15:40:08 +00:00
module Graphics.Diagram.Plotter where
2014-09-30 22:05:29 +00:00
2014-10-14 19:24:21 +00:00
import Data.Monoid
import Diagrams.Prelude hiding ((<>))
import Graphics.Diagram.Core
2014-12-02 18:00:50 +00:00
-- |Creates a Diagram that shows the coordinates from the points
-- as dots. The points and thickness of the dots can be controlled
-- via DiagProp.
coordPoints :: Diag
coordPoints = Diag cp
where
cp p (Object vt) = drawP vt (dotSize p) # fc black # lc black
cp p (Objects vts) = drawP (concat vts) (dotSize p) # fc black # lc black
2014-10-10 13:34:38 +00:00
-- |Show coordinates as text above all points.
coordPointsText :: Diag
coordPointsText = Diag cpt
where
cpt p (Object vt) = drawT vt p
cpt p (Objects vts) = drawT (concat vts) p
drawT [] _ = mempty
drawT vt p
2014-12-03 00:36:12 +00:00
| showCoordText p = position $ zip vt (pointToTextCoord <$> vt)
2014-11-16 03:37:34 +00:00
# translate (r2 (0, 10))
| otherwise = mempty
-- |Creates a Diagram that shows an XAxis which is bound
2014-11-15 23:49:20 +00:00
-- by the dimensions given in xDimension from DiagProp.
xAxis :: Diag
2014-10-09 22:19:05 +00:00
xAxis =
2014-11-16 15:45:51 +00:00
Diag hRule
<> Diag segments
<> Diag labels
where
2014-12-03 00:36:12 +00:00
hRule p _ = arrowAt (p2 (diagXmin p, if diagYmin p <= 0 then 0 else diagYmin p))
(r2 (diagWidth p, 0))
segments p _ = hcat' (with & sep .~ squareSize p)
(replicate (floor . (/) (diagWidth p) $ squareSize p)
(vrule 10))
2014-11-16 03:37:34 +00:00
# moveTo (p2 (diagXmin p, if diagYmin p <= 0 then 0 else diagYmin p))
2014-12-03 20:20:46 +00:00
labels p _ = position . zip (mkPoint <$> xs)
$ ((\x -> (text . show $ x) # scale 10) <$> xs)
2014-10-09 22:19:05 +00:00
where
xs :: [Int]
xs = take (floor . (/) (diagWidth p) $ squareSize p)
(iterate (+(floor . squareSize $ p)) (floor . diagXmin $ p))
mkPoint x = p2 (fromIntegral x,
-15 + (if diagYmin p <= 0 then 0 else diagYmin p))
-- |Creates a Diagram that shows an YAxis which is bound
2014-11-15 23:49:20 +00:00
-- by the dimensions given in yDimension from DiagProp.
yAxis :: Diag
2014-10-09 22:19:05 +00:00
yAxis =
2014-11-16 15:45:51 +00:00
Diag vRule
<> Diag segments
<> Diag labels
where
2014-12-03 20:20:46 +00:00
vRule p _ = arrowAt (p2 (if diagXmin p <= 0 then 0 else diagXmin p, diagYmin p))
(r2 (0, diagHeight p))
segments p _ = vcat' (with & sep .~ squareSize p)
(replicate (floor . (/) (diagHeight p) $ squareSize p)
(hrule 10))
2014-11-16 03:37:34 +00:00
# alignB
# moveTo (p2 (if diagXmin p <= 0 then 0 else diagXmin p, diagYmin p))
2014-12-03 20:20:46 +00:00
labels p _ = position . zip (mkPoint <$> ys)
$ ((\x -> (text . show $ x) # scale 10) <$> ys)
2014-10-09 19:37:45 +00:00
where
ys :: [Int]
ys = take (floor . (/) (diagHeight p) $ squareSize p)
(iterate (+(floor . squareSize $ p)) (floor . diagYmin $ p))
mkPoint y = p2 (-15 + (if diagXmin p <= 0 then 0 else diagXmin p),
fromIntegral y)
-- |Creates a Diagram that shows a white rectangle which is a little
2014-10-10 15:40:08 +00:00
-- bit bigger than both X and Y axis dimensions from DiagProp.
whiteRectB :: Diag
2014-10-09 22:42:15 +00:00
whiteRectB = Diag rect'
where
rect' p _ = rect (diagWidth p + (diagWidth p / 10))
(diagHeight p + (diagHeight p / 10))
# lwG 0.00
# bg white
2014-12-03 20:20:46 +00:00
# moveTo (p2 (diagWidthOffset p, diagHeightOffset p))
where
2014-10-11 01:59:21 +00:00
2014-10-09 23:08:53 +00:00
-- |Create a grid across the whole diagram with squares of the
-- given size in DiagProp.
2014-10-09 16:45:37 +00:00
grid :: Diag
2014-10-14 19:24:21 +00:00
grid = Diag xGrid <> Diag yGrid
2014-10-09 16:45:37 +00:00
where
yGrid p _
2014-12-03 00:36:12 +00:00
| haveGrid p = hcat' (with & sep .~ squareSize p)
(replicate (floor . (/) (diagWidth p) $ squareSize p)
(vrule $ diagHeight p))
2014-11-16 03:37:34 +00:00
# moveTo (p2 (diagXmin p, diagHeightOffset p)) # lw ultraThin
| otherwise = mempty
xGrid p _
2014-12-03 00:36:12 +00:00
| haveGrid p = vcat' (with & sep .~ squareSize p)
(replicate (floor . (/) (diagHeight p) $ squareSize p)
(hrule $ diagWidth p))
2014-11-16 03:37:34 +00:00
# alignB # moveTo (p2 (diagWidthOffset p, diagYmin p)) # lw ultraThin
| otherwise = mempty
plotterBG :: Diag
plotterBG = mconcat [xAxis, yAxis, grid, whiteRectB]