{- |
A renderer-independent description of a chart, made from a report.

This is to charts what "Hledger.Write.Spreadsheet" is to tables:
each chart-capable report has an adapter that turns its report data into a 'Chart',
choosing a useful default chart for that report (see doc/SPEC-charts.md, "Smart defaults"),
and renderers turn a 'Chart' into text, SVG, CSV or JSON.

The model carries the report's own display information
(period headings, account display names, commodity display styles, the report title)
so that every renderer labels things exactly as the report's table would.
Signs are already as the report shows them (eg normal-balance flipping done by bs/is/cf).

Values are kept exact here ('Quantity'); renderers convert them to Double only for drawing.
-}

{-# 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)


-- | A chart: a title, an x axis, and one or more panels drawn against that x axis.
-- Typically there is one panel per commodity.
-- A chart with no panels, or whose panels have no series, has no data; renderers should say so, not fail.
data Chart = Chart
  { Chart -> CsvValue
chTitle  :: Text     -- ^ the report's title, as its table would show it (may be empty)
  , Chart -> XAxis
chXAxis  :: XAxis    -- ^ the x positions, shared by all panels
  , Chart -> [Panel]
chPanels :: [Panel]  -- ^ the panels, in display order (eg largest commodity first)
  , Chart -> [CsvValue]
chNotes  :: [Text]   -- ^ notes to show below the chart, eg about what its values leave out
  } 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)

-- | The x positions of a chart: what each value in a series corresponds to.
-- Every series in the chart has one value per x position (renderers must not assume this, and should ignore extras).
--
-- Renderers choose orientation from this: periods and dates are drawn along a horizontal time axis in SVG,
-- categories as horizontal bars. Text charts always draw one row per x position.
data XAxis
  = XCategories [ChartLabel]     -- ^ unordered categories, eg the accounts of a single-period balance report
  | XPeriods [(DateSpan, Text)]  -- ^ consecutive report periods, each with its heading as the report's table shows it
  | XDates [Day]                 -- ^ individual dates, possibly irregular and repeated, eg one per transaction in a register
  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)

-- | A name for a category or a series.
-- For accounts this comes from the report's display name, so it follows the report's tree/flat mode and depth.
data ChartLabel = ChartLabel
  { ChartLabel -> CsvValue
lbFull   :: Text                -- ^ the full name, eg a full account name; used in CSV/JSON and tooltips
  , ChartLabel -> CsvValue
lbShort  :: Text                -- ^ the name to display, eg a leaf account name in tree mode
  , ChartLabel -> Int
lbIndent :: Int                 -- ^ indentation level to display it with (0 for none)
  , ChartLabel -> Maybe AccountType
lbType   :: Maybe AccountType   -- ^ the account type, if this names an account (or accounts) of a known type;
                                    --   charts colour accounts by their main type (see 'seriesColours')
  , ChartLabel -> Bool
lbAccount :: Bool               -- ^ does this name an account (or accounts) ? Otherwise it's something else, eg a total.
  , ChartLabel -> [CsvValue]
lbTipLines :: [Text]            -- ^ more lines for tooltips, eg the accounts that an "other" series combines
  } 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)

