From 2233c799b1a9c6d9862ea285e19beae34ce441dc Mon Sep 17 00:00:00 2001 From: Alex Lang Date: Tue, 18 Oct 2011 18:18:38 +0900 Subject: [PATCH] defined to-be-exported plot function and added module comments --- Data/ZoomCache/Plot.hs | 61 +++++++++++++++++++++++++++++++++++------- 1 file changed, 51 insertions(+), 10 deletions(-) diff --git a/Data/ZoomCache/Plot.hs b/Data/ZoomCache/Plot.hs index e592962..766ec70 100644 --- a/Data/ZoomCache/Plot.hs +++ b/Data/ZoomCache/Plot.hs @@ -1,25 +1,66 @@ -import Data.ZoomCache +-- | +-- Module : Data.ZoomCache.Gnuplot +-- Copyright : Alex Lang +-- License : BSD3-style (see LICENSE) +-- +-- Maintainer : Alex Lang +-- Stability : unstable +-- Portability : unknown +-- +-- Plotting zoom-cache files with gnuplot +---------------------------------------------------------------------- -plot :: FilePath -> TrackNo -> Int -> IO () -plot = undefined +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 + 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 () + => TrackNo -> (Stream a -> ) + -> Iteratee [Word8] m () mapTrack n = I.joinI . (enumStreamFromCF n) . I.mapChunks --- I think adopting this order of arguments makes more sense -zoomListPlotLevel :: C a => FilePath -> TrackNo -> Int -> IO [Stream a] -zoomListPlotLevel fp tn lvl = + + +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 + ))