hledger/Commands/Histogram.hs

49 lines
1.5 KiB
Haskell
Raw Normal View History

{-|
Print a histogram report.
-}
2009-06-02 22:29:01 +04:00
module Commands.Histogram
where
import Prelude hiding (putStr)
import Ledger
import Options
import System.IO.UTF8
barchar = '*'
-- | Print a histogram of some statistic per reporting interval, such as
-- number of transactions per day.
histogram :: [Opt] -> [String] -> Ledger -> IO ()
2009-09-22 19:56:59 +04:00
histogram opts args = putStr . showHistogram opts args
2009-04-04 23:13:53 +04:00
showHistogram :: [Opt] -> [String] -> Ledger -> String
showHistogram opts args l = concatMap (printDayWith countBar) daytxns
where
i = intervalFromOpts opts
interval | i == NoInterval = Daily
| otherwise = i
fullspan = rawLedgerDateSpan $ rawledger l
days = filter (DateSpan Nothing Nothing /=) $ splitSpan interval fullspan
daytxns = [(s, filter (isTransactionInDateSpan s) ts) | s <- days]
2009-06-02 22:29:01 +04:00
-- same as Register
ts = sortBy (comparing tdate) $ filterempties $ filter matchapats $ filterdepth $ ledgerTransactions l
filterempties
| Empty `elem` opts = id
| otherwise = filter (not . isZeroMixedAmount . tamount)
2009-09-22 19:56:59 +04:00
matchapats = matchpats apats . taccount
(apats,_) = parsePatternArgs args
2009-09-22 15:55:11 +04:00
filterdepth | interval == NoInterval = filter (\t -> accountNameLevel (taccount t) <= depth)
| otherwise = id
depth = depthFromOpts opts
2009-04-04 23:13:53 +04:00
printDayWith f (DateSpan b _, ts) = printf "%s %s\n" (show $ fromJust b) (f ts)
countBar ts = replicate (length ts) barchar
2009-09-22 19:56:59 +04:00
total = show . sumTransactions
-- totalBar ts = replicate (sumTransactions ts) barchar