-- | One chart area with its own y axis, holding series of values in a single unit.
data Panel = Panel
  { Panel -> Amount
pnUnit      :: Amount     -- ^ the unit of all values in this panel, as a template amount whose quantity is ignored:
                              --   its commodity symbol and display style are used to show values and axis ticks.
                              --   Eg a currency, @%@ (as produced by --percent), or no commodity with precision 0 for counts.
  , Panel -> [Series]
pnSeries    :: [Series]   -- ^ the series, in display order (legend order)
  , Panel -> [CsvValue]
pnGroupNames :: [Text]    -- ^ names for the groups of series, by group number (see 'seGroup'), if they have names;
                              --   eg a compound report's section titles. Text charts show them.
  , Panel -> Maybe Quantity
pnScale     :: Maybe Quantity  -- ^ a fixed scale, if one was requested: the amount spanned by the chart (see 'scaleSpans'),
                              --   so that charts with the same scale are drawn at the same size per amount.
                              --   Otherwise the chart is scaled to fit its values.
  , Panel -> BarLayout
pnLayout    :: BarLayout  -- ^ how to draw bars of several groups
  , Panel -> Bool
pnCounts    :: Bool       -- ^ are the values counts of whole things (eg postings) ? Then text charts draw at most one cell per unit,
                              --   so that small counts are not magnified (unless a fixed scale is set).
  , Panel -> [Summary]
pnSummary   :: [Summary]  -- ^ values summarising the whole chart, shown below it (eg with -T and -A, the total and the average per period)
  , Panel -> [Maybe CsvValue]
pnValueLabels :: [Maybe Text]  -- ^ text to show at each x position (or none there), eg a holding's gain percent:
                                   --   in graphical charts instead of its value (which the axis shows), in a smaller font
                                   --   (over time, only if they all fit); in text charts after it.
                                   --   Text beginning with + is shown in the revenue colour; with -, in the expense colour
                                   --   in graphical charts, the negative style in text charts.
                                   --   If empty, the values are shown as usual
  } 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)

-- | A value summarising a panel's values over the whole x axis, such as their total or their average.
data Summary = Summary
  { Summary -> SummaryKind
suKind  :: SummaryKind
  , Summary -> CsvValue
suLabel :: Text    -- ^ its name, as the report's table would show it (eg "Total", "Average")
  , Summary -> Amount
suValue :: Amount  -- ^ its value, with its own display style (eg an average of counts has a decimal place)
  } 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    -- ^ the total of all x positions' values
  | SummaryAverage  -- ^ the average per x position, which SVG charts also draw as a line across the plot (with a legend entry)
  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)

-- | How to draw the bars of a panel whose series are in several groups (eg a compound report's sections).
data BarLayout
  = Diverging  -- ^ all groups stacked in one bar at each x position, positive values above (or right of) zero and negative ones below
  | Grouped    -- ^ each group stacked in its own bar, the groups' bars side by side (in text, a row per group), with the total marked separately;
               --   values are shown with their usual sign as positive (see 'setChartLayout')
  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)

-- | A named sequence of values, one per x position.
data Series = Series
  { Series -> ChartLabel
seLabel  :: ChartLabel
  , Series -> SeriesRole
seRole   :: SeriesRole
  , Series -> Mark
seMark   :: Mark
  , Series -> [Maybe Quantity]
seValues :: [Maybe Quantity]  -- ^ one value per x position; Nothing means no value there (a gap, not zero).
                                  --   At each x position, bar series' values are stacked: positive values above zero, negative values below.
  , Series -> Int
seGroup  :: Int               -- ^ which group this series belongs to (eg its section, in a compound report chart; otherwise 0).
                                  --   Groups are stacked together, or drawn side by side, as the panel's 'pnLayout' says.
  , Series -> Int
seSign   :: Int               -- ^ the usual sign of this series' values, if it has one (1 or -1; otherwise 0).
                                  --   Values of the opposite sign are contra amounts, which renderers may show distinctly;
                                  --   eg a refund (negative expense) in a compound report chart.
  } 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)

-- | What a series represents, which renderers may show differently (eg a muted colour for 'SeriesOther').
data SeriesRole
  = SeriesItem   -- ^ an ordinary series, eg one account
  | SeriesOther  -- ^ several items combined, eg "other (5 accounts)", to limit the number of series
  | SeriesTotal  -- ^ a total of other series, eg the net total of stacked bars, or net worth
  | SeriesShortfall  -- ^ an amount missing from what the bar stands for, eg the part of a holding's cost basis lost (beyond its value):
                     --   drawn hatched, in its place in the stack
  | SeriesProvisional  -- ^ an amount not yet final, eg an unrealised gain or loss: drawn hatched, in its place in the stack
  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)

-- | How a series is drawn.
data Mark
  = MarkBar     -- ^ a bar from zero to the value
  | MarkLine    -- ^ a line joining the values (in text charts, drawn as bars)
  | MarkMarker  -- ^ a marker at the value, not joined, eg a net total over stacked bars, or a budget goal
  | MarkNone    -- ^ not drawn: values shown only as text (text charts' value column, tooltips, chart data),
                --   eg holdings' values, which their bars' solid parts reach already
  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)

