{-# LANGUAGE CPP #-}
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE OverloadedStrings #-}
module Hledger.Write.Chart (
Chart(..),
XAxis(..),
ChartLabel(..),
Panel(..),
BarLayout(..),
Series(..),
SeriesRole(..),
Mark(..),
Summary(..),
SummaryKind(..),
Colour(..),
seriesColours,
Colouring(..),
chartColouring,
stableSlots,
stableSlotsFavouringFirst,
categoryTints,
seriesSize,
maxTints,
mainAccountType,
SeriesValues,
isContra,
isHatched,
hatchedSeries,
legendListsSeries,
seriesValues,
valueAt,
setChartScales,
chartWithoutTotalMarks,
setChartLayout,
scaleSpans,
chartAsCsv,
tests_Hledger_Write_Chart,
) where
import Control.Applicative ((<|>))
import Data.Bits (shiftR, xor)
import Data.Char (ord)
import Data.List (find, nub, partition, sortOn)
#if !MIN_VERSION_base(4,20,0)
import Data.List (foldl')
#endif
import Data.Map qualified as M
import Data.Maybe (catMaybes, fromMaybe, isJust, isNothing, listToMaybe)
import Data.Ord (Down(..))
import Data.Set qualified as S
import Data.Word (Word32)
import Data.Array (Array, bounds, inRange, listArray, (!))
import Data.Aeson (ToJSON)
import Data.Text (Text)
import Data.Text qualified as T
import Data.Time (Day)
import GHC.Generics (Generic)
import Hledger.Data.Amount (AmountFormat(..), machineFmt, nullamt, showAmountWith)
import Hledger.Data.Dates (showDate)
import Hledger.Data.Json ()
import Hledger.Data.AccountType (isAccountSubtypeOf)
import Hledger.Data.Types (AccountType(..), Amount(..), DateSpan(..), Quantity)
import Hledger.Utils.Test
import Hledger.Write.Csv (CSV)
data Chart = Chart
{ Chart -> CsvValue
chTitle :: Text
, Chart -> XAxis
chXAxis :: XAxis
, Chart -> [Panel]
chPanels :: [Panel]
, Chart -> [CsvValue]
chNotes :: [Text]
} deriving (Int -> Chart -> ShowS
[Chart] -> ShowS
Chart -> String
(Int -> Chart -> ShowS)
-> (Chart -> String) -> ([Chart] -> ShowS) -> Show Chart
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Chart -> ShowS
showsPrec :: Int -> Chart -> ShowS
$cshow :: Chart -> String
show :: Chart -> String
$cshowList :: [Chart] -> ShowS
showList :: [Chart] -> ShowS
Show, Chart -> Chart -> Bool
(Chart -> Chart -> Bool) -> (Chart -> Chart -> Bool) -> Eq Chart
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Chart -> Chart -> Bool
== :: Chart -> Chart -> Bool
$c/= :: Chart -> Chart -> Bool
/= :: Chart -> Chart -> Bool
Eq, (forall x. Chart -> Rep Chart x)
-> (forall x. Rep Chart x -> Chart) -> Generic Chart
forall x. Rep Chart x -> Chart
forall x. Chart -> Rep Chart x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. Chart -> Rep Chart x
from :: forall x. Chart -> Rep Chart x
$cto :: forall x. Rep Chart x -> Chart
to :: forall x. Rep Chart x -> Chart
Generic, [Chart] -> Encoding
[Chart] -> Value
Chart -> Bool
Chart -> Encoding
Chart -> Value
(Chart -> Value)
-> (Chart -> Encoding)
-> ([Chart] -> Value)
-> ([Chart] -> Encoding)
-> (Chart -> Bool)
-> ToJSON Chart
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> (a -> Bool)
-> ToJSON a
$ctoJSON :: Chart -> Value
toJSON :: Chart -> Value
$ctoEncoding :: Chart -> Encoding
toEncoding :: Chart -> Encoding
$ctoJSONList :: [Chart] -> Value
toJSONList :: [Chart] -> Value
$ctoEncodingList :: [Chart] -> Encoding
toEncodingList :: [Chart] -> Encoding
$comitField :: Chart -> Bool
omitField :: Chart -> Bool
ToJSON)
data XAxis
= XCategories [ChartLabel]
| XPeriods [(DateSpan, Text)]
| XDates [Day]
deriving (Int -> XAxis -> ShowS
[XAxis] -> ShowS
XAxis -> String
(Int -> XAxis -> ShowS)
-> (XAxis -> String) -> ([XAxis] -> ShowS) -> Show XAxis
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> XAxis -> ShowS
showsPrec :: Int -> XAxis -> ShowS
$cshow :: XAxis -> String
show :: XAxis -> String
$cshowList :: [XAxis] -> ShowS
showList :: [XAxis] -> ShowS
Show, XAxis -> XAxis -> Bool
(XAxis -> XAxis -> Bool) -> (XAxis -> XAxis -> Bool) -> Eq XAxis
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: XAxis -> XAxis -> Bool
== :: XAxis -> XAxis -> Bool
$c/= :: XAxis -> XAxis -> Bool
/= :: XAxis -> XAxis -> Bool
Eq, (forall x. XAxis -> Rep XAxis x)
-> (forall x. Rep XAxis x -> XAxis) -> Generic XAxis
forall x. Rep XAxis x -> XAxis
forall x. XAxis -> Rep XAxis x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. XAxis -> Rep XAxis x
from :: forall x. XAxis -> Rep XAxis x
$cto :: forall x. Rep XAxis x -> XAxis
to :: forall x. Rep XAxis x -> XAxis
Generic, [XAxis] -> Encoding
[XAxis] -> Value
XAxis -> Bool
XAxis -> Encoding
XAxis -> Value
(XAxis -> Value)
-> (XAxis -> Encoding)
-> ([XAxis] -> Value)
-> ([XAxis] -> Encoding)
-> (XAxis -> Bool)
-> ToJSON XAxis
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> (a -> Bool)
-> ToJSON a
$ctoJSON :: XAxis -> Value
toJSON :: XAxis -> Value
$ctoEncoding :: XAxis -> Encoding
toEncoding :: XAxis -> Encoding
$ctoJSONList :: [XAxis] -> Value
toJSONList :: [XAxis] -> Value
$ctoEncodingList :: [XAxis] -> Encoding
toEncodingList :: [XAxis] -> Encoding
$comitField :: XAxis -> Bool
omitField :: XAxis -> Bool
ToJSON)
data ChartLabel = ChartLabel
{ ChartLabel -> CsvValue
lbFull :: Text
, ChartLabel -> CsvValue
lbShort :: Text
, ChartLabel -> Int
lbIndent :: Int
, ChartLabel -> Maybe AccountType
lbType :: Maybe AccountType
, ChartLabel -> Bool
lbAccount :: Bool
, ChartLabel -> [CsvValue]
lbTipLines :: [Text]
} deriving (Int -> ChartLabel -> ShowS
[ChartLabel] -> ShowS
ChartLabel -> String
(Int -> ChartLabel -> ShowS)
-> (ChartLabel -> String)
-> ([ChartLabel] -> ShowS)
-> Show ChartLabel
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ChartLabel -> ShowS
showsPrec :: Int -> ChartLabel -> ShowS
$cshow :: ChartLabel -> String
show :: ChartLabel -> String
$cshowList :: [ChartLabel] -> ShowS
showList :: [ChartLabel] -> ShowS
Show, ChartLabel -> ChartLabel -> Bool
(ChartLabel -> ChartLabel -> Bool)
-> (ChartLabel -> ChartLabel -> Bool) -> Eq ChartLabel
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: ChartLabel -> ChartLabel -> Bool
== :: ChartLabel -> ChartLabel -> Bool
$c/= :: ChartLabel -> ChartLabel -> Bool
/= :: ChartLabel -> ChartLabel -> Bool
Eq, (forall x. ChartLabel -> Rep ChartLabel x)
-> (forall x. Rep ChartLabel x -> ChartLabel) -> Generic ChartLabel
forall x. Rep ChartLabel x -> ChartLabel
forall x. ChartLabel -> Rep ChartLabel x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. ChartLabel -> Rep ChartLabel x
from :: forall x. ChartLabel -> Rep ChartLabel x
$cto :: forall x. Rep ChartLabel x -> ChartLabel
to :: forall x. Rep ChartLabel x -> ChartLabel
Generic, [ChartLabel] -> Encoding
[ChartLabel] -> Value
ChartLabel -> Bool
ChartLabel -> Encoding
ChartLabel -> Value
(ChartLabel -> Value)
-> (ChartLabel -> Encoding)
-> ([ChartLabel] -> Value)
-> ([ChartLabel] -> Encoding)
-> (ChartLabel -> Bool)
-> ToJSON ChartLabel
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> (a -> Bool)
-> ToJSON a
$ctoJSON :: ChartLabel -> Value
toJSON :: ChartLabel -> Value
$ctoEncoding :: ChartLabel -> Encoding
toEncoding :: ChartLabel -> Encoding
$ctoJSONList :: [ChartLabel] -> Value
toJSONList :: [ChartLabel] -> Value
$ctoEncodingList :: [ChartLabel] -> Encoding
toEncodingList :: [ChartLabel] -> Encoding
$comitField :: ChartLabel -> Bool
omitField :: ChartLabel -> Bool
ToJSON)
data Panel = Panel
{ Panel -> Amount
pnUnit :: Amount
, Panel -> [Series]
pnSeries :: [Series]
, Panel -> [CsvValue]
pnGroupNames :: [Text]
, Panel -> Maybe Quantity
pnScale :: Maybe Quantity
, Panel -> BarLayout
pnLayout :: BarLayout
, Panel -> Bool
pnCounts :: Bool
, Panel -> [Summary]
pnSummary :: [Summary]
, Panel -> [Maybe CsvValue]
pnValueLabels :: [Maybe Text]
} deriving (Int -> Panel -> ShowS
[Panel] -> ShowS
Panel -> String
(Int -> Panel -> ShowS)
-> (Panel -> String) -> ([Panel] -> ShowS) -> Show Panel
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Panel -> ShowS
showsPrec :: Int -> Panel -> ShowS
$cshow :: Panel -> String
show :: Panel -> String
$cshowList :: [Panel] -> ShowS
showList :: [Panel] -> ShowS
Show, Panel -> Panel -> Bool
(Panel -> Panel -> Bool) -> (Panel -> Panel -> Bool) -> Eq Panel
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Panel -> Panel -> Bool
== :: Panel -> Panel -> Bool
$c/= :: Panel -> Panel -> Bool
/= :: Panel -> Panel -> Bool
Eq, (forall x. Panel -> Rep Panel x)
-> (forall x. Rep Panel x -> Panel) -> Generic Panel
forall x. Rep Panel x -> Panel
forall x. Panel -> Rep Panel x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. Panel -> Rep Panel x
from :: forall x. Panel -> Rep Panel x
$cto :: forall x. Rep Panel x -> Panel
to :: forall x. Rep Panel x -> Panel
Generic, [Panel] -> Encoding
[Panel] -> Value
Panel -> Bool
Panel -> Encoding
Panel -> Value
(Panel -> Value)
-> (Panel -> Encoding)
-> ([Panel] -> Value)
-> ([Panel] -> Encoding)
-> (Panel -> Bool)
-> ToJSON Panel
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> (a -> Bool)
-> ToJSON a
$ctoJSON :: Panel -> Value
toJSON :: Panel -> Value
$ctoEncoding :: Panel -> Encoding
toEncoding :: Panel -> Encoding
$ctoJSONList :: [Panel] -> Value
toJSONList :: [Panel] -> Value
$ctoEncodingList :: [Panel] -> Encoding
toEncodingList :: [Panel] -> Encoding
$comitField :: Panel -> Bool
omitField :: Panel -> Bool
ToJSON)
data Summary = Summary
{ Summary -> SummaryKind
suKind :: SummaryKind
, Summary -> CsvValue
suLabel :: Text
, Summary -> Amount
suValue :: Amount
} deriving (Int -> Summary -> ShowS
[Summary] -> ShowS
Summary -> String
(Int -> Summary -> ShowS)
-> (Summary -> String) -> ([Summary] -> ShowS) -> Show Summary
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Summary -> ShowS
showsPrec :: Int -> Summary -> ShowS
$cshow :: Summary -> String
show :: Summary -> String
$cshowList :: [Summary] -> ShowS
showList :: [Summary] -> ShowS
Show, Summary -> Summary -> Bool
(Summary -> Summary -> Bool)
-> (Summary -> Summary -> Bool) -> Eq Summary
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Summary -> Summary -> Bool
== :: Summary -> Summary -> Bool
$c/= :: Summary -> Summary -> Bool
/= :: Summary -> Summary -> Bool
Eq, (forall x. Summary -> Rep Summary x)
-> (forall x. Rep Summary x -> Summary) -> Generic Summary
forall x. Rep Summary x -> Summary
forall x. Summary -> Rep Summary x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. Summary -> Rep Summary x
from :: forall x. Summary -> Rep Summary x
$cto :: forall x. Rep Summary x -> Summary
to :: forall x. Rep Summary x -> Summary
Generic, [Summary] -> Encoding
[Summary] -> Value
Summary -> Bool
Summary -> Encoding
Summary -> Value
(Summary -> Value)
-> (Summary -> Encoding)
-> ([Summary] -> Value)
-> ([Summary] -> Encoding)
-> (Summary -> Bool)
-> ToJSON Summary
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> (a -> Bool)
-> ToJSON a
$ctoJSON :: Summary -> Value
toJSON :: Summary -> Value
$ctoEncoding :: Summary -> Encoding
toEncoding :: Summary -> Encoding
$ctoJSONList :: [Summary] -> Value
toJSONList :: [Summary] -> Value
$ctoEncodingList :: [Summary] -> Encoding
toEncodingList :: [Summary] -> Encoding
$comitField :: Summary -> Bool
omitField :: Summary -> Bool
ToJSON)
data SummaryKind
= SummaryTotal
| SummaryAverage
deriving (Int -> SummaryKind -> ShowS
[SummaryKind] -> ShowS
SummaryKind -> String
(Int -> SummaryKind -> ShowS)
-> (SummaryKind -> String)
-> ([SummaryKind] -> ShowS)
-> Show SummaryKind
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> SummaryKind -> ShowS
showsPrec :: Int -> SummaryKind -> ShowS
$cshow :: SummaryKind -> String
show :: SummaryKind -> String
$cshowList :: [SummaryKind] -> ShowS
showList :: [SummaryKind] -> ShowS
Show, SummaryKind -> SummaryKind -> Bool
(SummaryKind -> SummaryKind -> Bool)
-> (SummaryKind -> SummaryKind -> Bool) -> Eq SummaryKind
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: SummaryKind -> SummaryKind -> Bool
== :: SummaryKind -> SummaryKind -> Bool
$c/= :: SummaryKind -> SummaryKind -> Bool
/= :: SummaryKind -> SummaryKind -> Bool
Eq, (forall x. SummaryKind -> Rep SummaryKind x)
-> (forall x. Rep SummaryKind x -> SummaryKind)
-> Generic SummaryKind
forall x. Rep SummaryKind x -> SummaryKind
forall x. SummaryKind -> Rep SummaryKind x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. SummaryKind -> Rep SummaryKind x
from :: forall x. SummaryKind -> Rep SummaryKind x
$cto :: forall x. Rep SummaryKind x -> SummaryKind
to :: forall x. Rep SummaryKind x -> SummaryKind
Generic, [SummaryKind] -> Encoding
[SummaryKind] -> Value
SummaryKind -> Bool
SummaryKind -> Encoding
SummaryKind -> Value
(SummaryKind -> Value)
-> (SummaryKind -> Encoding)
-> ([SummaryKind] -> Value)
-> ([SummaryKind] -> Encoding)
-> (SummaryKind -> Bool)
-> ToJSON SummaryKind
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> (a -> Bool)
-> ToJSON a
$ctoJSON :: SummaryKind -> Value
toJSON :: SummaryKind -> Value
$ctoEncoding :: SummaryKind -> Encoding
toEncoding :: SummaryKind -> Encoding
$ctoJSONList :: [SummaryKind] -> Value
toJSONList :: [SummaryKind] -> Value
$ctoEncodingList :: [SummaryKind] -> Encoding
toEncodingList :: [SummaryKind] -> Encoding
$comitField :: SummaryKind -> Bool
omitField :: SummaryKind -> Bool
ToJSON)
data BarLayout
= Diverging
| Grouped
deriving (Int -> BarLayout -> ShowS
[BarLayout] -> ShowS
BarLayout -> String
(Int -> BarLayout -> ShowS)
-> (BarLayout -> String)
-> ([BarLayout] -> ShowS)
-> Show BarLayout
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> BarLayout -> ShowS
showsPrec :: Int -> BarLayout -> ShowS
$cshow :: BarLayout -> String
show :: BarLayout -> String
$cshowList :: [BarLayout] -> ShowS
showList :: [BarLayout] -> ShowS
Show, BarLayout -> BarLayout -> Bool
(BarLayout -> BarLayout -> Bool)
-> (BarLayout -> BarLayout -> Bool) -> Eq BarLayout
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: BarLayout -> BarLayout -> Bool
== :: BarLayout -> BarLayout -> Bool
$c/= :: BarLayout -> BarLayout -> Bool
/= :: BarLayout -> BarLayout -> Bool
Eq, (forall x. BarLayout -> Rep BarLayout x)
-> (forall x. Rep BarLayout x -> BarLayout) -> Generic BarLayout
forall x. Rep BarLayout x -> BarLayout
forall x. BarLayout -> Rep BarLayout x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. BarLayout -> Rep BarLayout x
from :: forall x. BarLayout -> Rep BarLayout x
$cto :: forall x. Rep BarLayout x -> BarLayout
to :: forall x. Rep BarLayout x -> BarLayout
Generic, [BarLayout] -> Encoding
[BarLayout] -> Value
BarLayout -> Bool
BarLayout -> Encoding
BarLayout -> Value
(BarLayout -> Value)
-> (BarLayout -> Encoding)
-> ([BarLayout] -> Value)
-> ([BarLayout] -> Encoding)
-> (BarLayout -> Bool)
-> ToJSON BarLayout
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> (a -> Bool)
-> ToJSON a
$ctoJSON :: BarLayout -> Value
toJSON :: BarLayout -> Value
$ctoEncoding :: BarLayout -> Encoding
toEncoding :: BarLayout -> Encoding
$ctoJSONList :: [BarLayout] -> Value
toJSONList :: [BarLayout] -> Value
$ctoEncodingList :: [BarLayout] -> Encoding
toEncodingList :: [BarLayout] -> Encoding
$comitField :: BarLayout -> Bool
omitField :: BarLayout -> Bool
ToJSON)
data Series = Series
{ Series -> ChartLabel
seLabel :: ChartLabel
, Series -> SeriesRole
seRole :: SeriesRole
, Series -> Mark
seMark :: Mark
, Series -> [Maybe Quantity]
seValues :: [Maybe Quantity]
, Series -> Int
seGroup :: Int
, Series -> Int
seSign :: Int
} deriving (Int -> Series -> ShowS
[Series] -> ShowS
Series -> String
(Int -> Series -> ShowS)
-> (Series -> String) -> ([Series] -> ShowS) -> Show Series
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Series -> ShowS
showsPrec :: Int -> Series -> ShowS
$cshow :: Series -> String
show :: Series -> String
$cshowList :: [Series] -> ShowS
showList :: [Series] -> ShowS
Show, Series -> Series -> Bool
(Series -> Series -> Bool)
-> (Series -> Series -> Bool) -> Eq Series
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Series -> Series -> Bool
== :: Series -> Series -> Bool
$c/= :: Series -> Series -> Bool
/= :: Series -> Series -> Bool
Eq, (forall x. Series -> Rep Series x)
-> (forall x. Rep Series x -> Series) -> Generic Series
forall x. Rep Series x -> Series
forall x. Series -> Rep Series x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. Series -> Rep Series x
from :: forall x. Series -> Rep Series x
$cto :: forall x. Rep Series x -> Series
to :: forall x. Rep Series x -> Series
Generic, [Series] -> Encoding
[Series] -> Value
Series -> Bool
Series -> Encoding
Series -> Value
(Series -> Value)
-> (Series -> Encoding)
-> ([Series] -> Value)
-> ([Series] -> Encoding)
-> (Series -> Bool)
-> ToJSON Series
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> (a -> Bool)
-> ToJSON a
$ctoJSON :: Series -> Value
toJSON :: Series -> Value
$ctoEncoding :: Series -> Encoding
toEncoding :: Series -> Encoding
$ctoJSONList :: [Series] -> Value
toJSONList :: [Series] -> Value
$ctoEncodingList :: [Series] -> Encoding
toEncodingList :: [Series] -> Encoding
$comitField :: Series -> Bool
omitField :: Series -> Bool
ToJSON)
data SeriesRole
= SeriesItem
| SeriesOther
| SeriesTotal
| SeriesShortfall
| SeriesProvisional
deriving (Int -> SeriesRole -> ShowS
[SeriesRole] -> ShowS
SeriesRole -> String
(Int -> SeriesRole -> ShowS)
-> (SeriesRole -> String)
-> ([SeriesRole] -> ShowS)
-> Show SeriesRole
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> SeriesRole -> ShowS
showsPrec :: Int -> SeriesRole -> ShowS
$cshow :: SeriesRole -> String
show :: SeriesRole -> String
$cshowList :: [SeriesRole] -> ShowS
showList :: [SeriesRole] -> ShowS
Show, SeriesRole -> SeriesRole -> Bool
(SeriesRole -> SeriesRole -> Bool)
-> (SeriesRole -> SeriesRole -> Bool) -> Eq SeriesRole
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: SeriesRole -> SeriesRole -> Bool
== :: SeriesRole -> SeriesRole -> Bool
$c/= :: SeriesRole -> SeriesRole -> Bool
/= :: SeriesRole -> SeriesRole -> Bool
Eq, (forall x. SeriesRole -> Rep SeriesRole x)
-> (forall x. Rep SeriesRole x -> SeriesRole) -> Generic SeriesRole
forall x. Rep SeriesRole x -> SeriesRole
forall x. SeriesRole -> Rep SeriesRole x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. SeriesRole -> Rep SeriesRole x
from :: forall x. SeriesRole -> Rep SeriesRole x
$cto :: forall x. Rep SeriesRole x -> SeriesRole
to :: forall x. Rep SeriesRole x -> SeriesRole
Generic, [SeriesRole] -> Encoding
[SeriesRole] -> Value
SeriesRole -> Bool
SeriesRole -> Encoding
SeriesRole -> Value
(SeriesRole -> Value)
-> (SeriesRole -> Encoding)
-> ([SeriesRole] -> Value)
-> ([SeriesRole] -> Encoding)
-> (SeriesRole -> Bool)
-> ToJSON SeriesRole
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> (a -> Bool)
-> ToJSON a
$ctoJSON :: SeriesRole -> Value
toJSON :: SeriesRole -> Value
$ctoEncoding :: SeriesRole -> Encoding
toEncoding :: SeriesRole -> Encoding
$ctoJSONList :: [SeriesRole] -> Value
toJSONList :: [SeriesRole] -> Value
$ctoEncodingList :: [SeriesRole] -> Encoding
toEncodingList :: [SeriesRole] -> Encoding
$comitField :: SeriesRole -> Bool
omitField :: SeriesRole -> Bool
ToJSON)
data Mark
= MarkBar
| MarkLine
| MarkMarker
| MarkNone
deriving (Int -> Mark -> ShowS
[Mark] -> ShowS
Mark -> String
(Int -> Mark -> ShowS)
-> (Mark -> String) -> ([Mark] -> ShowS) -> Show Mark
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Mark -> ShowS
showsPrec :: Int -> Mark -> ShowS
$cshow :: Mark -> String
show :: Mark -> String
$cshowList :: [Mark] -> ShowS
showList :: [Mark] -> ShowS
Show, Mark -> Mark -> Bool
(Mark -> Mark -> Bool) -> (Mark -> Mark -> Bool) -> Eq Mark
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Mark -> Mark -> Bool
== :: Mark -> Mark -> Bool
$c/= :: Mark -> Mark -> Bool
/= :: Mark -> Mark -> Bool
Eq, (forall x. Mark -> Rep Mark x)
-> (forall x. Rep Mark x -> Mark) -> Generic Mark
forall x. Rep Mark x -> Mark
forall x. Mark -> Rep Mark x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. Mark -> Rep Mark x
from :: forall x. Mark -> Rep Mark x
$cto :: forall x. Rep Mark x -> Mark
to :: forall x. Rep Mark x -> Mark
Generic, [Mark] -> Encoding
[Mark] -> Value
Mark -> Bool
Mark -> Encoding
Mark -> Value
(Mark -> Value)
-> (Mark -> Encoding)
-> ([Mark] -> Value)
-> ([Mark] -> Encoding)
-> (Mark -> Bool)
-> ToJSON Mark
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> (a -> Bool)
-> ToJSON a
$ctoJSON :: Mark -> Value
toJSON :: Mark -> Value
$ctoEncoding :: Mark -> Encoding
toEncoding :: Mark -> Encoding
$ctoJSONList :: [Mark] -> Value
toJSONList :: [Mark] -> Value
$ctoEncodingList :: [Mark] -> Encoding
toEncodingList :: [Mark] -> Encoding
$comitField :: Mark -> Bool
omitField :: Mark -> Bool
ToJSON)
isContra :: Series -> Quantity -> Bool
isContra :: Series -> Quantity -> Bool
isContra Series
s Quantity
q = Series -> Int
seSign Series
s Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= Int
0 Bool -> Bool -> Bool
&& Quantity
q Quantity -> Quantity -> Bool
forall a. Eq a => a -> a -> Bool
/= Quantity
0 Bool -> Bool -> Bool
&& Quantity -> Quantity
forall a. Num a => a -> a
signum Quantity
q Quantity -> Quantity -> Bool
forall a. Eq a => a -> a -> Bool
/= Int -> Quantity
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Series -> Int
seSign Series
s)
isHatched :: Series -> Quantity -> Bool
isHatched :: Series -> Quantity -> Bool
isHatched Series
s Quantity
q = Series -> Bool
hatchedSeries Series
s Bool -> Bool -> Bool
|| Series -> Quantity -> Bool
isContra Series
s Quantity
q
hatchedSeries :: Series -> Bool
hatchedSeries :: Series -> Bool
hatchedSeries Series
s = Series -> SeriesRole
seRole Series
s SeriesRole -> [SeriesRole] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [SeriesRole
SeriesShortfall, SeriesRole
SeriesProvisional]
legendListsSeries :: Bool -> [Series] -> Bool
legendListsSeries :: Bool -> [Series] -> Bool
legendListsSeries Bool
categories [Series]
ss =
[Series] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Series]
ss Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
1 Bool -> Bool -> Bool
|| (Series -> Bool) -> [Series] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any ((SeriesRole -> SeriesRole -> Bool
forall a. Eq a => a -> a -> Bool
== SeriesRole
SeriesOther) (SeriesRole -> Bool) -> (Series -> SeriesRole) -> Series -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Series -> SeriesRole
seRole) [Series]
ss Bool -> Bool -> Bool
|| (Bool -> Bool
not Bool
categories Bool -> Bool -> Bool
&& (Series -> Bool) -> [Series] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any (ChartLabel -> Bool
lbAccount (ChartLabel -> Bool) -> (Series -> ChartLabel) -> Series -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Series -> ChartLabel
seLabel) [Series]
ss)
mainAccountType :: AccountType -> AccountType
mainAccountType :: AccountType -> AccountType
mainAccountType AccountType
t = AccountType -> Maybe AccountType -> AccountType
forall a. a -> Maybe a -> a
fromMaybe AccountType
t (Maybe AccountType -> AccountType)
-> Maybe AccountType -> AccountType
forall a b. (a -> b) -> a -> b
$ (AccountType -> Bool) -> [AccountType] -> Maybe AccountType
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Maybe a
find (AccountType
t AccountType -> AccountType -> Bool
`isAccountSubtypeOf`) [AccountType
Asset, AccountType
Liability, AccountType
Equity, AccountType
Revenue, AccountType
Expense]
data Colour
= TypeColour AccountType Int
| IndexColour Int
| LoneColour
| NeutralColour
| OtherColour (Maybe AccountType)
| TotalColour
deriving (Int -> Colour -> ShowS
[Colour] -> ShowS
Colour -> String
(Int -> Colour -> ShowS)
-> (Colour -> String) -> ([Colour] -> ShowS) -> Show Colour
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Colour -> ShowS
showsPrec :: Int -> Colour -> ShowS
$cshow :: Colour -> String
show :: Colour -> String
$cshowList :: [Colour] -> ShowS
showList :: [Colour] -> ShowS
Show, Colour -> Colour -> Bool
(Colour -> Colour -> Bool)
-> (Colour -> Colour -> Bool) -> Eq Colour
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Colour -> Colour -> Bool
== :: Colour -> Colour -> Bool
$c/= :: Colour -> Colour -> Bool
/= :: Colour -> Colour -> Bool
Eq)
maxTints :: Int
maxTints :: Int
maxTints = Int
8
categoricalColours :: Int
categoricalColours :: Int
categoricalColours = Int
8
data Colouring
= ByName
| ByType
| Single
deriving (Int -> Colouring -> ShowS
[Colouring] -> ShowS
Colouring -> String
(Int -> Colouring -> ShowS)
-> (Colouring -> String)
-> ([Colouring] -> ShowS)
-> Show Colouring
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Colouring -> ShowS
showsPrec :: Int -> Colouring -> ShowS
$cshow :: Colouring -> String
show :: Colouring -> String
$cshowList :: [Colouring] -> ShowS
showList :: [Colouring] -> ShowS
Show, Colouring -> Colouring -> Bool
(Colouring -> Colouring -> Bool)
-> (Colouring -> Colouring -> Bool) -> Eq Colouring
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Colouring -> Colouring -> Bool
== :: Colouring -> Colouring -> Bool
$c/= :: Colouring -> Colouring -> Bool
/= :: Colouring -> Colouring -> Bool
Eq)
chartColouring :: Chart -> Colouring
chartColouring :: Chart -> Colouring
chartColouring Chart{[Panel]
chPanels :: Chart -> [Panel]
chPanels :: [Panel]
chPanels}
| (Panel -> Bool) -> [Panel] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all ((Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
1) (Int -> Bool) -> (Panel -> Int) -> Panel -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Series] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length ([Series] -> Int) -> (Panel -> [Series]) -> Panel -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Series -> Bool) -> [Series] -> [Series]
forall a. (a -> Bool) -> [a] -> [a]
filter ((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] -> [Series]) -> (Panel -> [Series]) -> Panel -> [Series]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Panel -> [Series]
pnSeries) [Panel]
chPanels = Colouring
Single
| [Bool] -> Bool
forall (t :: * -> *). Foldable t => t Bool -> Bool
or [Maybe AccountType -> Bool
forall a. Maybe a -> Bool
isJust (Maybe AccountType -> Bool) -> Maybe AccountType -> Bool
forall a b. (a -> b) -> a -> b
$ ChartLabel -> Maybe AccountType
lbType (ChartLabel -> Maybe AccountType)
-> ChartLabel -> Maybe AccountType
forall a b. (a -> b) -> a -> b
$ Series -> ChartLabel
seLabel Series
s | Panel
p <- [Panel]
chPanels, Series
s <- Panel -> [Series]
pnSeries Panel
p, Series -> SeriesRole
seRole Series
s SeriesRole -> [SeriesRole] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [SeriesRole
SeriesItem, SeriesRole
SeriesShortfall, SeriesRole
SeriesProvisional]] = Colouring
ByType
| Bool
otherwise = Colouring
ByName
seriesColours :: Colouring -> [Series] -> [Colour]
seriesColours :: Colouring -> [Series] -> [Colour]
seriesColours Colouring
colouring [Series]
series = (Series -> Int -> Colour) -> [Series] -> [Int] -> [Colour]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith Series -> Int -> Colour
colour [Series]
series' [Int]
slots
where
series' :: [Series]
series'
| Colouring
colouring Colouring -> Colouring -> Bool
forall a. Eq a => a -> a -> Bool
== Colouring
ByName = [Series
s{seLabel = (seLabel s){lbType = Nothing}} | Series
s <- [Series]
series]
| Bool
otherwise = [Series]
series
typeOf :: Series -> Maybe AccountType
typeOf Series
s = AccountType -> AccountType
mainAccountType (AccountType -> AccountType)
-> Maybe AccountType -> Maybe AccountType
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ChartLabel -> Maybe AccountType
lbType (Series -> ChartLabel
seLabel Series
s)
onlyone :: Bool
onlyone = [Series] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Series
s | Series
s <- [Series]
series, Series -> SeriesRole
seRole Series
s SeriesRole -> SeriesRole -> Bool
forall a. Eq a => a -> a -> Bool
/= SeriesRole
SeriesTotal] Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
1
slots :: [Int]
slots = [ 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 -> Map Int Int -> Maybe Int
forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup Int
i Map Int Int
slotmap | Int
i <- [Int
0 .. [Series] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Series]
series' Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1] ]
slotmap :: Map Int Int
slotmap = [(Int, Int)] -> Map Int Int
forall k a. Ord k => [(k, a)] -> Map k a
M.fromList ([(Int, Int)] -> Map Int Int) -> [(Int, Int)] -> Map Int Int
forall a b. (a -> b) -> a -> b
$ [[(Int, Int)]] -> [(Int, Int)]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat
[ [Int] -> [Int] -> [(Int, Int)]
forall a b. [a] -> [b] -> [(a, b)]
zip [Int]
is ([Int] -> [(Int, Int)]) -> [Int] -> [(Int, Int)]
forall a b. (a -> b) -> a -> b
$ Maybe AccountType -> [(CsvValue, Quantity)] -> [Int]
forall {a} {a}. Ord a => Maybe a -> [(CsvValue, a)] -> [Int]
slotsfor Maybe AccountType
t [(ChartLabel -> CsvValue
lbFull (ChartLabel -> CsvValue) -> ChartLabel -> CsvValue
forall a b. (a -> b) -> a -> b
$ Series -> ChartLabel
seLabel Series
s, Series -> Quantity
seriesSize Series
s) | Series
s <- [Series]
ss]
| Maybe AccountType
t <- [Maybe AccountType] -> [Maybe AccountType]
forall a. Eq a => [a] -> [a]
nub ([Maybe AccountType] -> [Maybe AccountType])
-> [Maybe AccountType] -> [Maybe AccountType]
forall a b. (a -> b) -> a -> b
$ ((Int, Series) -> Maybe AccountType)
-> [(Int, Series)] -> [Maybe AccountType]
forall a b. (a -> b) -> [a] -> [b]
map (Series -> Maybe AccountType
typeOf (Series -> Maybe AccountType)
-> ((Int, Series) -> Series) -> (Int, Series) -> Maybe AccountType
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Int, Series) -> Series
forall a b. (a, b) -> b
snd) [(Int, Series)]
named
, let ([Int]
is, [Series]
ss) = [(Int, Series)] -> ([Int], [Series])
forall a b. [(a, b)] -> ([a], [b])
unzip [(Int
i, Series
s) | (Int
i, Series
s) <- [(Int, Series)]
named, Series -> Maybe AccountType
typeOf Series
s Maybe AccountType -> Maybe AccountType -> Bool
forall a. Eq a => a -> a -> Bool
== Maybe AccountType
t] ]
named :: [(Int, Series)]
named = [(Int
i, Series
s) | (Int
i, Series
s) <- [Int] -> [Series] -> [(Int, Series)]
forall a b. [a] -> [b] -> [(a, b)]
zip [Int
0 :: Int ..] [Series]
series', Series -> SeriesRole
seRole Series
s SeriesRole -> SeriesRole -> Bool
forall a. Eq a => a -> a -> Bool
== SeriesRole
SeriesItem, ChartLabel -> Bool
lbAccount (Series -> ChartLabel
seLabel Series
s) Bool -> Bool -> Bool
|| Maybe AccountType -> Bool
forall a. Maybe a -> Bool
isNothing (Series -> Maybe AccountType
typeOf Series
s)]
slotsfor :: Maybe a -> [(CsvValue, a)] -> [Int]
slotsfor Maybe a
t = if Maybe a -> Bool
forall a. Maybe a -> Bool
isJust Maybe a
t then Double -> Int -> [(CsvValue, a)] -> [Int]
forall a. Ord a => Double -> Int -> [(CsvValue, a)] -> [Int]
stableSlotsFavouringFirst Double
0.5 Int
maxTints else Int -> [(CsvValue, a)] -> [Int]
forall a. Ord a => Int -> [(CsvValue, a)] -> [Int]
stableSlots Int
categoricalColours
colour :: Series -> Int -> Colour
colour Series
s Int
k = case (Series -> SeriesRole
seRole Series
s, Series -> Maybe AccountType
typeOf Series
s) of
(SeriesRole
SeriesTotal, Maybe AccountType
_) -> Colour
TotalColour
(SeriesRole
SeriesOther, Maybe AccountType
t) -> Maybe AccountType -> Colour
OtherColour Maybe AccountType
t
(SeriesRole
_, Just AccountType
t)
| ChartLabel -> Bool
lbAccount (Series -> ChartLabel
seLabel Series
s) -> AccountType -> Int -> Colour
TypeColour AccountType
t Int
k
| Bool
otherwise -> AccountType -> Int -> Colour
TypeColour AccountType
t Int
0
(SeriesRole
_, Maybe AccountType
Nothing)
| Bool
onlyone Bool -> Bool -> Bool
&& (Colouring
colouring Colouring -> Colouring -> Bool
forall a. Eq a => a -> a -> Bool
== Colouring
Single Bool -> Bool -> Bool
|| Bool -> Bool
not (ChartLabel -> Bool
lbAccount (ChartLabel -> Bool) -> ChartLabel -> Bool
forall a b. (a -> b) -> a -> b
$ Series -> ChartLabel
seLabel Series
s)) -> Colour
LoneColour
| Bool
otherwise -> Int -> Colour
IndexColour Int
k
categoryTints :: [ChartLabel] -> [Maybe Quantity] -> [Int]
categoryTints :: [ChartLabel] -> [Maybe Quantity] -> [Int]
categoryTints [ChartLabel]
labels [Maybe Quantity]
vals = [Int -> Int -> Map Int Int -> Int
forall k a. Ord k => a -> k -> Map k a -> a
M.findWithDefault Int
0 Int
j Map Int Int
tints | Int
j <- [Int
0 .. [ChartLabel] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [ChartLabel]
labels Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1]]
where
cats :: [(Int, Maybe AccountType, (CsvValue, Quantity))]
cats = [(Int
j, AccountType -> AccountType
mainAccountType (AccountType -> AccountType)
-> Maybe AccountType -> Maybe AccountType
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ChartLabel -> Maybe AccountType
lbType ChartLabel
l, (ChartLabel -> CsvValue
lbFull ChartLabel
l, 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 Maybe Quantity
v)) | (Int
j, ChartLabel
l, Maybe Quantity
v) <- [Int]
-> [ChartLabel]
-> [Maybe Quantity]
-> [(Int, ChartLabel, Maybe Quantity)]
forall a b c. [a] -> [b] -> [c] -> [(a, b, c)]
zip3 [Int
0 :: Int ..] [ChartLabel]
labels ([Maybe Quantity]
vals [Maybe Quantity] -> [Maybe Quantity] -> [Maybe Quantity]
forall a. [a] -> [a] -> [a]
++ Maybe Quantity -> [Maybe Quantity]
forall a. a -> [a]
repeat Maybe Quantity
forall a. Maybe a
Nothing)]
tints :: Map Int Int
tints = [(Int, Int)] -> Map Int Int
forall k a. Ord k => [(k, a)] -> Map k a
M.fromList ([(Int, Int)] -> Map Int Int) -> [(Int, Int)] -> Map Int Int
forall a b. (a -> b) -> a -> b
$ [[(Int, Int)]] -> [(Int, Int)]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat
[ [Int] -> [Int] -> [(Int, Int)]
forall a b. [a] -> [b] -> [(a, b)]
zip [Int]
js ([Int] -> [(Int, Int)]) -> [Int] -> [(Int, Int)]
forall a b. (a -> b) -> a -> b
$ Double -> Int -> [(CsvValue, Quantity)] -> [Int]
forall a. Ord a => Double -> Int -> [(CsvValue, a)] -> [Int]
stableSlotsFavouringFirst Double
0.5 Int
maxTints [(CsvValue, Quantity)]
items
| Maybe AccountType
t <- [Maybe AccountType] -> [Maybe AccountType]
forall a. Eq a => [a] -> [a]
nub [Maybe AccountType
t | (Int
_, Maybe AccountType
t, (CsvValue, Quantity)
_) <- [(Int, Maybe AccountType, (CsvValue, Quantity))]
cats]
, let ([Int]
js, [(CsvValue, Quantity)]
items) = [(Int, (CsvValue, Quantity))] -> ([Int], [(CsvValue, Quantity)])
forall a b. [(a, b)] -> ([a], [b])
unzip [(Int
j, (CsvValue, Quantity)
item) | (Int
j, Maybe AccountType
t', (CsvValue, Quantity)
item) <- [(Int, Maybe AccountType, (CsvValue, Quantity))]
cats, Maybe AccountType
t' Maybe AccountType -> Maybe AccountType -> Bool
forall a. Eq a => a -> a -> Bool
== Maybe AccountType
t] ]
seriesSize :: Series -> Quantity
seriesSize :: Series -> Quantity
seriesSize = [Quantity] -> Quantity
forall a. Num a => [a] -> a
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum ([Quantity] -> Quantity)
-> (Series -> [Quantity]) -> Series -> Quantity
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Quantity -> Quantity) -> [Quantity] -> [Quantity]
forall a b. (a -> b) -> [a] -> [b]
map Quantity -> Quantity
forall a. Num a => a -> a
abs ([Quantity] -> [Quantity])
-> (Series -> [Quantity]) -> Series -> [Quantity]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Maybe Quantity] -> [Quantity]
forall a. [Maybe a] -> [a]
catMaybes ([Maybe Quantity] -> [Quantity])
-> (Series -> [Maybe Quantity]) -> Series -> [Quantity]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Series -> [Maybe Quantity]
seValues
stableSlots :: Ord a => Int -> [(Text, a)] -> [Int]
stableSlots :: forall a. Ord a => Int -> [(CsvValue, a)] -> [Int]
stableSlots = Double -> Int -> [(CsvValue, a)] -> [Int]
forall a. Ord a => Double -> Int -> [(CsvValue, a)] -> [Int]
stableSlotsFavouringFirst Double
0
stableSlotsFavouringFirst :: Ord a => Double -> Int -> [(Text, a)] -> [Int]
stableSlotsFavouringFirst :: forall a. Ord a => Double -> Int -> [(CsvValue, a)] -> [Int]
stableSlotsFavouringFirst Double
bias Int
n [(CsvValue, a)]
items
| Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0 = ((CsvValue, a) -> Int) -> [(CsvValue, a)] -> [Int]
forall a b. (a -> b) -> [a] -> [b]
map (Int -> (CsvValue, a) -> Int
forall a b. a -> b -> a
const Int
0) [(CsvValue, a)]
items
| Bool
otherwise = [ 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 -> Map Int Int -> Maybe Int
forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup Int
i Map Int Int
assigned | Int
i <- [Int
0 .. [(CsvValue, a)] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [(CsvValue, a)]
items Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1] ]
where
assigned :: Map Int Int
assigned = (Set Int, Map Int Int) -> Map Int Int
forall a b. (a, b) -> b
snd ((Set Int, Map Int Int) -> Map Int Int)
-> (Set Int, Map Int Int) -> Map Int Int
forall a b. (a -> b) -> a -> b
$ ((Set Int, Map Int Int)
-> (Int, (CsvValue, a)) -> (Set Int, Map Int Int))
-> (Set Int, Map Int Int)
-> [(Int, (CsvValue, a))]
-> (Set Int, Map Int Int)
forall b a. (b -> a -> b) -> b -> [a] -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' (Set Int, Map Int Int)
-> (Int, (CsvValue, a)) -> (Set Int, Map Int Int)
forall {k} {b}.
Ord k =>
(Set Int, Map k Int) -> (k, (CsvValue, b)) -> (Set Int, Map k Int)
place (Set Int
forall a. Set a
S.empty, Map Int Int
forall k a. Map k a
M.empty) ([(Int, (CsvValue, a))] -> (Set Int, Map Int Int))
-> [(Int, (CsvValue, a))] -> (Set Int, Map Int Int)
forall a b. (a -> b) -> a -> b
$ ((Int, (CsvValue, a)) -> (Down a, CsvValue, Int))
-> [(Int, (CsvValue, a))] -> [(Int, (CsvValue, a))]
forall b a. Ord b => (a -> b) -> [a] -> [a]
sortOn (\(Int
i, (CsvValue
nm, a
size)) -> (a -> Down a
forall a. a -> Down a
Down a
size, CsvValue
nm, Int
i)) ([(Int, (CsvValue, a))] -> [(Int, (CsvValue, a))])
-> [(Int, (CsvValue, a))] -> [(Int, (CsvValue, a))]
forall a b. (a -> b) -> a -> b
$ [Int] -> [(CsvValue, a)] -> [(Int, (CsvValue, a))]
forall a b. [a] -> [b] -> [(a, b)]
zip [Int
0 :: Int ..] [(CsvValue, a)]
items
place :: (Set Int, Map k Int) -> (k, (CsvValue, b)) -> (Set Int, Map k Int)
place (Set Int
used, Map k Int
m) (k
i, (CsvValue
nm, b
_)) =
let prefs :: [Int]
prefs = CsvValue -> [Int]
preferences CsvValue
nm
used' :: Set Int
used' = if Set Int -> Int
forall a. Set a -> Int
S.size Set Int
used Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
n then Set Int
forall a. Set a
S.empty else Set Int
used
slot :: Int
slot = Int -> Maybe Int -> Int
forall a. a -> Maybe a -> a
fromMaybe (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] -> Maybe Int
forall a. [a] -> Maybe a
listToMaybe [Int]
prefs) (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 -> Set Int -> Bool
forall a. Ord a => a -> Set a -> Bool
`S.notMember` Set Int
used') [Int]
prefs
in (Int -> Set Int -> Set Int
forall a. Ord a => a -> Set a -> Set a
S.insert Int
slot Set Int
used', k -> Int -> Map k Int -> Map k Int
forall k a. Ord k => k -> a -> Map k a -> Map k a
M.insert k
i Int
slot Map k Int
m)
preferences :: CsvValue -> [Int]
preferences CsvValue
nm = (Int -> Double) -> [Int] -> [Int]
forall b a. Ord b => (a -> b) -> [a] -> [a]
sortOn (\Int
k -> Double
bias Double -> Double -> Double
forall a. Num a => a -> a -> a
* Int -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
k Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Word32 -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral (CsvValue -> Word32
hash (CsvValue
nm CsvValue -> CsvValue -> CsvValue
forall a. Semigroup a => a -> a -> a
<> CsvValue
"/" CsvValue -> CsvValue -> CsvValue
forall a. Semigroup a => a -> a -> a
<> String -> CsvValue
T.pack (Int -> String
forall a. Show a => a -> String
show Int
k))) Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Double
2 Double -> Int -> Double
forall a b. (Num a, Integral b) => a -> b -> a
^ (Int
32 :: Int)) [Int
0 .. Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1]
hash :: Text -> Word32
hash :: CsvValue -> Word32
hash = Word32 -> Word32
forall {a}. (Num a, Bits a) => a -> a
fmix32 (Word32 -> Word32) -> (CsvValue -> Word32) -> CsvValue -> Word32
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Word32 -> Char -> Word32) -> Word32 -> CsvValue -> Word32
forall a. (a -> Char -> a) -> a -> CsvValue -> a
T.foldl' (\Word32
h Char
c -> (Word32
h Word32 -> Word32 -> Word32
forall a. Bits a => a -> a -> a
`xor` Int -> Word32
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Char -> Int
ord Char
c)) Word32 -> Word32 -> Word32
forall a. Num a => a -> a -> a
* Word32
16777619) Word32
2166136261
fmix32 :: a -> a
fmix32 a
h0 =
let h1 :: a
h1 = (a
h0 a -> a -> a
forall a. Bits a => a -> a -> a
`xor` (a
h0 a -> Int -> a
forall a. Bits a => a -> Int -> a
`shiftR` Int
16)) a -> a -> a
forall a. Num a => a -> a -> a
* a
0x85ebca6b
h2 :: a
h2 = (a
h1 a -> a -> a
forall a. Bits a => a -> a -> a
`xor` (a
h1 a -> Int -> a
forall a. Bits a => a -> Int -> a
`shiftR` Int
13)) a -> a -> a
forall a. Num a => a -> a -> a
* a
0xc2b2ae35
in a
h2 a -> a -> a
forall a. Bits a => a -> a -> a
`xor` (a
h2 a -> Int -> a
forall a. Bits a => a -> Int -> a
`shiftR` Int
16)
type SeriesValues = Array Int (Maybe Quantity)
seriesValues :: Series -> SeriesValues
seriesValues :: Series -> SeriesValues
seriesValues Series{[Maybe Quantity]
seValues :: Series -> [Maybe Quantity]
seValues :: [Maybe Quantity]
seValues} = (Int, Int) -> [Maybe Quantity] -> SeriesValues
forall i e. Ix i => (i, i) -> [e] -> Array i e
listArray (Int
0, [Maybe Quantity] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Maybe Quantity]
seValues Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) [Maybe Quantity]
seValues
valueAt :: SeriesValues -> Int -> Maybe Quantity
valueAt :: SeriesValues -> Int -> Maybe Quantity
valueAt SeriesValues
a Int
j = if (Int, Int) -> Int -> Bool
forall a. Ix a => (a, a) -> a -> Bool
inRange (SeriesValues -> (Int, Int)
forall i e. Array i e -> (i, i)
bounds SeriesValues
a) Int
j then SeriesValues
a SeriesValues -> Int -> Maybe Quantity
forall i e. Ix i => Array i e -> i -> e
! Int
j else Maybe Quantity
forall a. Maybe a
Nothing
setChartScales :: [Amount] -> Chart -> Chart
setChartScales :: [Amount] -> Chart -> Chart
setChartScales [Amount]
scales Chart
chart = Chart
chart{chPanels = map setscale $ chPanels chart}
where
setscale :: Panel -> Panel
setscale Panel
p = Panel
p{pnScale = lookup (acommodity $ pnUnit p) byc <|> lookup "" byc <|> pnScale p}
byc :: [(CsvValue, Quantity)]
byc = [(CsvValue, Quantity)] -> [(CsvValue, Quantity)]
forall a. [a] -> [a]
reverse [(Amount -> CsvValue
acommodity Amount
a, Quantity -> Quantity
forall a. Num a => a -> a
abs (Quantity -> Quantity) -> Quantity -> Quantity
forall a b. (a -> b) -> a -> b
$ Amount -> Quantity
aquantity Amount
a) | Amount
a <- [Amount]
scales]
chartWithoutTotalMarks :: Chart -> Chart
chartWithoutTotalMarks :: Chart -> Chart
chartWithoutTotalMarks Chart
chart = Chart
chart{chPanels = [p{pnSeries = map unmark $ pnSeries p} | p <- chPanels chart]}
where
unmark :: Series -> Series
unmark Series
s = if Series -> SeriesRole
seRole Series
s SeriesRole -> SeriesRole -> Bool
forall a. Eq a => a -> a -> Bool
== SeriesRole
SeriesTotal then Series
s{seMark = MarkNone} else Series
s
setChartLayout :: Maybe BarLayout -> Chart -> Chart
setChartLayout :: Maybe BarLayout -> Chart -> Chart
setChartLayout Maybe BarLayout
ml Chart
chart = Chart
chart{chPanels = map setlayout $ chPanels chart}
where
setlayout :: Panel -> Panel
setlayout Panel
p = Panel
p{pnLayout = l, pnSeries = ingrouporder $ map (asbar . resign l) (pnSeries p)}
where
l :: BarLayout
l = BarLayout -> Maybe BarLayout -> BarLayout
forall a. a -> Maybe a -> a
fromMaybe (Panel -> BarLayout
pnLayout Panel
p) Maybe BarLayout
ml
ingrouporder :: [Series] -> [Series]
ingrouporder [Series]
ss
| BarLayout
l BarLayout -> BarLayout -> Bool
forall a. Eq a => a -> a -> Bool
== BarLayout
Grouped = (Series -> Int) -> [Series] -> [Series]
forall b a. Ord b => (a -> b) -> [a] -> [a]
sortOn Series -> Int
seGroup [Series]
items [Series] -> [Series] -> [Series]
forall a. [a] -> [a] -> [a]
++ [Series]
totals
| Bool
otherwise = [Series]
ss
where ([Series]
totals, [Series]
items) = (Series -> Bool) -> [Series] -> ([Series], [Series])
forall a. (a -> Bool) -> [a] -> ([a], [a])
partition ((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]
ss
asbar :: Series -> Series
asbar Series
s | BarLayout
l BarLayout -> BarLayout -> Bool
forall a. Eq a => a -> a -> Bool
== BarLayout
Grouped, [CsvValue] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length (Panel -> [CsvValue]
pnGroupNames Panel
p) Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
1, 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 a. Eq a => a -> a -> Bool
== Mark
MarkLine = Series
s{seMark = MarkBar}
asbar Series
s = Series
s
resign :: BarLayout -> Series -> Series
resign BarLayout
Grouped Series
s | Series -> Int
seSign Series
s Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= Int
0 = Series
s{seValues = map (fmap (* fromIntegral (seSign s))) (seValues s), seSign = 1}
resign BarLayout
_ Series
s = Series
s
scaleSpans :: (Ord a, Fractional a) => a -> a -> a -> (a, a)
scaleSpans :: forall a. (Ord a, Fractional a) => a -> a -> a -> (a, a)
scaleSpans a
scale a
maxpos a
maxneg
| a
maxpos a -> a -> Bool
forall a. Ord a => a -> a -> Bool
<= a
0 Bool -> Bool -> Bool
&& a
maxneg a -> a -> Bool
forall a. Ord a => a -> a -> Bool
> a
0 = (a
0, a
scale)
| a
maxpos a -> a -> a
forall a. Num a => a -> a -> a
+ a
maxneg a -> a -> Bool
forall a. Ord a => a -> a -> Bool
<= a
scale = (a
scale a -> a -> a
forall a. Num a => a -> a -> a
- a
maxneg, a
maxneg)
| Bool
otherwise = (a
scale a -> a -> a
forall a. Num a => a -> a -> a
- a
neg, a
neg)
where neg :: a
neg = a
maxneg a -> a -> a
forall a. Num a => a -> a -> a
* a
scale a -> a -> a
forall a. Fractional a => a -> a -> a
/ (a
maxpos a -> a -> a
forall a. Num a => a -> a -> a
+ a
maxneg)
chartAsCsv :: Chart -> CSV
chartAsCsv :: Chart -> CSV
chartAsCsv Chart{XAxis
chXAxis :: Chart -> XAxis
chXAxis :: XAxis
chXAxis, [Panel]
chPanels :: Chart -> [Panel]
chPanels :: [Panel]
chPanels} =
[CsvValue
"commodity", CsvValue
"series", CsvValue
"role", CsvValue
"mark", CsvValue
"x", CsvValue
"value"]
[CsvValue] -> CSV -> CSV
forall a. a -> [a] -> [a]
: [CSV] -> CSV
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat
[ [ [Amount -> CsvValue
acommodity Amount
pnUnit, ChartLabel -> CsvValue
lbFull (Series -> ChartLabel
seLabel Series
s), SeriesRole -> CsvValue
forall {a}. IsString a => SeriesRole -> a
roleName (Series -> SeriesRole
seRole Series
s), Mark -> CsvValue
forall {a}. IsString a => Mark -> a
markName (Series -> Mark
seMark Series
s), CsvValue
x, CsvValue -> (Quantity -> CsvValue) -> Maybe Quantity -> CsvValue
forall b a. b -> (a -> b) -> Maybe a -> b
maybe CsvValue
"" (Amount -> Quantity -> CsvValue
showValue Amount
pnUnit) Maybe Quantity
mq]
| Series
s <- [Series]
pnSeries
, (CsvValue
x, Maybe Quantity
mq) <- [CsvValue] -> [Maybe Quantity] -> [(CsvValue, Maybe Quantity)]
forall a b. [a] -> [b] -> [(a, b)]
zip [CsvValue]
xlabels (Series -> [Maybe Quantity]
seValues Series
s)
]
CSV -> CSV -> CSV
forall a. [a] -> [a] -> [a]
++ [ [Amount -> CsvValue
acommodity Amount
pnUnit, CsvValue
suLabel, SummaryKind -> CsvValue
forall {a}. IsString a => SummaryKind -> a
summaryName SummaryKind
suKind, CsvValue
"", CsvValue
"", Amount -> Quantity -> CsvValue
showValue Amount
suValue (Amount -> Quantity
aquantity Amount
suValue)] | Summary{SummaryKind
suKind :: Summary -> SummaryKind
suKind :: SummaryKind
suKind, CsvValue
suLabel :: Summary -> CsvValue
suLabel :: CsvValue
suLabel, Amount
suValue :: Summary -> Amount
suValue :: Amount
suValue} <- [Summary]
pnSummary ]
| Panel{Amount
pnUnit :: Panel -> Amount
pnUnit :: Amount
pnUnit, [Series]
pnSeries :: Panel -> [Series]
pnSeries :: [Series]
pnSeries, [Summary]
pnSummary :: Panel -> [Summary]
pnSummary :: [Summary]
pnSummary} <- [Panel]
chPanels
]
where
xlabels :: [CsvValue]
xlabels = case XAxis
chXAxis of
XCategories [ChartLabel]
ls -> (ChartLabel -> CsvValue) -> [ChartLabel] -> [CsvValue]
forall a b. (a -> b) -> [a] -> [b]
map ChartLabel -> CsvValue
lbFull [ChartLabel]
ls
XPeriods [(DateSpan, CsvValue)]
ps -> ((DateSpan, CsvValue) -> CsvValue)
-> [(DateSpan, CsvValue)] -> [CsvValue]
forall a b. (a -> b) -> [a] -> [b]
map (DateSpan, CsvValue) -> CsvValue
forall a b. (a, b) -> b
snd [(DateSpan, CsvValue)]
ps
XDates [Day]
ds -> (Day -> CsvValue) -> [Day] -> [CsvValue]
forall a b. (a -> b) -> [a] -> [b]
map Day -> CsvValue
showDate [Day]
ds
showValue :: Amount -> Quantity -> CsvValue
showValue Amount
unit Quantity
q = String -> CsvValue
T.pack (String -> CsvValue) -> String -> CsvValue
forall a b. (a -> b) -> a -> b
$ AmountFormat -> Amount -> String
showAmountWith AmountFormat
machineFmt{displayCommodity=False} Amount
unit{aquantity=q}
roleName :: SeriesRole -> a
roleName SeriesRole
r = case SeriesRole
r of
SeriesRole
SeriesItem -> a
"item"
SeriesRole
SeriesOther -> a
"other"
SeriesRole
SeriesTotal -> a
"total"
SeriesRole
SeriesShortfall -> a
"shortfall"
SeriesRole
SeriesProvisional -> a
"provisional"
markName :: Mark -> a
markName Mark
m = case Mark
m of
Mark
MarkBar -> a
"bar"
Mark
MarkLine -> a
"line"
Mark
MarkMarker -> a
"marker"
Mark
MarkNone -> a
"none"
summaryName :: SummaryKind -> a
summaryName SummaryKind
k = case SummaryKind
k of
SummaryKind
SummaryTotal -> a
"rowtotal"
SummaryKind
SummaryAverage -> a
"average"
tests_Hledger_Write_Chart :: TestTree
tests_Hledger_Write_Chart :: TestTree
tests_Hledger_Write_Chart = String -> [TestTree] -> TestTree
testGroup String
"Write.Chart" [
String -> Assertion -> TestTree
testCase String
"scaleSpans" (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$ do
Quantity -> Quantity -> Quantity -> (Quantity, Quantity)
forall a. (Ord a, Fractional a) => a -> a -> a -> (a, a)
scaleSpans Quantity
100 Quantity
80 Quantity
0 (Quantity, Quantity) -> (Quantity, Quantity) -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= (Quantity
100 :: Quantity, Quantity
0)
Double -> Double -> Double -> (Double, Double)
forall a. (Ord a, Fractional a) => a -> a -> a -> (a, a)
scaleSpans Double
100 Double
0 Double
30 (Double, Double) -> (Double, Double) -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= (Double
0, Double
100)
Double -> Double -> Double -> (Double, Double)
forall a. (Ord a, Fractional a) => a -> a -> a -> (a, a)
scaleSpans Double
100 Double
50 Double
30 (Double, Double) -> (Double, Double) -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= (Double
70, Double
30)
Double -> Double -> Double -> (Double, Double)
forall a. (Ord a, Fractional a) => a -> a -> a -> (a, a)
scaleSpans Double
100 Double
150 Double
50 (Double, Double) -> (Double, Double) -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= (Double
75, Double
25)
Double -> Double -> Double -> (Double, Double)
forall a. (Ord a, Fractional a) => a -> a -> a -> (a, a)
scaleSpans Double
100 Double
0 Double
0 (Double, Double) -> (Double, Double) -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= (Double
100, Double
0)
,String -> Assertion -> TestTree
testCase String
"seriesColours" (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$ do
let ser :: CsvValue -> Maybe AccountType -> Series
ser CsvValue
n Maybe AccountType
t = ChartLabel
-> SeriesRole -> Mark -> [Maybe Quantity] -> Int -> Int -> Series
Series (CsvValue
-> CsvValue
-> Int
-> Maybe AccountType
-> Bool
-> [CsvValue]
-> ChartLabel
ChartLabel CsvValue
n CsvValue
n Int
0 Maybe AccountType
t Bool
True []) SeriesRole
SeriesItem Mark
MarkBar [Quantity -> Maybe Quantity
forall a. a -> Maybe a
Just Quantity
1] Int
0 Int
0
tints :: [Colour] -> [Int]
tints [Colour]
cs = [Int
k | TypeColour AccountType
_ Int
k <- [Colour]
cs]
let cs :: [Colour]
cs = Colouring -> [Series] -> [Colour]
seriesColours Colouring
ByType [CsvValue -> Maybe AccountType -> Series
ser CsvValue
"e1" (AccountType -> Maybe AccountType
forall a. a -> Maybe a
Just AccountType
Expense), CsvValue -> Maybe AccountType -> Series
ser CsvValue
"a" (AccountType -> Maybe AccountType
forall a. a -> Maybe a
Just AccountType
Cash), CsvValue -> Maybe AccountType -> Series
ser CsvValue
"u1" Maybe AccountType
forall a. Maybe a
Nothing, CsvValue -> Maybe AccountType -> Series
ser CsvValue
"e2" (AccountType -> Maybe AccountType
forall a. a -> Maybe a
Just AccountType
Expense), CsvValue -> Maybe AccountType -> Series
ser CsvValue
"u2" Maybe AccountType
forall a. Maybe a
Nothing]
[Colour] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Colour
c | c :: Colour
c@(TypeColour AccountType
Asset Int
_) <- [Colour]
cs] Int -> Int -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= Int
1
[Int] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length ([Int] -> [Int]
forall a. Eq a => [a] -> [a]
nub ([Int] -> [Int]) -> [Int] -> [Int]
forall a b. (a -> b) -> a -> b
$ [Colour] -> [Int]
tints [Colour
c | c :: Colour
c@(TypeColour AccountType
Expense Int
_) <- [Colour]
cs]) Int -> Int -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= Int
2
[Int] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length ([Int] -> [Int]
forall a. Eq a => [a] -> [a]
nub [Int
k | IndexColour Int
k <- [Colour]
cs]) Int -> Int -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= Int
2
[Int] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length ([Int] -> [Int]
forall a. Eq a => [a] -> [a]
nub ([Int] -> [Int]) -> [Int] -> [Int]
forall a b. (a -> b) -> a -> b
$ [Colour] -> [Int]
tints ([Colour] -> [Int]) -> [Colour] -> [Int]
forall a b. (a -> b) -> a -> b
$ Colouring -> [Series] -> [Colour]
seriesColours Colouring
ByType [CsvValue -> Maybe AccountType -> Series
ser (String -> CsvValue
T.pack (String -> CsvValue) -> String -> CsvValue
forall a b. (a -> b) -> a -> b
$ Int -> String
forall a. Show a => a -> String
show Int
i) (AccountType -> Maybe AccountType
forall a. a -> Maybe a
Just AccountType
Expense) | Int
i <- [Int
1 .. Int
maxTints]]) Int -> Int -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= Int
maxTints
let chartOf :: [Maybe AccountType] -> Chart
chartOf [Maybe AccountType]
ts = CsvValue -> XAxis -> [Panel] -> [CsvValue] -> Chart
Chart CsvValue
"" ([ChartLabel] -> XAxis
XCategories []) [Amount
-> [Series]
-> [CsvValue]
-> Maybe Quantity
-> BarLayout
-> Bool
-> [Summary]
-> [Maybe CsvValue]
-> Panel
Panel Amount
nullamt [CsvValue -> Maybe AccountType -> Series
ser (String -> CsvValue
T.pack (String -> CsvValue) -> String -> CsvValue
forall a b. (a -> b) -> a -> b
$ Int -> String
forall a. Show a => a -> String
show Int
i) Maybe AccountType
t | (Int
i, Maybe AccountType
t) <- [Int] -> [Maybe AccountType] -> [(Int, Maybe AccountType)]
forall a b. [a] -> [b] -> [(a, b)]
zip [Int
1 :: Int ..] [Maybe AccountType]
ts] [] Maybe Quantity
forall a. Maybe a
Nothing BarLayout
Diverging Bool
False [] []] []
Chart -> Colouring
chartColouring ([Maybe AccountType] -> Chart
chartOf [AccountType -> Maybe AccountType
forall a. a -> Maybe a
Just AccountType
Expense, AccountType -> Maybe AccountType
forall a. a -> Maybe a
Just AccountType
Expense]) Colouring -> Colouring -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= Colouring
ByType
Chart -> Colouring
chartColouring ([Maybe AccountType] -> Chart
chartOf [AccountType -> Maybe AccountType
forall a. a -> Maybe a
Just AccountType
Expense, Maybe AccountType
forall a. Maybe a
Nothing]) Colouring -> Colouring -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= Colouring
ByType
Chart -> Colouring
chartColouring ([Maybe AccountType] -> Chart
chartOf [Maybe AccountType
forall a. Maybe a
Nothing, Maybe AccountType
forall a. Maybe a
Nothing]) Colouring -> Colouring -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= Colouring
ByName
Chart -> Colouring
chartColouring ([Maybe AccountType] -> Chart
chartOf [AccountType -> Maybe AccountType
forall a. a -> Maybe a
Just AccountType
Expense]) Colouring -> Colouring -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= Colouring
Single
Colouring -> [Series] -> [Colour]
seriesColours Colouring
ByType [CsvValue -> Maybe AccountType -> Series
ser CsvValue
"e2" (AccountType -> Maybe AccountType
forall a. a -> Maybe a
Just AccountType
Expense), CsvValue -> Maybe AccountType -> Series
ser CsvValue
"e1" (AccountType -> Maybe AccountType
forall a. a -> Maybe a
Just AccountType
Expense)] [Colour] -> [Colour] -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= [Colour] -> [Colour]
forall a. [a] -> [a]
reverse (Colouring -> [Series] -> [Colour]
seriesColours Colouring
ByType [CsvValue -> Maybe AccountType -> Series
ser CsvValue
"e1" (AccountType -> Maybe AccountType
forall a. a -> Maybe a
Just AccountType
Expense), CsvValue -> Maybe AccountType -> Series
ser CsvValue
"e2" (AccountType -> Maybe AccountType
forall a. a -> Maybe a
Just AccountType
Expense)])
let rs :: [Colour]
rs = Colouring -> [Series] -> [Colour]
seriesColours Colouring
ByName [CsvValue -> Maybe AccountType -> Series
ser (String -> CsvValue
T.pack (String -> CsvValue) -> String -> CsvValue
forall a b. (a -> b) -> a -> b
$ Int -> String
forall a. Show a => a -> String
show Int
i) (AccountType -> Maybe AccountType
forall a. a -> Maybe a
Just AccountType
Revenue) | Int
i <- [Int
1 .. Int
categoricalColours]]
[Int] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length ([Int] -> [Int]
forall a. Eq a => [a] -> [a]
nub [Int
k | IndexColour Int
k <- [Colour]
rs]) Int -> Int -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= Int
categoricalColours
Colouring -> [Series] -> [Colour]
seriesColours Colouring
ByName (Int -> [Series] -> [Series]
forall a. Int -> [a] -> [a]
take Int
1 [CsvValue -> Maybe AccountType -> Series
ser (String -> CsvValue
T.pack (String -> CsvValue) -> String -> CsvValue
forall a b. (a -> b) -> a -> b
$ Int -> String
forall a. Show a => a -> String
show Int
i) (AccountType -> Maybe AccountType
forall a. a -> Maybe a
Just AccountType
Revenue) | Int
i <- [Int
1 .. Int
categoricalColours]]) [Colour] -> [Colour] -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= Int -> [Colour] -> [Colour]
forall a. Int -> [a] -> [a]
take Int
1 [Colour]
rs
let nonacct :: CsvValue -> AccountType -> Series
nonacct CsvValue
n AccountType
t = (CsvValue -> Maybe AccountType -> Series
ser CsvValue
n (AccountType -> Maybe AccountType
forall a. a -> Maybe a
Just AccountType
t)){seLabel = ChartLabel n n 0 (Just t) False []}
Colouring -> [Series] -> [Colour]
seriesColours Colouring
ByType [CsvValue -> AccountType -> Series
nonacct CsvValue
n AccountType
t | (CsvValue
n, AccountType
t) <- [(CsvValue
"Kosten", AccountType
Asset), (CsvValue
"Gewinn", AccountType
Revenue), (CsvValue
"Verlust", AccountType
Expense)]]
[Colour] -> [Colour] -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= [AccountType -> Int -> Colour
TypeColour AccountType
Asset Int
0, AccountType -> Int -> Colour
TypeColour AccountType
Revenue Int
0, AccountType -> Int -> Colour
TypeColour AccountType
Expense Int
0]
Colouring -> [Series] -> [Colour]
seriesColours Colouring
ByName [(CsvValue -> Maybe AccountType -> Series
ser CsvValue
"u" Maybe AccountType
forall a. Maybe a
Nothing){seLabel = ChartLabel "u" "u" 0 Nothing False []}] [Colour] -> [Colour] -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= [Colour
LoneColour]
Colouring -> [Series] -> [Colour]
seriesColours Colouring
ByName [(CsvValue -> Maybe AccountType -> Series
ser CsvValue
"o" Maybe AccountType
forall a. Maybe a
Nothing){seRole = SeriesOther}, (CsvValue -> Maybe AccountType -> Series
ser CsvValue
"t" Maybe AccountType
forall a. Maybe a
Nothing){seRole = SeriesTotal}]
[Colour] -> [Colour] -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= [Maybe AccountType -> Colour
OtherColour Maybe AccountType
forall a. Maybe a
Nothing, Colour
TotalColour]
Colouring -> [Series] -> [Colour]
seriesColours Colouring
Single [CsvValue -> Maybe AccountType -> Series
ser CsvValue
"e" (AccountType -> Maybe AccountType
forall a. a -> Maybe a
Just AccountType
Expense)] [Colour] -> [Colour] -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= (Int -> Colour) -> [Int] -> [Colour]
forall a b. (a -> b) -> [a] -> [b]
map (AccountType -> Int -> Colour
TypeColour AccountType
Expense) (Double -> Int -> [(CsvValue, Quantity)] -> [Int]
forall a. Ord a => Double -> Int -> [(CsvValue, a)] -> [Int]
stableSlotsFavouringFirst Double
0.5 Int
maxTints [(CsvValue
"e", Quantity
1 :: Quantity)])
Colouring -> [Series] -> [Colour]
seriesColours Colouring
Single [CsvValue -> Maybe AccountType -> Series
ser CsvValue
"u" Maybe AccountType
forall a. Maybe a
Nothing] [Colour] -> [Colour] -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= [Colour
LoneColour]
,String -> Assertion -> TestTree
testCase String
"stableSlots" (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$ do
let items :: [(CsvValue, Int)]
items = [(CsvValue
"a", Int
3), (CsvValue
"b", Int
2), (CsvValue
"c", Int
1)] :: [(Text, Int)]
slots :: [Int]
slots = Int -> [(CsvValue, Int)] -> [Int]
forall a. Ord a => Int -> [(CsvValue, a)] -> [Int]
stableSlots Int
8 [(CsvValue, Int)]
items
[Int] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length ([Int] -> [Int]
forall a. Eq a => [a] -> [a]
nub [Int]
slots) Int -> Int -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= Int
3
Int -> [(CsvValue, Int)] -> [Int]
forall a. Ord a => Int -> [(CsvValue, a)] -> [Int]
stableSlots Int
8 ([(CsvValue, Int)] -> [(CsvValue, Int)]
forall a. [a] -> [a]
reverse [(CsvValue, Int)]
items) [Int] -> [Int] -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= [Int] -> [Int]
forall a. [a] -> [a]
reverse [Int]
slots
(Int -> Bool) -> [Int] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (Int -> [Int] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Int
0 .. Int
7]) [Int]
slots Bool -> Bool -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= Bool
True
[Int] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length ([Int] -> [Int]
forall a. Eq a => [a] -> [a]
nub ([Int] -> [Int]) -> [Int] -> [Int]
forall a b. (a -> b) -> a -> b
$ Int -> [(CsvValue, Int)] -> [Int]
forall a. Ord a => Int -> [(CsvValue, a)] -> [Int]
stableSlots Int
2 [(CsvValue, Int)]
items) Int -> Int -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= Int
2
Int -> [(CsvValue, Int)] -> [Int]
forall a. Ord a => Int -> [(CsvValue, a)] -> [Int]
stableSlots Int
0 [(CsvValue, Int)]
items [Int] -> [Int] -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= [Int
0, Int
0, Int
0]
let many :: [Int]
many = Int -> [(CsvValue, Int)] -> [Int]
forall a. Ord a => Int -> [(CsvValue, a)] -> [Int]
stableSlots Int
4 [(String -> CsvValue
T.pack (String -> CsvValue) -> String -> CsvValue
forall a b. (a -> b) -> a -> b
$ Int -> String
forall a. Show a => a -> String
show Int
i, Int
i) | Int
i <- [Int
1 .. Int
8 :: Int]]
[[Int] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length ([Int] -> Int) -> [Int] -> Int
forall a b. (a -> b) -> a -> b
$ (Int -> Bool) -> [Int] -> [Int]
forall a. (a -> Bool) -> [a] -> [a]
filter (Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
k) [Int]
many | Int
k <- [Int
0 .. Int
3]] [Int] -> [Int] -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= [Int
2, Int
2, Int
2, Int
2]
,String -> Assertion -> TestTree
testCase String
"categoryTints" (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$ do
let lbl :: CsvValue -> Maybe AccountType -> ChartLabel
lbl CsvValue
n Maybe AccountType
t = CsvValue
-> CsvValue
-> Int
-> Maybe AccountType
-> Bool
-> [CsvValue]
-> ChartLabel
ChartLabel CsvValue
n CsvValue
n Int
0 Maybe AccountType
t Bool
True []
ts :: [Int]
ts = [ChartLabel] -> [Maybe Quantity] -> [Int]
categoryTints [CsvValue -> Maybe AccountType -> ChartLabel
lbl CsvValue
"e1" (AccountType -> Maybe AccountType
forall a. a -> Maybe a
Just AccountType
Expense), CsvValue -> Maybe AccountType -> ChartLabel
lbl CsvValue
"a" (AccountType -> Maybe AccountType
forall a. a -> Maybe a
Just AccountType
Asset), CsvValue -> Maybe AccountType -> ChartLabel
lbl CsvValue
"e2" (AccountType -> Maybe AccountType
forall a. a -> Maybe a
Just AccountType
Expense), CsvValue -> Maybe AccountType -> ChartLabel
lbl CsvValue
"e3" (AccountType -> Maybe AccountType
forall a. a -> Maybe a
Just AccountType
Expense)] [Quantity -> Maybe Quantity
forall a. a -> Maybe a
Just Quantity
3, Quantity -> Maybe Quantity
forall a. a -> Maybe a
Just Quantity
2, Quantity -> Maybe Quantity
forall a. a -> Maybe a
Just Quantity
1, Maybe Quantity
forall a. Maybe a
Nothing]
[Int] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Int]
ts Int -> Int -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= Int
4
[Int] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length ([Int] -> [Int]
forall a. Eq a => [a] -> [a]
nub [Int
t | (Int
i, Int
t) <- [Int] -> [Int] -> [(Int, Int)]
forall a b. [a] -> [b] -> [(a, b)]
zip [Int
0 :: Int ..] [Int]
ts, Int
i Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= Int
1]) Int -> Int -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= Int
3
]