/
Plot.hs
66 lines (54 loc) · 2.09 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
-- |
-- Module : Data.ZoomCache.Gnuplot
-- Copyright : Alex Lang
-- License : BSD3-style (see LICENSE)
--
-- Maintainer : Alex Lang <me@alang.ca>
-- Stability : unstable
-- Portability : unknown
--
-- Plotting zoom-cache files with gnuplot
----------------------------------------------------------------------
import qualified Data.Iteratee as I
import Data.ZoomCache.Common
import Graphics.Gnuplot.Simple
plot :: FilePath -> TrackNo -> Int -> IO ()
plot fp tn lvl = do
streams <- getStreams fp tn
let candles = map getSummaryCandleVals $ mayMaybe maybeSummaryLevel streams
plotListStyle [] (defaultStyle{plotType = CandleSticks}) candles
zoomEither :: (Stream a -> b) -> FilePath -> TrackNo -> IO b
zoomEither fun fp tn = do
cf <- getCacheFile fp
let t = getTrackType tn cf
case t of
Just ZDouble -> I.fileDriverRandom
(mapTrack tn (fun :: Stream Double -> IO ()))
fp
Just ZInt -> I.fileDriverRandom
(mapTrack tn (fun :: Stream Int -> IO ()))
fp
Nothing -> fail "Invalid Track"
mapTrack :: (Functor m, MonadIO m, ZReadable a)
=> TrackNo -> (Stream a -> )
-> Iteratee [Word8] m ()
mapTrack n = I.joinI . (enumStreamFromCF n) . I.mapChunks
listStreams :: C a => FilePath -> TrackNo -> IO [Stream a]
listStreams fp tn =
I.fileDriverRandom (I.joinI $ enumStreamFromCF tn getChunks) fp
-- As things stand, we are doing too much processing after running the
-- iteratee. Most of it can be moved before.
maybeSummaryLevel :: Int -> Stream a -> Maybe (Summary a)
maybeSummaryLevel _ (StreamPacket _ _ _) = Nothing
maybeSummaryLevel lvl (StreamSummary file tn sum) =
case summaryLevel sum of
lvl -> Just sum
_ -> Nothing
maybeSummaryLevel _ StreamNull = Nothing
getSummaryCandleVals :: Summary a -> (TimeStamp, (a, a, a, a))
getSummaryCandleVals s = ( summaryCloseTime s
, ( summaryOpen s
, summaryMin s
, summaryMax s
, summaryClose s
))