-
Notifications
You must be signed in to change notification settings - Fork 3
/
Plot.hs
708 lines (639 loc) · 28.2 KB
/
Plot.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE Rank2Types #-}
{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE BangPatterns #-}
{- | Example implementation for displaying signals
-}
module Plot(
StyledSignal
, SignalStyle(..)
, PlotStyle(..)
, AnySignal(..)
, defaultPlotStyle
, defaultSignalStyle
, discreteSignalsWithStyle
, signalsWithStyle
, display
, drawStringLabel
, LabelStyle(..)
, Pair(..)
, Mono(..)
, Resource(..)
, runPixmap
, Pixmap(..)
, Canvas(..)
, PlotCoordinates(..)
, PictureCoordinates(..)
, CoordinateMapping(..)
, HasDoubleList(..)
, ticksWithTime
, ticksWithPhase
, plotSignals
, plotSpectrum
, withNewStyle
) where
import Graphics.PDF
import Displayable
import Viewer
import Text.Printf
import qualified Graphics.PDF as PDF(Orientation(..))
import Control.Monad(when)
import Data.Maybe(isJust,fromJust)
import Signal
import Fixed(HasDoubleRepresentation(..))
import MultiRate
import Control.Monad.State.Strict
import qualified Data.Vector.Unboxed.Mutable as M
import qualified Data.Vector.Unboxed as U
import Control.Monad.ST
import Control.Monad.Primitive
import Data.Word
import Data.Bits
import Data.List(sortBy,unfoldr,sort)
import Data.Function(on)
import Data.STRef
import Common
import Debug.Trace
debug a = trace (show a) a
maximumPoints = 800
-- | A list fo signals wth style information for the plot and the signals
-- The signals are using the same units
data StyledSignal a b = StyledSignal Bool [a] [[b]] (PlotStyle a b)
withNewStyle :: StyledSignal a b -> (PlotStyle a b -> PlotStyle a b) -> StyledSignal a b
withNewStyle (StyledSignal b t s style) f = StyledSignal b t s (f style)
-- | Style for a signal (only color used in this version)
data SignalStyle = SignalStyle {
signalColor :: !Color
, signalWidth :: !Double
, signalOpacity :: !Double
}
-- | Style for a label
data LabelStyle = LabelStyle !Int !Justification !(PDF.Orientation)
{-
Default styles
-}
hUnitStyle = LabelStyle 7 LeftJustification PDF.E
vUnitStyle = LabelStyle 7 Centered PDF.S
hTickStyle = LabelStyle 7 Centered PDF.N
vTickStyle = LabelStyle 7 RightJustification PDF.W
titleStyle = LabelStyle 14 Centered PDF.N
-- | Draw a string value with style and wrapping
drawStringLabel :: LabelStyle
-> String
-> PDFFloat
-> PDFFloat
-> PDFFloat
-> PDFFloat
-> Draw ()
drawStringLabel (LabelStyle fs j o) s x y w h = do
let (r,b) = drawTextBox x y w h o NormalParagraph (Font (PDFFont Times_Roman fs) black black) $ do
setJustification j
paragraph $ do
txt $ s
b
-- | Default style for a signal
defaultSignalStyle :: Double -> Color -> SignalStyle
defaultSignalStyle opacity color = SignalStyle color 1.0 opacity
-- | Label for time
ticksWithTime :: Time -> (Double -> String)
ticksWithTime t d = printf "%.2f" (d * getT t)
ticksWithPhase :: Int -> (Double -> String)
ticksWithPhase nb n = printf "%.2f pi" (2 * n / fromIntegral nb :: Double)
plotSignals :: Int -- ^ Nb points
-> Time -- ^ Sampling period
-> [AnySignal] -- ^ Signals
-> StyledSignal Double Double
plotSignals nb sp l = discreteSignalsWithStyle nb (plotStyle {horizontalTickRepresentation = ticksWithTime sp}) l
where
lightBlue = Rgb 0.4 0.4 1.0
lightRed = Rgb 1.0 0.4 0.4
lightGreen = Rgb 0.4 1.0 0.4
lightYellow = Rgb 1.0 1.0 0.4
plotStyle = (defaultPlotStyle { title = Just "Temporal"
, signalStyles = [ defaultSignalStyle 0.8 lightBlue
, defaultSignalStyle 0.8 lightRed
, defaultSignalStyle 0.8 lightGreen
, defaultSignalStyle 0.8 lightYellow
]
, verticalLabel = Just "Amplitude"
, horizontalLabel = Just "s"
})
plotSpectrum :: Int -- ^ Total Nb points
-> [AnySignal] -- ^ Signals
-> StyledSignal Double Double
plotSpectrum nb l = discreteSignalsWithStyle nb (plotStyle {horizontalTickRepresentation = ticksWithPhase nb}) l
where
lightBlue = Rgb 0.4 0.4 1.0
lightRed = Rgb 1.0 0.4 0.4
lightGreen = Rgb 0.4 1.0 0.4
lightYellow = Rgb 1.0 1.0 0.4
plotStyle = (defaultPlotStyle { title = Just "Frequential"
, signalStyles = [ defaultSignalStyle 0.8 lightBlue
, defaultSignalStyle 0.8 lightRed
, defaultSignalStyle 0.8 lightGreen
, defaultSignalStyle 0.8 lightYellow
]
, verticalLabel = Just "Energy"
, horizontalLabel = Just "phase"
, horizontalBounds =Just (0, fromIntegral nb)
})
type PictureCoordinates a b = (a,b) -> Point
type PlotCoordinates a b = Point -> (a,b)
type CoordinateMapping a b = (PictureCoordinates a b, PlotCoordinates a b)
-- | Style for a plot
data PlotStyle a b = PlotStyle {
title :: Maybe String
, leftMargin :: !Double
, rightMargin :: !Double
, topMargin :: !Double
, bottomMargin :: !Double
, horizontalTickValues :: Double -> Double -> [Double]
, verticalTickValues :: Double -> Double -> [Double]
, horizontalTickRepresentation :: Double -> String
, verticalTickRepresentation :: Double -> String
, horizontalLabel :: Maybe String
, verticalLabel:: Maybe String
, prologRsrc :: Int -> Int -> CoordinateMapping a b -> PDF (Maybe (PDFReference RawImage))
, prolog :: Int -> Int ->Maybe (PDFReference RawImage) -> CoordinateMapping a b -> Draw ()
, epilog :: Int -> Int ->CoordinateMapping a b -> Draw ()
, signalStyles :: [SignalStyle]
, axis :: !Bool
, interpolation :: !Bool
, horizontalBounds :: Maybe (Double,Double)
, verticalBounds :: Maybe (Double,Double)
}
-- | Default ticks values in [ma,mb]
tenTicks :: Double -> Double -> [Double]
tenTicks ma mb = map (\t -> (fromIntegral t)/(fromIntegral 10)*(mb-ma) + ma) ([0..10] :: [Int])
-- | Formatting function for floats
simpleFloat :: HasDoubleRepresentation a => a -> String
simpleFloat a =
let s = printf "%1.2g" (toDouble a)
in
s
-- | Default style for plots
defaultPlotStyle :: PlotStyle a b
defaultPlotStyle =
PlotStyle {
title = Nothing
, leftMargin = 50
, rightMargin = 50
, topMargin = 50
, bottomMargin = 20
, horizontalTickValues = tenTicks
, verticalTickValues = tenTicks
, horizontalTickRepresentation = simpleFloat
, verticalTickRepresentation = simpleFloat
, horizontalLabel = Just "s"
, verticalLabel = Just "Energy"
, prologRsrc = \_ -> \_ -> \_ -> return Nothing
, prolog = \_ -> \_ -> \_ -> \_ -> return ()
, epilog = \_ -> \_ -> \_ -> return ()
, signalStyles = repeat (defaultSignalStyle 1.0 (Rgb 0.6 0.6 1.0))
, axis = True
, interpolation = True
, horizontalBounds = Nothing
, verticalBounds = Nothing
}
-- | Create a plot description with signals and a plot style
signalsWithStyle :: Bool -> [a] -> [[b]] -> PlotStyle a b -> StyledSignal a b
signalsWithStyle c times signals style = StyledSignal c times signals style
class HasDoubleList m where
toDoubleList :: Int -> m -> [Double]
instance HasDoubleRepresentation b => HasDoubleList [b] where
toDoubleList nb = map toDouble . take nb
instance HasDoubleRepresentation b => HasDoubleList (Signal b) where
toDoubleList nb = map toDouble . takeS nb
instance HasDoubleRepresentation b => HasDoubleList (Sampled t b) where
toDoubleList nb = toDoubleList nb . getSignal
data AnySignal = forall b. HasDoubleList b => AS b
instance HasDoubleList AnySignal where
toDoubleList nb (AS s) = toDoubleList nb s
-- | Create a plot description with discrete signals and a plot style
discreteSignalsWithStyle :: Int
-> PlotStyle Double Double
-> [AnySignal]
-> StyledSignal Double Double
discreteSignalsWithStyle nbPoints style signals1 =
let theTimes = {-# SCC "InternalTimes" #-} toDoubleList nbPointsToDraw $ ([0,1..] :: [Int])
reduce = (nbPoints `quot` maximumPoints) - 1
nbPointsToDraw = nbPoints
timedSignal s = (toDoubleList nbPointsToDraw) s
theCurves :: [[Double]]
theCurves = {-# SCC theCurves #-} map timedSignal signals1
--complex = True
complex = reduce >= 0
in
signalsWithStyle complex theTimes theCurves style
type Drawing a = (Int -> Int -> PDF a, a -> Int -> Int -> Draw ())
data Pair a = Pair (Drawing a) (Drawing a) (Draw () -> Draw () -> Draw ())
data Mono a = Mono (Drawing a) (Draw () -> Draw ())
instance Displayable (Pair a) (a,a) where
drawing (Pair pa pb c) =
let (ra, da) = pa
(rb, db) = pb
sep = 20.0 :: Double
r = \w -> \h -> do
let h' = floor $ ((fromIntegral h - sep) / 2.0)
a <- ra w h'
b <- rb w h'
return (a,b)
action = \(a,b) -> \w -> \h -> do
let h' = floor ((fromIntegral h - sep) / 2.0)
c (da a w h') (db b w h')
in
(r,action)
instance Displayable (Mono a) a where
drawing (Mono pa c) =
let (ra,da) = pa
action = \a -> \w -> \h -> c (da a w h)
in
(ra, action)
class Monad m => Canvas m where
moveTo :: Point -> m ()
lineTo :: Point -> m ()
setColor :: Color -> Double -> m ()
resetAlpha :: m ()
drawPath :: m ()
pixel :: Point -> m ()
instance Canvas Draw where
moveTo = beginPath
lineTo = addLineToPath
pixel p = do
beginPath p
addLineToPath p
setColor c a = do
strokeColor c
setStrokeAlpha a
resetAlpha = setStrokeAlpha 1.0
drawPath = strokePath
instance Functor Complex where
fmap f (a :+ b) = f a :+ f b
ppixel :: Point -> PState -> STRef s (M.MVector s Word32) -> ST s (STRef s (M.MVector s Word32))
ppixel pa@(xa :+ ya) p mvref = do
mv <- readSTRef mvref
let maxLen = w p * h p - 1
Rgb r g b = currentColor p
alpha = currentAlpha p
ri,gi,bi :: Word32
ri = (floor $ r * 255) .&. 0x0FF
gi = (floor $ g * 255) .&. 0x0FF
bi = (floor $ b * 255) .&. 0x0FF
c :: Word32
c = (ri `shift` 16) .|. (gi `shift` 8) .|. bi
wi = w p
he = h p
plotPoint :: M.MVector s Word32 -> (Complex Int) -> ST s ()
plotPoint !a (x :+ y) | pos >= 0 && pos < maxLen = M.write a pos c
| otherwise = return ()
where
pos = wi * (he - 1 - y) + x
plotPoint mv (floor xa :+ floor ya)
return mvref
drawLine :: Point -> PState -> STRef s (M.MVector s Word32) -> ST s (STRef s (M.MVector s Word32))
drawLine pb@(xb :+ yb) p mvref = do
mv <- readSTRef mvref
let maxLen = w p * h p - 1
pa@(xa :+ ya) = currentPos p
Rgb r g b = currentColor p
alpha = currentAlpha p
ri,gi,bi :: Word32
ri = (floor $ r * 255) .&. 0x0FF
gi = (floor $ g * 255) .&. 0x0FF
bi = (floor $ b * 255) .&. 0x0FF
c :: Word32
c = (ri `shift` 16) .|. (gi `shift` 8) .|. bi
wi = w p
he = h p
vertical = floor xb == floor xa
horizontal = floor yb == floor ya
{-# INLINE plotPoint #-}
plotPoint :: M.MVector s Word32 -> (Complex Int) -> ST s ()
plotPoint !a (x :+ y) | pos >= 0 && pos < maxLen = do
old <- M.read a pos
let new = combineColor old
M.write a pos new
| otherwise = return ()
where
pos = wi * (he - 1 - y) + x
{-# INLINE combineColor #-}
combineColor :: Word32 -> Word32
combineColor old = c
--let oldr = (old `shiftR` 16) .&. 0x0FF
-- oldg = (old `shiftR` 8) .&. 0x0FF
-- oldb = old .&. 0x0FF
-- newr = (floor $ fromIntegral oldr*(1-alpha) + fromIntegral ri*alpha) .&. 0x0FF
-- newg = (floor $ fromIntegral oldg*(1-alpha) + fromIntegral gi*alpha) .&. 0x0FF
-- newb = (floor $ fromIntegral oldb*(1-alpha) + fromIntegral bi*alpha) .&. 0x0FF
-- new = (newr `shift` 16) .|. (newg `shift` 8) .|. newb
--in
--new
verticalLine :: M.MVector s Word32 -> Int -> Int -> Int -> ST s ()
verticalLine !a x yi yj =
let [y0,y1] = sort [yi,yj]
start | y0 >= 0 = y0
| otherwise = 0
end | y1 < he = y1
| otherwise = he - 1
pts = [start, start+1..end]
pos = wi*(he - 1 -start)+x
allPos | pos < maxLen = [pos, pos - wi .. 0]
| otherwise = [maxLen-1,maxLen-1-wi .. 0]
addPoint (p,_) = do
old <- M.read a p
let new = combineColor old
M.write a p new
in
mapM_ addPoint (zip allPos pts)
horizontalLine :: M.MVector s Word32 -> Int -> Int -> Int -> ST s ()
horizontalLine !a y0 xi xj =
let [x0,x1] = sort [xi,xj]
start | x0 >= 0 = x0
| otherwise = 0
end | x1 < wi = x1
| otherwise = wi - 1
pts = [start, start+1..end]
pos = wi*(he - 1 - y) + start
y | y0 == he = he-1
| otherwise = y0
allPos | pos >=0 = [pos, pos + 1 .. maxLen - 1]
| otherwise = [0,1 .. maxLen-1]
addPoint (p,_) = do
old <- M.read a p
let new = combineColor old
M.write a p new
in
mapM_ addPoint (zip allPos pts)
reversal thep@(x :+ y) | floor xb == floor xa = thep
| floor yb == floor ya = thep
| steep > 1.0 = (y :+ x)
| otherwise = thep
where
steep = abs ((yb - ya) / (xb - xa))
line s e =
let f x = floor $! x
[s',e'] = sortBy (compare `on` realPart) [s,e]
x0 = f (realPart s')
y0 = f (imagPart s')
x1 = f (realPart e')
y1 = f (imagPart e')
dx = x1 - x0 :: Int
dy = y1 - y0 :: Int
sy = signum dy
start = x0 :+ y0
in
if dx == 0
then
if dy == 0
then [start]
else let vertical thep@(x :+ y) | sy > 0 && y > y1 = Nothing
| sy < 0 && y < y1 = Nothing
| otherwise = Just (thep, x :+ (y+sy))
in
start:unfoldr vertical start
else
let delta = fromIntegral (abs dy) / fromIntegral dx :: Double
step (c@(cx :+ cy),err) | cx > x1 = Nothing
| newErr > 0.5 = Just (c , ((cx+1) :+ (cy+sy) , newErr - 1.0) )
| otherwise = Just (c , ((cx +1) :+ cy, newErr))
where
newErr = err + delta
in
start:unfoldr step (start,0.0)
let action | vertical = verticalLine mv (floor xa) (floor ya) (floor yb)
| horizontal = horizontalLine mv (floor ya) (floor xa) (floor xb)
| otherwise = mapM_ (plotPoint mv . reversal) $ line (reversal pa) (reversal pb)
action
return mvref
data PState = PState { currentColor :: !Color
, currentAlpha :: !Double
, currentPos :: !Point
, w :: !Int
, h :: !Int
, pixels :: forall s. ST s (STRef s (M.MVector s Word32))
}
data Pixmap a = Pixmap {getPixmap :: State (PState ) a}
instance Monad (Pixmap ) where
return = Pixmap . return
(Pixmap a) >>= f = Pixmap (a >>= getPixmap . f)
instance MonadState (PState ) (Pixmap ) where
get = Pixmap get
put s = Pixmap (put s)
instance Canvas (Pixmap ) where
moveTo p = modify $! \s ->
s {currentPos = p}
lineTo p = modify $! \s ->
let newPixel = pixels s >>= drawLine p s
in
s {currentPos = p, pixels = newPixel}
pixel p = modify $! \s ->
let newPixel = pixels s >>= ppixel p s
in
s {currentPos = p, pixels = newPixel}
setColor c a = modify $! \s ->
s {currentColor = c, currentAlpha = a}
drawPath = return ()
resetAlpha = modify $! \s ->
s {currentAlpha = 1.0}
runPixmap :: Int -> Int -> Pixmap () -> PDF (PDFReference RawImage)
runPixmap width height p =
let finalState = execState (getPixmap p) $ (PState (Rgb 0.0 0.0 0.0) 1.0 (0.0 :+ 0.0) width height startState)
startState :: ST s (STRef s (M.MVector s Word32))
startState = do
v <- M.replicate (width*height) 0x00FFFFFF
newSTRef v
result p = runST $ do
refmv <- pixels p
mv <- readSTRef refmv
U.freeze mv
in
createPDFRawImage (fromIntegral width) (fromIntegral height) False $ (result finalState)
segmentedDraw :: Canvas c
=> ((a,b) -> Point)
-> (a,b)
-> [(a,b)]
-> c ()
segmentedDraw pt h (n:l) = do
let (ha :+ hb) = pt h
(na :+ nb) = pt n
lineTo (na :+ hb)
lineTo (na :+ nb)
segmentedDraw pt n l
segmentedDraw _ h [] = return ()
getPath :: Canvas c
=> PlotStyle a b
-> ((a,b) -> Point)
-> [(a,b)]
-> c ()
getPath s pt l = do
moveTo (pt . head $ l)
if (interpolation s)
then do
mapM_ (lineTo . pt) (tail l)
else do
segmentedDraw pt (head l) (tail l)
simplifySignal:: (HasDoubleRepresentation a, Ord b)
=> ((a,b) -> Point)
-> ([(a,b)],SignalStyle)
-> ([(a,b)],SignalStyle)
simplifySignal pt (l,sty) = (simpl s0 v0 v0 (tail l), sty)
where
test (c,x) (a,y) =
let (ta :+ _) = pt (c,x)
(tb :+ _) = pt (a,y)
in
floor ta == floor tb
getNew l = let (s,v) = head l
in
(s, v)
(s0,v0) = getNew l
simpl !c !mi !ma [] = (c,mi):(c,ma):[]
simpl !c !mi !ma ((a,b):l) | test (c,mi) (a,b) = simpl c (min mi b) (max ma b) l
| otherwise =
if null l
then (c,mi):(c,ma):[]
else let (s,v) = getNew l
in
(c,mi):(c,ma):simpl s v v l
drawSignal :: (Canvas c, Show b, Show a)
=> PlotStyle a b
-> ((a,b) -> Point)
-> ([(a,b)],SignalStyle)
-> c ()
drawSignal s pt (l,signalstyle) = do
setColor (signalColor signalstyle) (signalOpacity signalstyle)
getPath s pt l
drawPath
resetAlpha
data Resource = R { plotPixmap :: Maybe (PDFReference RawImage)
, prologPixmap :: Maybe (PDFReference RawImage)
}
-- | A plot description is Displayable
instance (Show b, Show a, Ord a, Ord b, HasDoubleRepresentation a, HasDoubleRepresentation b) => Displayable (StyledSignal a b) Resource where
drawing (StyledSignal complex theTimes signals s) =
let notInf = not . isInfinite
(ta,tb) = maybe ( minimum . filter notInf . map toDouble $ theTimes
, maximum . filter notInf . map toDouble $ theTimes) id (horizontalBounds s)
(ya,yb) = maybe ( minimum . map (minimum . filter notInf . map toDouble ) $ signals
, maximum . map (maximum . filter notInf . map toDouble ) $ signals) id (verticalBounds s)
timed = map (zip theTimes)
action myRsrc wi hi = do
let width = fromIntegral wi
height = fromIntegral hi
imgw = floor $ width - leftMargin s - rightMargin s
imgh = floor $ height - leftMargin s - rightMargin s
tickSize = 6
tickLabelSep = 5
hUnitSep = 5
vUnitSep = 15
titleSep = 5
h a = (toDouble a - ta) / (tb - ta)*(width - leftMargin s - rightMargin s) + leftMargin s
v b = (toDouble b - ya) / (yb - ya)*(height - topMargin s - bottomMargin s) + bottomMargin s
pt (a,b) = (h a) :+ (v b)
iw a = fromDouble $ (toDouble a - leftMargin s) / rw * (tb - ta) + ta
where
rw = width - leftMargin s - rightMargin s
ih a = fromDouble $ (toDouble a - bottomMargin s) / rh * (yb - ya) + ya
where
rh = height - topMargin s - bottomMargin s
ippt (a :+ b) = (iw a,ih b)
drawVTick x y = do
let (a :+ b) = pt (x,y)
stroke $ Line (a - tickSize) b a b
drawStringLabel vTickStyle ((verticalTickRepresentation s) y)
(a - tickSize - tickLabelSep) b (leftMargin s) (bottomMargin s)
drawHTick y x = do
let (a :+ b) = pt (x,y)
stroke $ Line a b a (b - tickSize)
drawStringLabel hTickStyle ((horizontalTickRepresentation s) x)
a (b - tickSize - tickLabelSep) (leftMargin s) (bottomMargin s)
drawYAxis x = do
strokeColor black
let (sa :+ sb) = pt (x,ya)
(_ :+ eb) = pt (x,yb)
stroke $ Line sa sb sa eb
mapM_ (drawVTick x) (filter (\y -> y >= ya && y <= yb) $ (verticalTickValues s) ya yb)
drawXAxis y = do
strokeColor black
let (sa :+ sb) = pt (ta,y)
(ea :+ _) = pt (tb,y)
stroke $ Line sa sb ea sb
mapM_ (drawHTick y) (filter (\t -> t >= ta && t <= tb) $ (horizontalTickValues s) ta tb)
drawHLabel _ Nothing = return ()
drawHLabel y (Just label) = do
let b = v y
drawStringLabel hUnitStyle label (width - rightMargin s + hUnitSep) b (rightMargin s - hUnitSep) (bottomMargin s)
drawYLabel _ Nothing = return ()
drawYLabel x (Just label) = do
let a = h x
drawStringLabel vUnitStyle label a (height - topMargin s + vUnitSep) (leftMargin s) (topMargin s - vUnitSep)
withNewContext $ do
addShape $ Rectangle (leftMargin s :+ bottomMargin s) ((width - rightMargin s) :+ (height - topMargin s))
setAsClipPath
let R imgR roR = myRsrc
(prolog s) imgw imgh roR (pt,ippt)
case imgR of
Nothing -> mapM_ (drawSignal s pt) (zip (timed signals) (cycle $ signalStyles s))
(Just imgref) -> do
withNewContext $ do
applyMatrix $ translate (leftMargin s :+ bottomMargin s)
drawXObject imgref
if (axis s)
then do
let xaxis = if ta <=0 && tb >=0 then 0 else ta
yaxis = if ya <=0 && yb >=0 then 0 else ya
drawXAxis yaxis
drawYAxis xaxis
drawHLabel yaxis (horizontalLabel s)
drawYLabel xaxis (verticalLabel s)
else do
let xaxis = ta
yaxis = ya
drawXAxis yaxis
drawYAxis xaxis
drawHLabel yaxis (horizontalLabel s)
drawYLabel xaxis (verticalLabel s)
when (isJust (title s)) $ do
let t = fromJust (title s)
drawStringLabel titleStyle t (width / 2.0) (height - titleSep) width (topMargin s)
withNewContext $ do
addShape $ Rectangle (leftMargin s :+ bottomMargin s) ((width - rightMargin s) :+ (height - topMargin s))
setAsClipPath
(epilog s) imgw imgh (pt,ippt)
in
let rsrc wi hi | complex = do
let rw = fromIntegral wi - leftMargin s - rightMargin s
rh = fromIntegral hi - topMargin s - bottomMargin s
h a = (toDouble a - ta) / (tb - ta)*rw
v b = (toDouble b - ya) / (yb - ya)*rh
ppt (a,b) = h a :+ v b
iw a = fromDouble $ toDouble a / rw * (tb - ta) + ta
ih a = fromDouble $ toDouble a / rh * (yb - ya) + ya
ippt (a :+ b) = (iw a,ih b)
pr <- (prologRsrc s) (floor rw) (floor rh) (ppt,ippt)
r <- runPixmap (floor rw) (floor rh) $ do
mapM_ (drawSignal s ppt . simplifySignal ppt) $ (zip (timed signals) (cycle $ signalStyles s))
--setColor (Rgb 1.0 0 0) 1.0
--moveTo (0 :+ 0)
--lineTo (100 :+ 100)
--setColor (Rgb 0 0 1.0) 1.0
--moveTo (100 :+ 200)
--lineTo (200 :+ 200)
--moveTo (200 :+ 100)
--lineTo (100 :+ 100)
return $! R (Just $! r) $! pr
| otherwise = do
let rw = fromIntegral wi - leftMargin s - rightMargin s
rh = fromIntegral hi - topMargin s - bottomMargin s
h a = (toDouble a - ta) / (tb - ta)*rw
v b = (toDouble b - ya) / (yb - ya)*rh
ppt (a,b) = h a :+ v b
iw a = fromDouble $ toDouble a / rw * (tb - ta) + ta
ih a = fromDouble $ toDouble a / rh * (yb - ya) + ya
ippt (a :+ b) = (iw a,ih b)
pr <- (prologRsrc s) (floor rw) (floor rh) (ppt,ippt)
return (R Nothing $! pr)
in
(rsrc, action)