-- | Is this value of the series a contra amount (of the opposite sign to the series' usual sign) ?
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)

-- | Is this value of the series drawn hatched ? Contra amounts are, and the values of hatched series (see 'hatchedSeries').
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

-- | Is this series drawn hatched throughout, in its colour (and so shown hatched in legends) ?
-- Shortfalls and provisional amounts are (see 'SeriesShortfall', 'SeriesProvisional').
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]

-- | Should a legend list these drawn series (on a category axis, or not) ?
-- Yes when there are several, or one is an "other" series (whose muted colour needs explaining),
-- or a lone series names an account on a time axis (where nothing else would name it).
-- A lone series on a category axis is not listed: its bars are labelled on the axis, and coloured by category.
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)

-- | The main account type (asset, liability, equity, revenue or expense) of an account type, which is one of those or a subtype of one.
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]

-- | How a series (or a bar) is coloured, in a renderer-independent way.
data Colour
  = TypeColour AccountType Int  -- ^ the conventional hue of this main account type, in its Nth tint (to distinguish several of that type)
  | IndexColour Int             -- ^ the Nth categorical colour, for series with no account type
  | LoneColour                  -- ^ a hue that no account type uses, for a chart's only series when it is not an account,
                                --   or for an account of unknown type in a chart with one account per panel
  | NeutralColour               -- ^ a neutral colour, eg for the contra amounts legend swatch
  | OtherColour (Maybe AccountType)  -- ^ a muted colour, for a series combining several items: grey, tinged with their main account type's hue if any
  | TotalColour                 -- ^ the ink (text) colour, for a total
  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)

-- | How many tints of a type's colour renderers provide, to distinguish several accounts of one type.
maxTints :: Int
maxTints :: Int
maxTints = Int
8

-- | How many categorical colours renderers provide (see 'IndexColour').
categoricalColours :: Int
categoricalColours :: Int
categoricalColours = Int
8

-- | How a chart's accounts are coloured (decided for the whole chart, so that colours mean the same in all of its panels).
data Colouring
  = ByName  -- ^ in distinct colours chosen by name, to tell them apart: when none has a known type
  | ByType  -- ^ by main account type (in tints chosen by name): when any has a known type, even if all are of one type,
            --   so that a type's hue means the same in every chart, and an account keeps its colour in other charts
  | Single  -- ^ by type, or if unknown in the lone colour: when each panel shows just one account, so there is nothing to tell apart
  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)

-- | How this chart's accounts should be coloured: considering all of its panels, so that eg in a balance sheet,
-- a commodity's panel with just one asset account shows it in the assets' hue, like the other panels.
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

-- | Colours for a panel's series, in order, for the chart's colouring (see 'chartColouring').
-- Coloured by type, each account of known type gets its main type's hue, in one of several tints
-- (shared when there are more than 'maxTints' of a type). Otherwise, and accounts of unknown type, get categorical colours;
-- except that a lone series which is not an account (eg a total, or "..." for all accounts), or a lone account of unknown type
-- in a chart of single accounts, gets a hue that no account type uses.
--
-- The tints and categorical colours are chosen by the series' names, not their positions or number (see 'stableSlots'),
-- so that an account keeps its colour in other charts: eg after removing other accounts (even down to just one), or sorting differently.
-- A series of known type which is not an account (eg a holding's cost or gain) has its type's first tint,
-- so that its colour doesn't depend on its name (which may be translated).
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
    -- each series' slot among the other ordinary series of its type (or with no type)
    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] ]
    -- the ordinary series which choose tints or categorical colours by name: accounts, and series of unknown type
    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)]
    -- a type's tints are chosen favouring the first, most saturated ones (the paler ones look washed out),
    -- so that they are used mainly when several accounts of the type need telling apart; categorical colours, without bias
    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

-- | A tint for each of these categories (eg a single-period chart's accounts, with their values), chosen by name
-- among the categories of the same main account type, as 'seriesColours' chooses them for series:
-- so that accounts of one type can be told apart, and tend to keep their tints in a multi-period chart.
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] ]

