DIAGRAM: improve style and readability

This commit is contained in:
hasufell 2014-10-11 03:59:21 +02:00
parent bfa3665985
commit 7fb3588300
No known key found for this signature in database
GPG Key ID: 220CD1C5BDEED020
2 changed files with 66 additions and 42 deletions

View File

@ -17,7 +17,7 @@ coordPoints :: Diag
coordPoints = Diag cp coordPoints = Diag cp
where where
cp p vt = cp p vt =
position (zip (filter (inRange (dX p) (dY p)) $ vt) position (zip (filter (inRange (dX p) (dY p)) vt)
(repeat dot)) (repeat dot))
where where
dot = (circle $ t p :: Diagram Cairo R2) # fc black dot = (circle $ t p :: Diagram Cairo R2) # fc black
@ -48,7 +48,7 @@ convexHullPoints :: Diag
convexHullPoints = Diag chp convexHullPoints = Diag chp
where where
chp p vt = chp p vt =
position (zip (filter (inRange (dX p) (dY p)) $ vtch) position (zip (filter (inRange (dX p) (dY p)) vtch)
(repeat dot)) (repeat dot))
where where
dot = (circle $ t p :: Diagram Cairo R2) # fc red # lc red dot = (circle $ t p :: Diagram Cairo R2) # fc red # lc red
@ -106,26 +106,26 @@ convexHullLinesInterval p xs =
-- by the dimensions given in xD from DiagProp. -- by the dimensions given in xD from DiagProp.
xAxis :: Diag xAxis :: Diag
xAxis = xAxis =
(Diag hRule) `mappend` Diag hRule `mappend`
(Diag segments) `mappend` Diag segments `mappend`
(Diag labels) Diag labels
where where
hRule p _ = hRule p _ =
arrowAt (p2 (xlD p,0)) arrowAt (p2 (xmin p,0))
(r2 (xuD p - xlD p, 0)) (r2 (w' p, 0))
segments p _ = segments p _ =
hcat' (with & sep .~ (sqS p)) hcat' (with & sep .~ sqS p)
(take (floor . (/) (xuD p - xlD p) $ (sqS p)) . (replicate (floor . (/) (w' p) $ sqS p)
repeat $ (vrule 10)) # (vrule 10)) #
moveTo (p2 (xlD p,0)) moveTo (p2 (xmin p,0))
labels p _ = labels p _ =
position $ position $
zip (mkPoint <$> xs) zip (mkPoint <$> xs)
((\x -> (text . show $ x) # scale 10) <$> xs) ((\x -> (text . show $ x) # scale 10) <$> xs)
where where
xs :: [Int] xs :: [Int]
xs = take (floor . (/) (xuD p - xlD p) $ (sqS p)) xs = take (floor . (/) (w' p) $ sqS p)
(iterate (+(floor . sqS $ p)) (floor . xlD $ p)) (iterate (+(floor . sqS $ p)) (floor . xmin $ p))
mkPoint x = p2 (fromIntegral x, -15) mkPoint x = p2 (fromIntegral x, -15)
@ -133,27 +133,27 @@ xAxis =
-- by the dimensions given in yD from DiagProp. -- by the dimensions given in yD from DiagProp.
yAxis :: Diag yAxis :: Diag
yAxis = yAxis =
(Diag vRule) `mappend` Diag vRule `mappend`
(Diag segments) `mappend` Diag segments `mappend`
(Diag labels) Diag labels
where where
vRule p _ = vRule p _ =
arrowAt (p2 (0, ylD p)) arrowAt (p2 (0, ymin p))
(r2 (0, yuD p - ylD p)) (r2 (0, h' p))
segments p _ = segments p _ =
vcat' (with & sep .~ (sqS p)) vcat' (with & sep .~ sqS p)
(take (floor . (/) (yuD p - ylD p) $ (sqS p)) . (replicate (floor . (/) (h' p) $ sqS p)
repeat $ (hrule 10)) # (hrule 10)) #
alignB # alignB #
moveTo (p2 (0, (ylD p))) moveTo (p2 (0, ymin p))
labels p _ = labels p _ =
position $ position $
zip (mkPoint <$> ys) zip (mkPoint <$> ys)
((\x -> (text . show $ x) # scale 10) <$> ys) ((\x -> (text . show $ x) # scale 10) <$> ys)
where where
ys :: [Int] ys :: [Int]
ys = take (floor . (/) (yuD p - ylD p) $ (sqS p)) ys = take (floor . (/) (h' p) $ sqS p)
(iterate (+(floor . sqS $ p)) (floor . ylD $ p)) (iterate (+(floor . sqS $ p)) (floor . ymin $ p))
mkPoint y = p2 (-15, fromIntegral y) mkPoint y = p2 (-15, fromIntegral y)
@ -162,10 +162,11 @@ yAxis =
whiteRectB :: Diag whiteRectB :: Diag
whiteRectB = Diag rect' whiteRectB = Diag rect'
where where
rect' p _ = whiteRect (w' + 50) (h' + 50) # moveTo (p2 (w' / 2, h' / 2)) rect' p _ =
whiteRect (w' p + 50) (h' p + 50) #
moveTo (p2 (wOff p, hOff p))
where where
w' = xuD p - xlD p
h' = yuD p - ylD p
-- |Create a white rectangle with the given width and height. -- |Create a white rectangle with the given width and height.
@ -180,14 +181,15 @@ grid = Diag xGrid `mappend` Diag yGrid
where where
yGrid p _ = yGrid p _ =
hcat' (with & sep .~ sqS p) hcat' (with & sep .~ sqS p)
(replicate (floor . (/) (xuD p - xlD p) $ sqS p) (replicate (floor . (/) (w' p) $ sqS p)
(vrule $ yuD p - ylD p)) # (vrule $ h' p)) #
moveTo (p2 (xlD p, ylD p + ((yuD p - ylD p) / 2))) # moveTo (p2 (xmin p, hOff p)) #
lw ultraThin lw ultraThin
xGrid p _ = xGrid p _ =
vcat' (with & sep .~ sqS p) vcat' (with & sep .~ sqS p)
(replicate (floor . (/) (yuD p - ylD p) $ sqS p) (replicate (floor . (/) (h' p) $ sqS p)
(hrule $ xuD p - xlD p)) # (hrule $ w' p)) #
alignB # alignB #
moveTo (p2 (xlD p + ((xuD p - xlD p) / 2), ylD p)) # moveTo (p2 (wOff p, ymin p)) #
lw ultraThin lw ultraThin
where

View File

@ -59,23 +59,45 @@ defaultProp = MkProp 2 (0,500) (0,500) 0 False False 50
-- |Extract the lower bound of the x-axis dimension. -- |Extract the lower bound of the x-axis dimension.
xlD :: DiagProp -> Double xmin :: DiagProp -> Double
xlD = fst . dX xmin = fst . dX
-- |Extract the upper bound of the x-axis dimension. -- |Extract the upper bound of the x-axis dimension.
xuD :: DiagProp -> Double xmax :: DiagProp -> Double
xuD = snd . dX xmax = snd . dX
-- |Extract the lower bound of the y-axis dimension. -- |Extract the lower bound of the y-axis dimension.
ylD :: DiagProp -> Double ymin :: DiagProp -> Double
ylD = fst . dY ymin = fst . dY
-- |Extract the upper bound of the y-axis dimension. -- |Extract the upper bound of the y-axis dimension.
yuD :: DiagProp -> Double ymax :: DiagProp -> Double
yuD = snd . dY ymax = snd . dY
-- |The full width of the x dimension.
w' :: DiagProp -> Double
w' p = xmax p - xmin p
-- |The full height of the y dimension.
h' :: DiagProp -> Double
h' p = ymax p - ymin p
-- |The offset on the x-axis to move the grid and the white rectangle
-- to the right place.
wOff :: DiagProp -> Double
wOff p = xmin p + (w' p / 2)
-- |The offset on the y-axis to move the grid and the white rectangle
-- to the right place.
hOff :: DiagProp -> Double
hOff p = ymin p + (h' p / 2)
-- |Returns the specified diagram if True is passed, -- |Returns the specified diagram if True is passed,