{-# LANGUAGE CPP #-}
{-# LANGUAGE OverloadedStrings #-}
module Hledger.Write.Chart.Text (
TextChartOpts(..),
Style(..),
ColourMode(..),
StyledLine,
chartAsStyledLines,
styledLinesAsText,
tests_Hledger_Write_Chart_Text,
) where
import Data.List (find, nub, partition, sortOn)
#if !MIN_VERSION_base(4,20,0)
import Data.List (foldl')
#endif
import Control.Monad (join, mfilter)
import Data.Maybe (fromMaybe, isJust, isNothing)
import Data.Text (Text)
import Data.Text qualified as T
import Data.Text.Lazy qualified as TL
import Safe (atMay, maximumDef)
import Data.Colour.SRGB (sRGB24read)
import System.Console.ANSI (Color(..), ColorIntensity(..), ConsoleLayer(..), SGR(..), setSGRCode)
import Text.DocLayout (realLength)
import Hledger.Data.Amount (nullamt, oneLineNoCostFmt, showAmountWith)
import Hledger.Data.Dates (showDate)
import Hledger.Data.Types (AccountType(..), Amount(..), Quantity)
import Hledger.Utils.IO (color, faint')
import Hledger.Utils.Test
import Hledger.Utils.Text (fitText)
import Hledger.Write.Chart
import Hledger.Write.Chart.Palette (colourHex)
data TextChartOpts = TextChartOpts
{ TextChartOpts -> Int
tcWidth :: Int
, TextChartOpts -> Bool
tcUnicode :: Bool
}
data Style
= StylePlain
| StyleSeries Int
| StyleType AccountType Int
| StyleOther (Maybe AccountType)
| StyleLone
| StyleNegative
deriving (Int -> Style -> ShowS
[Style] -> ShowS
Style -> String
(Int -> Style -> ShowS)
-> (Style -> String) -> ([Style] -> ShowS) -> Show Style
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Style -> ShowS
showsPrec :: Int -> Style -> ShowS
$cshow :: Style -> String
show :: Style -> String
$cshowList :: [Style] -> ShowS
showList :: [Style] -> ShowS
Show, Style -> Style -> Bool
(Style -> Style -> Bool) -> (Style -> Style -> Bool) -> Eq Style
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Style -> Style -> Bool
== :: Style -> Style -> Bool
$c/= :: Style -> Style -> Bool
/= :: Style -> Style -> Bool
Eq)
type StyledLine = [(Style, Text)]
data ColourMode
= NoColour
| BasicColour
| RgbColour Bool
deriving (Int -> ColourMode -> ShowS
[ColourMode] -> ShowS
ColourMode -> String
(Int -> ColourMode -> ShowS)
-> (ColourMode -> String)
-> ([ColourMode] -> ShowS)
-> Show ColourMode
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ColourMode -> ShowS
showsPrec :: Int -> ColourMode -> ShowS
$cshow :: ColourMode -> String
show :: ColourMode -> String
$cshowList :: [ColourMode] -> ShowS
showList :: [ColourMode] -> ShowS
Show, ColourMode -> ColourMode -> Bool
(ColourMode -> ColourMode -> Bool)
-> (ColourMode -> ColourMode -> Bool) -> Eq ColourMode
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: ColourMode -> ColourMode -> Bool
== :: ColourMode -> ColourMode -> Bool
$c/= :: ColourMode -> ColourMode -> Bool
/= :: ColourMode -> ColourMode -> Bool
Eq)
styledLinesAsText :: ColourMode -> [StyledLine] -> TL.Text
styledLinesAsText :: ColourMode -> [StyledLine] -> Text
styledLinesAsText ColourMode
mode = [Text] -> Text
TL.fromChunks ([Text] -> Text)
-> ([StyledLine] -> [Text]) -> [StyledLine] -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (StyledLine -> [Text]) -> [StyledLine] -> [Text]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (\StyledLine
l -> ((Style, Text) -> Text) -> StyledLine -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map (Style, Text) -> Text
piece StyledLine
l [Text] -> [Text] -> [Text]
forall a. [a] -> [a] -> [a]
++ [Text
"\n"])
where
piece :: (Style, Text) -> Text
piece (Style
st, Text
t) = case ColourMode
mode of
ColourMode
NoColour -> Text
t
ColourMode
BasicColour -> case Style
st of
Style
StylePlain -> Text
t
StyleSeries Int
n -> String -> Text
T.pack (String -> Text) -> String -> Text
forall a b. (a -> b) -> a -> b
$ (ColorIntensity -> Color -> ShowS)
-> (ColorIntensity, Color) -> ShowS
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry ColorIntensity -> Color -> ShowS
color ((ColorIntensity, Color)
-> [(ColorIntensity, Color)] -> Int -> (ColorIntensity, Color)
forall a. a -> [a] -> Int -> a
cyclicAt (ColorIntensity
Dull, Color
Blue) [(ColorIntensity, Color)]
indexColours Int
n) ShowS -> ShowS
forall a b. (a -> b) -> a -> b
$ Text -> String
T.unpack Text
t
StyleType AccountType
at Int
n -> String -> Text
T.pack (String -> Text) -> String -> Text
forall a b. (a -> b) -> a -> b
$ ColorIntensity -> Color -> ShowS
color (if Int -> Bool
forall a. Integral a => a -> Bool
even Int
n then ColorIntensity
Dull else ColorIntensity
Vivid) (AccountType -> Color
typeColour AccountType
at) ShowS -> ShowS
forall a b. (a -> b) -> a -> b
$ Text -> String
T.unpack Text
t
StyleOther Maybe AccountType
_ -> String -> Text
T.pack (String -> Text) -> String -> Text
forall a b. (a -> b) -> a -> b
$ ShowS
faint' ShowS -> ShowS
forall a b. (a -> b) -> a -> b
$ Text -> String
T.unpack Text
t
Style
StyleLone -> String -> Text
T.pack (String -> Text) -> String -> Text
forall a b. (a -> b) -> a -> b
$ ColorIntensity -> Color -> ShowS
color ColorIntensity
Dull Color
Cyan ShowS -> ShowS
forall a b. (a -> b) -> a -> b
$ Text -> String
T.unpack Text
t
Style
StyleNegative -> Text -> Text
negative Text
t
RgbColour Bool
dark -> case Style
st of
Style
StylePlain -> Text
t
StyleSeries Int
n -> Text -> Text -> Text
rgb (Bool -> Colour -> Text
colourHex Bool
dark (Colour -> Text) -> Colour -> Text
forall a b. (a -> b) -> a -> b
$ Int -> Colour
IndexColour Int
n) Text
t
StyleType AccountType
at Int
n -> Text -> Text -> Text
rgb (Bool -> Colour -> Text
colourHex Bool
dark (Colour -> Text) -> Colour -> Text
forall a b. (a -> b) -> a -> b
$ AccountType -> Int -> Colour
TypeColour AccountType
at Int
n) Text
t
StyleOther Maybe AccountType
mt -> Text -> Text -> Text
rgb (Bool -> Colour -> Text
colourHex Bool
dark (Colour -> Text) -> Colour -> Text
forall a b. (a -> b) -> a -> b
$ Maybe AccountType -> Colour
OtherColour Maybe AccountType
mt) Text
t
Style
StyleLone -> Text -> Text -> Text
rgb (Bool -> Colour -> Text
colourHex Bool
dark Colour
LoneColour) Text
t
Style
StyleNegative -> Text -> Text
negative Text
t
negative :: Text -> Text
negative Text
t = String -> Text
T.pack (String -> Text) -> String -> Text
forall a b. (a -> b) -> a -> b
$ ColorIntensity -> Color -> ShowS
color ColorIntensity
Dull Color
Red ShowS -> ShowS
forall a b. (a -> b) -> a -> b
$ Text -> String
T.unpack Text
t
rgb :: Text -> Text -> Text
rgb Text
hex Text
t = String -> Text
T.pack ([SGR] -> String
setSGRCode [ConsoleLayer -> Colour Float -> SGR
SetRGBColor ConsoleLayer
Foreground (Colour Float -> SGR) -> Colour Float -> SGR
forall a b. (a -> b) -> a -> b
$ String -> Colour Float
forall b. (Ord b, Floating b) => String -> Colour b
sRGB24read (String -> Colour Float) -> String -> Colour Float
forall a b. (a -> b) -> a -> b
$ Text -> String
T.unpack Text
hex]) Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
t Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> String -> Text
T.pack ([SGR] -> String
setSGRCode [])
typeColour :: AccountType -> Color
typeColour AccountType
at = case AccountType
at of
AccountType
Asset -> Color
Blue
AccountType
Liability -> Color
Yellow
AccountType
Equity -> Color
Magenta
AccountType
Revenue -> Color
Green
AccountType
Expense -> Color
Red
AccountType
_ -> Color
Magenta
indexColours :: [(ColorIntensity, Color)]
indexColours =
[(ColorIntensity
Dull, Color
Blue), (ColorIntensity
Dull, Color
Yellow), (ColorIntensity
Dull, Color
Magenta), (ColorIntensity
Dull, Color
Cyan), (ColorIntensity
Dull, Color
Green), (ColorIntensity
Vivid, Color
Blue), (ColorIntensity
Vivid, Color
Yellow), (ColorIntensity
Vivid, Color
Magenta)]
chartAsStyledLines :: TextChartOpts -> Chart -> [StyledLine]
chartAsStyledLines :: TextChartOpts -> Chart -> [StyledLine]
chartAsStyledLines TextChartOpts
opts c :: Chart
c@Chart{Text
chTitle :: Text
chTitle :: Chart -> Text
chTitle, XAxis
chXAxis :: XAxis
chXAxis :: Chart -> XAxis
chXAxis, [Panel]
chPanels :: [Panel]
chPanels :: Chart -> [Panel]
chPanels, [Text]
chNotes :: [Text]
chNotes :: Chart -> [Text]
chNotes} =
[StyledLine]
titlelines [StyledLine] -> [StyledLine] -> [StyledLine]
forall a. [a] -> [a] -> [a]
++ [[(Style
StylePlain, Text
"No data")] | [Panel] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Panel]
chPanels] [StyledLine] -> [StyledLine] -> [StyledLine]
forall a. [a] -> [a] -> [a]
++ ((Int, Panel) -> [StyledLine]) -> [(Int, Panel)] -> [StyledLine]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (\(Int
i, Panel
p) -> [[] | Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
0] [StyledLine] -> [StyledLine] -> [StyledLine]
forall a. [a] -> [a] -> [a]
++ TextChartOpts
-> XAxis -> Colouring -> Bool -> Panel -> [StyledLine]
panelLines TextChartOpts
opts XAxis
chXAxis (Chart -> Colouring
chartColouring Chart
c) ([Panel] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Panel]
chPanels Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
1) Panel
p) ([Int] -> [Panel] -> [(Int, Panel)]
forall a b. [a] -> [b] -> [(a, b)]
zip [Int
0 :: Int ..] [Panel]
chPanels)
[StyledLine] -> [StyledLine] -> [StyledLine]
forall a. [a] -> [a] -> [a]
++ [StyledLine]
notelines
where
titlelines :: [StyledLine]
titlelines = if Text -> Bool
T.null Text
chTitle then [] else [[(Style
StylePlain, Text
chTitle)], []]
notelines :: [StyledLine]
notelines = if [Text] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Text]
chNotes then [] else [] StyledLine -> [StyledLine] -> [StyledLine]
forall a. a -> [a] -> [a]
: [[(Style
StylePlain, Text
n)] | Text
n <- [Text]
chNotes]
data Row = Row
{ Row -> Text
rowLabel :: Text
, Row -> Text
rowSubLabel :: Text
, Row -> Maybe Quantity
rowValue :: Maybe Quantity
, Row -> [(Style, Char, Quantity)]
rowSegments :: [(Style, Char, Quantity)]
, Row -> Maybe Quantity
rowMark :: Maybe Quantity
, Row -> (Maybe Quantity, Maybe Quantity)
rowSides :: (Maybe Quantity, Maybe Quantity)
, Row -> Maybe Text
rowNote :: Maybe Text
}
panelLines :: TextChartOpts -> XAxis -> Colouring -> Bool -> Panel -> [StyledLine]
panelLines :: TextChartOpts
-> XAxis -> Colouring -> Bool -> Panel -> [StyledLine]
panelLines TextChartOpts{Int
tcWidth :: TextChartOpts -> Int
tcWidth :: Int
tcWidth, Bool
tcUnicode :: TextChartOpts -> Bool
tcUnicode :: Bool
tcUnicode} XAxis
xaxis Colouring
colouring Bool
showheading Panel{Amount
pnUnit :: Amount
pnUnit :: Panel -> Amount
pnUnit, [Series]
pnSeries :: [Series]
pnSeries :: Panel -> [Series]
pnSeries, [Text]
pnGroupNames :: [Text]
pnGroupNames :: Panel -> [Text]
pnGroupNames, Maybe Quantity
pnScale :: Maybe Quantity
pnScale :: Panel -> Maybe Quantity
pnScale, BarLayout
pnLayout :: BarLayout
pnLayout :: Panel -> BarLayout
pnLayout, Bool
pnCounts :: Bool
pnCounts :: Panel -> Bool
pnCounts, [Summary]
pnSummary :: [Summary]
pnSummary :: Panel -> [Summary]
pnSummary, [Maybe Text]
pnValueLabels :: Panel -> [Maybe Text]
pnValueLabels :: [Maybe Text]
pnValueLabels} =
[[(Style
StylePlain, Amount -> Text
acommodity Amount
pnUnit)] | Bool
showheading, Bool -> Bool
not (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b
$ Text -> Bool
T.null (Text -> Bool) -> Text -> Bool
forall a b. (a -> b) -> a -> b
$ Amount -> Text
acommodity Amount
pnUnit]
[StyledLine] -> [StyledLine] -> [StyledLine]
forall a. [a] -> [a] -> [a]
++ [StyledLine]
legend
[StyledLine] -> [StyledLine] -> [StyledLine]
forall a. [a] -> [a] -> [a]
++ [StyledLine -> StyledLine
trimEnd StyledLine
headerline | Bool
diverging, Bool -> Bool
not (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b
$ (Text -> Bool) -> [Text] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all Text -> Bool
T.null [Text
posname, Text
negname, Text
netname]]
[StyledLine] -> [StyledLine] -> [StyledLine]
forall a. [a] -> [a] -> [a]
++ (StyledLine -> StyledLine) -> [StyledLine] -> [StyledLine]
forall a b. (a -> b) -> [a] -> [b]
map StyledLine -> StyledLine
trimEnd [StyledLine]
rowlines
[StyledLine] -> [StyledLine] -> [StyledLine]
forall a. [a] -> [a] -> [a]
++ (if [Summary] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Summary]
pnSummary then [] else [[], [(Style
StylePlain, Text
summaryline)]])
where
summaryline :: Text
summaryline = Text -> [Text] -> Text
T.intercalate Text
", " [Summary -> Text
suLabel Summary
su Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> String -> Text
T.pack (AmountFormat -> Amount -> String
showAmountWith AmountFormat
oneLineNoCostFmt (Amount -> String) -> Amount -> String
forall a b. (a -> b) -> a -> b
$ Summary -> Amount
suValue Summary
su) | Summary
su <- [Summary]
pnSummary]
drawn :: [Series]
drawn = [Series
s | Series
s <- [Series]
pnSeries, Series -> SeriesRole
seRole Series
s SeriesRole -> SeriesRole -> Bool
forall a. Eq a => a -> a -> Bool
/= SeriesRole
SeriesTotal, Series -> Mark
seMark Series
s Mark -> [Mark] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`notElem` [Mark
MarkMarker, Mark
MarkNone]]
mtotal :: Maybe Series
mtotal = (Series -> Bool) -> [Series] -> Maybe Series
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Maybe a
find ((SeriesRole -> SeriesRole -> Bool
forall a. Eq a => a -> a -> Bool
== SeriesRole
SeriesTotal) (SeriesRole -> Bool) -> (Series -> SeriesRole) -> Series -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Series -> SeriesRole
seRole) [Series]
pnSeries
single :: Bool
single = [Series] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Series]
drawn Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
1
styled :: [(Style, Char, SeriesValues)]
styled = (Int -> Colour -> Series -> (Style, Char, SeriesValues))
-> [Int] -> [Colour] -> [Series] -> [(Style, Char, SeriesValues)]
forall a b c d. (a -> b -> c -> d) -> [a] -> [b] -> [c] -> [d]
zipWith3 Int -> Colour -> Series -> (Style, Char, SeriesValues)
seriesStyle [Int -> Maybe Int -> Int
forall a. a -> Maybe a -> a
fromMaybe Int
0 (Maybe Int -> Int) -> Maybe Int -> Int
forall a b. (a -> b) -> a -> b
$ Int -> [(Int, Int)] -> Maybe Int
forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup Int
i [(Int, Int)]
fillslots | Int
i <- [Int
0 .. [Series] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Series]
drawn Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1]] (Colouring -> [Series] -> [Colour]
seriesColours Colouring
colouring [Series]
drawn) [Series]
drawn
fillslots :: [(Int, Int)]
fillslots = [[(Int, Int)]] -> [(Int, Int)]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat
[ if [(Int, Series)] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [(Int, Series)]
bar Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
1 then [(Int
i, Int
0) | (Int
i, Series
_) <- [(Int, Series)]
bar] else (Int -> Int) -> [(Int, Series)] -> [(Int, Int)]
forall {b} {a}. (Int -> b) -> [(a, Series)] -> [(a, b)]
slotsfor Int -> Int
forall a. a -> a
id [(Int, Series)]
items [(Int, Int)] -> [(Int, Int)] -> [(Int, Int)]
forall a. [a] -> [a] -> [a]
++ (Int -> Int) -> [(Int, Series)] -> [(Int, Int)]
forall {b} {a}. (Int -> b) -> [(a, Series)] -> [(a, b)]
slotsfor (\Int
k -> String -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length String
fillchars Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1 Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
k) [(Int, Series)]
others
| [(Int, Series)]
bar <- [[(Int, Series)]]
bars, let ([(Int, Series)]
items, [(Int, Series)]
others) = ((Int, Series) -> Bool)
-> [(Int, Series)] -> ([(Int, Series)], [(Int, Series)])
forall a. (a -> Bool) -> [a] -> ([a], [a])
partition ((SeriesRole -> SeriesRole -> Bool
forall a. Eq a => a -> a -> Bool
== SeriesRole
SeriesItem) (SeriesRole -> Bool)
-> ((Int, Series) -> SeriesRole) -> (Int, Series) -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Series -> SeriesRole
seRole (Series -> SeriesRole)
-> ((Int, Series) -> Series) -> (Int, Series) -> SeriesRole
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Int, Series) -> Series
forall a b. (a, b) -> b
snd) [(Int, Series)]
bar ]
where
bars :: [[(Int, Series)]]
bars = if Bool
grouped then [[(Int
i, Series
s) | (Int
i, Series
s) <- [(Int, Series)]
indexed, Series -> Int
seGroup Series
s Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
g] | Int
g <- [Int] -> [Int]
forall a. Eq a => [a] -> [a]
nub ([Int] -> [Int]) -> [Int] -> [Int]
forall a b. (a -> b) -> a -> b
$ ((Int, Series) -> Int) -> [(Int, Series)] -> [Int]
forall a b. (a -> b) -> [a] -> [b]
map (Series -> Int
seGroup (Series -> Int)
-> ((Int, Series) -> Series) -> (Int, Series) -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Int, Series) -> Series
forall a b. (a, b) -> b
snd) [(Int, Series)]
indexed] else [[(Int, Series)]
indexed]
indexed :: [(Int, Series)]
indexed = [Int] -> [Series] -> [(Int, Series)]
forall a b. [a] -> [b] -> [(a, b)]
zip [Int
0 :: Int ..] [Series]
drawn
slotsfor :: (Int -> b) -> [(a, Series)] -> [(a, b)]
slotsfor Int -> b
f [(a, Series)]
ss = [a] -> [b] -> [(a, b)]
forall a b. [a] -> [b] -> [(a, b)]
zip (((a, Series) -> a) -> [(a, Series)] -> [a]
forall a b. (a -> b) -> [a] -> [b]
map (a, Series) -> a
forall a b. (a, b) -> a
fst [(a, Series)]
ss) ([b] -> [(a, b)]) -> [b] -> [(a, b)]
forall a b. (a -> b) -> a -> b
$ (Int -> b) -> [Int] -> [b]
forall a b. (a -> b) -> [a] -> [b]
map Int -> b
f ([Int] -> [b]) -> [Int] -> [b]
forall a b. (a -> b) -> a -> b
$ Double -> Int -> [(Text, Quantity)] -> [Int]
forall a. Ord a => Double -> Int -> [(Text, a)] -> [Int]
stableSlotsFavouringFirst Double
0.5 (String -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length String
fillchars) [(ChartLabel -> Text
lbFull (ChartLabel -> Text) -> ChartLabel -> Text
forall a b. (a -> b) -> a -> b
$ Series -> ChartLabel
seLabel Series
s, Series -> Quantity
seriesSize Series
s) | (a
_, Series
s) <- [(a, Series)]
ss]
styledseries :: [((Style, Char, SeriesValues), Series)]
styledseries = [(Style, Char, SeriesValues)]
-> [Series] -> [((Style, Char, SeriesValues), Series)]
forall a b. [a] -> [b] -> [(a, b)]
zip [(Style, Char, SeriesValues)]
styled [Series]
drawn
groups :: [Int]
groups = [Int] -> [Int]
forall a. Eq a => [a] -> [a]
nub ([Int] -> [Int]) -> [Int] -> [Int]
forall a b. (a -> b) -> a -> b
$ (Series -> Int) -> [Series] -> [Int]
forall a b. (a -> b) -> [a] -> [b]
map Series -> Int
seGroup [Series]
drawn
groupName :: Int -> Text
groupName Int
k = Text -> Maybe Text -> Text
forall a. a -> Maybe a -> a
fromMaybe Text
"" (Maybe Text -> Text) -> Maybe Text -> Text
forall a b. (a -> b) -> a -> b
$ [Text]
pnGroupNames [Text] -> Int -> Maybe Text
forall a. [a] -> Int -> Maybe a
`atMay` Int
k
groupSign :: Int -> Int
groupSign Int
k = Int -> Maybe Int -> Int
forall a. a -> Maybe a -> a
fromMaybe Int
1 (Maybe Int -> Int) -> Maybe Int -> Int
forall a b. (a -> b) -> a -> b
$ (Int -> Bool) -> [Int] -> Maybe Int
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Maybe a
find (Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= Int
0) [Series -> Int
seSign Series
s | Series
s <- [Series]
drawn, Series -> Int
seGroup Series
s Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
k]
grouped :: Bool
grouped = BarLayout
pnLayout BarLayout -> BarLayout -> Bool
forall a. Eq a => a -> a -> Bool
== BarLayout
Grouped Bool -> Bool -> Bool
&& [Text] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Text]
pnGroupNames Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
1
diverging :: Bool
diverging = Bool -> Bool
not Bool
grouped Bool -> Bool -> Bool
&& Maybe Series -> Bool
forall a. Maybe a -> Bool
isJust Maybe Series
mtotal Bool -> Bool -> Bool
&& (Series -> Bool) -> [Series] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any ((Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= Int
0) (Int -> Bool) -> (Series -> Int) -> Series -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Series -> Int
seSign) [Series]
drawn
marktotal :: Maybe Series
marktotal = (Series -> Bool) -> Maybe Series -> Maybe Series
forall (m :: * -> *) a. MonadPlus m => (a -> Bool) -> m a -> m a
mfilter ((Mark -> Mark -> Bool
forall a. Eq a => a -> a -> Bool
/= Mark
MarkNone) (Mark -> Bool) -> (Series -> Mark) -> Series -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Series -> Mark
seMark) Maybe Series
mtotal
marked :: Bool
marked = Maybe Series -> Bool
forall a. Maybe a -> Bool
isJust Maybe Series
marktotal
seriesStyle :: Int -> Colour -> Series -> (Style, Char, SeriesValues)
seriesStyle Int
i Colour
colour Series
s = (Colour -> Style
colourStyle Colour
colour, Int -> Char
fillChar Int
i, Series -> SeriesValues
seriesValues Series
s)
colourStyle :: Colour -> Style
colourStyle Colour
colour = case Colour
colour of
TypeColour AccountType
at Int
k -> AccountType -> Int -> Style
StyleType AccountType
at Int
k
IndexColour Int
k -> Int -> Style
StyleSeries Int
k
Colour
LoneColour -> Style
StyleLone
Colour
NeutralColour -> Style
StylePlain
OtherColour Maybe AccountType
mt -> Maybe AccountType -> Style
StyleOther Maybe AccountType
mt
Colour
TotalColour -> Style
StylePlain
categoryStyle :: Int -> Style -> Style
categoryStyle Int
j Style
st = case (XAxis
xaxis, Bool
single) of
(XCategories [ChartLabel]
ls, Bool
True) | Just ChartLabel
l <- [ChartLabel]
ls [ChartLabel] -> Int -> Maybe ChartLabel
forall a. [a] -> Int -> Maybe a
`atMay` Int
j, Just AccountType
at <- ChartLabel -> Maybe AccountType
lbType ChartLabel
l -> AccountType -> Int -> Style
StyleType (AccountType -> AccountType
mainAccountType AccountType
at) (Int -> Maybe Int -> Int
forall a. a -> Maybe a -> a
fromMaybe Int
0 (Maybe Int -> Int) -> Maybe Int -> Int
forall a b. (a -> b) -> a -> b
$ [Int]
categorytints [Int] -> Int -> Maybe Int
forall a. [a] -> Int -> Maybe a
`atMay` Int
j)
(XAxis, Bool)
_ -> Style
st
categorytints :: [Int]
categorytints = case (XAxis
xaxis, [Series]
drawn) of
(XCategories [ChartLabel]
ls, [Series
s]) -> [ChartLabel] -> [Maybe Quantity] -> [Int]
categoryTints [ChartLabel]
ls (Series -> [Maybe Quantity]
seValues Series
s)
(XAxis, [Series])
_ -> []
mtotalvals :: Maybe SeriesValues
mtotalvals = Series -> SeriesValues
seriesValues (Series -> SeriesValues) -> Maybe Series -> Maybe SeriesValues
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe Series
mtotal
fillchars :: String
fillchars = if Bool
tcUnicode then String
"█▓▒░" else String
"#=+:"
fillChar :: Int -> Char
fillChar = Char -> String -> Int -> Char
forall a. a -> [a] -> Int -> a
cyclicAt Char
' ' String
fillchars
xlabels :: [Text]
xlabels = case XAxis
xaxis of
XCategories [ChartLabel]
ls -> [Int -> Text -> Text
T.replicate (Int
2 Int -> Int -> Int
forall a. Num a => a -> a -> a
* ChartLabel -> Int
lbIndent ChartLabel
l) Text
" " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> ChartLabel -> Text
lbShort ChartLabel
l | ChartLabel
l <- [ChartLabel]
ls]
XPeriods [(DateSpan, Text)]
ps -> ((DateSpan, Text) -> Text) -> [(DateSpan, Text)] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map (DateSpan, Text) -> Text
forall a b. (a, b) -> b
snd [(DateSpan, Text)]
ps
XDates [Day]
ds -> (Day -> Text) -> [Day] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map Day -> Text
showDate [Day]
ds
segmentsAt :: Int
-> [((Style, Char, SeriesValues), Series)]
-> [(Style, Char, Quantity)]
segmentsAt Int
j [((Style, Char, SeriesValues), Series)]
sers = ((Bool, (Style, Char, Quantity)) -> (Style, Char, Quantity))
-> [(Bool, (Style, Char, Quantity))] -> [(Style, Char, Quantity)]
forall a b. (a -> b) -> [a] -> [b]
map (Bool, (Style, Char, Quantity)) -> (Style, Char, Quantity)
forall a b. (a, b) -> b
snd ([(Bool, (Style, Char, Quantity))] -> [(Style, Char, Quantity)])
-> [(Bool, (Style, Char, Quantity))] -> [(Style, Char, Quantity)]
forall a b. (a -> b) -> a -> b
$ ((Bool, (Style, Char, Quantity)) -> Bool)
-> [(Bool, (Style, Char, Quantity))]
-> [(Bool, (Style, Char, Quantity))]
forall b a. Ord b => (a -> b) -> [a] -> [a]
sortOn (Bool -> Bool
not (Bool -> Bool)
-> ((Bool, (Style, Char, Quantity)) -> Bool)
-> (Bool, (Style, Char, Quantity))
-> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Bool, (Style, Char, Quantity)) -> Bool
forall a b. (a, b) -> a
fst)
[ (Series -> Quantity -> Bool
isContra Series
s Quantity
q, (Int -> Style -> Style
categoryStyle Int
j Style
st, if Series -> Quantity -> Bool
isHatched Series
s Quantity
q then Char
contrachar else Char
c, Quantity
q))
| ((Style
st, Char
c, SeriesValues
vals), Series
s) <- [((Style, Char, SeriesValues), Series)]
sers, Just Quantity
q <- [SeriesValues -> Int -> Maybe Quantity
valueAt SeriesValues
vals Int
j], Quantity
q Quantity -> Quantity -> Bool
forall a. Eq a => a -> a -> Bool
/= Quantity
0 ]
contrachar :: Char
contrachar = if Bool
tcUnicode then Char
'╱' else Char
'/'
rows :: [Row]
rows
| Bool
grouped =
[[Row]] -> [Row]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat
[ [ Text
-> Text
-> Maybe Quantity
-> [(Style, Char, Quantity)]
-> Maybe Quantity
-> (Maybe Quantity, Maybe Quantity)
-> Maybe Text
-> Row
Row (if Int
i Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0 then Text
lbl else Text
"") (Int -> Text
groupName Int
k) (Int -> Int -> Maybe Quantity
groupTotal Int
j Int
k) (Int
-> [((Style, Char, SeriesValues), Series)]
-> [(Style, Char, Quantity)]
segmentsAt Int
j [((Style, Char, SeriesValues), Series)
ss | ss :: ((Style, Char, SeriesValues), Series)
ss@((Style, Char, SeriesValues)
_, Series
s) <- [((Style, Char, SeriesValues), Series)]
styledseries, Series -> Int
seGroup Series
s Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
k]) Maybe Quantity
forall a. Maybe a
Nothing (Maybe Quantity
forall a. Maybe a
Nothing, Maybe Quantity
forall a. Maybe a
Nothing) Maybe Text
forall a. Maybe a
Nothing
| (Int
i, Int
k) <- [Int] -> [Int] -> [(Int, Int)]
forall a b. [a] -> [b] -> [(a, b)]
zip [Int
0 :: Int ..] [Int]
groups ]
[Row] -> [Row] -> [Row]
forall a. [a] -> [a] -> [a]
++ [ Text
-> Text
-> Maybe Quantity
-> [(Style, Char, Quantity)]
-> Maybe Quantity
-> (Maybe Quantity, Maybe Quantity)
-> Maybe Text
-> Row
Row Text
"" (ChartLabel -> Text
lbShort (ChartLabel -> Text) -> ChartLabel -> Text
forall a b. (a -> b) -> a -> b
$ Series -> ChartLabel
seLabel Series
t) Maybe Quantity
tval [] (if Bool
marked then Maybe Quantity
tval else Maybe Quantity
forall a. Maybe a
Nothing) (Maybe Quantity
forall a. Maybe a
Nothing, Maybe Quantity
forall a. Maybe a
Nothing) Maybe Text
forall a. Maybe a
Nothing | Just Series
t <- [Maybe Series
mtotal], let tval :: Maybe Quantity
tval = SeriesValues -> Int -> Maybe Quantity
valueAt (Series -> SeriesValues
seriesValues Series
t) Int
j ]
| (Int
j, Text
lbl) <- [Int] -> [Text] -> [(Int, Text)]
forall a b. [a] -> [b] -> [(a, b)]
zip [Int
0 ..] [Text]
xlabels ]
| Bool
otherwise =
[ Text
-> Text
-> Maybe Quantity
-> [(Style, Char, Quantity)]
-> Maybe Quantity
-> (Maybe Quantity, Maybe Quantity)
-> Maybe Text
-> Row
Row Text
lbl Text
"" Maybe Quantity
rowval (Int
-> [((Style, Char, SeriesValues), Series)]
-> [(Style, Char, Quantity)]
segmentsAt Int
j [((Style, Char, SeriesValues), Series)]
styledseries) (if Bool
marked then Maybe Quantity
rowval else Maybe Quantity
forall a. Maybe a
Nothing) (Int -> Int -> Maybe Quantity
sideTotal Int
j Int
1, Int -> Int -> Maybe Quantity
sideTotal Int
j (-Int
1)) (Maybe (Maybe Text) -> Maybe Text
forall (m :: * -> *) a. Monad m => m (m a) -> m a
join (Maybe (Maybe Text) -> Maybe Text)
-> Maybe (Maybe Text) -> Maybe Text
forall a b. (a -> b) -> a -> b
$ [Maybe Text]
pnValueLabels [Maybe Text] -> Int -> Maybe (Maybe Text)
forall a. [a] -> Int -> Maybe a
`atMay` Int
j)
| (Int
j, Text
lbl) <- [Int] -> [Text] -> [(Int, Text)]
forall a b. [a] -> [b] -> [(a, b)]
zip [Int
0 ..] [Text]
xlabels
, let rowval :: Maybe Quantity
rowval = case (Maybe SeriesValues
mtotalvals, [(Style, Char, SeriesValues)]
styled) of
(Just SeriesValues
tvals, [(Style, Char, SeriesValues)]
_) -> SeriesValues -> Int -> Maybe Quantity
valueAt SeriesValues
tvals Int
j
(Maybe SeriesValues
_, [(Style
_, Char
_, SeriesValues
vals)]) -> SeriesValues -> Int -> Maybe Quantity
valueAt SeriesValues
vals Int
j
(Maybe SeriesValues, [(Style, Char, SeriesValues)])
_ -> Maybe Quantity
forall a. Maybe a
Nothing
, Bool -> Bool
not (Bool
isCategories Bool -> Bool -> Bool
&& ((Style, Char, SeriesValues) -> Bool)
-> [(Style, Char, SeriesValues)] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (\(Style
_, Char
_, SeriesValues
vals) -> Maybe Quantity -> Bool
forall a. Maybe a -> Bool
isNothing (Maybe Quantity -> Bool) -> Maybe Quantity -> Bool
forall a b. (a -> b) -> a -> b
$ SeriesValues -> Int -> Maybe Quantity
valueAt SeriesValues
vals Int
j) [(Style, Char, SeriesValues)]
styled)
]
isCategories :: Bool
isCategories = case XAxis
xaxis of XCategories [ChartLabel]
_ -> Bool
True; XAxis
_ -> Bool
False
rowlines :: [StyledLine]
rowlines = (Row -> StyledLine) -> [Row] -> [StyledLine]
forall a b. (a -> b) -> [a] -> [b]
map Row -> StyledLine
rowLine [Row]
rows
drawnstyles :: [Style]
drawnstyles = [Style] -> [Style]
forall a. Eq a => [a] -> [a]
nub [Style
st | StyledLine
l <- [StyledLine]
rowlines, (Style
st, Text
t) <- StyledLine
l, Style
st Style -> Style -> Bool
forall a. Eq a => a -> a -> Bool
/= Style
StylePlain, Bool -> Bool
not (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b
$ Text -> Bool
T.null (Text -> Bool) -> Text -> Bool
forall a b. (a -> b) -> a -> b
$ Text -> Text
T.strip Text
t]
legend :: [StyledLine]
legend
| Bool -> [Series] -> Bool
legendListsSeries Bool
isCategories [Series]
drawn = [StyledLine] -> [StyledLine]
wrapLegend (
[ [(Style
st, Char -> Text
T.singleton (Char -> Text) -> Char -> Text
forall a b. (a -> b) -> a -> b
$ if Series -> Bool
hatchedSeries Series
s then Char
contrachar else Char
c), (Style
StylePlain, Text
" " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> ChartLabel -> Text
lbFull (Series -> ChartLabel
seLabel Series
s))]
| ((Style
st, Char
c, SeriesValues
_), Series
s) <- [(Style, Char, SeriesValues)]
-> [Series] -> [((Style, Char, SeriesValues), Series)]
forall a b. [a] -> [b] -> [(a, b)]
zip [(Style, Char, SeriesValues)]
styled [Series]
drawn, Style
st Style -> [Style] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Style]
drawnstyles ]
[StyledLine] -> [StyledLine] -> [StyledLine]
forall a. [a] -> [a] -> [a]
++ [ [(Style
StylePlain, Text
markchar), (Style
StylePlain, Text
" " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> ChartLabel -> Text
lbFull (Series -> ChartLabel
seLabel Series
t))] | Just Series
t <- [Maybe Series
marktotal] ]
[StyledLine] -> [StyledLine] -> [StyledLine]
forall a. [a] -> [a] -> [a]
++ [ [(Style
StylePlain, Char -> Text
T.singleton Char
contrachar), (Style
StylePlain, Text
" contra")]
| [Bool] -> Bool
forall (t :: * -> *). Foldable t => t Bool -> Bool
or [Series -> Quantity -> Bool
isContra Series
s Quantity
q | Series
s <- [Series]
drawn, Just Quantity
q <- Series -> [Maybe Quantity]
seValues Series
s] ]
) [StyledLine] -> [StyledLine] -> [StyledLine]
forall a. [a] -> [a] -> [a]
++ [[]]
| Bool
otherwise = []
wrapLegend :: [StyledLine] -> [StyledLine]
wrapLegend = [StyledLine] -> [StyledLine]
forall a. [a] -> [a]
reverse ([StyledLine] -> [StyledLine])
-> ([StyledLine] -> [StyledLine]) -> [StyledLine] -> [StyledLine]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ([StyledLine] -> StyledLine -> [StyledLine])
-> [StyledLine] -> [StyledLine] -> [StyledLine]
forall b a. (b -> a -> b) -> b -> [a] -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' [StyledLine] -> StyledLine -> [StyledLine]
addItem []
where
addItem :: [StyledLine] -> StyledLine -> [StyledLine]
addItem [] StyledLine
item = [StyledLine
item]
addItem (StyledLine
l:[StyledLine]
ls) StyledLine
item
| StyledLine -> Int
forall {a}. [(a, Text)] -> Int
lineWidth StyledLine
l Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
2 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ StyledLine -> Int
forall {a}. [(a, Text)] -> Int
lineWidth StyledLine
item Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
tcWidth = (StyledLine
l StyledLine -> StyledLine -> StyledLine
forall a. [a] -> [a] -> [a]
++ [(Style
StylePlain, Text
" ")] StyledLine -> StyledLine -> StyledLine
forall a. [a] -> [a] -> [a]
++ StyledLine
item) StyledLine -> [StyledLine] -> [StyledLine]
forall a. a -> [a] -> [a]
: [StyledLine]
ls
| Bool
otherwise = StyledLine
item StyledLine -> [StyledLine] -> [StyledLine]
forall a. a -> [a] -> [a]
: StyledLine
l StyledLine -> [StyledLine] -> [StyledLine]
forall a. a -> [a] -> [a]
: [StyledLine]
ls
lineWidth :: [(a, Text)] -> Int
lineWidth = [Int] -> Int
forall a. Num a => [a] -> a
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum ([Int] -> Int) -> ([(a, Text)] -> [Int]) -> [(a, Text)] -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((a, Text) -> Int) -> [(a, Text)] -> [Int]
forall a b. (a -> b) -> [a] -> [b]
map (Text -> Int
forall a. HasChars a => a -> Int
realLength (Text -> Int) -> ((a, Text) -> Text) -> (a, Text) -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a, Text) -> Text
forall a b. (a, b) -> b
snd)
showValue :: Quantity -> Text
showValue Quantity
q = String -> Text
T.pack (String -> Text) -> String -> Text
forall a b. (a -> b) -> a -> b
$ AmountFormat -> Amount -> String
showAmountWith AmountFormat
oneLineNoCostFmt Amount
pnUnit{aquantity = q}
labelw :: Int
labelw = Int -> Int -> Int
forall a. Ord a => a -> a -> a
min (Int
tcWidth Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` Int
3) (Int -> Int) -> Int -> Int
forall a b. (a -> b) -> a -> b
$ Int -> [Int] -> Int
forall a. Ord a => a -> [a] -> a
maximumDef Int
0 ([Int] -> Int) -> [Int] -> Int
forall a b. (a -> b) -> a -> b
$ (Row -> Int) -> [Row] -> [Int]
forall a b. (a -> b) -> [a] -> [b]
map (Text -> Int
forall a. HasChars a => a -> Int
realLength (Text -> Int) -> (Row -> Text) -> Row -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Row -> Text
rowLabel) [Row]
rows
sublabelw :: Int
sublabelw = Int -> Int -> Int
forall a. Ord a => a -> a -> a
min (Int
tcWidth Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` Int
4) (Int -> Int) -> Int -> Int
forall a b. (a -> b) -> a -> b
$ Int -> [Int] -> Int
forall a. Ord a => a -> [a] -> a
maximumDef Int
0 ([Int] -> Int) -> [Int] -> Int
forall a b. (a -> b) -> a -> b
$ (Row -> Int) -> [Row] -> [Int]
forall a b. (a -> b) -> [a] -> [b]
map (Text -> Int
forall a. HasChars a => a -> Int
realLength (Text -> Int) -> (Row -> Text) -> Row -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Row -> Text
rowSubLabel) [Row]
rows
valuew :: Int
valuew = Int -> [Int] -> Int
forall a. Ord a => a -> [a] -> a
maximumDef Int
0 [Text -> Int
forall a. HasChars a => a -> Int
realLength (Quantity -> Text
showValue Quantity
q) | Row{rowValue :: Row -> Maybe Quantity
rowValue = Just Quantity
q} <- [Row]
rows]
hasvalues :: Bool
hasvalues = (Row -> Bool) -> [Row] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any (Maybe Quantity -> Bool
forall a. Maybe a -> Bool
isJust (Maybe Quantity -> Bool) -> (Row -> Maybe Quantity) -> Row -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Row -> Maybe Quantity
rowValue) [Row]
rows
notew :: Int
notew = Int -> [Int] -> Int
forall a. Ord a => a -> [a] -> a
maximumDef Int
0 [Text -> Int
forall a. HasChars a => a -> Int
realLength Text
t | Row{rowNote :: Row -> Maybe Text
rowNote = Just Text
t} <- [Row]
rows]
notecolumn :: Text -> StyledLine
notecolumn Text
t = if Int
notew Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
0 then [(Style
StylePlain, Text
" "), (Text -> Style
noteStyle Text
t, Maybe Int -> Maybe Int -> Bool -> Bool -> Text -> Text
fitText (Int -> Maybe Int
forall a. a -> Maybe a
Just Int
notew) Maybe Int
forall a. Maybe a
Nothing Bool
False Bool
False Text
t)] else []
noteStyle :: Text -> Style
noteStyle Text
t
| Text
"-" Text -> Text -> Bool
`T.isPrefixOf` Text
t = Style
StyleNegative
| Text
"+" Text -> Text -> Bool
`T.isPrefixOf` Text
t = AccountType -> Int -> Style
StyleType AccountType
Revenue Int
0
| Bool
otherwise = Style
StylePlain
sideTotal :: Int -> Int -> Maybe Quantity
sideTotal Int
j Int
sign = Int -> Int -> [Series] -> Maybe Quantity
forall {a}. Integral a => a -> Int -> [Series] -> Maybe Quantity
signedTotal Int
sign Int
j [Series
s | Series
s <- [Series]
drawn, Series -> Int
seSign Series
s Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
sign]
groupTotal :: Int -> Int -> Maybe Quantity
groupTotal Int
j Int
k = Int -> Int -> [Series] -> Maybe Quantity
forall {a}. Integral a => a -> Int -> [Series] -> Maybe Quantity
signedTotal (Int -> Int
groupSign Int
k) Int
j [Series
s | Series
s <- [Series]
drawn, Series -> Int
seGroup Series
s Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
k]
signedTotal :: a -> Int -> [Series] -> Maybe Quantity
signedTotal a
sign Int
j [Series]
sers = case [Quantity
q | Series
s <- [Series]
sers, Just Quantity
q <- [SeriesValues -> Int -> Maybe Quantity
valueAt (Series -> SeriesValues
seriesValues Series
s) Int
j]] of
[] -> Maybe Quantity
forall a. Maybe a
Nothing
[Quantity]
qs -> Quantity -> Maybe Quantity
forall a. a -> Maybe a
Just (Quantity -> Maybe Quantity) -> Quantity -> Maybe Quantity
forall a b. (a -> b) -> a -> b
$ a -> Quantity
forall a b. (Integral a, Num b) => a -> b
fromIntegral a
sign Quantity -> Quantity -> Quantity
forall a. Num a => a -> a -> a
* [Quantity] -> Quantity
forall a. Num a => [a] -> a
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum [Quantity]
qs
posSum :: Row -> Maybe Quantity
posSum = (Maybe Quantity, Maybe Quantity) -> Maybe Quantity
forall a b. (a, b) -> a
fst ((Maybe Quantity, Maybe Quantity) -> Maybe Quantity)
-> (Row -> (Maybe Quantity, Maybe Quantity))
-> Row
-> Maybe Quantity
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Row -> (Maybe Quantity, Maybe Quantity)
rowSides
negSum :: Row -> Maybe Quantity
negSum = (Maybe Quantity, Maybe Quantity) -> Maybe Quantity
forall a b. (a, b) -> b
snd ((Maybe Quantity, Maybe Quantity) -> Maybe Quantity)
-> (Row -> (Maybe Quantity, Maybe Quantity))
-> Row
-> Maybe Quantity
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Row -> (Maybe Quantity, Maybe Quantity)
rowSides
negsumw :: Int
negsumw = if Bool
diverging then Int -> [Int] -> Int
forall a. Ord a => a -> [a] -> a
maximumDef Int
0 (Text -> Int
forall a. HasChars a => a -> Int
realLength Text
negname Int -> [Int] -> [Int]
forall a. a -> [a] -> [a]
: [Text -> Int
forall a. HasChars a => a -> Int
realLength (Quantity -> Text
showValue Quantity
q) | Just Quantity
q <- (Row -> Maybe Quantity) -> [Row] -> [Maybe Quantity]
forall a b. (a -> b) -> [a] -> [b]
map Row -> Maybe Quantity
negSum [Row]
rows]) else Int
0
possumw :: Int
possumw = if Bool
diverging then Int -> [Int] -> Int
forall a. Ord a => a -> [a] -> a
maximumDef Int
0 (Text -> Int
forall a. HasChars a => a -> Int
realLength Text
posname Int -> [Int] -> [Int]
forall a. a -> [a] -> [a]
: [Text -> Int
forall a. HasChars a => a -> Int
realLength (Quantity -> Text
showValue Quantity
q) | Just Quantity
q <- (Row -> Maybe Quantity) -> [Row] -> [Maybe Quantity]
forall a b. (a -> b) -> [a] -> [b]
map Row -> Maybe Quantity
posSum [Row]
rows]) else Int
0
sideName :: Int -> Text
sideName Int
sign = Text -> [Text] -> Text
T.intercalate Text
", " ([Text] -> Text) -> [Text] -> Text
forall a b. (a -> b) -> a -> b
$ (Text -> Bool) -> [Text] -> [Text]
forall a. (a -> Bool) -> [a] -> [a]
filter (Bool -> Bool
not (Bool -> Bool) -> (Text -> Bool) -> Text -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Bool
T.null) [Int -> Text
groupName Int
k | Int
k <- [Int]
groups, Int -> Int
groupSign Int
k Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
sign]
(Text
posname, Text
negname) = (Int -> Text
sideName Int
1, Int -> Text
sideName (-Int
1))
netname :: Text
netname = if Bool
hasvalues then Text -> (Series -> Text) -> Maybe Series -> Text
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Text
"" (ChartLabel -> Text
lbShort (ChartLabel -> Text) -> (Series -> ChartLabel) -> Series -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Series -> ChartLabel
seLabel) Maybe Series
mtotal else Text
""
headerline :: StyledLine
headerline = Int -> StyledLine -> StyledLine
forall a. Int -> [a] -> [a]
drop Int
1 (StyledLine -> StyledLine) -> StyledLine -> StyledLine
forall a b. (a -> b) -> a -> b
$
(if Int
labelw Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
0 then [(Style
StylePlain, Text
" "), (Style
StylePlain, Int -> Text -> Text
T.replicate Int
labelw Text
" ")] else [])
StyledLine -> StyledLine -> StyledLine
forall a. [a] -> [a] -> [a]
++ (if Bool
hasvalues then [(Style
StylePlain, Text
" "), (Style
StylePlain, Maybe Int -> Maybe Int -> Bool -> Bool -> Text -> Text
fitText (Int -> Maybe Int
forall a. a -> Maybe a
Just Int
valuew) Maybe Int
forall a. Maybe a
Nothing Bool
False Bool
False Text
netname)] else [])
StyledLine -> StyledLine -> StyledLine
forall a. [a] -> [a] -> [a]
++ Text -> StyledLine
notecolumn Text
""
StyledLine -> StyledLine -> StyledLine
forall a. [a] -> [a] -> [a]
++ [(Style
StylePlain, Text
" ")]
StyledLine -> StyledLine -> StyledLine
forall a. [a] -> [a] -> [a]
++ (if Int
negsumw Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
0 then [(Style
StylePlain, Maybe Int -> Maybe Int -> Bool -> Bool -> Text -> Text
fitText (Int -> Maybe Int
forall a. a -> Maybe a
Just Int
negsumw) Maybe Int
forall a. Maybe a
Nothing Bool
False Bool
False Text
negname), (Style
StylePlain, Text
" ")] else [])
StyledLine -> StyledLine -> StyledLine
forall a. [a] -> [a] -> [a]
++ (if Int
possumw Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
0 then [(Style
StylePlain, Int -> Text -> Text
T.replicate Int
barw Text
" "), (Style
StylePlain, Text
" "), (Style
StylePlain, Text
posname)] else [])
barw :: Int
barw = Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
10 (Int -> Int) -> Int -> Int
forall a b. (a -> b) -> a -> b
$ Int
tcWidth Int -> Int -> Int
forall a. Num a => a -> a -> a
- (if Int
labelw Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
0 then Int
labelw Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
2 else Int
0) Int -> Int -> Int
forall a. Num a => a -> a -> a
- (if Int
sublabelw Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
0 then Int
sublabelw Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
2 else Int
0) Int -> Int -> Int
forall a. Num a => a -> a -> a
- (if Bool
hasvalues then Int
valuew Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
2 else Int
0) Int -> Int -> Int
forall a. Num a => a -> a -> a
- (if Int
notew Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
0 then Int
notew Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
2 else Int
0)
Int -> Int -> Int
forall a. Num a => a -> a -> a
- (if Int
negsumw Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
0 then Int
negsumw Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1 else Int
0) Int -> Int -> Int
forall a. Num a => a -> a -> a
- (if Int
possumw Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
0 then Int
possumw Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1 else Int
0)
posTotal :: Row -> Quantity
posTotal Row
r = [Quantity] -> Quantity
forall a. Num a => [a] -> a
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum [Quantity
q | (Style
_, Char
_, Quantity
q) <- Row -> [(Style, Char, Quantity)]
rowSegments Row
r, Quantity
q Quantity -> Quantity -> Bool
forall a. Ord a => a -> a -> Bool
> Quantity
0] Quantity -> Quantity -> Quantity
forall a. Ord a => a -> a -> a
`max` Quantity -> (Quantity -> Quantity) -> Maybe Quantity -> Quantity
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Quantity
0 (Quantity -> Quantity -> Quantity
forall a. Ord a => a -> a -> a
max Quantity
0) (Row -> Maybe Quantity
rowMark Row
r)
negTotal :: Row -> Quantity
negTotal Row
r = [Quantity] -> Quantity
forall a. Num a => [a] -> a
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum [Quantity -> Quantity
forall a. Num a => a -> a
abs Quantity
q | (Style
_, Char
_, Quantity
q) <- Row -> [(Style, Char, Quantity)]
rowSegments Row
r, Quantity
q Quantity -> Quantity -> Bool
forall a. Ord a => a -> a -> Bool
< Quantity
0] Quantity -> Quantity -> Quantity
forall a. Ord a => a -> a -> a
`max` Quantity -> (Quantity -> Quantity) -> Maybe Quantity -> Quantity
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Quantity
0 (Quantity -> Quantity
forall a. Num a => a -> a
abs (Quantity -> Quantity)
-> (Quantity -> Quantity) -> Quantity -> Quantity
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Quantity -> Quantity -> Quantity
forall a. Ord a => a -> a -> a
min Quantity
0) (Row -> Maybe Quantity
rowMark Row
r)
maxpos :: Quantity
maxpos = Quantity -> [Quantity] -> Quantity
forall a. Ord a => a -> [a] -> a
maximumDef Quantity
0 ([Quantity] -> Quantity) -> [Quantity] -> Quantity
forall a b. (a -> b) -> a -> b
$ (Row -> Quantity) -> [Row] -> [Quantity]
forall a b. (a -> b) -> [a] -> [b]
map Row -> Quantity
posTotal [Row]
rows
maxneg :: Quantity
maxneg = Quantity -> [Quantity] -> Quantity
forall a. Ord a => a -> [a] -> a
maximumDef Quantity
0 ([Quantity] -> Quantity) -> [Quantity] -> Quantity
forall a b. (a -> b) -> a -> b
$ (Row -> Quantity) -> [Row] -> [Quantity]
forall a b. (a -> b) -> [a] -> [b]
map Row -> Quantity
negTotal [Row]
rows
hasaxis :: Bool
hasaxis = Quantity
maxneg Quantity -> Quantity -> Bool
forall a. Ord a => a -> a -> Bool
> Quantity
0
(Quantity
posspan, Quantity
negspan) = case Maybe Quantity
pnScale of
Just Quantity
s | Quantity
s Quantity -> Quantity -> Bool
forall a. Ord a => a -> a -> Bool
> Quantity
0 -> Quantity -> Quantity -> Quantity -> (Quantity, Quantity)
forall a. (Ord a, Fractional a) => a -> a -> a -> (a, a)
scaleSpans Quantity
s Quantity
maxpos Quantity
maxneg
Maybe Quantity
_ -> (Quantity
maxpos, Quantity
maxneg)
area :: Int
area = if Bool
hasaxis then Int
barw Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1 else Int
barw
scale :: Double
scale :: Double
scale
| Quantity
posspan Quantity -> Quantity -> Quantity
forall a. Num a => a -> a -> a
+ Quantity
negspan Quantity -> Quantity -> Bool
forall a. Eq a => a -> a -> Bool
== Quantity
0 = Double
0
| Bool
pnCounts Bool -> Bool -> Bool
&& Maybe Quantity -> Bool
forall a. Maybe a -> Bool
isNothing Maybe Quantity
pnScale = Double -> Double -> Double
forall a. Ord a => a -> a -> a
min Double
1 Double
fillscale
| Bool
otherwise = Double
fillscale
where fillscale :: Double
fillscale = Int -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
area Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Quantity -> Double
forall a b. (Real a, Fractional b) => a -> b
realToFrac (Quantity
posspan Quantity -> Quantity -> Quantity
forall a. Num a => a -> a -> a
+ Quantity
negspan)
leftw :: Int
leftw
| Bool -> Bool
not Bool
hasaxis = Int
0
| Bool
otherwise = Double -> Int
forall b. Integral b => Double -> b
forall a b. (RealFrac a, Integral b) => a -> b
round (Double
scale Double -> Double -> Double
forall a. Num a => a -> a -> a
* Quantity -> Double
forall a b. (Real a, Fractional b) => a -> b
realToFrac Quantity
negspan)
rightw :: Int
rightw = Int
area Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
leftw
axischar :: Text
axischar = if Bool
tcUnicode then Text
"│" else Text
"|"
markchar :: Text
markchar = if Bool
tcUnicode then Text
"┃" else Text
"!"
clippedRight :: Quantity -> Bool
clippedRight Quantity
q = Maybe Quantity -> Bool
forall a. Maybe a -> Bool
isJust Maybe Quantity
pnScale Bool -> Bool -> Bool
&& Quantity
q Quantity -> Quantity -> Bool
forall a. Ord a => a -> a -> Bool
> Quantity
posspan
clippedLeft :: Quantity -> Bool
clippedLeft Quantity
q = Maybe Quantity -> Bool
forall a. Maybe a -> Bool
isJust Maybe Quantity
pnScale Bool -> Bool -> Bool
&& Quantity
q Quantity -> Quantity -> Bool
forall a. Ord a => a -> a -> Bool
> Quantity
negspan
clipright :: Text
clipright = if Bool
tcUnicode then Text
"▸" else Text
">"
clipleft :: Text
clipleft = if Bool
tcUnicode then Text
"◂" else Text
"<"
rowLine :: Row -> StyledLine
rowLine r :: Row
r@Row{Text
rowLabel :: Row -> Text
rowLabel :: Text
rowLabel, Text
rowSubLabel :: Row -> Text
rowSubLabel :: Text
rowSubLabel, Maybe Quantity
rowValue :: Row -> Maybe Quantity
rowValue :: Maybe Quantity
rowValue, Maybe Text
rowNote :: Row -> Maybe Text
rowNote :: Maybe Text
rowNote} =
Int -> StyledLine -> StyledLine
forall a. Int -> [a] -> [a]
drop Int
1 (StyledLine -> StyledLine) -> StyledLine -> StyledLine
forall a b. (a -> b) -> a -> b
$
(if Int
labelw Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
0 then [(Style
StylePlain, Text
" "), (Style
StylePlain, Maybe Int -> Maybe Int -> Bool -> Bool -> Text -> Text
fitText (Int -> Maybe Int
forall a. a -> Maybe a
Just Int
labelw) (Int -> Maybe Int
forall a. a -> Maybe a
Just Int
labelw) Bool
True Bool
True Text
rowLabel)] else [])
StyledLine -> StyledLine -> StyledLine
forall a. [a] -> [a] -> [a]
++ (if Int
sublabelw Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
0 then [(Style
StylePlain, Text
" "), (Style
StylePlain, Maybe Int -> Maybe Int -> Bool -> Bool -> Text -> Text
fitText (Int -> Maybe Int
forall a. a -> Maybe a
Just Int
sublabelw) (Int -> Maybe Int
forall a. a -> Maybe a
Just Int
sublabelw) Bool
True Bool
True Text
rowSubLabel)] else [])
StyledLine -> StyledLine -> StyledLine
forall a. [a] -> [a] -> [a]
++ (if Bool
hasvalues
then [(Style
StylePlain, Text
" "), (Style -> (Quantity -> Style) -> Maybe Quantity -> Style
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Style
StylePlain Quantity -> Style
forall {a}. (Ord a, Num a) => a -> Style
valueStyle Maybe Quantity
rowValue, Maybe Int -> Maybe Int -> Bool -> Bool -> Text -> Text
fitText (Int -> Maybe Int
forall a. a -> Maybe a
Just Int
valuew) Maybe Int
forall a. Maybe a
Nothing Bool
False Bool
False (Text -> Text) -> Text -> Text
forall a b. (a -> b) -> a -> b
$ Text -> (Quantity -> Text) -> Maybe Quantity -> Text
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Text
"" Quantity -> Text
showValue Maybe Quantity
rowValue)]
else [])
StyledLine -> StyledLine -> StyledLine
forall a. [a] -> [a] -> [a]
++ Text -> StyledLine
notecolumn (Text -> Maybe Text -> Text
forall a. a -> Maybe a -> a
fromMaybe Text
"" Maybe Text
rowNote)
StyledLine -> StyledLine -> StyledLine
forall a. [a] -> [a] -> [a]
++ [(Style
StylePlain, Text
" ")]
StyledLine -> StyledLine -> StyledLine
forall a. [a] -> [a] -> [a]
++ (if Int
negsumw Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
0 then [(Style -> (Quantity -> Style) -> Maybe Quantity -> Style
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Style
StylePlain Quantity -> Style
forall {a}. (Ord a, Num a) => a -> Style
valueStyle (Row -> Maybe Quantity
negSum Row
r), Maybe Int -> Maybe Int -> Bool -> Bool -> Text -> Text
fitText (Int -> Maybe Int
forall a. a -> Maybe a
Just Int
negsumw) Maybe Int
forall a. Maybe a
Nothing Bool
False Bool
False (Text -> Text) -> Text -> Text
forall a b. (a -> b) -> a -> b
$ Text -> (Quantity -> Text) -> Maybe Quantity -> Text
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Text
"" Quantity -> Text
showValue (Row -> Maybe Quantity
negSum Row
r)), (Style
StylePlain, Text
" ")] else [])
StyledLine -> StyledLine -> StyledLine
forall a. [a] -> [a] -> [a]
++ (if Int
possumw Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
0 then Int -> StyledLine -> StyledLine
padTo Int
barw StyledLine
bars StyledLine -> StyledLine -> StyledLine
forall a. [a] -> [a] -> [a]
++ [(Style
StylePlain, Text
" "), (Style -> (Quantity -> Style) -> Maybe Quantity -> Style
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Style
StylePlain Quantity -> Style
forall {a}. (Ord a, Num a) => a -> Style
valueStyle (Row -> Maybe Quantity
posSum Row
r), Text -> (Quantity -> Text) -> Maybe Quantity -> Text
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Text
"" Quantity -> Text
showValue (Row -> Maybe Quantity
posSum Row
r))] else StyledLine
bars)
where
bars :: StyledLine
bars = (StyledLine -> StyledLine)
-> (Quantity -> StyledLine -> StyledLine)
-> Maybe Quantity
-> StyledLine
-> StyledLine
forall b a. b -> (a -> b) -> Maybe a -> b
maybe StyledLine -> StyledLine
forall a. a -> a
id Quantity -> StyledLine -> StyledLine
overlayMark (Row -> Maybe Quantity
rowMark Row
r) (StyledLine -> StyledLine) -> StyledLine -> StyledLine
forall a b. (a -> b) -> a -> b
$ (if Bool
hasaxis then Row -> StyledLine
negativeBar Row
r StyledLine -> StyledLine -> StyledLine
forall a. [a] -> [a] -> [a]
++ [(Style
StylePlain, Text
axischar)] else []) StyledLine -> StyledLine -> StyledLine
forall a. [a] -> [a] -> [a]
++ Row -> StyledLine
positiveBar Row
r
padTo :: Int -> StyledLine -> StyledLine
padTo Int
w StyledLine
l = StyledLine
l StyledLine -> StyledLine -> StyledLine
forall a. [a] -> [a] -> [a]
++ [(Style
StylePlain, Int -> Text -> Text
T.replicate (Int
w Int -> Int -> Int
forall a. Num a => a -> a -> a
- [Int] -> Int
forall a. Num a => [a] -> a
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum (((Style, Text) -> Int) -> StyledLine -> [Int]
forall a b. (a -> b) -> [a] -> [b]
map (Text -> Int
forall a. HasChars a => a -> Int
realLength (Text -> Int) -> ((Style, Text) -> Text) -> (Style, Text) -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Style, Text) -> Text
forall a b. (a, b) -> b
snd) StyledLine
l)) Text
" ")]
overlayMark :: Quantity -> StyledLine -> StyledLine
overlayMark Quantity
q = Int -> (Style, Text) -> StyledLine -> StyledLine
overlayAt Int
col (Style
StylePlain, Text
markchar)
where
col :: Int
col | Quantity
q Quantity -> Quantity -> Bool
forall a. Ord a => a -> a -> Bool
> Quantity
0 = Int
leftw Int -> Int -> Int
forall a. Num a => a -> a -> a
+ (if Bool
hasaxis then Int
1 else Int
0) Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
1 (Int -> Int -> Int
forall a. Ord a => a -> a -> a
min Int
rightw (Int -> Int) -> Int -> Int
forall a b. (a -> b) -> a -> b
$ Int -> Quantity -> Int
cells Int
1 Quantity
q) Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1
| Quantity
q Quantity -> Quantity -> Bool
forall a. Ord a => a -> a -> Bool
< Quantity
0 = Int
leftw Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
1 (Int -> Int -> Int
forall a. Ord a => a -> a -> a
min Int
leftw (Int -> Int) -> Int -> Int
forall a b. (a -> b) -> a -> b
$ Int -> Quantity -> Int
cells Int
1 (Quantity -> Quantity
forall a. Num a => a -> a
abs Quantity
q))
| Bool
otherwise = if Bool
hasaxis then Int
leftw else Int
0
valueStyle :: a -> Style
valueStyle a
q = if a
q a -> a -> Bool
forall a. Ord a => a -> a -> Bool
< a
0 then Style
StyleNegative else Style
StylePlain
positiveBar :: Row -> StyledLine
positiveBar Row{[(Style, Char, Quantity)]
rowSegments :: Row -> [(Style, Char, Quantity)]
rowSegments :: [(Style, Char, Quantity)]
rowSegments} = case [(Style
st, Char
c, Quantity
q) | (Style
st, Char
c, Quantity
q) <- [(Style, Char, Quantity)]
rowSegments, Quantity
q Quantity -> Quantity -> Bool
forall a. Ord a => a -> a -> Bool
> Quantity
0] of
[(Style
st, Char
c, Quantity
q)] | Bool
tcUnicode Bool -> Bool -> Bool
&& Char
c Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'█' Bool -> Bool -> Bool
&& [(Style, Char, Quantity)] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [(Style, Char, Quantity)]
rowSegments Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
1 ->
let eighths :: Int
eighths = Int -> Quantity -> Int
cells Int
8 Quantity
q
in if Quantity -> Bool
clippedRight Quantity
q
then [(Style
st, Int -> Text -> Text
T.replicate (Int
rightw Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) Text
"█" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
clipright)]
else let e :: Int
e = Int -> Int -> Int
forall a. Ord a => a -> a -> a
min (Int
rightw Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
8) Int
eighths in [(Style
st, Int -> Text -> Text
T.replicate (Int
e Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` Int
8) Text
"█" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Int -> Text
forall {b}. IsString b => Int -> b
partialBlock (Int
e Int -> Int -> Int
forall a. Integral a => a -> a -> a
`mod` Int
8))]
[(Style, Char, Quantity)]
segs -> Int
-> (Quantity -> Bool)
-> (Text -> Text)
-> [(Style, Char, Quantity)]
-> StyledLine
forall {a}.
Int
-> (Quantity -> Bool)
-> (Text -> Text)
-> [(a, Char, Quantity)]
-> [(a, Text)]
segmentPieces Int
rightw Quantity -> Bool
clippedRight (\Text
t -> Int -> Text -> Text
T.dropEnd Int
1 Text
t Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
clipright) [(Style, Char, Quantity)]
segs
negativeBar :: Row -> StyledLine
negativeBar Row{[(Style, Char, Quantity)]
rowSegments :: Row -> [(Style, Char, Quantity)]
rowSegments :: [(Style, Char, Quantity)]
rowSegments} =
let pieces :: StyledLine
pieces = Int
-> (Quantity -> Bool)
-> (Text -> Text)
-> [(Style, Char, Quantity)]
-> StyledLine
forall {a}.
Int
-> (Quantity -> Bool)
-> (Text -> Text)
-> [(a, Char, Quantity)]
-> [(a, Text)]
segmentPieces Int
leftw Quantity -> Bool
clippedLeft (\Text
t -> Text
clipleft Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Int -> Text -> Text
T.drop Int
1 Text
t) [(Style
st, Char
c, Quantity -> Quantity
forall a. Num a => a -> a
abs Quantity
q) | (Style
st, Char
c, Quantity
q) <- [(Style, Char, Quantity)]
rowSegments, Quantity
q Quantity -> Quantity -> Bool
forall a. Ord a => a -> a -> Bool
< Quantity
0]
used :: Int
used = [Int] -> Int
forall a. Num a => [a] -> a
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum ([Int] -> Int) -> [Int] -> Int
forall a b. (a -> b) -> a -> b
$ ((Style, Text) -> Int) -> StyledLine -> [Int]
forall a b. (a -> b) -> [a] -> [b]
map (Text -> Int
forall a. HasChars a => a -> Int
realLength (Text -> Int) -> ((Style, Text) -> Text) -> (Style, Text) -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Style, Text) -> Text
forall a b. (a, b) -> b
snd) StyledLine
pieces
in (Style
StylePlain, Int -> Text -> Text
T.replicate (Int
leftw Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
used) Text
" ") (Style, Text) -> StyledLine -> StyledLine
forall a. a -> [a] -> [a]
: StyledLine -> StyledLine
forall a. [a] -> [a]
reverse StyledLine
pieces
segmentPieces :: Int
-> (Quantity -> Bool)
-> (Text -> Text)
-> [(a, Char, Quantity)]
-> [(a, Text)]
segmentPieces Int
maxw Quantity -> Bool
clipped Text -> Text
clip [(a, Char, Quantity)]
segs =
let bounds :: [Int]
bounds = (Quantity -> Int) -> [Quantity] -> [Int]
forall a b. (a -> b) -> [a] -> [b]
map (Int -> Int -> Int
forall a. Ord a => a -> a -> a
min Int
maxw (Int -> Int) -> (Quantity -> Int) -> Quantity -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Quantity -> Int
cells Int
1) ([Quantity] -> [Int]) -> [Quantity] -> [Int]
forall a b. (a -> b) -> a -> b
$ (Quantity -> Quantity -> Quantity) -> [Quantity] -> [Quantity]
forall a. (a -> a -> a) -> [a] -> [a]
scanl1 Quantity -> Quantity -> Quantity
forall a. Num a => a -> a -> a
(+) [Quantity
q | (a
_, Char
_, Quantity
q) <- [(a, Char, Quantity)]
segs]
widths :: [Int]
widths = (Int -> Int -> Int) -> [Int] -> [Int] -> [Int]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith (-) [Int]
bounds (Int
0 Int -> [Int] -> [Int]
forall a. a -> [a] -> [a]
: [Int]
bounds)
pieces :: [(a, Text)]
pieces = [(a
st, Int -> Text -> Text
T.replicate Int
w (Char -> Text
T.singleton Char
c)) | ((a
st, Char
c, Quantity
_), Int
w) <- [(a, Char, Quantity)] -> [Int] -> [((a, Char, Quantity), Int)]
forall a b. [a] -> [b] -> [(a, b)]
zip [(a, Char, Quantity)]
segs [Int]
widths, Int
w Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
0]
in case ([(a, Text)] -> [(a, Text)]
forall a. [a] -> [a]
reverse [(a, Text)]
pieces, Quantity -> Bool
clipped (Quantity -> Bool) -> Quantity -> Bool
forall a b. (a -> b) -> a -> b
$ [Quantity] -> Quantity
forall a. Num a => [a] -> a
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum [Quantity
q | (a
_, Char
_, Quantity
q) <- [(a, Char, Quantity)]
segs]) of
((a
st, Text
t) : [(a, Text)]
rest, Bool
True) -> [(a, Text)] -> [(a, Text)]
forall a. [a] -> [a]
reverse ((a
st, Text -> Text
clip Text
t) (a, Text) -> [(a, Text)] -> [(a, Text)]
forall a. a -> [a] -> [a]
: [(a, Text)]
rest)
([(a, Text)], Bool)
_ -> [(a, Text)]
pieces
cells :: Int -> Quantity -> Int
cells :: Int -> Quantity -> Int
cells Int
perunit Quantity
q = Double -> Int
forall b. Integral b => Double -> b
forall a b. (RealFrac a, Integral b) => a -> b
round (Quantity -> Double
forall a b. (Real a, Fractional b) => a -> b
realToFrac Quantity
q Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
scale Double -> Double -> Double
forall a. Num a => a -> a -> a
* Int -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
perunit)
partialBlock :: Int -> b
partialBlock Int
n = b -> Maybe b -> b
forall a. a -> Maybe a -> a
fromMaybe b
"" (Maybe b -> b) -> Maybe b -> b
forall a b. (a -> b) -> a -> b
$ [b
"", b
"▏", b
"▎", b
"▍", b
"▌", b
"▋", b
"▊", b
"▉"] [b] -> Int -> Maybe b
forall a. [a] -> Int -> Maybe a
`atMay` Int
n
cyclicAt :: a -> [a] -> Int -> a
cyclicAt :: forall a. a -> [a] -> Int -> a
cyclicAt a
d [a]
xs Int
n = if [a] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [a]
xs then a
d else a -> Maybe a -> a
forall a. a -> Maybe a -> a
fromMaybe a
d (Maybe a -> a) -> Maybe a -> a
forall a b. (a -> b) -> a -> b
$ [a]
xs [a] -> Int -> Maybe a
forall a. [a] -> Int -> Maybe a
`atMay` (Int
n Int -> Int -> Int
forall a. Integral a => a -> a -> a
`mod` [a] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [a]
xs)
overlayAt :: Int -> (Style, Text) -> StyledLine -> StyledLine
overlayAt :: Int -> (Style, Text) -> StyledLine -> StyledLine
overlayAt Int
i (Style, Text)
new = Int -> StyledLine -> StyledLine
go Int
0
where
go :: Int -> StyledLine -> StyledLine
go Int
col [] = [(Style
StylePlain, Int -> Text -> Text
T.replicate (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
col) Text
" "), (Style, Text)
new]
go Int
col ((Style
st, Text
t) : StyledLine
rest)
| Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
col Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
n = [(Style
st, Int -> Text -> Text
T.take (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
col) Text
t) | Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
col] StyledLine -> StyledLine -> StyledLine
forall a. [a] -> [a] -> [a]
++ [(Style, Text)
new] StyledLine -> StyledLine -> StyledLine
forall a. [a] -> [a] -> [a]
++ [(Style
st, Int -> Text -> Text
T.drop (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
col Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) Text
t) | Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
col Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1 Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
n] StyledLine -> StyledLine -> StyledLine
forall a. [a] -> [a] -> [a]
++ StyledLine
rest
| Bool
otherwise = (Style
st, Text
t) (Style, Text) -> StyledLine -> StyledLine
forall a. a -> [a] -> [a]
: Int -> StyledLine -> StyledLine
go (Int
col Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
n) StyledLine
rest
where n :: Int
n = Text -> Int
T.length Text
t
trimEnd :: StyledLine -> StyledLine
trimEnd :: StyledLine -> StyledLine
trimEnd = StyledLine -> StyledLine
forall a. [a] -> [a]
reverse (StyledLine -> StyledLine)
-> (StyledLine -> StyledLine) -> StyledLine -> StyledLine
forall b c a. (b -> c) -> (a -> b) -> a -> c
. StyledLine -> StyledLine
forall {a}. [(a, Text)] -> [(a, Text)]
dropSpaces (StyledLine -> StyledLine)
-> (StyledLine -> StyledLine) -> StyledLine -> StyledLine
forall b c a. (b -> c) -> (a -> b) -> a -> c
. StyledLine -> StyledLine
forall a. [a] -> [a]
reverse
where
dropSpaces :: [(a, Text)] -> [(a, Text)]
dropSpaces ((a
st, Text
t) : [(a, Text)]
rest)
| Text -> Bool
T.null (Text -> Text
T.stripEnd Text
t) = [(a, Text)] -> [(a, Text)]
dropSpaces [(a, Text)]
rest
| Bool
otherwise = (a
st, Text -> Text
T.stripEnd Text
t) (a, Text) -> [(a, Text)] -> [(a, Text)]
forall a. a -> [a] -> [a]
: [(a, Text)]
rest
dropSpaces [] = []
tests_Hledger_Write_Chart_Text :: TestTree
tests_Hledger_Write_Chart_Text :: TestTree
tests_Hledger_Write_Chart_Text = String -> [TestTree] -> TestTree
testGroup String
"Write.Chart.Text" [
String -> Assertion -> TestTree
testCase String
"trimEnd" (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$ do
StyledLine -> StyledLine
trimEnd [(Style
StylePlain, Text
"a "), (Style
StylePlain, Text
" ")] StyledLine -> StyledLine -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= [(Style
StylePlain, Text
"a")]
StyledLine -> StyledLine
trimEnd [] StyledLine -> StyledLine -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= []
,String -> Assertion -> TestTree
testCase String
"single series, scaled to the width" (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$ do
let lbl :: Text -> ChartLabel
lbl Text
t = Text
-> Text -> Int -> Maybe AccountType -> Bool -> [Text] -> ChartLabel
ChartLabel Text
t Text
t Int
0 Maybe AccountType
forall a. Maybe a
Nothing Bool
True []
chart :: Chart
chart = Text -> XAxis -> [Panel] -> [Text] -> Chart
Chart Text
"" ([ChartLabel] -> XAxis
XCategories [Text -> ChartLabel
lbl Text
"a", Text -> ChartLabel
lbl Text
"b"])
[Amount
-> [Series]
-> [Text]
-> Maybe Quantity
-> BarLayout
-> Bool
-> [Summary]
-> [Maybe Text]
-> Panel
Panel Amount
nullamt [ChartLabel
-> SeriesRole -> Mark -> [Maybe Quantity] -> Int -> Int -> Series
Series (Text -> ChartLabel
lbl Text
"s") SeriesRole
SeriesItem Mark
MarkBar [Quantity -> Maybe Quantity
forall a. a -> Maybe a
Just Quantity
1, Quantity -> Maybe Quantity
forall a. a -> Maybe a
Just Quantity
2] Int
0 Int
0] [] Maybe Quantity
forall a. Maybe a
Nothing BarLayout
Diverging Bool
False [] []] []
(StyledLine -> Text) -> [StyledLine] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map ([Text] -> Text
T.concat ([Text] -> Text) -> (StyledLine -> [Text]) -> StyledLine -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((Style, Text) -> Text) -> StyledLine -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map (Style, Text) -> Text
forall a b. (a, b) -> b
snd) (TextChartOpts -> Chart -> [StyledLine]
chartAsStyledLines (Int -> Bool -> TextChartOpts
TextChartOpts Int
20 Bool
True) Chart
chart)
[Text] -> [Text] -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= [Text
"a 1 ███████", Text
"b 2 ██████████████"]
,String -> Assertion -> TestTree
testCase String
"mixed signs grow from an axis" (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$ do
let lbl :: Text -> ChartLabel
lbl Text
t = Text
-> Text -> Int -> Maybe AccountType -> Bool -> [Text] -> ChartLabel
ChartLabel Text
t Text
t Int
0 Maybe AccountType
forall a. Maybe a
Nothing Bool
True []
chart :: Chart
chart = Text -> XAxis -> [Panel] -> [Text] -> Chart
Chart Text
"" ([ChartLabel] -> XAxis
XCategories [Text -> ChartLabel
lbl Text
"a", Text -> ChartLabel
lbl Text
"b"])
[Amount
-> [Series]
-> [Text]
-> Maybe Quantity
-> BarLayout
-> Bool
-> [Summary]
-> [Maybe Text]
-> Panel
Panel Amount
nullamt [ChartLabel
-> SeriesRole -> Mark -> [Maybe Quantity] -> Int -> Int -> Series
Series (Text -> ChartLabel
lbl Text
"s") SeriesRole
SeriesItem Mark
MarkBar [Quantity -> Maybe Quantity
forall a. a -> Maybe a
Just (-Quantity
1), Quantity -> Maybe Quantity
forall a. a -> Maybe a
Just Quantity
2] Int
0 Int
0] [] Maybe Quantity
forall a. Maybe a
Nothing BarLayout
Diverging Bool
False [] []] []
(StyledLine -> Text) -> [StyledLine] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map ([Text] -> Text
T.concat ([Text] -> Text) -> (StyledLine -> [Text]) -> StyledLine -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((Style, Text) -> Text) -> StyledLine -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map (Style, Text) -> Text
forall a b. (a, b) -> b
snd) (TextChartOpts -> Chart -> [StyledLine]
chartAsStyledLines (Int -> Bool -> TextChartOpts
TextChartOpts Int
21 Bool
False) Chart
chart)
[Text] -> [Text] -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= [Text
"a -1 ####|", Text
"b 2 |#########"]
]