-- | A series' size, for ranking: the sum of its absolute values.
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

-- | Give each of these names one of n slots (0 to n-1), chosen by the name rather than its position,
-- so that a name tends to get the same slot in other lists: eg a colour that stays with an account in successive charts.
-- Each name has its own order of preference for the slots (from a hash of it), and gets the first one still free.
-- The names choose in order of their sizes (largest first; then by name), so that adding or removing small items
-- (or removing the largest) disturbs few others; positions don't matter.
-- When there are more names than slots, slots are shared: once all are taken, the names choose among them all again.
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

-- | Like 'stableSlots', but with each name's preferences biased towards the first slots, by this much
-- (0: not at all; 1: fully, so that every name has the same order of preference).
-- Eg with 0.5, the last of 4 slots is never a name's first choice, and the first is its first choice most often.
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)
    -- a name's slots, in its order of preference
    -- (by the slot's position, if biased, plus a pseudo-random fraction from the name)
    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]
    -- a simple, stable hash: FNV-1a (32 bit), with murmur3's finaliser to mix in the last characters well
    -- (which here are the slot number; without it, names had only a few dozen orders of preference)
    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)

-- | A series' values, indexed by x position, for fast lookup by renderers.
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

-- | The value at this x position, if there is one.
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

-- | Give each panel a fixed scale (see 'pnScale'): the absolute quantity of the scale amount in the panel's commodity,
-- or else of the one with no commodity symbol. When several are in the same commodity, the last one is used.
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]

-- | Don't draw the totals (or nets): give each panel's total series no mark ('MarkNone'),
-- so that it is still shown as values where those are shown (eg in tooltips), but not drawn.
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

-- | Set the bar layout of every panel to this one, if given (otherwise keeping each panel's own), and prepare the panels for it.
-- This should be applied before drawing a chart, or showing its data.
-- The grouped layout is for comparing the groups' sizes, so its series are shown with their usual sign as positive
-- (eg expenses as well as revenues upward, like a compound report's tables), and their contra amounts as negative.
-- (Applying this again does nothing more, since those series' usual sign is then positive.)
-- And since only bars can be side by side, in a panel with several groups, series drawn as lines (eg end balances) become bars.
-- Its series are put in their groups' order.
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
        -- with the grouped layout, each group's series together, in the groups' order, then any total
        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

-- | With a fixed scale (the amount that a chart spans), and the largest positive and (absolute) negative extents of the values,
-- how much of the scale is given to each direction: (above or right of zero, below or left of zero).
-- Negative values get the room they need and positive values the rest (all of it if there are no negative values;
-- if there are only negative values, they get all of it).
-- If the values don't fit, both directions are shrunk in proportion, and the values beyond them will be clipped.
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)

-- | Render a chart's data as CSV (or TSV) records, in long ("tidy") form: one record per value,
-- with a header record. This shows exactly what the chart shows, after any combining of series,
-- and can be charted with other tools.
--
-- Values are shown with the panel unit's display precision, without commodity symbol or digit group marks.
-- A gap (no value) is shown as an empty field.
-- Each panel's summary values (see 'pnSummary') follow its series, with role "rowtotal" or "average", and no mark or x.
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]
     -- in a chart coloured by type, accounts get their types' hues, in different tints; untyped series get categorical colours
     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
     -- as many accounts of one type as there are tints get a tint each
     [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
     -- a chart is coloured by type when any account has a type, even if all are of one type; by name when none has
     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
     -- colours follow the accounts, not their positions
     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)])
     -- coloured by name (as when no account has a type) accounts get categorical colours, all different
     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
     -- and keep them when fewer are shown, down to one
     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
     -- typed series which are not accounts (eg a holding's cost, gain and loss) get their types' first tints, whatever their names
     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]
     -- a lone series which is not an account gets the lone colour; others and totals get theirs
     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]
     -- in a chart of single accounts, a lone account has its type's hue, or if unknown the lone colour
     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                       -- different slots, while there are enough
     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  -- positions don't matter
     (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         -- more items than slots: they share them
     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]
     -- with twice as many items as slots, each slot is shared by two
     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  -- the categories of a type get different tints
  ]