{- |
Draw a 'Chart' as SVG, a self-contained graphical file viewable in web browsers and many image viewers.

Charts over time (periods or dates) are drawn with time along the x axis:
bar series as vertical bars (stacked, with a 2px gap between segments), line series as 2px lines,
and totals as ink-coloured line marks across each period's bars, or as an ink-coloured line.
Charts of categories (eg the accounts of a single-period report) are drawn as horizontal bars,
with labels on the left and an aligned value column on the right.
There is one panel per commodity, one above the other, and a legend listing the series (see 'legendListsSeries'), and any average line.
Each mark has a tooltip (an SVG title) giving its x label, series and value,
and for an "other" series, the accounts it combines, one per line.

The SVG includes its own styles, with a colour-blind-safe palette and a dark-mode variant
(used when the viewer prefers a dark colour scheme). Text widths are estimated, since fonts are not known.
Coordinates are rounded to one decimal place, so the output is deterministic.

A page embedding a chart can draw it with 'SvgOptions', eg without its styles, which the page then supplies
('chartStylesheet').
-}

{-# LANGUAGE CPP #-}
{-# LANGUAGE OverloadedStrings #-}

module Hledger.Write.Chart.Svg (
  chartAsSvg,
  chartAsSvgWith,
  chartSvgMarkup,
  SvgOptions(..),
  defaultSvgOptions,
  chartSvgMarkupWith,
  chartPanelsSvgMarkupWith,
  chartStylesheet,
  tests_Hledger_Write_Chart_Svg,
) where

import Control.Monad (forM_, join, mfilter, unless, when)
import Data.Char (isDigit, isUpper)
import Data.Function (on)
import Data.List (find, groupBy, nub, sortOn)
#if !MIN_VERSION_base(4,20,0)
import Data.List (foldl')
#endif
import Data.Map qualified as M
import Data.Array qualified as Arr
import Data.Decimal (realFracToDecimal)
import Data.Maybe (catMaybes, fromMaybe, isJust, isNothing)
import Data.Time (Day, addDays, diffDays, fromGregorian, toGregorian)
import Data.Text (Text)
import Data.Text qualified as T
import Data.Text.Lazy qualified as TL
import Safe (atMay, headMay, lastMay, maximumDef, minimumDef)
import Text.Blaze (AttributeValue, (!?), customAttribute, toValue)
import Text.Blaze.Svg.Renderer.Text (renderSvg)
import Text.Blaze.Svg11 ((!), Svg)
import Text.Blaze.Svg11 qualified as S
import Text.Blaze.Svg11.Attributes qualified as A
import Text.DocLayout qualified as DL
import Text.Printf (printf)

import Hledger.Data.Amount (nullamt, oneLineNoCostFmt, showAmountWith)
import Hledger.Data.Dates (nulldatespan, showDate)
import Hledger.Data.Types (AccountType(..), Amount(..), AmountPrecision(..), AmountStyle(..), Quantity)
import Hledger.Utils.Test
import Hledger.Write.Chart
import Hledger.Write.Chart.Palette (colourHex)


-- | Render a chart as a complete SVG document, with a dark variant for viewers preferring a dark colour scheme.
chartAsSvg :: Chart -> TL.Text
chartAsSvg :: Chart -> Text
chartAsSvg = Bool -> Chart -> Text
chartAsSvgWith Bool
True

-- | Render a chart as SVG, with or without the dark variant
-- (eg without, when embedding it in a page that is always light).
-- It has one element per line, for readability.
chartAsSvgWith :: Bool -> Chart -> TL.Text
chartAsSvgWith :: Bool -> Chart -> Text
chartAsSvgWith Bool
dark = (Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"\n") (Text -> Text) -> (Chart -> Text) -> Chart -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HasCallStack => Text -> Text -> Text -> Text
Text -> Text -> Text -> Text
TL.replace Text
"><" Text
">\n<" (Text -> Text) -> (Chart -> Text) -> Chart -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Svg -> Text
renderSvg (Svg -> Text) -> (Chart -> Svg) -> Chart -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bool -> Chart -> Svg
chartSvgMarkup Bool
dark

-- | A chart as SVG markup, which can also be embedded in HTML; with or without the dark variant.
chartSvgMarkup :: Bool -> Chart -> Svg
chartSvgMarkup :: Bool -> Chart -> Svg
chartSvgMarkup Bool
dark = SvgOptions -> Chart -> Svg
chartSvgMarkupWith SvgOptions
defaultSvgOptions{svgDark = dark}

-- | How to draw a chart as SVG.
data SvgOptions = SvgOptions
  { SvgOptions -> Bool
svgStyles     :: Bool    -- ^ include the chart's styles ? A page embedding the chart can leave them out and supply
                             --   'chartStylesheet' itself, eg one whose Content-Security-Policy allows no inline styles.
  , SvgOptions -> Bool
svgDark       :: Bool    -- ^ include the styles' dark variant, for viewers preferring a dark colour scheme ?
  , SvgOptions -> Double
svgWidth      :: Double  -- ^ the chart's width, in pixels. A page shrinks a chart wider than it shows it, text and all,
                             --   so it can draw the chart as wide as that instead.
  , SvgOptions -> Double
svgPlotHeight :: Double  -- ^ the height of each plot of values over time or of categories as columns, in pixels,
                             --   not counting its labels; also deciding how many y ticks it has (see 'plotIntervals').
                             --   (Horizontal bars are as tall as their rows.)
  , SvgOptions -> Int -> Maybe Text
svgPointLink  :: Int -> Maybe Text  -- ^ a link for each x position (by index), if any: lines' tooltip targets there link to it,
                                        --   eg to a page's row for that transaction. They are left out of the tab order, being many.
  , SvgOptions -> Bool
svgDateScale  :: Bool    -- ^ give each plot of values by date its date scale, for a page's script (eg to select dates by
                             --   dragging across it): a transparent rectangle over the plot (class plot), with its first and last
                             --   dates (data-d0, data-d1) and their x positions (data-x0, data-x1), between which dates are spaced evenly ?
  , SvgOptions -> Maybe Day
svgToday      :: Maybe Day  -- ^ today's date, if each plot of values by date should be shaded as of it: the future, after it,
                                --   and the past where values are below zero (eg an overdrawn account), with the zero line in red.
  , SvgOptions -> Text
svgIds        :: Text    -- ^ the prefix of the ids that the chart's elements refer to (its clipping paths and hatching).
                             --   Charts on one page need different ones when one may be hidden while another is shown,
                             --   since a hidden chart's patterns and clipping paths may not apply to another's elements.
  }

-- | A self-contained chart: with its styles, including their dark variant, 880px wide, plots 260px high, no links, no date scale, and no shading.
defaultSvgOptions :: SvgOptions
defaultSvgOptions :: SvgOptions
defaultSvgOptions = SvgOptions
  { svgStyles :: Bool
svgStyles     = Bool
True
  , svgDark :: Bool
svgDark       = Bool
True
  , svgWidth :: Double
svgWidth      = Double
880
  , svgPlotHeight :: Double
svgPlotHeight = Double
260
  , svgPointLink :: Int -> Maybe Text
svgPointLink  = Maybe Text -> Int -> Maybe Text
forall a b. a -> b -> a
const Maybe Text
forall a. Maybe a
Nothing
  , svgDateScale :: Bool
svgDateScale  = Bool
False
  , svgToday :: Maybe Day
svgToday      = Maybe Day
forall a. Maybe a
Nothing
  , svgIds :: Text
svgIds        = Text
"hledger-chart"
  }

-- | A chart as SVG markup, which can also be embedded in HTML, drawn with these options.
chartSvgMarkupWith :: SvgOptions -> Chart -> Svg
chartSvgMarkupWith :: SvgOptions -> Chart -> Svg
chartSvgMarkupWith SvgOptions
opts Chart
c = Colouring -> SvgOptions -> Chart -> Svg
chartSvgMarkupColoured (Chart -> Colouring
chartColouring Chart
c) SvgOptions
opts Chart
c

-- | Each of a chart's panels as a chart of its own, in SVG markup drawn with these options,
-- coloured as in the whole chart; eg for a page to show one commodity's panel at a time.
-- Each has its own ids (see 'svgIds'), the given prefix numbered.
chartPanelsSvgMarkupWith :: SvgOptions -> Chart -> [Svg]
chartPanelsSvgMarkupWith :: SvgOptions -> Chart -> [Svg]
chartPanelsSvgMarkupWith SvgOptions
opts Chart
c =
  [ Colouring -> SvgOptions -> Chart -> Svg
chartSvgMarkupColoured (Chart -> Colouring
chartColouring Chart
c) SvgOptions
opts{svgIds = svgIds opts <> "-" <> T.pack (show k)} Chart
c{chPanels = [p]}
  | (Int
k, Panel
p) <- [Int] -> [Panel] -> [(Int, Panel)]
forall a b. [a] -> [b] -> [(a, b)]
zip [Int
0 :: Int ..] (Chart -> [Panel]
chPanels Chart
c) ]

-- | A chart as SVG markup, drawn with these options and this colouring.
chartSvgMarkupColoured :: Colouring -> SvgOptions -> Chart -> Svg
chartSvgMarkupColoured :: Colouring -> SvgOptions -> Chart -> Svg
chartSvgMarkupColoured Colouring
colouring opts :: SvgOptions
opts@SvgOptions{Bool
svgStyles :: SvgOptions -> Bool
svgStyles :: Bool
svgStyles, Bool
svgDark :: SvgOptions -> Bool
svgDark :: Bool
svgDark, Double
svgWidth :: SvgOptions -> Double
svgWidth :: Double
svgWidth} Chart{Text
chTitle :: Text
chTitle :: Chart -> Text
chTitle, XAxis
chXAxis :: XAxis
chXAxis :: Chart -> XAxis
chXAxis, [Panel]
chPanels :: Chart -> [Panel]
chPanels :: [Panel]
chPanels, [Text]
chNotes :: [Text]
chNotes :: Chart -> [Text]
chNotes} =
  Svg -> Svg
S.svg
    (Svg -> Svg) -> Attribute -> Svg -> Svg
forall h. Attributable h => h -> Attribute -> h
! Tag -> AttributeValue -> Attribute
customAttribute Tag
"xmlns" AttributeValue
"http://www.w3.org/2000/svg"
    (Svg -> Svg) -> Attribute -> Svg -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.class_ AttributeValue
"hledger-chart"
    (Svg -> Svg) -> Attribute -> Svg -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.width (Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue (Text -> AttributeValue) -> Text -> AttributeValue
forall a b. (a -> b) -> a -> b
$ Double -> Text
num Double
svgWidth)
    (Svg -> Svg) -> Attribute -> Svg -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.height (Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue (Text -> AttributeValue) -> Text -> AttributeValue
forall a b. (a -> b) -> a -> b
$ Double -> Text
num Double
totalheight)
    (Svg -> Svg) -> Attribute -> Svg -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.viewbox (Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue (Text -> AttributeValue) -> Text -> AttributeValue
forall a b. (a -> b) -> a -> b
$ Text
"0 0 " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Double -> Text
num Double
svgWidth Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Double -> Text
num Double
totalheight)
    (Svg -> Svg) -> Attribute -> Svg -> Svg
forall h. Attributable h => h -> Attribute -> h
! Tag -> AttributeValue -> Attribute
customAttribute Tag
"role" AttributeValue
"img"
    (Svg -> Svg) -> (Bool, Attribute) -> Svg -> Svg
forall h. Attributable h => h -> (Bool, Attribute) -> h
!? (Bool -> Bool
not (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b
$ Text -> Bool
T.null Text
chTitle, Tag -> AttributeValue -> Attribute
customAttribute Tag
"aria-label" (Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue Text
chTitle))
    (Svg -> Svg) -> Svg -> Svg
forall a b. (a -> b) -> a -> b
$ do
      Bool -> Svg -> Svg
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (Text -> Bool
T.null Text
chTitle) (Svg -> Svg) -> Svg -> Svg
forall a b. (a -> b) -> a -> b
$ Svg -> Svg
S.title (Text -> Svg
forall a. ToMarkup a => a -> Svg
S.toMarkup Text
chTitle)
      Bool -> Svg -> Svg
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when Bool
svgStyles (Svg -> Svg) -> Svg -> Svg
forall a b. (a -> b) -> a -> b
$ Svg -> Svg
S.style (Text -> Svg
forall a. ToMarkup a => a -> Svg
S.toMarkup (Text -> Svg) -> Text -> Svg
forall a b. (a -> b) -> a -> b
$ Bool -> Bool -> Text
stylesheet (Maybe Day -> Bool
forall a. Maybe a -> Bool
isJust (Maybe Day -> Bool) -> Maybe Day -> Bool
forall a b. (a -> b) -> a -> b
$ SvgOptions -> Maybe Day
svgToday SvgOptions
opts) Bool
svgDark)
      Svg
S.rect Svg -> Attribute -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.class_ AttributeValue
"bg" Svg -> Attribute -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.width AttributeValue
"100%" Svg -> Attribute -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.height AttributeValue
"100%"
      Bool -> Svg -> Svg
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (Text -> Bool
T.null Text
chTitle) (Svg -> Svg) -> Svg -> Svg
forall a b. (a -> b) -> a -> b
$
        Svg -> Svg
S.text_ (Svg -> Svg) -> Attribute -> Svg -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.class_ AttributeValue
"title" (Svg -> Svg) -> Attribute -> Svg -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.x (Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue (Text -> AttributeValue) -> Text -> AttributeValue
forall a b. (a -> b) -> a -> b
$ Double -> Text
num Double
pad) (Svg -> Svg) -> Attribute -> Svg -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.y (Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue (Text -> AttributeValue) -> Text -> AttributeValue
forall a b. (a -> b) -> a -> b
$ Double -> Text
num (Double
pad Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
17)) (Svg -> Svg) -> Svg -> Svg
forall a b. (a -> b) -> a -> b
$ Text -> Svg
forall a. ToMarkup a => a -> Svg
S.toMarkup Text
chTitle
      [Svg] -> Svg
forall (t :: * -> *) (m :: * -> *) a.
(Foldable t, Monad m) =>
t (m a) -> m ()
sequence_ [Svg]
panelmarkups
      Bool -> Svg -> Svg
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when ([Panel] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Panel]
chPanels) (Svg -> Svg) -> Svg -> Svg
forall a b. (a -> b) -> a -> b
$
        Svg -> Svg
S.text_ (Svg -> Svg) -> Attribute -> Svg -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.class_ AttributeValue
"label" (Svg -> Svg) -> Attribute -> Svg -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.x (Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue (Text -> AttributeValue) -> Text -> AttributeValue
forall a b. (a -> b) -> a -> b
$ Double -> Text
num Double
pad) (Svg -> Svg) -> Attribute -> Svg -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.y (Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue (Text -> AttributeValue) -> Text -> AttributeValue
forall a b. (a -> b) -> a -> b
$ Double -> Text
num (Double
pad Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
titleheight Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
16)) (Svg -> Svg) -> Svg -> Svg
forall a b. (a -> b) -> a -> b
$ Svg
"No data"
      -- the notes, below the panels
      [(Int, Text)] -> ((Int, Text) -> Svg) -> Svg
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ ([Int] -> [Text] -> [(Int, Text)]
forall a b. [a] -> [b] -> [(a, b)]
zip [Int
0 :: Int ..] [Text]
chNotes) (((Int, Text) -> Svg) -> Svg) -> ((Int, Text) -> Svg) -> Svg
forall a b. (a -> b) -> a -> b
$ \(Int
i, Text
n) ->
        Svg -> Svg
S.text_ (Svg -> Svg) -> Attribute -> Svg -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.class_ AttributeValue
"note" (Svg -> Svg) -> Attribute -> Svg -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.x (Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue (Text -> AttributeValue) -> Text -> AttributeValue
forall a b. (a -> b) -> a -> b
$ Double -> Text
num Double
pad) (Svg -> Svg) -> Attribute -> Svg -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.y (Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue (Text -> AttributeValue) -> Text -> AttributeValue
forall a b. (a -> b) -> a -> b
$ Double -> Text
num (Double
contentbottom Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
14 Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
noteHeight Double -> Double -> Double
forall a. Num a => a -> a -> a
* Int -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
i)) (Svg -> Svg) -> Svg -> Svg
forall a b. (a -> b) -> a -> b
$ Text -> Svg
forall a. ToMarkup a => a -> Svg
S.toMarkup Text
n
  where
    titleheight :: Double
titleheight = if Text -> Bool
T.null Text
chTitle then Double
0 else Double
38
    showheadings :: Bool
showheadings = [Panel] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Panel]
chPanels Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
1
    (Double
panelsbottom, [Svg]
panelmarkups) = ((Double, [Svg]) -> Panel -> (Double, [Svg]))
-> (Double, [Svg]) -> [Panel] -> (Double, [Svg])
forall b a. (b -> a -> b) -> b -> [a] -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl (Double, [Svg]) -> Panel -> (Double, [Svg])
stackPanel (Double
pad Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
titleheight, []) [Panel]
chPanels
    contentbottom :: Double
contentbottom = if [Panel] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Panel]
chPanels then Double
panelsbottom Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
14 Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
pad else Double
panelsbottom
    totalheight :: Double
totalheight = Double
contentbottom Double -> Double -> Double
forall a. Num a => a -> a -> a
+ (if [Text] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Text]
chNotes then Double
0 else Double
noteHeight Double -> Double -> Double
forall a. Num a => a -> a -> a
* Int -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral ([Text] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Text]
chNotes) Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
pad Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Double
2)
    stackPanel :: (Double, [Svg]) -> Panel -> (Double, [Svg])
stackPanel (Double
top, [Svg]
ms) Panel
p =
      let (Double
h, Svg
m) = SvgOptions
-> XAxis -> Colouring -> Bool -> Double -> Panel -> (Double, Svg)
panelSvg SvgOptions
opts XAxis
chXAxis Colouring
colouring Bool
showheadings Double
top Panel
p
      in (Double
top Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
h Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
panelGap, [Svg]
ms [Svg] -> [Svg] -> [Svg]
forall a. [a] -> [a] -> [a]
++ [Svg
m])

-- Layout constants, in pixels. (The chart's width and its plots' height are in 'SvgOptions'.)
pad, panelGap, legendRowHeight, rowHeight, noteHeight :: Double
pad :: Double
pad             = Double
16
panelGap :: Double
panelGap        = Double
16
legendRowHeight :: Double
legendRowHeight = Double
24
rowHeight :: Double
rowHeight       = Double
28   -- height of a row in a horizontal bar chart
noteHeight :: Double
noteHeight      = Double
22   -- height of a note's line, below the panels

-- | The narrowest a chart's periods can be (the chart's width divided by their number, in pixels)
-- for a total to be marked across their bars. The bars are 3/4 of their period (so here, 24px wide),
-- and a mark is 4px wider than them on each side, so below this the marks would run into each other.
minMarkedPeriodWidth :: Double
minMarkedPeriodWidth :: Double
minMarkedPeriodWidth = Double
32

-- | How many intervals a plot this high (in pixels) divides its y axis into, roughly: about one per 50px, from 2 to 5.
plotIntervals :: Double -> Int
plotIntervals :: Double -> Int
plotIntervals Double
h = Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
2 (Int -> Int) -> Int -> Int
forall a b. (a -> b) -> a -> b
$ Int -> Int -> Int
forall a. Ord a => a -> a -> a
min Int
5 (Int -> Int) -> Int -> Int
forall a b. (a -> b) -> a -> b
$ Double -> Int
forall b. Integral b => Double -> b
forall a b. (RealFrac a, Integral b) => a -> b
floor (Double
h Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Double
50)

-- | A value label's classes (see 'pnValueLabels'): a gain (beginning with +) in the revenue colour,
-- a loss (beginning with -) in the expense colour, as text charts show them.
vlabelClass :: Text -> AttributeValue
vlabelClass :: Text -> AttributeValue
vlabelClass Text
t
  | Text
"+" Text -> Text -> Bool
`T.isPrefixOf` Text
t = AttributeValue
"value vlabel gain"
  | Text
"-" Text -> Text -> Bool
`T.isPrefixOf` Text
t = AttributeValue
"value vlabel loss"
  | Bool
otherwise            = AttributeValue
"value vlabel"

-- | The size of value labels' font (see 'pnValueLabels'), relative to the usual 14px: 12px, so that more of them fit.
valueLabelScale :: Double
valueLabelScale :: Double
valueLabelScale = Double
12 Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Double
14

-- | Estimated rendered width of a value label (see 'textWidth').
labelWidth :: Text -> Double
labelWidth :: Text -> Double
labelWidth = (Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
valueLabelScale) (Double -> Double) -> (Text -> Double) -> Text -> Double
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Double
textWidth

-- | Estimated rendered width of some text, in the 14px system sans font (fonts are not known),
-- from the widths of kinds of character (measured with SF Pro; Segoe UI and others are similar or narrower).
textWidth :: Text -> Double
textWidth :: Text -> Double
textWidth = (Double -> Char -> Double) -> Double -> Text -> Double
forall a. (a -> Char -> a) -> a -> Text -> a
T.foldl' (\Double
w Char
c -> Double
w Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Char -> Double
forall {a}. Fractional a => Char -> a
glyphWidth Char
c) Double
0
  where
    glyphWidth :: Char -> a
glyphWidth Char
c
      | Char -> Int
DL.charWidth Char
c Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
2               = a
14
      | Char -> Int
DL.charWidth Char
c Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0               = a
0
      | Char -> Bool
isDigit Char
c                         = a
8.7  -- tabular digits, as in ticks and values
      | Char
c Char -> [Char] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` ([Char]
"mwMW" :: String)       = a
12
      | Char -> Bool
isUpper Char
c                         = a
9.5
      | Char
c Char -> [Char] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` ([Char]
"fijlrt" :: String)     = a
4.2
      | Char
c Char -> [Char] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` ([Char]
" .,:;'|!()" :: String) = a
3.8
      | Bool
otherwise                         = a
7.9

-- | A series to draw, with its CSS class and the values it draws.
data DrawSeries = DrawSeries
  { DrawSeries -> Text
dsClass  :: Text
  , DrawSeries -> Series
dsSeries :: Series
  , DrawSeries -> SeriesValues
dsValues :: SeriesValues
  }

-- | Draw one panel with its top at the given y; return its height and markup.
panelSvg :: SvgOptions -> XAxis -> Colouring -> Bool -> Double -> Panel -> (Double, Svg)
panelSvg :: SvgOptions
-> XAxis -> Colouring -> Bool -> Double -> Panel -> (Double, Svg)
panelSvg SvgOptions
opts XAxis
xaxis Colouring
colouring Bool
showheading Double
top Panel{Amount
pnUnit :: Amount
pnUnit :: Panel -> Amount
pnUnit, [Series]
pnSeries :: [Series]
pnSeries :: Panel -> [Series]
pnSeries, Maybe Quantity
pnScale :: Maybe Quantity
pnScale :: Panel -> Maybe Quantity
pnScale, BarLayout
pnLayout :: BarLayout
pnLayout :: Panel -> BarLayout
pnLayout, [Summary]
pnSummary :: [Summary]
pnSummary :: Panel -> [Summary]
pnSummary, [Maybe Text]
pnValueLabels :: Panel -> [Maybe Text]
pnValueLabels :: [Maybe Text]
pnValueLabels} =
  (Double
headingh Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
legendh Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
bodyh Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
summaryh, do
    Bool -> Svg -> Svg
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Double
headingh Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
> Double
0) (Svg -> Svg) -> Svg -> Svg
forall a b. (a -> b) -> a -> b
$
      Svg -> Svg
S.text_ (Svg -> Svg) -> Attribute -> Svg -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.class_ AttributeValue
"heading" (Svg -> Svg) -> Attribute -> Svg -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.x (Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue (Text -> AttributeValue) -> Text -> AttributeValue
forall a b. (a -> b) -> a -> b
$ Double -> Text
num Double
pad) (Svg -> Svg) -> Attribute -> Svg -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.y (Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue (Text -> AttributeValue) -> Text -> AttributeValue
forall a b. (a -> b) -> a -> b
$ Double -> Text
num (Double
top Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
16)) (Svg -> Svg) -> Svg -> Svg
forall a b. (a -> b) -> a -> b
$ Text -> Svg
forall a. ToMarkup a => a -> Svg
S.toMarkup (Amount -> Text
acommodity Amount
pnUnit)
    -- a hatching pattern, for contra amounts and shortfalls
    Bool -> Svg -> Svg
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when Bool
hashatching (Svg -> Svg) -> Svg -> Svg
forall a b. (a -> b) -> a -> b
$ Svg -> Svg
S.defs (Svg -> Svg) -> Svg -> Svg
forall a b. (a -> b) -> a -> b
$ Svg -> Svg
S.pattern (Svg -> Svg) -> Attribute -> Svg -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.id_ (Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue (Text -> AttributeValue) -> Text -> AttributeValue
forall a b. (a -> b) -> a -> b
$ Text -> Text
hatchId (Text -> Text) -> Text -> Text
forall a b. (a -> b) -> a -> b
$ SvgOptions -> Text
svgIds SvgOptions
opts) (Svg -> Svg) -> Attribute -> Svg -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.width AttributeValue
"5" (Svg -> Svg) -> Attribute -> Svg -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.height AttributeValue
"5" (Svg -> Svg) -> Attribute -> Svg -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.patternunits AttributeValue
"userSpaceOnUse" (Svg -> Svg) -> Attribute -> Svg -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.patterntransform AttributeValue
"rotate(45)" (Svg -> Svg) -> Svg -> Svg
forall a b. (a -> b) -> a -> b
$
      Svg
S.line Svg -> Attribute -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.class_ AttributeValue
"hatch" Svg -> Attribute -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.x1 AttributeValue
"0" Svg -> Attribute -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.y1 AttributeValue
"0" Svg -> Attribute -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.x2 AttributeValue
"0" Svg -> Attribute -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.y2 AttributeValue
"5"
    Svg
legendm
    Svg
bodym
    Bool -> Svg -> Svg
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Double
summaryh Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
> Double
0) (Svg -> Svg) -> Svg -> Svg
forall a b. (a -> b) -> a -> b
$
      Svg -> Svg
S.text_ (Svg -> Svg) -> Attribute -> Svg -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.class_ AttributeValue
"label" (Svg -> Svg) -> Attribute -> Svg -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.x (Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue (Text -> AttributeValue) -> Text -> AttributeValue
forall a b. (a -> b) -> a -> b
$ Double -> Text
num Double
pad) (Svg -> Svg) -> Attribute -> Svg -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.y (Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue (Text -> AttributeValue) -> Text -> AttributeValue
forall a b. (a -> b) -> a -> b
$ Double -> Text
num (Double
top Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
headingh Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
legendh Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
bodyh Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
16)) (Svg -> Svg) -> Svg -> Svg
forall a b. (a -> b) -> a -> b
$ Text -> Svg
forall a. ToMarkup a => a -> Svg
S.toMarkup Text
summarytext)
  where
    headingh :: Double
headingh = if Bool
showheading Bool -> Bool -> Bool
&& Bool -> Bool
not (Text -> Bool
T.null (Text -> Bool) -> Text -> Bool
forall a b. (a -> b) -> a -> b
$ Amount -> Text
acommodity Amount
pnUnit) then Double
28 else Double
0
    -- the summary values, on a line below the plot, eg "Total 11, Average 0.9"
    summaryh :: Double
summaryh = if [Summary] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Summary]
pnSummary then Double
0 else Double
26
    summarytext :: Text
summarytext = Text -> [Text] -> Text
T.intercalate Text
", " [Summary -> Text
suLabel Summary
su Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Summary -> Text
showSummary Summary
su | Summary
su <- [Summary]
pnSummary]
    showSummary :: Summary -> Text
showSummary = [Char] -> Text
T.pack ([Char] -> Text) -> (Summary -> [Char]) -> Summary -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. AmountFormat -> Amount -> [Char]
showAmountWith AmountFormat
oneLineNoCostFmt (Amount -> [Char]) -> (Summary -> Amount) -> Summary -> [Char]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Summary -> Amount
suValue
    -- the average, drawn as a line across a chart of periods
    maverage :: Maybe Summary
maverage = case XAxis
xaxis of
      XPeriods [(DateSpan, Text)]
_ -> (Summary -> Bool) -> [Summary] -> Maybe Summary
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Maybe a
find ((SummaryKind -> SummaryKind -> Bool
forall a. Eq a => a -> a -> Bool
== SummaryKind
SummaryAverage) (SummaryKind -> Bool)
-> (Summary -> SummaryKind) -> Summary -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Summary -> SummaryKind
suKind) [Summary]
pnSummary
      XAxis
_          -> Maybe Summary
forall a. Maybe a
Nothing
    -- The series drawn as marks: items and "other" (bars or lines), and the total (dots or a line). Other markers are not drawn yet.
    items :: [Series]
items = [Series
s | Series
s <- [Series]
pnSeries, Series -> SeriesRole
seRole Series
s SeriesRole -> SeriesRole -> Bool
forall a. Eq a => a -> a -> Bool
/= SeriesRole
SeriesTotal, Series -> Mark
seMark Series
s Mark -> [Mark] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`notElem` [Mark
MarkMarker, Mark
MarkNone]]
    mtotal :: Maybe Series
mtotal = (Series -> Bool) -> [Series] -> Maybe Series
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Maybe a
find ((SeriesRole -> SeriesRole -> Bool
forall a. Eq a => a -> a -> Bool
== SeriesRole
SeriesTotal) (SeriesRole -> Bool) -> (Series -> SeriesRole) -> Series -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Series -> SeriesRole
seRole) [Series]
pnSeries
    -- The total, if it's to be marked: always (even where it is just the end of the bars, or zero), since a graphical
    -- chart draws it only when asked for (--chart=net, hledger-web's N key); but not when it would be marked across
    -- the bars of periods too narrow for it (eg weekly over years):
    -- the marks, wider than the bars, would run together and hide them.
    -- A total line over the bars of just one period (eg a single-period balance sheet's net) would be a single point,
    -- so it is marked across them instead.
    -- A total that isn't to be drawn (MarkNone) is shown only as the value, in tooltips.
    mtotalmark :: Maybe Series
mtotalmark = case Maybe Series
mtotal of
      Just Series
t | Series -> Mark
seMark Series
t Mark -> Mark -> Bool
forall a. Eq a => a -> a -> Bool
== Mark
MarkNone -> Maybe Series
forall a. Maybe a
Nothing
      Just Series
t | Bool
crowded Bool -> Bool -> Bool
&& Series -> Mark
seMark Series
t Mark -> Mark -> Bool
forall a. Eq a => a -> a -> Bool
/= Mark
MarkLine Bool -> Bool -> Bool
&& Bool
hasbars -> Maybe Series
forall a. Maybe a
Nothing
      Just Series
t | Maybe Int
xcount Maybe Int -> Maybe Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int -> Maybe Int
forall a. a -> Maybe a
Just Int
1 Bool -> Bool -> Bool
&& Series -> Mark
seMark Series
t Mark -> Mark -> Bool
forall a. Eq a => a -> a -> Bool
== Mark
MarkLine Bool -> Bool -> Bool
&& Bool
hasbars -> Series -> Maybe Series
forall a. a -> Maybe a
Just Series
t{seMark = MarkMarker}
      Maybe Series
_ -> Maybe Series
mtotal
    hasbars :: Bool
hasbars = (Series -> Bool) -> [Series] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any ((Mark -> Mark -> Bool
forall a. Eq a => a -> a -> Bool
== Mark
MarkBar) (Mark -> Bool) -> (Series -> Mark) -> Series -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Series -> Mark
seMark) [Series]
items
    -- the number of x positions, on a time axis
    xcount :: Maybe Int
xcount = case XAxis
xaxis of
      XPeriods [(DateSpan, Text)]
ps    -> Int -> Maybe Int
forall a. a -> Maybe a
Just (Int -> Maybe Int) -> Int -> Maybe Int
forall a b. (a -> b) -> a -> b
$ [(DateSpan, Text)] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [(DateSpan, Text)]
ps
      XDates [Day]
ds      -> Int -> Maybe Int
forall a. a -> Maybe a
Just (Int -> Maybe Int) -> Int -> Maybe Int
forall a b. (a -> b) -> a -> b
$ [Day] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Day]
ds
      XCategories [ChartLabel]
_  -> Maybe Int
forall a. Maybe a
Nothing
    crowded :: Bool
crowded = Bool -> (Int -> Bool) -> Maybe Int -> Bool
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Bool
False (\Int
k -> SvgOptions -> Double
svgWidth SvgOptions
opts Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Int -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
1 Int
k) Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
< Double
minMarkedPeriodWidth) Maybe Int
xcount
    drawn :: [DrawSeries]
drawn = (Colour -> Series -> DrawSeries)
-> [Colour] -> [Series] -> [DrawSeries]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith (\Colour
c Series
s -> Text -> Series -> SeriesValues -> DrawSeries
DrawSeries (Colour -> Text
colourClass Colour
c) Series
s (Series -> SeriesValues
seriesValues Series
s)) (Colouring -> [Series] -> [Colour]
seriesColours Colouring
colouring [Series]
items) [Series]
items
    listseries :: Bool
listseries = Bool -> [Series] -> Bool
legendListsSeries (case XAxis
xaxis of XCategories [ChartLabel]
_ -> Bool
True; XAxis
_ -> Bool
False) ((DrawSeries -> Series) -> [DrawSeries] -> [Series]
forall a b. (a -> b) -> [a] -> [b]
map DrawSeries -> Series
dsSeries [DrawSeries]
drawn)
    (Double
legendh, Svg
legendm) = Text
-> Double
-> Double
-> Bool
-> [(Text, Mark, Text, Bool)]
-> Maybe Series
-> Bool
-> Maybe Text
-> (Double, Svg)
legendSvg (SvgOptions -> Text
svgIds SvgOptions
opts) (SvgOptions -> Double
svgWidth SvgOptions
opts) (Double
top Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
headingh) Bool
listseries
                           [(DrawSeries -> Text
dsClass DrawSeries
d, Series -> Mark
seMark Series
s, ChartLabel -> Text
lbFull (ChartLabel -> Text) -> ChartLabel -> Text
forall a b. (a -> b) -> a -> b
$ Series -> ChartLabel
seLabel Series
s, Series -> Bool
hatchedSeries Series
s) | DrawSeries
d <- [DrawSeries]
drawn, let s :: Series
s = DrawSeries -> Series
dsSeries DrawSeries
d]
                           (if [Series] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Series]
items Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
1 then Maybe Series
mtotalmark else Maybe Series
forall a. Maybe a
Nothing) Bool
hascontra (Summary -> Text
suLabel (Summary -> Text) -> Maybe Summary -> Maybe Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe Summary
maverage)
    hascontra :: Bool
hascontra = [Bool] -> Bool
forall (t :: * -> *). Foldable t => t Bool -> Bool
or [Series -> Quantity -> Bool
isContra Series
s Quantity
q | Series
s <- [Series]
items, Series -> Mark
seMark Series
s Mark -> Mark -> Bool
forall a. Eq a => a -> a -> Bool
== Mark
MarkBar, Just Quantity
q <- Series -> [Maybe Quantity]
seValues Series
s]
    hashatching :: Bool
hashatching = [Bool] -> Bool
forall (t :: * -> *). Foldable t => t Bool -> Bool
or [Series -> Quantity -> Bool
isHatched Series
s Quantity
q | Series
s <- [Series]
items, Series -> Mark
seMark Series
s Mark -> Mark -> Bool
forall a. Eq a => a -> a -> Bool
== Mark
MarkBar, Just Quantity
q <- Series -> [Maybe Quantity]
seValues Series
s]
    (Double
bodyh, Svg
bodym) = case XAxis
xaxis of
      XCategories [ChartLabel]
ls -> Text
-> Double
-> Double
-> Double
-> Maybe Quantity
-> Amount
-> [DrawSeries]
-> Maybe Series
-> Maybe Series
-> [Maybe Text]
-> [ChartLabel]
-> (Double, Svg)
categoriesSvg (SvgOptions -> Text
svgIds SvgOptions
opts) (SvgOptions -> Double
svgWidth SvgOptions
opts) (SvgOptions -> Double
svgPlotHeight SvgOptions
opts) (Double
top Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
headingh Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
legendh) Maybe Quantity
pnScale Amount
pnUnit [DrawSeries]
drawn Maybe Series
mtotal Maybe Series
mtotalmark [Maybe Text]
pnValueLabels [ChartLabel]
ls
      XPeriods [(DateSpan, Text)]
ps    -> SvgOptions
-> Double
-> Maybe Quantity
-> BarLayout
-> Amount
-> [DrawSeries]
-> Maybe Series
-> Maybe (Text, Quantity)
-> [Maybe Text]
-> XScale
-> (Double, Svg)
timeSvg SvgOptions
opts (Double
top Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
headingh Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
legendh) Maybe Quantity
pnScale BarLayout
pnLayout Amount
pnUnit [DrawSeries]
drawn Maybe Series
mtotalmark Maybe (Text, Quantity)
averageline [Maybe Text]
pnValueLabels ([Text] -> XScale
EvenScale ([Text] -> XScale) -> [Text] -> XScale
forall a b. (a -> b) -> a -> b
$ ((DateSpan, Text) -> Text) -> [(DateSpan, Text)] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map (DateSpan, Text) -> Text
forall a b. (a, b) -> b
snd [(DateSpan, Text)]
ps)
      XDates [Day]
ds      -> SvgOptions
-> Double
-> Maybe Quantity
-> BarLayout
-> Amount
-> [DrawSeries]
-> Maybe Series
-> Maybe (Text, Quantity)
-> [Maybe Text]
-> XScale
-> (Double, Svg)
timeSvg SvgOptions
opts (Double
top Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
headingh Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
legendh) Maybe Quantity
pnScale BarLayout
pnLayout Amount
pnUnit [DrawSeries]
drawn Maybe Series
mtotalmark Maybe (Text, Quantity)
forall a. Maybe a
Nothing [Maybe Text]
pnValueLabels ([Day] -> XScale
DateScale [Day]
ds)
    averageline :: Maybe (Text, Quantity)
averageline = (\Summary
su -> (Summary -> Text
suLabel Summary
su Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" · " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Summary -> Text
showSummary Summary
su, Amount -> Quantity
aquantity (Amount -> Quantity) -> Amount -> Quantity
forall a b. (a -> b) -> a -> b
$ Summary -> Amount
suValue Summary
su)) (Summary -> (Text, Quantity))
-> Maybe Summary -> Maybe (Text, Quantity)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe Summary
maverage

-- | A legend: a swatch and label for each series, flowing left to right, if they are to be listed (eg when there are several).
-- A series drawn hatched (a shortfall) has a hatched swatch. If there are contra amounts, a neutral hatched swatch is shown for them too.
-- If an average line is drawn (with this label), a dashed swatch is shown for it, even when the series are not listed.
legendSvg :: Text -> Double -> Double -> Bool -> [(Text, Mark, Text, Bool)] -> Maybe Series -> Bool -> Maybe Text -> (Double, Svg)
legendSvg :: Text
-> Double
-> Double
-> Bool
-> [(Text, Mark, Text, Bool)]
-> Maybe Series
-> Bool
-> Maybe Text
-> (Double, Svg)
legendSvg Text
ids Double
width Double
top Bool
listseries [(Text, Mark, Text, Bool)]
entries Maybe Series
mtotal Bool
hascontra Maybe Text
maveragelabel
  | [(Text, Mark, Text, Bool)] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [(Text, Mark, Text, Bool)]
allentries = (Double
0, Svg
forall a. Monoid a => a
mempty)
  | Bool
otherwise = (Int -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral ([[(Text, Mark, Text, Bool)]] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [[(Text, Mark, Text, Bool)]]
rows) Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
legendRowHeight Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
8, ((Int, [(Text, Mark, Text, Bool)]) -> Svg)
-> [(Int, [(Text, Mark, Text, Bool)])] -> Svg
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (Int, [(Text, Mark, Text, Bool)]) -> Svg
drawRow ([Int]
-> [[(Text, Mark, Text, Bool)]]
-> [(Int, [(Text, Mark, Text, Bool)])]
forall a b. [a] -> [b] -> [(a, b)]
zip [Int
0 ..] [[(Text, Mark, Text, Bool)]]
rows))
  where
    allentries :: [(Text, Mark, Text, Bool)]
allentries
      | Bool -> Bool
not Bool
listseries = [(Text, Mark, Text, Bool)]
averageentry
      | Bool
otherwise = [(Text, Mark, Text, Bool)]
entries
          [(Text, Mark, Text, Bool)]
-> [(Text, Mark, Text, Bool)] -> [(Text, Mark, Text, Bool)]
forall a. [a] -> [a] -> [a]
++ [(Text
"total", if Series -> Mark
seMark Series
t Mark -> Mark -> Bool
forall a. Eq a => a -> a -> Bool
== Mark
MarkLine Bool -> Bool -> Bool
|| ((Text, Mark, Text, Bool) -> Bool)
-> [(Text, Mark, Text, Bool)] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (\(Text
_, Mark
mk, Text
_, Bool
_) -> Mark
mk Mark -> Mark -> Bool
forall a. Eq a => a -> a -> Bool
== Mark
MarkLine) [(Text, Mark, Text, Bool)]
entries then Mark
MarkLine else Mark
MarkMarker, ChartLabel -> Text
lbFull (Series -> ChartLabel
seLabel Series
t), Bool
False) | Just Series
t <- [Maybe Series
mtotal]]
          [(Text, Mark, Text, Bool)]
-> [(Text, Mark, Text, Bool)] -> [(Text, Mark, Text, Bool)]
forall a. [a] -> [a] -> [a]
++ [(Text, Mark, Text, Bool)]
averageentry
          [(Text, Mark, Text, Bool)]
-> [(Text, Mark, Text, Bool)] -> [(Text, Mark, Text, Bool)]
forall a. [a] -> [a] -> [a]
++ [(Colour -> Text
colourClass Colour
NeutralColour, Mark
MarkBar, Text
"contra", Bool
True) | Bool
hascontra]
    averageentry :: [(Text, Mark, Text, Bool)]
averageentry = [(Text
averageClass, Mark
MarkLine, Text
lbl, Bool
False) | Just Text
lbl <- [Maybe Text
maveragelabel]]
    averageClass :: Text
averageClass = Text
"average"
    itemWidth :: (a, b, Text, d) -> Double
itemWidth (a
_, b
_, Text
lbl, d
_) = Double
20 Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Text -> Double
textWidth Text
lbl Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
18
    rows :: [[(Text, Mark, Text, Bool)]]
rows = [[(Text, Mark, Text, Bool)]] -> [[(Text, Mark, Text, Bool)]]
forall a. [a] -> [a]
reverse ([[(Text, Mark, Text, Bool)]] -> [[(Text, Mark, Text, Bool)]])
-> [[(Text, Mark, Text, Bool)]] -> [[(Text, Mark, Text, Bool)]]
forall a b. (a -> b) -> a -> b
$ ([(Text, Mark, Text, Bool)] -> [(Text, Mark, Text, Bool)])
-> [[(Text, Mark, Text, Bool)]] -> [[(Text, Mark, Text, Bool)]]
forall a b. (a -> b) -> [a] -> [b]
map [(Text, Mark, Text, Bool)] -> [(Text, Mark, Text, Bool)]
forall a. [a] -> [a]
reverse ([[(Text, Mark, Text, Bool)]] -> [[(Text, Mark, Text, Bool)]])
-> [[(Text, Mark, Text, Bool)]] -> [[(Text, Mark, Text, Bool)]]
forall a b. (a -> b) -> a -> b
$ ([[(Text, Mark, Text, Bool)]]
 -> (Text, Mark, Text, Bool) -> [[(Text, Mark, Text, Bool)]])
-> [[(Text, Mark, Text, Bool)]]
-> [(Text, Mark, Text, Bool)]
-> [[(Text, Mark, Text, Bool)]]
forall b a. (b -> a -> b) -> b -> [a] -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl [[(Text, Mark, Text, Bool)]]
-> (Text, Mark, Text, Bool) -> [[(Text, Mark, Text, Bool)]]
forall {a} {b} {d}.
[[(a, b, Text, d)]] -> (a, b, Text, d) -> [[(a, b, Text, d)]]
place [] [(Text, Mark, Text, Bool)]
allentries
    place :: [[(a, b, Text, d)]] -> (a, b, Text, d) -> [[(a, b, Text, d)]]
place [] (a, b, Text, d)
e = [[(a, b, Text, d)
e]]
    place ([(a, b, Text, d)]
r:[[(a, b, Text, d)]]
rs) (a, b, Text, d)
e
      | [Double] -> Double
forall a. Num a => [a] -> a
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum (((a, b, Text, d) -> Double) -> [(a, b, Text, d)] -> [Double]
forall a b. (a -> b) -> [a] -> [b]
map (a, b, Text, d) -> Double
forall {a} {b} {d}. (a, b, Text, d) -> Double
itemWidth [(a, b, Text, d)]
r) Double -> Double -> Double
forall a. Num a => a -> a -> a
+ (a, b, Text, d) -> Double
forall {a} {b} {d}. (a, b, Text, d) -> Double
itemWidth (a, b, Text, d)
e Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
<= Double
width Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
2 Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
pad = ((a, b, Text, d)
e (a, b, Text, d) -> [(a, b, Text, d)] -> [(a, b, Text, d)]
forall a. a -> [a] -> [a]
: [(a, b, Text, d)]
r) [(a, b, Text, d)] -> [[(a, b, Text, d)]] -> [[(a, b, Text, d)]]
forall a. a -> [a] -> [a]
: [[(a, b, Text, d)]]
rs
      | Bool
otherwise = [(a, b, Text, d)
e] [(a, b, Text, d)] -> [[(a, b, Text, d)]] -> [[(a, b, Text, d)]]
forall a. a -> [a] -> [a]
: [(a, b, Text, d)]
r [(a, b, Text, d)] -> [[(a, b, Text, d)]] -> [[(a, b, Text, d)]]
forall a. a -> [a] -> [a]
: [[(a, b, Text, d)]]
rs
    drawRow :: (Int, [(Text, Mark, Text, Bool)]) -> Svg
    drawRow :: (Int, [(Text, Mark, Text, Bool)]) -> Svg
drawRow (Int
i, [(Text, Mark, Text, Bool)]
row) = do
      let y :: Double
y = Double
top Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Int -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
i Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
legendRowHeight Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
10
          xs :: [Double]
xs = (Double -> Double -> Double) -> Double -> [Double] -> [Double]
forall b a. (b -> a -> b) -> b -> [a] -> [b]
scanl Double -> Double -> Double
forall a. Num a => a -> a -> a
(+) Double
pad (((Text, Mark, Text, Bool) -> Double)
-> [(Text, Mark, Text, Bool)] -> [Double]
forall a b. (a -> b) -> [a] -> [b]
map (Text, Mark, Text, Bool) -> Double
forall {a} {b} {d}. (a, b, Text, d) -> Double
itemWidth [(Text, Mark, Text, Bool)]
row)
      [(Double, (Text, Mark, Text, Bool))]
-> ((Double, (Text, Mark, Text, Bool)) -> Svg) -> Svg
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ ([Double]
-> [(Text, Mark, Text, Bool)]
-> [(Double, (Text, Mark, Text, Bool))]
forall a b. [a] -> [b] -> [(a, b)]
zip [Double]
xs [(Text, Mark, Text, Bool)]
row) (((Double, (Text, Mark, Text, Bool)) -> Svg) -> Svg)
-> ((Double, (Text, Mark, Text, Bool)) -> Svg) -> Svg
forall a b. (a -> b) -> a -> b
$ \(Double
x, (Text
cls, Mark
mk, Text
lbl, Bool
hatched)) -> do
        case Mark
mk of
          Mark
MarkLine | Text
cls Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
averageClass ->  -- a dashed line, a little longer so that it ends with a dash
                          Svg
S.line Svg -> Attribute -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.class_ AttributeValue
"average" Svg -> Attribute -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.x1 (Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue (Text -> AttributeValue) -> Text -> AttributeValue
forall a b. (a -> b) -> a -> b
$ Double -> Text
num Double
x) Svg -> Attribute -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.x2 (Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue (Text -> AttributeValue) -> Text -> AttributeValue
forall a b. (a -> b) -> a -> b
$ Double -> Text
num (Double
x Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
14))
                          Svg -> Attribute -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.y1 (Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue (Text -> AttributeValue) -> Text -> AttributeValue
forall a b. (a -> b) -> a -> b
$ Double -> Text
num Double
y) Svg -> Attribute -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.y2 (Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue (Text -> AttributeValue) -> Text -> AttributeValue
forall a b. (a -> b) -> a -> b
$ Double -> Text
num Double
y)
          Mark
MarkLine   -> Svg
S.line Svg -> Attribute -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.class_ (AttributeValue
"line " AttributeValue -> AttributeValue -> AttributeValue
forall a. Semigroup a => a -> a -> a
<> Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue Text
cls) Svg -> Attribute -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.x1 (Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue (Text -> AttributeValue) -> Text -> AttributeValue
forall a b. (a -> b) -> a -> b
$ Double -> Text
num Double
x) Svg -> Attribute -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.x2 (Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue (Text -> AttributeValue) -> Text -> AttributeValue
forall a b. (a -> b) -> a -> b
$ Double -> Text
num (Double
x Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
12))
                          Svg -> Attribute -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.y1 (Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue (Text -> AttributeValue) -> Text -> AttributeValue
forall a b. (a -> b) -> a -> b
$ Double -> Text
num Double
y) Svg -> Attribute -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.y2 (Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue (Text -> AttributeValue) -> Text -> AttributeValue
forall a b. (a -> b) -> a -> b
$ Double -> Text
num Double
y)
          Mark
MarkMarker -> Svg
S.line Svg -> Attribute -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.class_ (AttributeValue
"mark " AttributeValue -> AttributeValue -> AttributeValue
forall a. Semigroup a => a -> a -> a
<> Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue Text
cls) Svg -> Attribute -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.x1 (Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue (Text -> AttributeValue) -> Text -> AttributeValue
forall a b. (a -> b) -> a -> b
$ Double -> Text
num Double
x) Svg -> Attribute -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.x2 (Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue (Text -> AttributeValue) -> Text -> AttributeValue
forall a b. (a -> b) -> a -> b
$ Double -> Text
num (Double
x Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
12))
                          Svg -> Attribute -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.y1 (Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue (Text -> AttributeValue) -> Text -> AttributeValue
forall a b. (a -> b) -> a -> b
$ Double -> Text
num Double
y) Svg -> Attribute -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.y2 (Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue (Text -> AttributeValue) -> Text -> AttributeValue
forall a b. (a -> b) -> a -> b
$ Double -> Text
num Double
y)
          Mark
MarkBar | Bool
hatched -> do
                          Double -> Double -> Svg
swatch Double
x Double
y Svg -> Attribute -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.class_ (AttributeValue
"fill " AttributeValue -> AttributeValue -> AttributeValue
forall a. Semigroup a => a -> a -> a
<> Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue Text
cls)
                          Double -> Double -> Svg
swatch Double
x Double
y Svg -> Attribute -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.fill (Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue (Text -> AttributeValue) -> Text -> AttributeValue
forall a b. (a -> b) -> a -> b
$ Text
"url(#" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text -> Text
hatchId Text
ids Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
")")
          Mark
MarkBar    -> Double -> Double -> Svg
swatch Double
x Double
y Svg -> Attribute -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.class_ (AttributeValue
"fill " AttributeValue -> AttributeValue -> AttributeValue
forall a. Semigroup a => a -> a -> a
<> Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue Text
cls)
          Mark
MarkNone   -> Svg
forall a. Monoid a => a
mempty
        -- the total's label is classed like its swatch and its line or marks, so that a page can hide them all (eg hledger-web's N key)
        Svg -> Svg
S.text_ (Svg -> Svg) -> Attribute -> Svg -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.class_ (if Text
cls Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
"total" then AttributeValue
"label total" else AttributeValue
"label") (Svg -> Svg) -> Attribute -> Svg -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.x (Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue (Text -> AttributeValue) -> Text -> AttributeValue
forall a b. (a -> b) -> a -> b
$ Double -> Text
num (Double
x Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
20)) (Svg -> Svg) -> Attribute -> Svg -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.y (Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue (Text -> AttributeValue) -> Text -> AttributeValue
forall a b. (a -> b) -> a -> b
$ Double -> Text
num (Double
y Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
5)) (Svg -> Svg) -> Svg -> Svg
forall a b. (a -> b) -> a -> b
$ Text -> Svg
forall a. ToMarkup a => a -> Svg
S.toMarkup Text
lbl
    swatch :: Double -> Double -> Svg
swatch Double
x Double
y = Svg
S.rect Svg -> Attribute -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.x (Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue (Text -> AttributeValue) -> Text -> AttributeValue
forall a b. (a -> b) -> a -> b
$ Double -> Text
num Double
x) Svg -> Attribute -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.y (Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue (Text -> AttributeValue) -> Text -> AttributeValue
forall a b. (a -> b) -> a -> b
$ Double -> Text
num (Double
y Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
6)) Svg -> Attribute -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.width AttributeValue
"12" Svg -> Attribute -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.height AttributeValue
"12" Svg -> Attribute -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.rx AttributeValue
"3"

-- | How x positions are placed along a time axis: evenly, with these labels (eg report periods),
-- or by date, on a time scale (eg a register's transactions; lines are then drawn as steps).
data XScale = EvenScale [Text] | DateScale [Day]

-- | Draw series over time, along the given x scale:
-- vertical stacked bars (with the grouped layout, a stack per group, side by side), lines, and total marks or lines.
timeSvg :: SvgOptions -> Double -> Maybe Quantity -> BarLayout -> Amount -> [DrawSeries] -> Maybe Series -> Maybe (Text, Quantity) -> [Maybe Text] -> XScale -> (Double, Svg)
timeSvg :: SvgOptions
-> Double
-> Maybe Quantity
-> BarLayout
-> Amount
-> [DrawSeries]
-> Maybe Series
-> Maybe (Text, Quantity)
-> [Maybe Text]
-> XScale
-> (Double, Svg)
timeSvg SvgOptions{svgWidth :: SvgOptions -> Double
svgWidth = Double
width, svgPlotHeight :: SvgOptions -> Double
svgPlotHeight = Double
plotheight, Int -> Maybe Text
svgPointLink :: SvgOptions -> Int -> Maybe Text
svgPointLink :: Int -> Maybe Text
svgPointLink, Bool
svgDateScale :: SvgOptions -> Bool
svgDateScale :: Bool
svgDateScale, Maybe Day
svgToday :: SvgOptions -> Maybe Day
svgToday :: Maybe Day
svgToday, Text
svgIds :: SvgOptions -> Text
svgIds :: Text
svgIds} Double
top Maybe Quantity
mscale BarLayout
layout Amount
unit [DrawSeries]
drawn Maybe Series
mtotal Maybe (Text, Quantity)
maverage [Maybe Text]
vlabels XScale
xscale = (Double
plotheight Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
32 Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
labelroom, do
    -- the plot's area with its date scale, if wanted, beneath the rest so that their tooltips and links still work
    Bool -> Svg -> Svg
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Bool
svgDateScale Bool -> Bool -> Bool
&& Maybe (Array Int Day) -> Bool
forall a. Maybe a -> Bool
isJust Maybe (Array Int Day)
datearray) (Svg -> Svg) -> Svg -> Svg
forall a b. (a -> b) -> a -> b
$
      Svg
S.rect Svg -> Attribute -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.class_ AttributeValue
"plot"
        Svg -> Attribute -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.x (Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue (Text -> AttributeValue) -> Text -> AttributeValue
forall a b. (a -> b) -> a -> b
$ Double -> Text
num Double
plotleft) Svg -> Attribute -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.y (Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue (Text -> AttributeValue) -> Text -> AttributeValue
forall a b. (a -> b) -> a -> b
$ Double -> Text
num Double
plottop) Svg -> Attribute -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.width (Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue (Text -> AttributeValue) -> Text -> AttributeValue
forall a b. (a -> b) -> a -> b
$ Double -> Text
num (Double
plotright Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
plotleft)) Svg -> Attribute -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.height (Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue (Text -> AttributeValue) -> Text -> AttributeValue
forall a b. (a -> b) -> a -> b
$ Double -> Text
num Double
plotheight)
        Svg -> Attribute -> Svg
forall h. Attributable h => h -> Attribute -> h
! Tag -> AttributeValue -> Attribute
customAttribute Tag
"data-d0" (Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue (Text -> AttributeValue) -> Text -> AttributeValue
forall a b. (a -> b) -> a -> b
$ Day -> Text
showDate Day
d0) Svg -> Attribute -> Svg
forall h. Attributable h => h -> Attribute -> h
! Tag -> AttributeValue -> Attribute
customAttribute Tag
"data-d1" (Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue (Text -> AttributeValue) -> Text -> AttributeValue
forall a b. (a -> b) -> a -> b
$ Day -> Text
showDate Day
d1)
        Svg -> Attribute -> Svg
forall h. Attributable h => h -> Attribute -> h
! Tag -> AttributeValue -> Attribute
customAttribute Tag
"data-x0" (Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue (Text -> AttributeValue) -> Text -> AttributeValue
forall a b. (a -> b) -> a -> b
$ Double -> Text
num (Double -> Text) -> Double -> Text
forall a b. (a -> b) -> a -> b
$ Day -> Double
xofday Day
d0) Svg -> Attribute -> Svg
forall h. Attributable h => h -> Attribute -> h
! Tag -> AttributeValue -> Attribute
customAttribute Tag
"data-x1" (Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue (Text -> AttributeValue) -> Text -> AttributeValue
forall a b. (a -> b) -> a -> b
$ Double -> Text
num (Double -> Text) -> Double -> Text
forall a b. (a -> b) -> a -> b
$ Day -> Double
xofday Day
d1)
    -- the shading as of today, if wanted: the past below zero, and the future, divided halfway between today and tomorrow
    Maybe Day -> (Day -> Svg) -> Svg
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ Maybe Day
mtoday ((Day -> Svg) -> Svg) -> (Day -> Svg) -> Svg
forall a b. (a -> b) -> a -> b
$ \Day
today -> do
      let xnow :: Double
xnow = Double -> Double -> Double -> Double
clamp Double
plotleft Double
plotright (Double -> Double) -> Double -> Double
forall a b. (a -> b) -> a -> b
$ Double -> Double
xofdays (Integer -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Day -> Day -> Integer
diffDays Day
today Day
d0) Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
0.5)
          yzero :: Double
yzero = Double -> Double
yof Double
0
          shade :: AttributeValue -> Double -> Double -> Double -> Double -> Svg
shade AttributeValue
cls Double
x Double
y Double
w Double
h = Bool -> Svg -> Svg
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Double
w Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
> Double
0 Bool -> Bool -> Bool
&& Double
h Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
> Double
0) (Svg -> Svg) -> Svg -> Svg
forall a b. (a -> b) -> a -> b
$ Svg
S.rect Svg -> Attribute -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.class_ (AttributeValue
"shade " AttributeValue -> AttributeValue -> AttributeValue
forall a. Semigroup a => a -> a -> a
<> AttributeValue
cls)
            Svg -> Attribute -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.x (Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue (Text -> AttributeValue) -> Text -> AttributeValue
forall a b. (a -> b) -> a -> b
$ Double -> Text
num Double
x) Svg -> Attribute -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.y (Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue (Text -> AttributeValue) -> Text -> AttributeValue
forall a b. (a -> b) -> a -> b
$ Double -> Text
num Double
y) Svg -> Attribute -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.width (Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue (Text -> AttributeValue) -> Text -> AttributeValue
forall a b. (a -> b) -> a -> b
$ Double -> Text
num Double
w) Svg -> Attribute -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.height (Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue (Text -> AttributeValue) -> Text -> AttributeValue
forall a b. (a -> b) -> a -> b
$ Double -> Text
num Double
h)
      AttributeValue -> Double -> Double -> Double -> Double -> Svg
shade AttributeValue
"past-negative" Double
plotleft Double
yzero (Double
xnow Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
plotleft) (Double
plotbottom Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
yzero)
      AttributeValue -> Double -> Double -> Double -> Double -> Svg
shade AttributeValue
"future" Double
xnow Double
plottop (Double
plotright Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
xnow) (Double
yzero Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
plottop)
      AttributeValue -> Double -> Double -> Double -> Double -> Svg
shade AttributeValue
"future-negative" Double
xnow Double
yzero (Double
plotright Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
xnow) (Double
plotbottom Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
yzero)
    -- gridlines and y tick labels
    [Double] -> (Double -> Svg) -> Svg
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [Double]
ticks ((Double -> Svg) -> Svg) -> (Double -> Svg) -> Svg
forall a b. (a -> b) -> a -> b
$ \Double
t -> do
      Svg
S.line Svg -> Attribute -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.class_ (if Double
t Double -> Double -> Bool
forall a. Eq a => a -> a -> Bool
== Double
0 then (if Maybe Day -> Bool
forall a. Maybe a -> Bool
isJust Maybe Day
mtoday then AttributeValue
"axis zero" else AttributeValue
"axis") else AttributeValue
"grid")
        Svg -> Attribute -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.x1 (Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue (Text -> AttributeValue) -> Text -> AttributeValue
forall a b. (a -> b) -> a -> b
$ Double -> Text
num Double
plotleft) Svg -> Attribute -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.x2 (Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue (Text -> AttributeValue) -> Text -> AttributeValue
forall a b. (a -> b) -> a -> b
$ Double -> Text
num Double
plotright) Svg -> Attribute -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.y1 (Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue (Text -> AttributeValue) -> Text -> AttributeValue
forall a b. (a -> b) -> a -> b
$ Double -> Text
num (Double -> Double
yof Double
t)) Svg -> Attribute -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.y2 (Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue (Text -> AttributeValue) -> Text -> AttributeValue
forall a b. (a -> b) -> a -> b
$ Double -> Text
num (Double -> Double
yof Double
t))
      Svg -> Svg
S.text_ (Svg -> Svg) -> Attribute -> Svg -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.class_ AttributeValue
"tick" (Svg -> Svg) -> Attribute -> Svg -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.x (Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue (Text -> AttributeValue) -> Text -> AttributeValue
forall a b. (a -> b) -> a -> b
$ Double -> Text
num (Double
plotleft Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
8)) (Svg -> Svg) -> Attribute -> Svg -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.y (Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue (Text -> AttributeValue) -> Text -> AttributeValue
forall a b. (a -> b) -> a -> b
$ Double -> Text
num (Double -> Double
yof Double
t Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
5)) (Svg -> Svg) -> Attribute -> Svg -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.textAnchor AttributeValue
"end" (Svg -> Svg) -> Svg -> Svg
forall a b. (a -> b) -> a -> b
$ Text -> Svg
forall a. ToMarkup a => a -> Svg
S.toMarkup (Double -> Text
ticklabel Double
t)
    -- x labels: periods' labels, thinned to avoid overlap; or ticks at calendar boundaries
    [(Double, Text)] -> ((Double, Text) -> Svg) -> Svg
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ (Double -> [(Double, Text)] -> [(Double, Text)]
placeXLabels Double
width [(Double, Text)]
shownxlabels) (((Double, Text) -> Svg) -> Svg) -> ((Double, Text) -> Svg) -> Svg
forall a b. (a -> b) -> a -> b
$ \(Double
x, Text
lbl) ->
      Svg -> Svg
S.text_ (Svg -> Svg) -> Attribute -> Svg -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.class_ AttributeValue
"tick" (Svg -> Svg) -> Attribute -> Svg -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.x (Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue (Text -> AttributeValue) -> Text -> AttributeValue
forall a b. (a -> b) -> a -> b
$ Double -> Text
num Double
x) (Svg -> Svg) -> Attribute -> Svg -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.y (Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue (Text -> AttributeValue) -> Text -> AttributeValue
forall a b. (a -> b) -> a -> b
$ Double -> Text
num (Double
plotbottom Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
21)) (Svg -> Svg) -> Attribute -> Svg -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.textAnchor AttributeValue
"middle" (Svg -> Svg) -> Svg -> Svg
forall a b. (a -> b) -> a -> b
$ Text -> Svg
forall a. ToMarkup a => a -> Svg
S.toMarkup Text
lbl
    -- bars: the stacks side by side (2px apart), each with its positive segments stacked upward and negative ones downward
    [(Int, Text)] -> ((Int, Text) -> Svg) -> Svg
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ ([Int] -> [Text] -> [(Int, Text)]
forall a b. [a] -> [b] -> [(a, b)]
zip [Int
0 ..] [Text]
xlabels) (((Int, Text) -> Svg) -> Svg) -> ((Int, Text) -> Svg) -> Svg
forall a b. (a -> b) -> a -> b
$ \(Int
j, Text
lbl) ->
      [(Int, [DrawSeries])] -> ((Int, [DrawSeries]) -> Svg) -> Svg
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ ([Int] -> [[DrawSeries]] -> [(Int, [DrawSeries])]
forall a b. [a] -> [b] -> [(a, b)]
zip [Int
0 :: Int ..] [[DrawSeries]]
stacks) (((Int, [DrawSeries]) -> Svg) -> Svg)
-> ((Int, [DrawSeries]) -> Svg) -> Svg
forall a b. (a -> b) -> a -> b
$ \(Int
k, [DrawSeries]
stk) -> do
        let x :: Double
x = Int -> Double
xcenter Int
j Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
groupwidth Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Double
2 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
* (Double
barwidth Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
2)
        Text -> Double -> [(DrawSeries, Quantity)] -> Double -> Svg
stack Text
lbl Double
x ([(DrawSeries, Quantity)] -> [(DrawSeries, Quantity)]
contraFirst [(DrawSeries
d, Quantity
q) | DrawSeries
d <- [DrawSeries]
stk, Just Quantity
q <- [SeriesValues -> Int -> Maybe Quantity
valueAt (DrawSeries -> SeriesValues
dsValues DrawSeries
d) Int
j], Quantity
q Quantity -> Quantity -> Bool
forall a. Ord a => a -> a -> Bool
> Quantity
0]) Double
1
        Text -> Double -> [(DrawSeries, Quantity)] -> Double -> Svg
stack Text
lbl Double
x ([(DrawSeries, Quantity)] -> [(DrawSeries, Quantity)]
contraFirst [(DrawSeries
d, Quantity
q) | DrawSeries
d <- [DrawSeries]
stk, Just Quantity
q <- [SeriesValues -> Int -> Maybe Quantity
valueAt (DrawSeries -> SeriesValues
dsValues DrawSeries
d) Int
j], Quantity
q Quantity -> Quantity -> Bool
forall a. Ord a => a -> a -> Bool
< Quantity
0]) (-Double
1)
    -- the value labels, if any (see 'pnValueLabels'): above each x position's bars (or below, if they are all negative),
    -- if they all fit (all or none, so that which are shown doesn't depend on their lengths; tooltips have them too)
    Bool -> Svg -> Svg
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when ((Text -> Bool) -> [Text] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all ((Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
<= Double
band Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
2) (Double -> Bool) -> (Text -> Double) -> Text -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Double
labelWidth) ([Text] -> Bool) -> [Text] -> Bool
forall a b. (a -> b) -> a -> b
$ [Maybe Text] -> [Text]
forall a. [Maybe a] -> [a]
catMaybes [Maybe Text]
vlabels) (Svg -> Svg) -> Svg -> Svg
forall a b. (a -> b) -> a -> b
$
     [(Int, Text)] -> ((Int, Text) -> Svg) -> Svg
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ ([Int] -> [Text] -> [(Int, Text)]
forall a b. [a] -> [b] -> [(a, b)]
zip [Int
0 ..] [Text]
xlabels) (((Int, Text) -> Svg) -> Svg) -> ((Int, Text) -> Svg) -> Svg
forall a b. (a -> b) -> a -> b
$ \(Int
j, Text
_) -> Maybe Text -> (Text -> Svg) -> Svg
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ (Maybe (Maybe Text) -> Maybe Text
forall (m :: * -> *) a. Monad m => m (m a) -> m a
join (Maybe (Maybe Text) -> Maybe Text)
-> Maybe (Maybe Text) -> Maybe Text
forall a b. (a -> b) -> a -> b
$ [Maybe Text]
vlabels [Maybe Text] -> Int -> Maybe (Maybe Text)
forall a. [a] -> Int -> Maybe a
`atMay` Int
j) ((Text -> Svg) -> Svg) -> (Text -> Svg) -> Svg
forall a b. (a -> b) -> a -> b
$ \Text
t -> do
      let ends :: Quantity -> [Double]
ends Quantity
sign = [[Double] -> Double
forall a. Num a => [a] -> a
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum [Quantity -> Double
toD Quantity
q | DrawSeries
d <- [DrawSeries]
stk, Just Quantity
q <- [SeriesValues -> Int -> Maybe Quantity
valueAt (DrawSeries -> SeriesValues
dsValues DrawSeries
d) Int
j], Quantity -> Quantity
forall a. Num a => a -> a
signum Quantity
q Quantity -> Quantity -> Bool
forall a. Eq a => a -> a -> Bool
== Quantity
sign] | [DrawSeries]
stk <- [[DrawSeries]]
stacks]
          up :: Double
up = Double -> [Double] -> Double
forall a. Ord a => a -> [a] -> a
maximumDef Double
0 (Quantity -> [Double]
ends Quantity
1)
          down :: Double
down = Double -> [Double] -> Double
forall a. Ord a => a -> [a] -> a
minimumDef Double
0 (Quantity -> [Double]
ends (-Quantity
1))
          vy :: Double
vy = if Double
up Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
> Double
0 Bool -> Bool -> Bool
|| Double
down Double -> Double -> Bool
forall a. Eq a => a -> a -> Bool
== Double
0 then Double -> Double
yof Double
up Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
6 else Double -> Double
yof Double
down Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
14
      Svg -> Svg
S.text_ (Svg -> Svg) -> Attribute -> Svg -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.class_ (Text -> AttributeValue
vlabelClass Text
t) (Svg -> Svg) -> Attribute -> Svg -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.x (Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue (Text -> AttributeValue) -> Text -> AttributeValue
forall a b. (a -> b) -> a -> b
$ Double -> Text
num (Int -> Double
xcenter Int
j)) (Svg -> Svg) -> Attribute -> Svg -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.y (Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue (Text -> AttributeValue) -> Text -> AttributeValue
forall a b. (a -> b) -> a -> b
$ Double -> Text
num Double
vy) (Svg -> Svg) -> Attribute -> Svg -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.textAnchor AttributeValue
"middle" (Svg -> Svg) -> Svg -> Svg
forall a b. (a -> b) -> a -> b
$ Text -> Svg
forall a. ToMarkup a => a -> Svg
S.toMarkup Text
t
    -- with a fixed scale, lines are clipped at the plot's edges
    Bool -> Svg -> Svg
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Maybe Quantity -> Bool
forall a. Maybe a -> Bool
isJust Maybe Quantity
mscale) (Svg -> Svg) -> Svg -> Svg
forall a b. (a -> b) -> a -> b
$ Svg -> Svg
S.defs (Svg -> Svg) -> Svg -> Svg
forall a b. (a -> b) -> a -> b
$ Svg -> Svg
S.clippath (Svg -> Svg) -> Attribute -> Svg -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.id_ (Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue Text
clipid) (Svg -> Svg) -> Svg -> Svg
forall a b. (a -> b) -> a -> b
$
      Svg
S.rect Svg -> Attribute -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.x (Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue (Text -> AttributeValue) -> Text -> AttributeValue
forall a b. (a -> b) -> a -> b
$ Double -> Text
num Double
plotleft) Svg -> Attribute -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.y (Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue (Text -> AttributeValue) -> Text -> AttributeValue
forall a b. (a -> b) -> a -> b
$ Double -> Text
num Double
plottop) Svg -> Attribute -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.width (Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue (Text -> AttributeValue) -> Text -> AttributeValue
forall a b. (a -> b) -> a -> b
$ Double -> Text
num (Double
plotright Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
plotleft)) Svg -> Attribute -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.height (Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue (Text -> AttributeValue) -> Text -> AttributeValue
forall a b. (a -> b) -> a -> b
$ Double -> Text
num Double
plotheight)
    let clipLines :: Svg -> Svg
clipLines = if Maybe Quantity -> Bool
forall a. Maybe a -> Bool
isJust Maybe Quantity
mscale then Svg -> Svg
S.g (Svg -> Svg) -> Attribute -> Svg -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.clipPath (Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue (Text -> AttributeValue) -> Text -> AttributeValue
forall a b. (a -> b) -> a -> b
$ Text
"url(#" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
clipid Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
")") else Svg -> Svg
forall a. a -> a
id
    -- lines
    Svg -> Svg
clipLines (Svg -> Svg) -> Svg -> Svg
forall a b. (a -> b) -> a -> b
$ [DrawSeries] -> (DrawSeries -> Svg) -> Svg
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [DrawSeries]
lines_ ((DrawSeries -> Svg) -> Svg) -> (DrawSeries -> Svg) -> Svg
forall a b. (a -> b) -> a -> b
$ \DrawSeries
d -> Text -> Series -> Svg
lineSvg (DrawSeries -> Text
dsClass DrawSeries
d) (DrawSeries -> Series
dsSeries DrawSeries
d)
    -- the total: as a line, or as a mark (a thin horizontal line) across each period's bars
    Maybe Series -> (Series -> Svg) -> Svg
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ Maybe Series
mtotal ((Series -> Svg) -> Svg) -> (Series -> Svg) -> Svg
forall a b. (a -> b) -> a -> b
$ \Series
t ->
      if Series -> Mark
seMark Series
t Mark -> Mark -> Bool
forall a. Eq a => a -> a -> Bool
== Mark
MarkLine Bool -> Bool -> Bool
|| [DrawSeries] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [DrawSeries]
bars then Svg -> Svg
clipLines (Svg -> Svg) -> Svg -> Svg
forall a b. (a -> b) -> a -> b
$ Text -> Series -> Svg
lineSvg Text
"total" Series
t
      else let tvals :: SeriesValues
tvals = Series -> SeriesValues
seriesValues Series
t in
        [(Int, Text)] -> ((Int, Text) -> Svg) -> Svg
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ ([Int] -> [Text] -> [(Int, Text)]
forall a b. [a] -> [b] -> [(a, b)]
zip [Int
0 ..] [Text]
xlabels) (((Int, Text) -> Svg) -> Svg) -> ((Int, Text) -> Svg) -> Svg
forall a b. (a -> b) -> a -> b
$ \(Int
j, Text
lbl) -> Maybe Quantity -> (Quantity -> Svg) -> Svg
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ (SeriesValues -> Int -> Maybe Quantity
valueAt SeriesValues
tvals Int
j) ((Quantity -> Svg) -> Svg) -> (Quantity -> Svg) -> Svg
forall a b. (a -> b) -> a -> b
$ \Quantity
q -> Bool -> Svg -> Svg
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int -> Bool
hasbars Int
j) (Svg -> Svg) -> Svg -> Svg
forall a b. (a -> b) -> a -> b
$ Text -> Svg -> Svg
withTooltip (Text -> Series -> Quantity -> Text
tooltip Text
lbl Series
t Quantity
q Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text -> (Text -> Text) -> Maybe Text -> Text
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Text
"" (Text
" · " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<>) (Maybe (Maybe Text) -> Maybe Text
forall (m :: * -> *) a. Monad m => m (m a) -> m a
join (Maybe (Maybe Text) -> Maybe Text)
-> Maybe (Maybe Text) -> Maybe Text
forall a b. (a -> b) -> a -> b
$ [Maybe Text]
vlabels [Maybe Text] -> Int -> Maybe (Maybe Text)
forall a. [a] -> Int -> Maybe a
`atMay` Int
j)) (Svg -> Svg) -> Svg -> Svg
forall a b. (a -> b) -> a -> b
$ do
          let y :: AttributeValue
y = Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue (Text -> AttributeValue) -> Text -> AttributeValue
forall a b. (a -> b) -> a -> b
$ Double -> Text
num (Double -> Double
yof (Double -> Double) -> Double -> Double
forall a b. (a -> b) -> a -> b
$ Quantity -> Double
toD Quantity
q)
              halfw :: Double
halfw = Double
groupwidth Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Double
2 Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
4
              x1 :: AttributeValue
x1 = Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue (Text -> AttributeValue) -> Text -> AttributeValue
forall a b. (a -> b) -> a -> b
$ Double -> Text
num (Int -> Double
xcenter Int
j Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
halfw)
              x2 :: AttributeValue
x2 = Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue (Text -> AttributeValue) -> Text -> AttributeValue
forall a b. (a -> b) -> a -> b
$ Double -> Text
num (Int -> Double
xcenter Int
j Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
halfw)
          Svg
S.line Svg -> Attribute -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.class_ AttributeValue
"mark total" Svg -> Attribute -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.x1 AttributeValue
x1 Svg -> Attribute -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.x2 AttributeValue
x2 Svg -> Attribute -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.y1 AttributeValue
y Svg -> Attribute -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.y2 AttributeValue
y
    -- the average, if any: a dashed line across the plot
    -- (with a solid target over it, since its dashes alone leave gaps)
    Maybe (Text, Quantity) -> ((Text, Quantity) -> Svg) -> Svg
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ Maybe (Text, Quantity)
maverage (((Text, Quantity) -> Svg) -> Svg)
-> ((Text, Quantity) -> Svg) -> Svg
forall a b. (a -> b) -> a -> b
$ \(Text
tip, Quantity
q) -> Text -> Svg -> Svg
withTooltip Text
tip (Svg -> Svg) -> Svg -> Svg
forall a b. (a -> b) -> a -> b
$ do
      let y :: Double
y = Double -> Double
yof (Double -> Double) -> Double -> Double
forall a b. (a -> b) -> a -> b
$ Quantity -> Double
toD Quantity
q
      Svg
S.line Svg -> Attribute -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.class_ AttributeValue
"average" Svg -> Attribute -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.x1 (Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue (Text -> AttributeValue) -> Text -> AttributeValue
forall a b. (a -> b) -> a -> b
$ Double -> Text
num Double
plotleft) Svg -> Attribute -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.x2 (Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue (Text -> AttributeValue) -> Text -> AttributeValue
forall a b. (a -> b) -> a -> b
$ Double -> Text
num Double
plotright) Svg -> Attribute -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.y1 (Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue (Text -> AttributeValue) -> Text -> AttributeValue
forall a b. (a -> b) -> a -> b
$ Double -> Text
num Double
y) Svg -> Attribute -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.y2 (Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue (Text -> AttributeValue) -> Text -> AttributeValue
forall a b. (a -> b) -> a -> b
$ Double -> Text
num Double
y)
      Text -> (Double, Double) -> [(Double, Double)] -> Svg
hitLine Text
"" (Double
plotleft, Double
y) [(Double
plotright, Double
y)]
  )
  where
    xlabels :: [Text]
xlabels = case XScale
xscale of
      EvenScale [Text]
ls -> [Text]
ls
      DateScale [Day]
ds -> (Day -> Text) -> [Day] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map Day -> Text
showDate [Day]
ds
    n :: Int
n = [Text] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Text]
xlabels
    -- the x labels shown: for periods, every k'th label, with the smallest k that leaves them all room
    -- (keeping them evenly spaced, even when the end ones are moved inward)
    shownxlabels :: [(Double, Text)]
shownxlabels = case XScale
xscale of
      EvenScale [Text]
_ -> [(Double, Text)] -> Maybe [(Double, Text)] -> [(Double, Text)]
forall a. a -> Maybe a -> a
fromMaybe [] (Maybe [(Double, Text)] -> [(Double, Text)])
-> Maybe [(Double, Text)] -> [(Double, Text)]
forall a b. (a -> b) -> a -> b
$ ([(Double, Text)] -> Bool)
-> [[(Double, Text)]] -> Maybe [(Double, Text)]
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Maybe a
find [(Double, Text)] -> Bool
fitsall [Int -> [(Double, Text)]
everykth Int
k | Int
k <- [Int
labelevery .. Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
labelevery Int
n]]
      DateScale [Day]
_ -> [(Day -> Double
xofday Day
d, Text
lbl) | (Day
d, Text
lbl) <- Day -> Day -> [(Day, Text)]
dateTicks Day
d0 Day
d1]
    everykth :: Int -> [(Double, Text)]
everykth Int
k = [(Int -> Double
xcenter Int
j, Text
lbl) | (Int
j, Text
lbl) <- [Int] -> [Text] -> [(Int, Text)]
forall a b. [a] -> [b] -> [(a, b)]
zip [Int
0 :: Int ..] [Text]
xlabels, Int
j Int -> Int -> Int
forall a. Integral a => a -> a -> a
`mod` Int
k Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0]
    fitsall :: [(Double, Text)] -> Bool
fitsall [(Double, Text)]
ls = [(Double, Text)] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length (Double -> [(Double, Text)] -> [(Double, Text)]
placeXLabels Double
width [(Double, Text)]
ls) Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== [(Double, Text)] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [(Double, Text)]
ls
    -- the date scale, if any: its dates (as an array, for fast lookup), first and last dates, and a date's x position (inset a little from the plot's edges)
    datearray :: Maybe (Array Int Day)
datearray = case XScale
xscale of
      DateScale [Day]
ds -> Array Int Day -> Maybe (Array Int Day)
forall a. a -> Maybe a
Just (Array Int Day -> Maybe (Array Int Day))
-> Array Int Day -> Maybe (Array Int Day)
forall a b. (a -> b) -> a -> b
$ (Int, Int) -> [Day] -> Array Int Day
forall i e. Ix i => (i, i) -> [e] -> Array i e
Arr.listArray (Int
0, [Day] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Day]
ds Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) [Day]
ds
      EvenScale [Text]
_  -> Maybe (Array Int Day)
forall a. Maybe a
Nothing
    (Day
d0, Day
d1) = case XScale
xscale of
      DateScale ds :: [Day]
ds@(Day
d:[Day]
_) -> (Day -> [Day] -> Day
forall a. Ord a => a -> [a] -> a
minimumDef Day
d [Day]
ds, Day -> [Day] -> Day
forall a. Ord a => a -> [a] -> a
maximumDef Day
d [Day]
ds)
      XScale
_                  -> (Integer -> Int -> Int -> Day
fromGregorian Integer
2000 Int
1 Int
1, Integer -> Int -> Int -> Day
fromGregorian Integer
2000 Int
1 Int
1)
    xofday :: Day -> Double
xofday Day
d = Double -> Double
xofdays (Double -> Double) -> Double -> Double
forall a b. (a -> b) -> a -> b
$ Integer -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Day -> Day -> Integer
diffDays Day
d Day
d0)
    xofdays :: Double -> Double
xofdays Double
k = Double
plotleft Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
8 Double -> Double -> Double
forall a. Num a => a -> a -> a
+ (Double
plotright Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
plotleft Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
16) Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
k Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Integer -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Integer -> Integer -> Integer
forall a. Ord a => a -> a -> a
max Integer
1 (Day -> Day -> Integer
diffDays Day
d1 Day
d0))
    -- today, if the plot is to be shaded as of it
    mtoday :: Maybe Day
mtoday = if Maybe (Array Int Day) -> Bool
forall a. Maybe a -> Bool
isJust Maybe (Array Int Day)
datearray then Maybe Day
svgToday else Maybe Day
forall a. Maybe a
Nothing
    bars :: [DrawSeries]
bars   = [DrawSeries
d | DrawSeries
d <- [DrawSeries]
drawn, Series -> Mark
seMark (DrawSeries -> Series
dsSeries DrawSeries
d) Mark -> Mark -> Bool
forall a. Eq a => a -> a -> Bool
== Mark
MarkBar]
    lines_ :: [DrawSeries]
lines_ = [DrawSeries
d | DrawSeries
d <- [DrawSeries]
drawn, Series -> Mark
seMark (DrawSeries -> Series
dsSeries DrawSeries
d) Mark -> Mark -> Bool
forall a. Eq a => a -> a -> Bool
== Mark
MarkLine]
    -- the bar series in stacks: one, or with the grouped layout, one per group (see 'seGroup')
    stacks :: [[DrawSeries]]
stacks = case BarLayout
layout of
      BarLayout
Grouped   -> [ [DrawSeries
d | DrawSeries
d <- [DrawSeries]
bars, Series -> Int
seGroup (DrawSeries -> Series
dsSeries DrawSeries
d) Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
k] | Int
k <- [Int] -> [Int]
forall a. Eq a => [a] -> [a]
nub ([Int] -> [Int]) -> [Int] -> [Int]
forall a b. (a -> b) -> a -> b
$ (DrawSeries -> Int) -> [DrawSeries] -> [Int]
forall a b. (a -> b) -> [a] -> [b]
map (Series -> Int
seGroup (Series -> Int) -> (DrawSeries -> Series) -> DrawSeries -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DrawSeries -> Series
dsSeries) [DrawSeries]
bars ]
      BarLayout
Diverging -> [[DrawSeries]
bars]
    -- the extent of the bars in each direction: each stack's sum, at each x position
    stackedsums :: Quantity -> [Double]
stackedsums Quantity
sign = [ [Double] -> Double
forall a. Num a => [a] -> a
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum [Quantity -> Double
toD Quantity
q | DrawSeries
d <- [DrawSeries]
stk, Just Quantity
q <- [SeriesValues -> Int -> Maybe Quantity
valueAt (DrawSeries -> SeriesValues
dsValues DrawSeries
d) Int
j], Quantity -> Quantity
forall a. Num a => a -> a
signum Quantity
q Quantity -> Quantity -> Bool
forall a. Eq a => a -> a -> Bool
== Quantity
sign] | Int
j <- [Int
0 .. Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1], [DrawSeries]
stk <- [[DrawSeries]]
stacks ]
    hasbars :: Int -> Bool
hasbars Int
j = (DrawSeries -> Bool) -> [DrawSeries] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any (\DrawSeries
d -> Bool -> (Quantity -> Bool) -> Maybe Quantity -> Bool
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Bool
False (Quantity -> Quantity -> Bool
forall a. Eq a => a -> a -> Bool
/= Quantity
0) (Maybe Quantity -> Bool) -> Maybe Quantity -> Bool
forall a b. (a -> b) -> a -> b
$ SeriesValues -> Int -> Maybe Quantity
valueAt (DrawSeries -> SeriesValues
dsValues DrawSeries
d) Int
j) [DrawSeries]
bars
    -- contra amounts are drawn nearest the axis
    contraFirst :: [(DrawSeries, Quantity)] -> [(DrawSeries, Quantity)]
contraFirst = ((DrawSeries, Quantity) -> Bool)
-> [(DrawSeries, Quantity)] -> [(DrawSeries, Quantity)]
forall b a. Ord b => (a -> b) -> [a] -> [a]
sortOn (\(DrawSeries
d, Quantity
q) -> Bool -> Bool
not (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b
$ Series -> Quantity -> Bool
isContra (DrawSeries -> Series
dsSeries DrawSeries
d) Quantity
q)
    pointvalues :: [Double]
pointvalues = [Quantity -> Double
toD Quantity
q | Series
s <- (DrawSeries -> Series) -> [DrawSeries] -> [Series]
forall a b. (a -> b) -> [a] -> [b]
map DrawSeries -> Series
dsSeries [DrawSeries]
lines_ [Series] -> [Series] -> [Series]
forall a. [a] -> [a] -> [a]
++ [Series] -> (Series -> [Series]) -> Maybe Series -> [Series]
forall b a. b -> (a -> b) -> Maybe a -> b
maybe [] Series -> [Series]
forall a. a -> [a]
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe Series
mtotal, Just Quantity
q <- Series -> [Maybe Quantity]
seValues Series
s]
    datamax :: Double
datamax = Double -> [Double] -> Double
forall a. Ord a => a -> [a] -> a
maximumDef Double
0 (Quantity -> [Double]
stackedsums Quantity
1 [Double] -> [Double] -> [Double]
forall a. [a] -> [a] -> [a]
++ [Double]
pointvalues)
    datamin :: Double
datamin = Double -> [Double] -> Double
forall a. Ord a => a -> [a] -> a
minimumDef Double
0 (Quantity -> [Double]
stackedsums (-Quantity
1) [Double] -> [Double] -> [Double]
forall a. [a] -> [a] -> [a]
++ [Double]
pointvalues)
    (Double
lo, Double
hi, [Double]
ticks, Int
tickdecimals) = Int
-> Maybe Quantity
-> Double
-> Double
-> (Double, Double, [Double], Int)
panelScale (Double -> Int
plotIntervals Double
plotheight) Maybe Quantity
mscale (Double -> Double -> Double
forall a. Ord a => a -> a -> a
min Double
0 Double
datamin) (Double -> Double -> Double
forall a. Ord a => a -> a -> a
max Double
0 Double
datamax)
    ticklabel :: Double -> Text
ticklabel = Amount -> Int -> [Double] -> Double -> Text
tickLabel Amount
unit Int
tickdecimals [Double]
ticks
    plotleft :: Double
plotleft = Double
pad Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double -> [Double] -> Double
forall a. Ord a => a -> [a] -> a
maximumDef Double
0 ((Double -> Double) -> [Double] -> [Double]
forall a b. (a -> b) -> [a] -> [b]
map (Text -> Double
textWidth (Text -> Double) -> (Double -> Text) -> Double -> Double
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Double -> Text
ticklabel) [Double]
ticks) Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
12
    plotright :: Double
plotright = Double
width Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
pad
    plottop :: Double
plottop = Double
top Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
6 Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
labelroom
    -- room above the plot for value labels above the tallest bars
    labelroom :: Double
labelroom = if [Maybe Text] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Maybe Text]
vlabels then Double
0 else Double
14
    plotbottom :: Double
plotbottom = Double
plottop Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
plotheight
    -- a value's y position, within the plot (for bars and marks), or anywhere (for lines, which are clipped)
    yof :: Double -> Double
yof = Double -> Double -> Double -> Double
clamp Double
plottop Double
plotbottom (Double -> Double) -> (Double -> Double) -> Double -> Double
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Double -> Double
yofline
    yofline :: Double -> Double
yofline Double
v = Double
plottop Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
plotheight Double -> Double -> Double
forall a. Num a => a -> a -> a
* (Double
hi Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
v) Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ (Double
hi Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
lo)
    -- an id for the plot's clipping path, unique within the chart
    clipid :: Text
clipid = Text
svgIds Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"-clip-" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Double -> Text
num Double
top
    band :: Double
band = (Double
plotright Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
plotleft) Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Int -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
1 Int
n)
    xcenter :: Int -> Double
xcenter Int
j = case Maybe (Array Int Day)
datearray of
      Just Array Int Day
a | (Int, Int) -> Int -> Bool
forall a. Ix a => (a, a) -> a -> Bool
Arr.inRange (Array Int Day -> (Int, Int)
forall i e. Array i e -> (i, i)
Arr.bounds Array Int Day
a) Int
j -> Day -> Double
xofday (Array Int Day
a Array Int Day -> Int -> Day
forall i e. Ix i => Array i e -> i -> e
Arr.! Int
j)
      Maybe (Array Int Day)
_ -> Double
plotleft Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
band Double -> Double -> Double
forall a. Num a => a -> a -> a
* (Int -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
j Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
0.5)
    nbars :: Int
nbars = Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
1 (Int -> Int) -> Int -> Int
forall a b. (a -> b) -> a -> b
$ [[DrawSeries]] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [[DrawSeries]]
stacks
    -- each bar's width, and the width of a group of side-by-side bars (2px apart)
    barwidth :: Double
barwidth
      | Double
band Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
< Double
4  = Double -> Double -> Double
forall a. Ord a => a -> a -> a
max Double
1 (Double
band Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
0.9 Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Int -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
nbars)
      | Bool
otherwise = Double -> Double -> Double
forall a. Ord a => a -> a -> a
max Double
1 (Double -> Double) -> Double -> Double
forall a b. (a -> b) -> a -> b
$ Double -> Double -> Double
forall a. Ord a => a -> a -> a
min Double
56 ((Double
band Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
0.75 Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
2 Double -> Double -> Double
forall a. Num a => a -> a -> a
* Int -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int
nbars Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)) Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Int -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
nbars)
    groupwidth :: Double
groupwidth = Int -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
nbars Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
barwidth Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
2 Double -> Double -> Double
forall a. Num a => a -> a -> a
* Int -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int
nbars Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)
    labelevery :: Int
labelevery = Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
1 (Int -> Int) -> Int -> Int
forall a b. (a -> b) -> a -> b
$ Double -> Int
forall b. Integral b => Double -> b
forall a b. (RealFrac a, Integral b) => a -> b
ceiling ((Double -> [Double] -> Double
forall a. Ord a => a -> [a] -> a
maximumDef Double
0 ((Text -> Double) -> [Text] -> [Double]
forall a b. (a -> b) -> [a] -> [b]
map Text -> Double
textWidth [Text]
xlabels) Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
12) Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Double
band) :: Int
    stack :: Text -> Double -> [(DrawSeries, Quantity)] -> Double -> Svg
    stack :: Text -> Double -> [(DrawSeries, Quantity)] -> Double -> Svg
stack Text
lbl Double
x [(DrawSeries, Quantity)]
segs = Text
-> (Double, Double)
-> (Double -> Double)
-> Double
-> Double
-> [(DrawSeries, Quantity, Text)]
-> Double
-> Svg
vstack Text
svgIds (Double
lo, Double
hi) Double -> Double
yof Double
x Double
barwidth [(DrawSeries
d, Quantity
q, Text -> Series -> Quantity -> Text
tooltip Text
lbl (DrawSeries -> Series
dsSeries DrawSeries
d) Quantity
q) | (DrawSeries
d, Quantity
q) <- [(DrawSeries, Quantity)]
segs]
    lineSvg :: Text -> Series -> Svg
    lineSvg :: Text -> Series -> Svg
lineSvg Text
cls Series
s = do
      -- runs of consecutive values, so that gaps break the line
      let pts :: [Maybe (Int, Double, Double, Quantity)]
pts = [ (Quantity -> (Int, Double, Double, Quantity))
-> Maybe Quantity -> Maybe (Int, Double, Double, Quantity)
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\Quantity
q -> (Int
j, Int -> Double
xcenter Int
j, Double -> Double
yofline (Quantity -> Double
toD Quantity
q), Quantity
q)) (SeriesValues -> Int -> Maybe Quantity
valueAt SeriesValues
vals Int
j) | let vals :: SeriesValues
vals = Series -> SeriesValues
seriesValues Series
s, Int
j <- [Int
0 .. Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1] ]
      [[(Int, Double, Double, Quantity)]]
-> ([(Int, Double, Double, Quantity)] -> Svg) -> Svg
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ ([Maybe (Int, Double, Double, Quantity)]
-> [[(Int, Double, Double, Quantity)]]
forall a. [Maybe a] -> [[a]]
runs [Maybe (Int, Double, Double, Quantity)]
pts) (([(Int, Double, Double, Quantity)] -> Svg) -> Svg)
-> ([(Int, Double, Double, Quantity)] -> Svg) -> Svg
forall a b. (a -> b) -> a -> b
$ \[(Int, Double, Double, Quantity)]
run ->
        Svg
S.polyline Svg -> Attribute -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.class_ (AttributeValue
"line " AttributeValue -> AttributeValue -> AttributeValue
forall a. Semigroup a => a -> a -> a
<> Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue Text
cls)
          Svg -> Attribute -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.points (Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue (Text -> AttributeValue) -> Text -> AttributeValue
forall a b. (a -> b) -> a -> b
$ [Text] -> Text
T.unwords [Double -> Text
num Double
x Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"," Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Double -> Text
num Double
y | (Double
x, Double
y) <- [(Double, Double)] -> [(Double, Double)]
linePoints [(Double
x, Double
y) | (Int
_, Double
x, Double
y, Quantity
_) <- [(Int, Double, Double, Quantity)]
run]])
      -- invisible larger targets, for tooltips, and links if any: on a date scale, a circle around each point where
      -- the line changes; on periods, each point's stretch of the line, halfway to its neighbours, so all of it has one
      let target :: (Int, b, c, Quantity) -> Svg -> Svg
target (Int
j, b
_, c
_, Quantity
q) = Int -> Svg -> Svg
linked Int
j (Svg -> Svg) -> (Svg -> Svg) -> Svg -> Svg
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Svg -> Svg
withTooltip (Text -> Series -> Quantity -> Text
tooltip (Text -> Maybe Text -> Text
forall a. a -> Maybe a -> a
fromMaybe Text
"" (Maybe Text -> Text) -> Maybe Text -> Text
forall a b. (a -> b) -> a -> b
$ [Text]
xlabels [Text] -> Int -> Maybe Text
forall a. [a] -> Int -> Maybe a
`atMay` Int
j) Series
s Quantity
q)
      case Maybe (Array Int Day)
datearray of
        Just Array Int Day
_ -> [(Int, Double, Double, Quantity)]
-> ((Int, Double, Double, Quantity) -> Svg) -> Svg
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ ([(Int, Double, Double, Quantity)]
-> [(Int, Double, Double, Quantity)]
forall {a} {a} {c} {d}.
RealFrac a =>
[(a, a, c, d)] -> [(a, a, c, d)]
hitPoints ([(Int, Double, Double, Quantity)]
 -> [(Int, Double, Double, Quantity)])
-> [(Int, Double, Double, Quantity)]
-> [(Int, Double, Double, Quantity)]
forall a b. (a -> b) -> a -> b
$ [(Int, Double, Double, Quantity)]
-> [(Int, Double, Double, Quantity)]
forall {a} {a} {b} {c}. Eq a => [(a, b, c, a)] -> [(a, b, c, a)]
changes ([(Int, Double, Double, Quantity)]
 -> [(Int, Double, Double, Quantity)])
-> [(Int, Double, Double, Quantity)]
-> [(Int, Double, Double, Quantity)]
forall a b. (a -> b) -> a -> b
$ [Maybe (Int, Double, Double, Quantity)]
-> [(Int, Double, Double, Quantity)]
forall a. [Maybe a] -> [a]
catMaybes [Maybe (Int, Double, Double, Quantity)]
pts) (((Int, Double, Double, Quantity) -> Svg) -> Svg)
-> ((Int, Double, Double, Quantity) -> Svg) -> Svg
forall a b. (a -> b) -> a -> b
$ \p :: (Int, Double, Double, Quantity)
p@(Int
_, Double
x, Double
y, Quantity
_) -> (Int, Double, Double, Quantity) -> Svg -> Svg
forall {b} {c}. (Int, b, c, Quantity) -> Svg -> Svg
target (Int, Double, Double, Quantity)
p (Svg -> Svg) -> Svg -> Svg
forall a b. (a -> b) -> a -> b
$
          Svg
S.circle Svg -> Attribute -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.class_ (Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue (Text -> AttributeValue) -> Text -> AttributeValue
forall a b. (a -> b) -> a -> b
$ Text -> Text -> Text
withClass Text
cls Text
"hit") Svg -> Attribute -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.cx (Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue (Text -> AttributeValue) -> Text -> AttributeValue
forall a b. (a -> b) -> a -> b
$ Double -> Text
num Double
x) Svg -> Attribute -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.cy (Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue (Text -> AttributeValue) -> Text -> AttributeValue
forall a b. (a -> b) -> a -> b
$ Double -> Text
num Double
y) Svg -> Attribute -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.r AttributeValue
"6"
        Maybe (Array Int Day)
Nothing -> [[(Int, Double, Double, Quantity)]]
-> ([(Int, Double, Double, Quantity)] -> Svg) -> Svg
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ ([Maybe (Int, Double, Double, Quantity)]
-> [[(Int, Double, Double, Quantity)]]
forall a. [Maybe a] -> [[a]]
runs [Maybe (Int, Double, Double, Quantity)]
pts) (([(Int, Double, Double, Quantity)] -> Svg) -> Svg)
-> ([(Int, Double, Double, Quantity)] -> Svg) -> Svg
forall a b. (a -> b) -> a -> b
$ \[(Int, Double, Double, Quantity)]
run -> let hs :: [(Int, Double, Double, Quantity)]
hs = [(Int, Double, Double, Quantity)]
-> [(Int, Double, Double, Quantity)]
forall {a} {a} {c} {d}.
RealFrac a =>
[(a, a, c, d)] -> [(a, a, c, d)]
hitPoints [(Int, Double, Double, Quantity)]
run in
          [(Maybe (Int, Double, Double, Quantity),
  (Int, Double, Double, Quantity),
  Maybe (Int, Double, Double, Quantity))]
-> ((Maybe (Int, Double, Double, Quantity),
     (Int, Double, Double, Quantity),
     Maybe (Int, Double, Double, Quantity))
    -> Svg)
-> Svg
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ ([Maybe (Int, Double, Double, Quantity)]
-> [(Int, Double, Double, Quantity)]
-> [Maybe (Int, Double, Double, Quantity)]
-> [(Maybe (Int, Double, Double, Quantity),
     (Int, Double, Double, Quantity),
     Maybe (Int, Double, Double, Quantity))]
forall a b c. [a] -> [b] -> [c] -> [(a, b, c)]
zip3 (Maybe (Int, Double, Double, Quantity)
forall a. Maybe a
Nothing Maybe (Int, Double, Double, Quantity)
-> [Maybe (Int, Double, Double, Quantity)]
-> [Maybe (Int, Double, Double, Quantity)]
forall a. a -> [a] -> [a]
: ((Int, Double, Double, Quantity)
 -> Maybe (Int, Double, Double, Quantity))
-> [(Int, Double, Double, Quantity)]
-> [Maybe (Int, Double, Double, Quantity)]
forall a b. (a -> b) -> [a] -> [b]
map (Int, Double, Double, Quantity)
-> Maybe (Int, Double, Double, Quantity)
forall a. a -> Maybe a
Just [(Int, Double, Double, Quantity)]
hs) [(Int, Double, Double, Quantity)]
hs (((Int, Double, Double, Quantity)
 -> Maybe (Int, Double, Double, Quantity))
-> [(Int, Double, Double, Quantity)]
-> [Maybe (Int, Double, Double, Quantity)]
forall a b. (a -> b) -> [a] -> [b]
map (Int, Double, Double, Quantity)
-> Maybe (Int, Double, Double, Quantity)
forall a. a -> Maybe a
Just (Int
-> [(Int, Double, Double, Quantity)]
-> [(Int, Double, Double, Quantity)]
forall a. Int -> [a] -> [a]
drop Int
1 [(Int, Double, Double, Quantity)]
hs) [Maybe (Int, Double, Double, Quantity)]
-> [Maybe (Int, Double, Double, Quantity)]
-> [Maybe (Int, Double, Double, Quantity)]
forall a. [a] -> [a] -> [a]
++ [Maybe (Int, Double, Double, Quantity)
forall a. Maybe a
Nothing])) (((Maybe (Int, Double, Double, Quantity),
   (Int, Double, Double, Quantity),
   Maybe (Int, Double, Double, Quantity))
  -> Svg)
 -> Svg)
-> ((Maybe (Int, Double, Double, Quantity),
     (Int, Double, Double, Quantity),
     Maybe (Int, Double, Double, Quantity))
    -> Svg)
-> Svg
forall a b. (a -> b) -> a -> b
$ \(Maybe (Int, Double, Double, Quantity)
mprev, p :: (Int, Double, Double, Quantity)
p@(Int
_, Double
x, Double
y, Quantity
_), Maybe (Int, Double, Double, Quantity)
mnext) -> (Int, Double, Double, Quantity) -> Svg -> Svg
forall {b} {c}. (Int, b, c, Quantity) -> Svg -> Svg
target (Int, Double, Double, Quantity)
p (Svg -> Svg) -> Svg -> Svg
forall a b. (a -> b) -> a -> b
$
            Text -> (Double, Double) -> [(Double, Double)] -> Svg
hitLine Text
cls (Double
x, Double
y) ([(Double, Double)] -> Svg) -> [(Double, Double)] -> Svg
forall a b. (a -> b) -> a -> b
$ Maybe (Int, Double, Double, Quantity)
-> (Int, Double, Double, Quantity)
-> Maybe (Int, Double, Double, Quantity)
-> [(Double, Double)]
forall {a} {b} {a} {d} {a} {d}.
(Fractional a, Fractional b) =>
Maybe (a, a, b, d)
-> (a, a, b, d) -> Maybe (a, a, b, d) -> [(a, b)]
stretchEnds Maybe (Int, Double, Double, Quantity)
mprev (Int, Double, Double, Quantity)
p Maybe (Int, Double, Double, Quantity)
mnext
    -- the ends of a point's stretch of a line: halfway to the previous point and to the next; or a lone point's short dash
    stretchEnds :: Maybe (a, a, b, d)
-> (a, a, b, d) -> Maybe (a, a, b, d) -> [(a, b)]
stretchEnds Maybe (a, a, b, d)
mprev (a
_, a
x, b
y, d
_) Maybe (a, a, b, d)
mnext = case (Maybe (a, a, b, d)
mprev, Maybe (a, a, b, d)
mnext) of
      (Maybe (a, a, b, d)
Nothing, Maybe (a, a, b, d)
Nothing) -> [(a
x a -> a -> a
forall a. Num a => a -> a -> a
- a
6, b
y), (a
x a -> a -> a
forall a. Num a => a -> a -> a
+ a
6, b
y)]
      (Maybe (a, a, b, d), Maybe (a, a, b, d))
_ -> [((a
x a -> a -> a
forall a. Num a => a -> a -> a
+ a
x') a -> a -> a
forall a. Fractional a => a -> a -> a
/ a
2, (b
y b -> b -> b
forall a. Num a => a -> a -> a
+ b
y') b -> b -> b
forall a. Fractional a => a -> a -> a
/ b
2) | Just (a
_, a
x', b
y', d
_) <- [Maybe (a, a, b, d)
mprev, Maybe (a, a, b, d)
mnext]]
    -- An invisible line a little wider than the visible ones, as a tooltip target: from a point out to each of the given ends.
    -- With the point first, and the line's class (its colour), a page's script can mark the point and show the colour.
    hitLine :: Text -> (Double, Double) -> [(Double, Double)] -> Svg
hitLine Text
cls (Double
x, Double
y) [(Double, Double)]
ends = Svg
S.path Svg -> Attribute -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.class_ (Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue (Text -> AttributeValue) -> Text -> AttributeValue
forall a b. (a -> b) -> a -> b
$ Text -> Text -> Text
withClass Text
cls Text
"hit-line")
      Svg -> Attribute -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.d (Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue (Text -> AttributeValue) -> Text -> AttributeValue
forall a b. (a -> b) -> a -> b
$ [Text] -> Text
T.unwords ([Text] -> Text) -> [Text] -> Text
forall a b. (a -> b) -> a -> b
$ [[Text]] -> [Text]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [[Text
"M", Double -> Text
num Double
x, Double -> Text
num Double
y, Text
"L", Double -> Text
num Double
ex, Double -> Text
num Double
ey] | (Double
ex, Double
ey) <- [(Double, Double)]
ends])
    withClass :: Text -> Text -> Text
withClass Text
cls Text
c = [Text] -> Text
T.unwords ([Text] -> Text) -> [Text] -> Text
forall a b. (a -> b) -> a -> b
$ Text
c Text -> [Text] -> [Text]
forall a. a -> [a] -> [a]
: [Text
cls | Bool -> Bool
not (Text -> Bool
T.null Text
cls)]
    -- the points a line is drawn through: thinned, and stepped on a date scale; or for a lone point
    -- (eg a single period's, or a register's one transaction), a short dash across it, like the legend's,
    -- since a line through one point is not drawn
    linePoints :: [(Double, Double)] -> [(Double, Double)]
linePoints [(Double, Double)]
ps = case [(Double, Double)]
ps of
      [(Double
x, Double
y)] -> [(Double
x Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
6, Double
y), (Double
x Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
6, Double
y)]
      [(Double, Double)]
_        -> [(Double, Double)] -> [(Double, Double)]
thinPoints ([(Double, Double)] -> [(Double, Double)])
-> [(Double, Double)] -> [(Double, Double)]
forall a b. (a -> b) -> a -> b
$ (if Maybe (Array Int Day) -> Bool
forall a. Maybe a -> Bool
isJust Maybe (Array Int Day)
datearray then [(Double, Double)] -> [(Double, Double)]
dropFlat ([(Double, Double)] -> [(Double, Double)])
-> ([(Double, Double)] -> [(Double, Double)])
-> [(Double, Double)]
-> [(Double, Double)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [(Double, Double)] -> [(Double, Double)]
steps else [(Double, Double)] -> [(Double, Double)]
forall a. a -> a
id) [(Double, Double)]
ps
    -- some markup for the given x position, as a link if it has one
    linked :: Int -> Svg -> Svg
linked Int
j Svg
m = case Int -> Maybe Text
svgPointLink Int
j of
      Just Text
url -> Svg -> Svg
S.a (Svg -> Svg) -> Attribute -> Svg -> Svg
forall h. Attributable h => h -> Attribute -> h
! Tag -> AttributeValue -> Attribute
customAttribute Tag
"href" (Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue Text
url) (Svg -> Svg) -> Attribute -> Svg -> Svg
forall h. Attributable h => h -> Attribute -> h
! Tag -> AttributeValue -> Attribute
customAttribute Tag
"tabindex" AttributeValue
"-1" (Svg -> Svg) -> Svg -> Svg
forall a b. (a -> b) -> a -> b
$ Svg
m
      Maybe Text
Nothing  -> Svg
m
    -- The points of a line by date where its value changes, and its first point. At the others nothing happened
    -- to it, eg a register's transactions in other commodities.
    changes :: [(a, b, c, a)] -> [(a, b, c, a)]
changes [(a, b, c, a)]
ps = Int -> [(a, b, c, a)] -> [(a, b, c, a)]
forall a. Int -> [a] -> [a]
take Int
1 [(a, b, c, a)]
ps [(a, b, c, a)] -> [(a, b, c, a)] -> [(a, b, c, a)]
forall a. [a] -> [a] -> [a]
++ [(a, b, c, a)
p | ((a
_, b
_, c
_, a
q0), p :: (a, b, c, a)
p@(a
_, b
_, c
_, a
q)) <- [(a, b, c, a)] -> [(a, b, c, a)] -> [((a, b, c, a), (a, b, c, a))]
forall a b. [a] -> [b] -> [(a, b)]
zip [(a, b, c, a)]
ps (Int -> [(a, b, c, a)] -> [(a, b, c, a)]
forall a. Int -> [a] -> [a]
drop Int
1 [(a, b, c, a)]
ps), a
q a -> a -> Bool
forall a. Eq a => a -> a -> Bool
/= a
q0]
    -- The points of a line given tooltip targets: all of them, or beyond 400, the last point in each 4px column,
    -- which keeps the SVG small while the targets (12px across) still cover the whole line.
    hitPoints :: [(a, a, c, d)] -> [(a, a, c, d)]
hitPoints [(a, a, c, d)]
ps
      | [(a, a, c, d)] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [(a, a, c, d)]
ps Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
400 = [(a, a, c, d)]
ps
      | Bool
otherwise = Map Integer (a, a, c, d) -> [(a, a, c, d)]
forall k a. Map k a -> [a]
M.elems (Map Integer (a, a, c, d) -> [(a, a, c, d)])
-> Map Integer (a, a, c, d) -> [(a, a, c, d)]
forall a b. (a -> b) -> a -> b
$ [(Integer, (a, a, c, d))] -> Map Integer (a, a, c, d)
forall k a. Ord k => [(k, a)] -> Map k a
M.fromList [(a -> Integer
forall b. Integral b => a -> b
forall a b. (RealFrac a, Integral b) => a -> b
floor (a
x a -> a -> a
forall a. Fractional a => a -> a -> a
/ a
4) :: Integer, (a, a, c, d)
p) | p :: (a, a, c, d)
p@(a
_, a
x, c
_, d
_) <- [(a, a, c, d)]
ps]
    tooltip :: Text -> Series -> Quantity -> Text
    tooltip :: Text -> Series -> Quantity -> Text
tooltip Text
lbl Series
s Quantity
q = Text -> [Text] -> Text
T.intercalate Text
" · " ((Text -> Bool) -> [Text] -> [Text]
forall a. (a -> Bool) -> [a] -> [a]
filter (Bool -> Bool
not (Bool -> Bool) -> (Text -> Bool) -> Text -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Bool
T.null) [Text
lbl, ChartLabel -> Text
lbFull (Series -> ChartLabel
seLabel Series
s), Amount -> Quantity -> Text
showValue Amount
unit Quantity
q]) Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Series -> Text
tipLines Series
s

-- | Place x axis labels: centred at their x positions, but kept within the chart,
-- leaving out any that would overlap the previous one (eg after being moved inward from the chart's edge).
placeXLabels :: Double -> [(Double, Text)] -> [(Double, Text)]
placeXLabels :: Double -> [(Double, Text)] -> [(Double, Text)]
placeXLabels Double
width = [(Double, Text)] -> [(Double, Text)]
forall a. [a] -> [a]
reverse ([(Double, Text)] -> [(Double, Text)])
-> ([(Double, Text)] -> [(Double, Text)])
-> [(Double, Text)]
-> [(Double, Text)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ([(Double, Text)] -> (Double, Text) -> [(Double, Text)])
-> [(Double, Text)] -> [(Double, Text)] -> [(Double, Text)]
forall b a. (b -> a -> b) -> b -> [a] -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' [(Double, Text)] -> (Double, Text) -> [(Double, Text)]
place []
  where
    place :: [(Double, Text)] -> (Double, Text) -> [(Double, Text)]
place [(Double, Text)]
kept (Double
x, Text
lbl) =
      let halfw :: Double
halfw = Text -> Double
textWidth Text
lbl Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Double
2
          x' :: Double
x' = Double -> Double -> Double
forall a. Ord a => a -> a -> a
max (Double
pad Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
halfw) (Double -> Double) -> Double -> Double
forall a b. (a -> b) -> a -> b
$ Double -> Double -> Double
forall a. Ord a => a -> a -> a
min (Double
width Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
pad Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
halfw) Double
x
      in case [(Double, Text)]
kept of
           (Double
px, Text
plbl) : [(Double, Text)]
_ | Double
x' Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
halfw Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
< Double
px Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Text -> Double
textWidth Text
plbl Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Double
2 Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
12 -> [(Double, Text)]
kept
           [(Double, Text)]
_ -> (Double
x', Text
lbl) (Double, Text) -> [(Double, Text)] -> [(Double, Text)]
forall a. a -> [a] -> [a]
: [(Double, Text)]
kept

-- | Draw series of categories (eg accounts): as vertical bars, or as horizontal bars when there are more than 'maxColumns' categories.
-- Categories with no values at all (eg accounts without this commodity) are left out.
-- With a single series, each category's bar has its own account type's colour, if known.
-- Categories with value labels (see 'pnValueLabels') are always drawn as horizontal bars, which have room for them all.
categoriesSvg :: Text -> Double -> Double -> Double -> Maybe Quantity -> Amount -> [DrawSeries] -> Maybe Series -> Maybe Series -> [Maybe Text] -> [ChartLabel] -> (Double, Svg)
categoriesSvg :: Text
-> Double
-> Double
-> Double
-> Maybe Quantity
-> Amount
-> [DrawSeries]
-> Maybe Series
-> Maybe Series
-> [Maybe Text]
-> [ChartLabel]
-> (Double, Svg)
categoriesSvg Text
ids Double
width Double
plotheight Double
top Maybe Quantity
mscale Amount
unit [DrawSeries]
drawn Maybe Series
mtotal Maybe Series
mtotalmark [Maybe Text]
vlabels [ChartLabel]
labels
  | [(ChartLabel, Int, [(DrawSeries, Quantity)])] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [(ChartLabel, Int, [(DrawSeries, Quantity)])]
rows Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
maxColumns Bool -> Bool -> Bool
&& [Maybe Text] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Maybe Text]
vlabels = Text
-> Double
-> Double
-> Double
-> Maybe Quantity
-> Amount
-> [(ChartLabel, Int, [(DrawSeries, Quantity)])]
-> (Int -> Maybe Quantity)
-> (Int -> [Text])
-> (Int -> Quantity -> Maybe Text)
-> Maybe Text
-> (Double, Svg)
columnsSvg Text
ids Double
width Double
plotheight Double
top Maybe Quantity
mscale Amount
unit [(ChartLabel, Int, [(DrawSeries, Quantity)])]
rows Int -> Maybe Quantity
rowValue Int -> [Text]
rowTip Int -> Quantity -> Maybe Text
valueText Maybe Text
mmark
  | Bool
otherwise = Text
-> Double
-> Double
-> Maybe Quantity
-> Amount
-> [(ChartLabel, Int, [(DrawSeries, Quantity)])]
-> (Int -> Maybe Quantity)
-> (Int -> [Text])
-> Bool
-> (Int -> Quantity -> Maybe Text)
-> Maybe Text
-> (Double, Svg)
barsSvg Text
ids Double
width Double
top Maybe Quantity
mscale Amount
unit [(ChartLabel, Int, [(DrawSeries, Quantity)])]
rows Int -> Maybe Quantity
rowValue Int -> [Text]
rowTip (Bool -> Bool
not (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b
$ [Maybe Text] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Maybe Text]
vlabels) Int -> Quantity -> Maybe Text
valueText Maybe Text
mmark
  where
    rows :: [(ChartLabel, Int, [(DrawSeries, Quantity)])]
rows = [ (ChartLabel
l, Int
j, [(Int -> ChartLabel -> DrawSeries -> DrawSeries
categoryColoured Int
j ChartLabel
l DrawSeries
d, Quantity
q) | DrawSeries
d <- [DrawSeries]
drawn, Just Quantity
q <- [SeriesValues -> Int -> Maybe Quantity
valueAt (DrawSeries -> SeriesValues
dsValues DrawSeries
d) Int
j]])
           | (Int
j, ChartLabel
l) <- [Int] -> [ChartLabel] -> [(Int, ChartLabel)]
forall a b. [a] -> [b] -> [(a, b)]
zip [Int
0 ..] [ChartLabel]
labels, Bool -> Bool
not (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b
$ (DrawSeries -> Bool) -> [DrawSeries] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (Maybe Quantity -> Bool
forall a. Maybe a -> Bool
isNothing (Maybe Quantity -> Bool)
-> (DrawSeries -> Maybe Quantity) -> DrawSeries -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (SeriesValues -> Int -> Maybe Quantity)
-> Int -> SeriesValues -> Maybe Quantity
forall a b c. (a -> b -> c) -> b -> a -> c
flip SeriesValues -> Int -> Maybe Quantity
valueAt Int
j (SeriesValues -> Maybe Quantity)
-> (DrawSeries -> SeriesValues) -> DrawSeries -> Maybe Quantity
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DrawSeries -> SeriesValues
dsValues) [DrawSeries]
drawn ]
    categoryColoured :: Int -> ChartLabel -> DrawSeries -> DrawSeries
categoryColoured Int
j ChartLabel
lbl DrawSeries
d = case ([DrawSeries]
drawn, ChartLabel -> Maybe AccountType
lbType ChartLabel
lbl) of
      ([DrawSeries
_], Just AccountType
at) -> DrawSeries
d{dsClass = colourClass $ TypeColour (mainAccountType at) (fromMaybe 0 $ tints `atMay` j)}
      ([DrawSeries], Maybe AccountType)
_              -> DrawSeries
d
    -- with a single series, each category's tint of its type's colour
    tints :: [Int]
tints = case [DrawSeries]
drawn of
      [DrawSeries
d] -> [ChartLabel] -> [Maybe Quantity] -> [Int]
categoryTints [ChartLabel]
labels (Series -> [Maybe Quantity]
seValues (Series -> [Maybe Quantity]) -> Series -> [Maybe Quantity]
forall a b. (a -> b) -> a -> b
$ DrawSeries -> Series
dsSeries DrawSeries
d)
      [DrawSeries]
_   -> []
    mtotalvals :: Maybe SeriesValues
mtotalvals = Series -> SeriesValues
seriesValues (Series -> SeriesValues) -> Maybe Series -> Maybe SeriesValues
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe Series
mtotal
    -- the name of the total, if it is to be marked across the bars, at the value
    mmark :: Maybe Text
mmark = ChartLabel -> Text
lbFull (ChartLabel -> Text) -> (Series -> ChartLabel) -> Series -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Series -> ChartLabel
seLabel (Series -> Text) -> Maybe Series -> Maybe Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe Series
mtotalmark
    -- A total that isn't drawn (MarkNone): its value, and the label shown instead if any, in the tooltips of each category's bar,
    -- eg "Value 2,500.00 USD · +66.7%".
    rowTip :: Int -> [Text]
rowTip Int
j = case (Maybe Series
mtotal, Int -> Maybe Quantity
rowValue Int
j) of
      (Just Series
t, Just Quantity
q) | Series -> Mark
seMark Series
t Mark -> Mark -> Bool
forall a. Eq a => a -> a -> Bool
== Mark
MarkNone ->
        (ChartLabel -> Text
lbFull (Series -> ChartLabel
seLabel Series
t) Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Amount -> Quantity -> Text
showValue Amount
unit Quantity
q) Text -> [Text] -> [Text]
forall a. a -> [a] -> [a]
: [Text] -> (Text -> [Text]) -> Maybe Text -> [Text]
forall b a. b -> (a -> b) -> Maybe a -> b
maybe [] Text -> [Text]
forall a. a -> [a]
forall (f :: * -> *) a. Applicative f => a -> f a
pure ((Text -> Bool) -> Maybe Text -> Maybe Text
forall (m :: * -> *) a. MonadPlus m => (a -> Bool) -> m a -> m a
mfilter (Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
/= Amount -> Quantity -> Text
showValue Amount
unit Quantity
q) (Maybe Text -> Maybe Text) -> Maybe Text -> Maybe Text
forall a b. (a -> b) -> a -> b
$ Int -> Quantity -> Maybe Text
valueText Int
j Quantity
q)
      (Maybe Series, Maybe Quantity)
_ -> []
    -- the text to show for each category's value: the value, or the label given instead (see 'pnValueLabels'), if any
    valueText :: Int -> Quantity -> Maybe Text
valueText Int
j Quantity
q
      | [Maybe Text] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Maybe Text]
vlabels = Text -> Maybe Text
forall a. a -> Maybe a
Just (Text -> Maybe Text) -> Text -> Maybe Text
forall a b. (a -> b) -> a -> b
$ Amount -> Quantity -> Text
showValue Amount
unit Quantity
q
      | Bool
otherwise    = Maybe (Maybe Text) -> Maybe Text
forall (m :: * -> *) a. Monad m => m (m a) -> m a
join (Maybe (Maybe Text) -> Maybe Text)
-> Maybe (Maybe Text) -> Maybe Text
forall a b. (a -> b) -> a -> b
$ [Maybe Text]
vlabels [Maybe Text] -> Int -> Maybe (Maybe Text)
forall a. [a] -> Int -> Maybe a
`atMay` Int
j
    -- the value to show for each category: the total's, or the single series'
    rowValue :: Int -> Maybe Quantity
rowValue Int
j = case (Maybe Series
mtotal, [DrawSeries]
drawn) of
      (Just Series
_, [DrawSeries]
_) -> Maybe SeriesValues
mtotalvals Maybe SeriesValues
-> (SeriesValues -> Maybe Quantity) -> Maybe Quantity
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (SeriesValues -> Int -> Maybe Quantity)
-> Int -> SeriesValues -> Maybe Quantity
forall a b c. (a -> b -> c) -> b -> a -> c
flip SeriesValues -> Int -> Maybe Quantity
valueAt Int
j
      (Maybe Series
_, [DrawSeries
d])    -> SeriesValues -> Int -> Maybe Quantity
valueAt (DrawSeries -> SeriesValues
dsValues DrawSeries
d) Int
j
      (Maybe Series, [DrawSeries])
_           -> Maybe Quantity
forall a. Maybe a
Nothing

-- | The most categories to draw as vertical bars; beyond this, they are drawn as horizontal bars (rows), which can show many more names.
-- (Or always, when they have value labels; see 'categoriesSvg'.)
maxColumns :: Int
maxColumns :: Int
maxColumns = Int
30

-- | Draw categories as vertical bars (stacked if several series), with their names below (angled if they don't fit),
-- and their values above the bars (when they fit), in a plot this high.
-- The values are shown as the given function says, if at all. If a total's name is given, the values (the total's) are also marked across the bars.
columnsSvg :: Text -> Double -> Double -> Double -> Maybe Quantity -> Amount -> [(ChartLabel, Int, [(DrawSeries, Quantity)])] -> (Int -> Maybe Quantity) -> (Int -> [Text]) -> (Int -> Quantity -> Maybe Text) -> Maybe Text -> (Double, Svg)
columnsSvg :: Text
-> Double
-> Double
-> Double
-> Maybe Quantity
-> Amount
-> [(ChartLabel, Int, [(DrawSeries, Quantity)])]
-> (Int -> Maybe Quantity)
-> (Int -> [Text])
-> (Int -> Quantity -> Maybe Text)
-> Maybe Text
-> (Double, Svg)
columnsSvg Text
ids Double
width Double
plotheight Double
top Maybe Quantity
mscale Amount
unit [(ChartLabel, Int, [(DrawSeries, Quantity)])]
rows Int -> Maybe Quantity
rowValue Int -> [Text]
rowTip Int -> Quantity -> Maybe Text
valueText Maybe Text
mmark = (Double
plotheight Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
22 Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
negroom Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
labelheight, do
    -- gridlines and y tick labels
    [Double] -> (Double -> Svg) -> Svg
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [Double]
ticks ((Double -> Svg) -> Svg) -> (Double -> Svg) -> Svg
forall a b. (a -> b) -> a -> b
$ \Double
t -> do
      Svg
S.line Svg -> Attribute -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.class_ (if Double
t Double -> Double -> Bool
forall a. Eq a => a -> a -> Bool
== Double
0 then AttributeValue
"axis" else AttributeValue
"grid")
        Svg -> Attribute -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.x1 (Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue (Text -> AttributeValue) -> Text -> AttributeValue
forall a b. (a -> b) -> a -> b
$ Double -> Text
num Double
plotleft) Svg -> Attribute -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.x2 (Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue (Text -> AttributeValue) -> Text -> AttributeValue
forall a b. (a -> b) -> a -> b
$ Double -> Text
num Double
plotright) Svg -> Attribute -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.y1 (Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue (Text -> AttributeValue) -> Text -> AttributeValue
forall a b. (a -> b) -> a -> b
$ Double -> Text
num (Double -> Double
yof Double
t)) Svg -> Attribute -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.y2 (Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue (Text -> AttributeValue) -> Text -> AttributeValue
forall a b. (a -> b) -> a -> b
$ Double -> Text
num (Double -> Double
yof Double
t))
      Svg -> Svg
S.text_ (Svg -> Svg) -> Attribute -> Svg -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.class_ AttributeValue
"tick" (Svg -> Svg) -> Attribute -> Svg -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.x (Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue (Text -> AttributeValue) -> Text -> AttributeValue
forall a b. (a -> b) -> a -> b
$ Double -> Text
num (Double
plotleft Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
8)) (Svg -> Svg) -> Attribute -> Svg -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.y (Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue (Text -> AttributeValue) -> Text -> AttributeValue
forall a b. (a -> b) -> a -> b
$ Double -> Text
num (Double -> Double
yof Double
t Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
5)) (Svg -> Svg) -> Attribute -> Svg -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.textAnchor AttributeValue
"end" (Svg -> Svg) -> Svg -> Svg
forall a b. (a -> b) -> a -> b
$ Text -> Svg
forall a. ToMarkup a => a -> Svg
S.toMarkup (Double -> Text
ticklabel Double
t)
    [(Int, (ChartLabel, Int, [(DrawSeries, Quantity)]))]
-> ((Int, (ChartLabel, Int, [(DrawSeries, Quantity)])) -> Svg)
-> Svg
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ ([Int]
-> [(ChartLabel, Int, [(DrawSeries, Quantity)])]
-> [(Int, (ChartLabel, Int, [(DrawSeries, Quantity)]))]
forall a b. [a] -> [b] -> [(a, b)]
zip [Int
0 :: Int ..] [(ChartLabel, Int, [(DrawSeries, Quantity)])]
rows) (((Int, (ChartLabel, Int, [(DrawSeries, Quantity)])) -> Svg)
 -> Svg)
-> ((Int, (ChartLabel, Int, [(DrawSeries, Quantity)])) -> Svg)
-> Svg
forall a b. (a -> b) -> a -> b
$ \(Int
i, (ChartLabel
lbl, Int
j, [(DrawSeries, Quantity)]
segs)) -> do
      let x :: Double
x = Int -> Double
forall {a}. Integral a => a -> Double
xcenter Int
i
          name :: Text
name = ChartLabel -> Text
shortName ChartLabel
lbl
          ly :: Double
ly = Double
plotbottom Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
negroom Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
16
      -- the category's name, below; angled (reading upward to the bar) if the names don't fit
      if Bool
angled
      then Svg -> Svg
S.text_ (Svg -> Svg) -> Attribute -> Svg -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.class_ AttributeValue
"label" (Svg -> Svg) -> Attribute -> Svg -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.x (Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue (Text -> AttributeValue) -> Text -> AttributeValue
forall a b. (a -> b) -> a -> b
$ Double -> Text
num (Double
x Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
4)) (Svg -> Svg) -> Attribute -> Svg -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.y (Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue (Text -> AttributeValue) -> Text -> AttributeValue
forall a b. (a -> b) -> a -> b
$ Double -> Text
num Double
ly) (Svg -> Svg) -> Attribute -> Svg -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.textAnchor AttributeValue
"end"
             (Svg -> Svg) -> Attribute -> Svg -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.transform (Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue (Text -> AttributeValue) -> Text -> AttributeValue
forall a b. (a -> b) -> a -> b
$ Text
"rotate(-45 " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Double -> Text
num (Double
x Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
4) Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Double -> Text
num Double
ly Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
")") (Svg -> Svg) -> Svg -> Svg
forall a b. (a -> b) -> a -> b
$ Text -> Svg
forall a. ToMarkup a => a -> Svg
S.toMarkup Text
name
      else Svg -> Svg
S.text_ (Svg -> Svg) -> Attribute -> Svg -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.class_ AttributeValue
"label" (Svg -> Svg) -> Attribute -> Svg -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.x (Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue (Text -> AttributeValue) -> Text -> AttributeValue
forall a b. (a -> b) -> a -> b
$ Double -> Text
num Double
x) (Svg -> Svg) -> Attribute -> Svg -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.y (Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue (Text -> AttributeValue) -> Text -> AttributeValue
forall a b. (a -> b) -> a -> b
$ Double -> Text
num Double
ly) (Svg -> Svg) -> Attribute -> Svg -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.textAnchor AttributeValue
"middle" (Svg -> Svg) -> Svg -> Svg
forall a b. (a -> b) -> a -> b
$ Text -> Svg
forall a. ToMarkup a => a -> Svg
S.toMarkup Text
name
      -- the bar
      let tip :: DrawSeries -> Quantity -> Text
tip DrawSeries
d Quantity
q = Text -> [Text] -> Text
T.intercalate Text
" · " ([Text] -> [Text]
forall a. Eq a => [a] -> [a]
nub ([Text] -> [Text]) -> [Text] -> [Text]
forall a b. (a -> b) -> a -> b
$ [ChartLabel -> Text
lbFull ChartLabel
lbl, ChartLabel -> Text
lbFull (Series -> ChartLabel
seLabel (Series -> ChartLabel) -> Series -> ChartLabel
forall a b. (a -> b) -> a -> b
$ DrawSeries -> Series
dsSeries DrawSeries
d), Amount -> Quantity -> Text
showValue Amount
unit Quantity
q] [Text] -> [Text] -> [Text]
forall a. [a] -> [a] -> [a]
++ Int -> [Text]
rowTip Int
j) Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Series -> Text
tipLines (DrawSeries -> Series
dsSeries DrawSeries
d)
          bx :: Double
bx = Double
x Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
barwidth Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Double
2
      Text
-> (Double, Double)
-> (Double -> Double)
-> Double
-> Double
-> [(DrawSeries, Quantity, Text)]
-> Double
-> Svg
vstack Text
ids (Double
lo, Double
hi) Double -> Double
yof Double
bx Double
barwidth [(DrawSeries
d, Quantity
q, DrawSeries -> Quantity -> Text
tip DrawSeries
d Quantity
q) | (DrawSeries
d, Quantity
q) <- [(DrawSeries, Quantity)]
segs, Quantity
q Quantity -> Quantity -> Bool
forall a. Ord a => a -> a -> Bool
> Quantity
0] Double
1
      Text
-> (Double, Double)
-> (Double -> Double)
-> Double
-> Double
-> [(DrawSeries, Quantity, Text)]
-> Double
-> Svg
vstack Text
ids (Double
lo, Double
hi) Double -> Double
yof Double
bx Double
barwidth [(DrawSeries
d, Quantity
q, DrawSeries -> Quantity -> Text
tip DrawSeries
d Quantity
q) | (DrawSeries
d, Quantity
q) <- [(DrawSeries, Quantity)]
segs, Quantity
q Quantity -> Quantity -> Bool
forall a. Ord a => a -> a -> Bool
< Quantity
0] (-Double
1)
      -- the total, if marked: a thin line across the bar
      Maybe Text -> (Text -> Svg) -> Svg
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ Maybe Text
mmark ((Text -> Svg) -> Svg) -> (Text -> Svg) -> Svg
forall a b. (a -> b) -> a -> b
$ \Text
tname -> Maybe Quantity -> (Quantity -> Svg) -> Svg
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ (Int -> Maybe Quantity
rowValue Int
j) ((Quantity -> Svg) -> Svg) -> (Quantity -> Svg) -> Svg
forall a b. (a -> b) -> a -> b
$ \Quantity
q -> Text -> Svg -> Svg
withTooltip (Text -> [Text] -> Text
T.intercalate Text
" · " ([Text] -> Text) -> [Text] -> Text
forall a b. (a -> b) -> a -> b
$ [Text] -> [Text]
forall a. Eq a => [a] -> [a]
nub ([Text] -> [Text]) -> [Text] -> [Text]
forall a b. (a -> b) -> a -> b
$ [ChartLabel -> Text
lbFull ChartLabel
lbl, Text
tname, Amount -> Quantity -> Text
showValue Amount
unit Quantity
q] [Text] -> [Text] -> [Text]
forall a. [a] -> [a] -> [a]
++ [Text] -> (Text -> [Text]) -> Maybe Text -> [Text]
forall b a. b -> (a -> b) -> Maybe a -> b
maybe [] Text -> [Text]
forall a. a -> [a]
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Int -> Quantity -> Maybe Text
valueText Int
j Quantity
q)) (Svg -> Svg) -> Svg -> Svg
forall a b. (a -> b) -> a -> b
$ do
        let y :: AttributeValue
y = Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue (Text -> AttributeValue) -> Text -> AttributeValue
forall a b. (a -> b) -> a -> b
$ Double -> Text
num (Double -> Double
yof (Double -> Double) -> Double -> Double
forall a b. (a -> b) -> a -> b
$ Quantity -> Double
toD Quantity
q)
        Svg
S.line Svg -> Attribute -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.class_ AttributeValue
"mark total" Svg -> Attribute -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.x1 (Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue (Text -> AttributeValue) -> Text -> AttributeValue
forall a b. (a -> b) -> a -> b
$ Double -> Text
num (Double
bx Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
4)) Svg -> Attribute -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.x2 (Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue (Text -> AttributeValue) -> Text -> AttributeValue
forall a b. (a -> b) -> a -> b
$ Double -> Text
num (Double
bx Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
barwidth Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
4)) Svg -> Attribute -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.y1 AttributeValue
y Svg -> Attribute -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.y2 AttributeValue
y
      -- the value, above the bar (or below, if negative), if it fits
      Maybe Quantity -> (Quantity -> Svg) -> Svg
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ (Int -> Maybe Quantity
rowValue Int
j) ((Quantity -> Svg) -> Svg) -> (Quantity -> Svg) -> Svg
forall a b. (a -> b) -> a -> b
$ \Quantity
q -> Maybe Text -> (Text -> Svg) -> Svg
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ (Int -> Quantity -> Maybe Text
valueText Int
j Quantity
q) ((Text -> Svg) -> Svg) -> (Text -> Svg) -> Svg
forall a b. (a -> b) -> a -> b
$ \Text
v -> do
        let pos :: Double
pos = [Double] -> Double
forall a. Num a => [a] -> a
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum [Quantity -> Double
toD Quantity
a | (DrawSeries
_, Quantity
a) <- [(DrawSeries, Quantity)]
segs, Quantity
a Quantity -> Quantity -> Bool
forall a. Ord a => a -> a -> Bool
> Quantity
0]
            neg :: Double
neg = [Double] -> Double
forall a. Num a => [a] -> a
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum [Quantity -> Double
toD Quantity
a | (DrawSeries
_, Quantity
a) <- [(DrawSeries, Quantity)]
segs, Quantity
a Quantity -> Quantity -> Bool
forall a. Ord a => a -> a -> Bool
< Quantity
0]
            cliproom :: Double
cliproom = if Double
pos Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
> Double
hi Bool -> Bool -> Bool
|| Double
neg Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
< Double
lo then Double
6 else Double
0  -- room for a clip mark
            vy :: Double
vy = if Quantity
q Quantity -> Quantity -> Bool
forall a. Ord a => a -> a -> Bool
>= Quantity
0 then Double -> Double
yof Double
pos Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
6 Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
cliproom else Double -> Double
yof Double
neg Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
14 Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
cliproom
        Bool -> Svg -> Svg
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Text -> Double
textWidth Text
v Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
<= Double
band Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
2) (Svg -> Svg) -> Svg -> Svg
forall a b. (a -> b) -> a -> b
$
          Svg -> Svg
S.text_ (Svg -> Svg) -> Attribute -> Svg -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.class_ AttributeValue
"value" (Svg -> Svg) -> Attribute -> Svg -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.x (Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue (Text -> AttributeValue) -> Text -> AttributeValue
forall a b. (a -> b) -> a -> b
$ Double -> Text
num Double
x) (Svg -> Svg) -> Attribute -> Svg -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.y (Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue (Text -> AttributeValue) -> Text -> AttributeValue
forall a b. (a -> b) -> a -> b
$ Double -> Text
num Double
vy) (Svg -> Svg) -> Attribute -> Svg -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.textAnchor AttributeValue
"middle" (Svg -> Svg) -> Svg -> Svg
forall a b. (a -> b) -> a -> b
$ Text -> Svg
forall a. ToMarkup a => a -> Svg
S.toMarkup Text
v
  )
  where
    n :: Int
n = [(ChartLabel, Int, [(DrawSeries, Quantity)])] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [(ChartLabel, Int, [(DrawSeries, Quantity)])]
rows
    stackedsums :: Quantity -> [Double]
stackedsums Quantity
sign = [[Double] -> Double
forall a. Num a => [a] -> a
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum [Quantity -> Double
toD Quantity
q | (DrawSeries
_, Quantity
q) <- [(DrawSeries, Quantity)]
segs, Quantity -> Quantity
forall a. Num a => a -> a
signum Quantity
q Quantity -> Quantity -> Bool
forall a. Eq a => a -> a -> Bool
== Quantity
sign] | (ChartLabel
_, Int
_, [(DrawSeries, Quantity)]
segs) <- [(ChartLabel, Int, [(DrawSeries, Quantity)])]
rows]
    (Double
lo, Double
hi, [Double]
ticks, Int
tickdecimals) = Int
-> Maybe Quantity
-> Double
-> Double
-> (Double, Double, [Double], Int)
panelScale (Double -> Int
plotIntervals Double
plotheight) Maybe Quantity
mscale (Double -> [Double] -> Double
forall a. Ord a => a -> [a] -> a
minimumDef Double
0 ([Double] -> Double) -> [Double] -> Double
forall a b. (a -> b) -> a -> b
$ Quantity -> [Double]
stackedsums (-Quantity
1)) (Double -> [Double] -> Double
forall a. Ord a => a -> [a] -> a
maximumDef Double
0 ([Double] -> Double) -> [Double] -> Double
forall a b. (a -> b) -> a -> b
$ Quantity -> [Double]
stackedsums Quantity
1)
    ticklabel :: Double -> Text
ticklabel = Amount -> Int -> [Double] -> Double -> Text
tickLabel Amount
unit Int
tickdecimals [Double]
ticks
    -- the plot's left edge: after the tick labels, and far enough right that angled names don't extend beyond the chart's left edge
    ticksright :: Double
ticksright = Double
pad Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double -> [Double] -> Double
forall a. Ord a => a -> [a] -> a
maximumDef Double
0 ((Double -> Double) -> [Double] -> [Double]
forall a b. (a -> b) -> [a] -> [b]
map (Text -> Double
textWidth (Text -> Double) -> (Double -> Text) -> Double -> Double
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Double -> Text
ticklabel) [Double]
ticks) Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
12
    plotleft :: Double
plotleft
      | Bool
angled    = Double -> Double -> Double
forall a. Ord a => a -> a -> a
max Double
ticksright (Double -> Double) -> Double -> Double
forall a b. (a -> b) -> a -> b
$ Double -> [Double] -> Double
forall a. Ord a => a -> [a] -> a
maximumDef Double
0 [Double
pad Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
0.71 Double -> Double -> Double
forall a. Num a => a -> a -> a
* Text -> Double
textWidth (ChartLabel -> Text
shortName ChartLabel
l) Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
4 Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double -> Double
bandwith Double
ticksright Double -> Double -> Double
forall a. Num a => a -> a -> a
* (Int -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
i Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
0.5) | (Int
i, (ChartLabel
l, Int
_, [(DrawSeries, Quantity)]
_)) <- [Int]
-> [(ChartLabel, Int, [(DrawSeries, Quantity)])]
-> [(Int, (ChartLabel, Int, [(DrawSeries, Quantity)]))]
forall a b. [a] -> [b] -> [(a, b)]
zip [Int
0 :: Int ..] [(ChartLabel, Int, [(DrawSeries, Quantity)])]
rows]
      | Bool
otherwise = Double
ticksright
    plotright :: Double
plotright = Double
width Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
pad
    plottop :: Double
plottop = Double
top Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
20  -- leaving room for values above the bars
    plotbottom :: Double
plotbottom = Double
plottop Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
plotheight
    yof :: Double -> Double
yof Double
v = Double
plottop Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
plotheight Double -> Double -> Double
forall a. Num a => a -> a -> a
* (Double
hi Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double -> Double -> Double -> Double
clamp Double
lo Double
hi Double
v) Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ (Double
hi Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
lo)
    -- the width of each category's band, with the plot starting at this x position
    bandwith :: Double -> Double
bandwith Double
left = (Double
plotright Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
left) Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Int -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
1 Int
n)
    band :: Double
band = Double -> Double
bandwith Double
plotleft
    xcenter :: a -> Double
xcenter a
i = Double
plotleft Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
band Double -> Double -> Double
forall a. Num a => a -> a -> a
* (a -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral a
i Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
0.5)
    barwidth :: Double
barwidth = Double -> Double -> Double
forall a. Ord a => a -> a -> a
max Double
1 (Double -> Double) -> Double -> Double
forall a b. (a -> b) -> a -> b
$ Double -> Double -> Double
forall a. Ord a => a -> a -> a
min Double
64 (Double
band Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
0.6)
    -- names: full account names (since there's no indentation), shortened if very long
    shortName :: ChartLabel -> Text
shortName ChartLabel
l = let t :: Text
t = ChartLabel -> Text
lbFull ChartLabel
l in if Text -> Int
T.length Text
t Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
30 then Int -> Text -> Text
T.take Int
29 Text
t Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"…" else Text
t
    namewidth :: Double
namewidth = Double -> [Double] -> Double
forall a. Ord a => a -> [a] -> a
maximumDef Double
0 [Text -> Double
textWidth (ChartLabel -> Text
shortName ChartLabel
l) | (ChartLabel
l, Int
_, [(DrawSeries, Quantity)]
_) <- [(ChartLabel, Int, [(DrawSeries, Quantity)])]
rows]
    -- whether names are angled: decided with the widest band (before any room is made for angled names), to avoid a cycle
    angled :: Bool
angled = Double
namewidth Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
> Double -> Double
bandwith Double
ticksright Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
4
    labelheight :: Double
labelheight = if Bool
angled then Double
namewidth Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
0.71 Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
16 else Double
24
    -- room below the plot for the values of negative bars, which may reach its bottom
    negroom :: Double
negroom = if ((ChartLabel, Int, [(DrawSeries, Quantity)]) -> Bool)
-> [(ChartLabel, Int, [(DrawSeries, Quantity)])] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any (((DrawSeries, Quantity) -> Bool)
-> [(DrawSeries, Quantity)] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any ((Quantity -> Quantity -> Bool
forall a. Ord a => a -> a -> Bool
< Quantity
0) (Quantity -> Bool)
-> ((DrawSeries, Quantity) -> Quantity)
-> (DrawSeries, Quantity)
-> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (DrawSeries, Quantity) -> Quantity
forall a b. (a, b) -> b
snd) ([(DrawSeries, Quantity)] -> Bool)
-> ((ChartLabel, Int, [(DrawSeries, Quantity)])
    -> [(DrawSeries, Quantity)])
-> (ChartLabel, Int, [(DrawSeries, Quantity)])
-> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (\(ChartLabel
_, Int
_, [(DrawSeries, Quantity)]
segs) -> [(DrawSeries, Quantity)]
segs)) [(ChartLabel, Int, [(DrawSeries, Quantity)])]
rows then (if Maybe Quantity -> Bool
forall a. Maybe a -> Bool
isJust Maybe Quantity
mscale then Double
22 else Double
16) else Double
0

-- | Draw categories as horizontal bars (rows; stacked if several series), with their names on the left (indented as in the report)
-- and an aligned value column on the right.
-- The values are shown as the given function says, if at all. If a total's name is given, the values (the total's) are also marked across the bars.
-- If the flag is true, the values' text is value labels (see 'pnValueLabels'), shown smaller.
barsSvg :: Text -> Double -> Double -> Maybe Quantity -> Amount -> [(ChartLabel, Int, [(DrawSeries, Quantity)])] -> (Int -> Maybe Quantity) -> (Int -> [Text]) -> Bool -> (Int -> Quantity -> Maybe Text) -> Maybe Text -> (Double, Svg)
barsSvg :: Text
-> Double
-> Double
-> Maybe Quantity
-> Amount
-> [(ChartLabel, Int, [(DrawSeries, Quantity)])]
-> (Int -> Maybe Quantity)
-> (Int -> [Text])
-> Bool
-> (Int -> Quantity -> Maybe Text)
-> Maybe Text
-> (Double, Svg)
barsSvg Text
ids Double
width Double
top Maybe Quantity
mscale Amount
unit [(ChartLabel, Int, [(DrawSeries, Quantity)])]
rows Int -> Maybe Quantity
rowValue Int -> [Text]
rowTip Bool
labelled Int -> Quantity -> Maybe Text
valueText Maybe Text
mmark = (Int -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral ([(ChartLabel, Int, [(DrawSeries, Quantity)])] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [(ChartLabel, Int, [(DrawSeries, Quantity)])]
rows) Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
rowHeight Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
32, do
    -- gridlines and x tick labels (labels thinned to avoid overlap, always including zero)
    [Double] -> (Double -> Svg) -> Svg
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [Double]
ticks ((Double -> Svg) -> Svg) -> (Double -> Svg) -> Svg
forall a b. (a -> b) -> a -> b
$ \Double
t -> do
      Svg
S.line Svg -> Attribute -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.class_ (if Double
t Double -> Double -> Bool
forall a. Eq a => a -> a -> Bool
== Double
0 then AttributeValue
"axis" else AttributeValue
"grid")
        Svg -> Attribute -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.x1 (Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue (Text -> AttributeValue) -> Text -> AttributeValue
forall a b. (a -> b) -> a -> b
$ Double -> Text
num (Double -> Double
xof Double
t)) Svg -> Attribute -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.x2 (Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue (Text -> AttributeValue) -> Text -> AttributeValue
forall a b. (a -> b) -> a -> b
$ Double -> Text
num (Double -> Double
xof Double
t)) Svg -> Attribute -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.y1 (Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue (Text -> AttributeValue) -> Text -> AttributeValue
forall a b. (a -> b) -> a -> b
$ Double -> Text
num Double
plottop) Svg -> Attribute -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.y2 (Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue (Text -> AttributeValue) -> Text -> AttributeValue
forall a b. (a -> b) -> a -> b
$ Double -> Text
num Double
plotbottom)
      Bool -> Svg -> Svg
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Double -> Integer
forall b. Integral b => Double -> b
forall a b. (RealFrac a, Integral b) => a -> b
round (Double
t Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Double
tickstep) Integer -> Integer -> Integer
forall a. Integral a => a -> a -> a
`mod` Integer
labelevery Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
== (Integer
0 :: Integer)) (Svg -> Svg) -> Svg -> Svg
forall a b. (a -> b) -> a -> b
$
        Svg -> Svg
S.text_ (Svg -> Svg) -> Attribute -> Svg -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.class_ AttributeValue
"tick" (Svg -> Svg) -> Attribute -> Svg -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.x (Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue (Text -> AttributeValue) -> Text -> AttributeValue
forall a b. (a -> b) -> a -> b
$ Double -> Text
num (Double -> Double
xof Double
t)) (Svg -> Svg) -> Attribute -> Svg -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.y (Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue (Text -> AttributeValue) -> Text -> AttributeValue
forall a b. (a -> b) -> a -> b
$ Double -> Text
num (Double
plotbottom Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
21)) (Svg -> Svg) -> Attribute -> Svg -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.textAnchor AttributeValue
"middle" (Svg -> Svg) -> Svg -> Svg
forall a b. (a -> b) -> a -> b
$ Text -> Svg
forall a. ToMarkup a => a -> Svg
S.toMarkup (Double -> Text
ticklabel Double
t)
    [(Int, (ChartLabel, Int, [(DrawSeries, Quantity)]))]
-> ((Int, (ChartLabel, Int, [(DrawSeries, Quantity)])) -> Svg)
-> Svg
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ ([Int]
-> [(ChartLabel, Int, [(DrawSeries, Quantity)])]
-> [(Int, (ChartLabel, Int, [(DrawSeries, Quantity)]))]
forall a b. [a] -> [b] -> [(a, b)]
zip [Int
0 :: Int ..] [(ChartLabel, Int, [(DrawSeries, Quantity)])]
rows) (((Int, (ChartLabel, Int, [(DrawSeries, Quantity)])) -> Svg)
 -> Svg)
-> ((Int, (ChartLabel, Int, [(DrawSeries, Quantity)])) -> Svg)
-> Svg
forall a b. (a -> b) -> a -> b
$ \(Int
i, (ChartLabel
lbl, Int
j, [(DrawSeries, Quantity)]
segs)) -> do
      let ymid :: Double
ymid = Double
plottop Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
rowHeight Double -> Double -> Double
forall a. Num a => a -> a -> a
* (Int -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
i Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
0.5)
      Svg -> Svg
S.text_ (Svg -> Svg) -> Attribute -> Svg -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.class_ AttributeValue
"label" (Svg -> Svg) -> Attribute -> Svg -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.x (Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue (Text -> AttributeValue) -> Text -> AttributeValue
forall a b. (a -> b) -> a -> b
$ Double -> Text
num (Double
pad Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
12 Double -> Double -> Double
forall a. Num a => a -> a -> a
* Int -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ChartLabel -> Int
lbIndent ChartLabel
lbl))) (Svg -> Svg) -> Attribute -> Svg -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.y (Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue (Text -> AttributeValue) -> Text -> AttributeValue
forall a b. (a -> b) -> a -> b
$ Double -> Text
num (Double
ymid Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
5))
        (Svg -> Svg) -> Svg -> Svg
forall a b. (a -> b) -> a -> b
$ Text -> Svg
forall a. ToMarkup a => a -> Svg
S.toMarkup (ChartLabel -> Text
lbShort ChartLabel
lbl)
      Maybe Quantity -> (Quantity -> Svg) -> Svg
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ (Int -> Maybe Quantity
rowValue Int
j) ((Quantity -> Svg) -> Svg) -> (Quantity -> Svg) -> Svg
forall a b. (a -> b) -> a -> b
$ \Quantity
q -> Maybe Text -> (Text -> Svg) -> Svg
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ (Int -> Quantity -> Maybe Text
valueText Int
j Quantity
q) ((Text -> Svg) -> Svg) -> (Text -> Svg) -> Svg
forall a b. (a -> b) -> a -> b
$ \Text
v ->
        Svg -> Svg
S.text_ (Svg -> Svg) -> Attribute -> Svg -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.class_ (if Bool
labelled then Text -> AttributeValue
vlabelClass Text
v else AttributeValue
"value") (Svg -> Svg) -> Attribute -> Svg -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.x (Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue (Text -> AttributeValue) -> Text -> AttributeValue
forall a b. (a -> b) -> a -> b
$ Double -> Text
num (Double
width Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
pad)) (Svg -> Svg) -> Attribute -> Svg -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.y (Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue (Text -> AttributeValue) -> Text -> AttributeValue
forall a b. (a -> b) -> a -> b
$ Double -> Text
num (Double
ymid Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
5)) (Svg -> Svg) -> Attribute -> Svg -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.textAnchor AttributeValue
"end"
          (Svg -> Svg) -> Svg -> Svg
forall a b. (a -> b) -> a -> b
$ Text -> Svg
forall a. ToMarkup a => a -> Svg
S.toMarkup Text
v
      ChartLabel
-> [Text] -> Double -> [(DrawSeries, Quantity)] -> Double -> Svg
stack ChartLabel
lbl (Int -> [Text]
rowTip Int
j) Double
ymid [(DrawSeries
d, Quantity
q) | (DrawSeries
d, Quantity
q) <- [(DrawSeries, Quantity)]
segs, Quantity
q Quantity -> Quantity -> Bool
forall a. Ord a => a -> a -> Bool
> Quantity
0] Double
1
      ChartLabel
-> [Text] -> Double -> [(DrawSeries, Quantity)] -> Double -> Svg
stack ChartLabel
lbl (Int -> [Text]
rowTip Int
j) Double
ymid [(DrawSeries
d, Quantity
q) | (DrawSeries
d, Quantity
q) <- [(DrawSeries, Quantity)]
segs, Quantity
q Quantity -> Quantity -> Bool
forall a. Ord a => a -> a -> Bool
< Quantity
0] (-Double
1)
      -- the total, if marked: a thin line across the bar
      Maybe Text -> (Text -> Svg) -> Svg
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ Maybe Text
mmark ((Text -> Svg) -> Svg) -> (Text -> Svg) -> Svg
forall a b. (a -> b) -> a -> b
$ \Text
tname -> Maybe Quantity -> (Quantity -> Svg) -> Svg
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ (Int -> Maybe Quantity
rowValue Int
j) ((Quantity -> Svg) -> Svg) -> (Quantity -> Svg) -> Svg
forall a b. (a -> b) -> a -> b
$ \Quantity
q -> Text -> Svg -> Svg
withTooltip (Text -> [Text] -> Text
T.intercalate Text
" · " ([Text] -> Text) -> [Text] -> Text
forall a b. (a -> b) -> a -> b
$ [Text] -> [Text]
forall a. Eq a => [a] -> [a]
nub ([Text] -> [Text]) -> [Text] -> [Text]
forall a b. (a -> b) -> a -> b
$ [ChartLabel -> Text
lbFull ChartLabel
lbl, Text
tname, Amount -> Quantity -> Text
showValue Amount
unit Quantity
q] [Text] -> [Text] -> [Text]
forall a. [a] -> [a] -> [a]
++ [Text] -> (Text -> [Text]) -> Maybe Text -> [Text]
forall b a. b -> (a -> b) -> Maybe a -> b
maybe [] Text -> [Text]
forall a. a -> [a]
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Int -> Quantity -> Maybe Text
valueText Int
j Quantity
q)) (Svg -> Svg) -> Svg -> Svg
forall a b. (a -> b) -> a -> b
$ do
        let x :: AttributeValue
x = Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue (Text -> AttributeValue) -> Text -> AttributeValue
forall a b. (a -> b) -> a -> b
$ Double -> Text
num (Double -> Double
xof (Double -> Double) -> Double -> Double
forall a b. (a -> b) -> a -> b
$ Quantity -> Double
toD Quantity
q)
        Svg
S.line Svg -> Attribute -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.class_ AttributeValue
"mark total" Svg -> Attribute -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.x1 AttributeValue
x Svg -> Attribute -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.x2 AttributeValue
x Svg -> Attribute -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.y1 (Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue (Text -> AttributeValue) -> Text -> AttributeValue
forall a b. (a -> b) -> a -> b
$ Double -> Text
num (Double
ymid Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
thickness Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Double
2 Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
4)) Svg -> Attribute -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.y2 (Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue (Text -> AttributeValue) -> Text -> AttributeValue
forall a b. (a -> b) -> a -> b
$ Double -> Text
num (Double
ymid Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
thickness Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Double
2 Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
4))
  )
  where
    stackedsums :: Quantity -> [Double]
stackedsums Quantity
sign = [[Double] -> Double
forall a. Num a => [a] -> a
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum [Quantity -> Double
toD Quantity
q | (DrawSeries
_, Quantity
q) <- [(DrawSeries, Quantity)]
segs, Quantity -> Quantity
forall a. Num a => a -> a
signum Quantity
q Quantity -> Quantity -> Bool
forall a. Eq a => a -> a -> Bool
== Quantity
sign] | (ChartLabel
_, Int
_, [(DrawSeries, Quantity)]
segs) <- [(ChartLabel, Int, [(DrawSeries, Quantity)])]
rows]
    (Double
lo, Double
hi, [Double]
ticks, Int
tickdecimals) = Int
-> Maybe Quantity
-> Double
-> Double
-> (Double, Double, [Double], Int)
panelScale Int
5 Maybe Quantity
mscale (Double -> [Double] -> Double
forall a. Ord a => a -> [a] -> a
minimumDef Double
0 ([Double] -> Double) -> [Double] -> Double
forall a b. (a -> b) -> a -> b
$ Quantity -> [Double]
stackedsums (-Quantity
1)) (Double -> [Double] -> Double
forall a. Ord a => a -> [a] -> a
maximumDef Double
0 ([Double] -> Double) -> [Double] -> Double
forall a b. (a -> b) -> a -> b
$ Quantity -> [Double]
stackedsums Quantity
1)
    ticklabel :: Double -> Text
ticklabel = Amount -> Int -> [Double] -> Double -> Text
tickLabel Amount
unit{acommodity = ""} Int
tickdecimals []
    labelwidth :: Double
labelwidth = Double -> Double -> Double
forall a. Ord a => a -> a -> a
min (Double
width Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
0.4) (Double -> Double) -> Double -> Double
forall a b. (a -> b) -> a -> b
$ Double -> [Double] -> Double
forall a. Ord a => a -> [a] -> a
maximumDef Double
0 [Double
12 Double -> Double -> Double
forall a. Num a => a -> a -> a
* Int -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ChartLabel -> Int
lbIndent ChartLabel
l) Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Text -> Double
textWidth (ChartLabel -> Text
lbShort ChartLabel
l) | (ChartLabel
l, Int
_, [(DrawSeries, Quantity)]
_) <- [(ChartLabel, Int, [(DrawSeries, Quantity)])]
rows]
    valuewidth :: Double
valuewidth = Double -> [Double] -> Double
forall a. Ord a => a -> [a] -> a
maximumDef Double
0 [(if Bool
labelled then Text -> Double
labelWidth else Text -> Double
textWidth) Text
v | (ChartLabel
_, Int
j, [(DrawSeries, Quantity)]
_) <- [(ChartLabel, Int, [(DrawSeries, Quantity)])]
rows, Just Quantity
q <- [Int -> Maybe Quantity
rowValue Int
j], Just Text
v <- [Int -> Quantity -> Maybe Text
valueText Int
j Quantity
q]]
    -- leave room for half of a tick label at each end
    edge :: Double
edge = Double -> [Double] -> Double
forall a. Ord a => a -> [a] -> a
maximumDef Double
0 ((Double -> Double) -> [Double] -> [Double]
forall a b. (a -> b) -> [a] -> [b]
map (Text -> Double
textWidth (Text -> Double) -> (Double -> Text) -> Double -> Double
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Double -> Text
ticklabel) [Double]
ticks) Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Double
2
    plotleft :: Double
plotleft = Double
pad Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double -> Double -> Double
forall a. Ord a => a -> a -> a
max Double
labelwidth Double
edge Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
12
    plotright :: Double
plotright = Double
width Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
pad Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double -> Double -> Double
forall a. Ord a => a -> a -> a
max Double
valuewidth Double
edge Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
16
    plottop :: Double
plottop = Double
top Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
4
    plotbottom :: Double
plotbottom = Double
plottop Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
rowHeight Double -> Double -> Double
forall a. Num a => a -> a -> a
* Int -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral ([(ChartLabel, Int, [(DrawSeries, Quantity)])] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [(ChartLabel, Int, [(DrawSeries, Quantity)])]
rows)
    xof :: Double -> Double
xof Double
v = Double
plotleft Double -> Double -> Double
forall a. Num a => a -> a -> a
+ (Double
plotright Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
plotleft) Double -> Double -> Double
forall a. Num a => a -> a -> a
* (Double -> Double -> Double -> Double
clamp Double
lo Double
hi Double
v Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
lo) Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ (Double
hi Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
lo)
    tickstep :: Double
tickstep = case [Double]
ticks of Double
t1:Double
t2:[Double]
_ -> Double
t2 Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
t1; [Double]
_ -> Double
1
    labelevery :: Integer
labelevery = Integer -> Integer -> Integer
forall a. Ord a => a -> a -> a
max Integer
1 (Integer -> Integer) -> Integer -> Integer
forall a b. (a -> b) -> a -> b
$ Double -> Integer
forall b. Integral b => Double -> b
forall a b. (RealFrac a, Integral b) => a -> b
ceiling ((Double -> [Double] -> Double
forall a. Ord a => a -> [a] -> a
maximumDef Double
0 ((Double -> Double) -> [Double] -> [Double]
forall a b. (a -> b) -> [a] -> [b]
map (Text -> Double
textWidth (Text -> Double) -> (Double -> Text) -> Double -> Double
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Double -> Text
ticklabel) [Double]
ticks) Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
12) Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Double -> Double -> Double
forall a. Ord a => a -> a -> a
max Double
1 (Double -> Double
xof Double
tickstep Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double -> Double
xof Double
0)) :: Integer
    thickness :: Double
thickness = Double
16
    stack :: ChartLabel -> [Text] -> Double -> [(DrawSeries, Quantity)] -> Double -> Svg
    stack :: ChartLabel
-> [Text] -> Double -> [(DrawSeries, Quantity)] -> Double -> Svg
stack ChartLabel
lbl [Text]
extratip Double
ymid [(DrawSeries, Quantity)]
segs Double
sign = do
      let cums :: [Double]
cums = (Double -> Double -> Double) -> [Double] -> [Double]
forall a. (a -> a -> a) -> [a] -> [a]
scanl1 Double -> Double -> Double
forall a. Num a => a -> a -> a
(+) (((DrawSeries, Quantity) -> Double)
-> [(DrawSeries, Quantity)] -> [Double]
forall a b. (a -> b) -> [a] -> [b]
map (Quantity -> Double
toD (Quantity -> Double)
-> ((DrawSeries, Quantity) -> Quantity)
-> (DrawSeries, Quantity)
-> Double
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (DrawSeries, Quantity) -> Quantity
forall a b. (a, b) -> b
snd) [(DrawSeries, Quantity)]
segs)
          starts :: [Double]
starts = Double
0 Double -> [Double] -> [Double]
forall a. a -> [a] -> [a]
: [Double]
cums
          clipped :: Bool
clipped = (Double -> Bool) -> Maybe Double -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any (\Double
c -> Double
c Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
> Double
hi Bool -> Bool -> Bool
|| Double
c Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
< Double
lo) ([Double] -> Maybe Double
forall a. [a] -> Maybe a
lastMay [Double]
cums)
          outer :: Double
outer = Double -> (Double -> Double) -> Maybe Double -> Double
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Double -> Double
xof Double
0) Double -> Double
xof ([Double] -> Maybe Double
forall a. [a] -> Maybe a
lastMay [Double]
cums)
      Text
-> Bool
-> Bool
-> Double
-> Double
-> Double
-> Double
-> Svg
-> Svg
roundedEnd Text
ids (Bool -> Bool
not Bool
clipped) Bool
False (Double -> Double
xof Double
0) (Double
ymid Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
thickness Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Double
2) Double
thickness (Double
outer Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double -> Double
xof Double
0) (Svg -> Svg) -> Svg -> Svg
forall a b. (a -> b) -> a -> b
$
        [(Int, (DrawSeries, Quantity), (Double, Double))]
-> ((Int, (DrawSeries, Quantity), (Double, Double)) -> Svg) -> Svg
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ ([Int]
-> [(DrawSeries, Quantity)]
-> [(Double, Double)]
-> [(Int, (DrawSeries, Quantity), (Double, Double))]
forall a b c. [a] -> [b] -> [c] -> [(a, b, c)]
zip3 [Int
0 :: Int ..] [(DrawSeries, Quantity)]
segs ([Double] -> [Double] -> [(Double, Double)]
forall a b. [a] -> [b] -> [(a, b)]
zip [Double]
starts [Double]
cums)) (((Int, (DrawSeries, Quantity), (Double, Double)) -> Svg) -> Svg)
-> ((Int, (DrawSeries, Quantity), (Double, Double)) -> Svg) -> Svg
forall a b. (a -> b) -> a -> b
$ \(Int
k, (DrawSeries
d, Quantity
q), (Double
c0, Double
c1)) -> do
          let gap :: Double
gap = if Int
k Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
0 Bool -> Bool -> Bool
&& Double -> Double
forall a. Num a => a -> a
abs (Double -> Double
xof Double
c1 Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double -> Double
xof Double
c0) Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
>= Double
4 then Double
2 else Double
0  -- no gap for tiny segments, which it would hide
              xbase :: Double
xbase = Double -> Double
xof Double
c0 Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
sign Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
gap   -- the end nearer the axis, after the gap (the sums are negative for negative segments)
              len :: Double
len = (Double -> Double
xof Double
c1 Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
xbase) Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
sign
          let path :: AttributeValue
path = Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue (Text -> AttributeValue) -> Text -> AttributeValue
forall a b. (a -> b) -> a -> b
$ Bool -> Double -> Double -> Double -> Double -> Bool -> Text
barPath Bool
False Double
xbase (Double
ymid Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
thickness Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Double
2) Double
thickness (Double
sign Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
len) Bool
False
          Bool -> Svg -> Svg
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Double
len Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
> Double
0.5) (Svg -> Svg) -> Svg -> Svg
forall a b. (a -> b) -> a -> b
$ Text -> Svg -> Svg
withTooltip (Text -> [Text] -> Text
T.intercalate Text
" · " ([Text] -> [Text]
forall a. Eq a => [a] -> [a]
nub ([Text] -> [Text]) -> [Text] -> [Text]
forall a b. (a -> b) -> a -> b
$ [ChartLabel -> Text
lbFull ChartLabel
lbl, ChartLabel -> Text
lbFull (Series -> ChartLabel
seLabel (Series -> ChartLabel) -> Series -> ChartLabel
forall a b. (a -> b) -> a -> b
$ DrawSeries -> Series
dsSeries DrawSeries
d), Amount -> Quantity -> Text
showValue Amount
unit Quantity
q] [Text] -> [Text] -> [Text]
forall a. [a] -> [a] -> [a]
++ [Text]
extratip) Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Series -> Text
tipLines (DrawSeries -> Series
dsSeries DrawSeries
d)) (Svg -> Svg) -> Svg -> Svg
forall a b. (a -> b) -> a -> b
$ do
            Svg
S.path Svg -> Attribute -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.class_ (AttributeValue
"fill " AttributeValue -> AttributeValue -> AttributeValue
forall a. Semigroup a => a -> a -> a
<> Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue (DrawSeries -> Text
dsClass DrawSeries
d)) Svg -> Attribute -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.d AttributeValue
path
            Bool -> Svg -> Svg
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Series -> Quantity -> Bool
isHatched (DrawSeries -> Series
dsSeries DrawSeries
d) Quantity
q) (Svg -> Svg) -> Svg -> Svg
forall a b. (a -> b) -> a -> b
$ Svg
S.path Svg -> Attribute -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.fill (Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue (Text -> AttributeValue) -> Text -> AttributeValue
forall a b. (a -> b) -> a -> b
$ Text
"url(#" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text -> Text
hatchId Text
ids Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
")") Svg -> Attribute -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.d AttributeValue
path
      -- a bar clipped by a fixed scale has a small triangle beyond its end
      Bool -> Svg -> Svg
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when Bool
clipped (Svg -> Svg) -> Svg -> Svg
forall a b. (a -> b) -> a -> b
$ Bool -> Double -> Double -> Double -> Double -> Svg
clipMark Bool
False (Double -> Double
xof (Double -> Double) -> Double -> Double
forall a b. (a -> b) -> a -> b
$ if Double
sign Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
> Double
0 then Double
hi else Double
lo) Double
ymid Double
thickness Double
sign

-- | One direction of a vertical stacked bar, with its left edge at x and the given width, on a y scale:
-- segments (each with its series, value and tooltip) from zero outward (up if sign is 1, down if -1),
-- 2px apart (except tiny ones, which a gap would hide), with a rounded end (see 'roundedEnd').
-- If the bar extends beyond the y scale's range (lo, hi), it is clipped, with a small triangle beyond its end instead.
vstack :: Text -> (Double, Double) -> (Double -> Double) -> Double -> Double -> [(DrawSeries, Quantity, Text)] -> Double -> Svg
vstack :: Text
-> (Double, Double)
-> (Double -> Double)
-> Double
-> Double
-> [(DrawSeries, Quantity, Text)]
-> Double
-> Svg
vstack Text
ids (Double
lo, Double
hi) Double -> Double
yof Double
x Double
width [(DrawSeries, Quantity, Text)]
segs Double
sign = do
  let cums :: [Double]
cums = (Double -> Double -> Double) -> [Double] -> [Double]
forall a. (a -> a -> a) -> [a] -> [a]
scanl1 Double -> Double -> Double
forall a. Num a => a -> a -> a
(+) [Quantity -> Double
toD Quantity
q | (DrawSeries
_, Quantity
q, Text
_) <- [(DrawSeries, Quantity, Text)]
segs]
      starts :: [Double]
starts = Double
0 Double -> [Double] -> [Double]
forall a. a -> [a] -> [a]
: [Double]
cums
      clipped :: Bool
clipped = (Double -> Bool) -> Maybe Double -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any (\Double
c -> Double
c Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
> Double
hi Bool -> Bool -> Bool
|| Double
c Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
< Double
lo) ([Double] -> Maybe Double
forall a. [a] -> Maybe a
lastMay [Double]
cums)
      outer :: Double
outer = Double -> (Double -> Double) -> Maybe Double -> Double
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Double -> Double
yof Double
0) Double -> Double
yof ([Double] -> Maybe Double
forall a. [a] -> Maybe a
lastMay [Double]
cums)
  Text
-> Bool
-> Bool
-> Double
-> Double
-> Double
-> Double
-> Svg
-> Svg
roundedEnd Text
ids (Bool -> Bool
not Bool
clipped) Bool
True Double
x (Double -> Double
yof Double
0) Double
width (Double
outer Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double -> Double
yof Double
0) (Svg -> Svg) -> Svg -> Svg
forall a b. (a -> b) -> a -> b
$
    [(Int, (DrawSeries, Quantity, Text), (Double, Double))]
-> ((Int, (DrawSeries, Quantity, Text), (Double, Double)) -> Svg)
-> Svg
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ ([Int]
-> [(DrawSeries, Quantity, Text)]
-> [(Double, Double)]
-> [(Int, (DrawSeries, Quantity, Text), (Double, Double))]
forall a b c. [a] -> [b] -> [c] -> [(a, b, c)]
zip3 [Int
0 :: Int ..] [(DrawSeries, Quantity, Text)]
segs ([Double] -> [Double] -> [(Double, Double)]
forall a b. [a] -> [b] -> [(a, b)]
zip [Double]
starts [Double]
cums)) (((Int, (DrawSeries, Quantity, Text), (Double, Double)) -> Svg)
 -> Svg)
-> ((Int, (DrawSeries, Quantity, Text), (Double, Double)) -> Svg)
-> Svg
forall a b. (a -> b) -> a -> b
$ \(Int
k, (DrawSeries
d, Quantity
q, Text
tip), (Double
c0, Double
c1)) -> do
      let gap :: Double
gap = if Int
k Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
0 Bool -> Bool -> Bool
&& Double -> Double
forall a. Num a => a -> a
abs (Double -> Double
yof Double
c1 Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double -> Double
yof Double
c0) Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
>= Double
4 then Double
2 else Double
0
          ybase :: Double
ybase = Double -> Double
yof Double
c0 Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
sign Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
gap   -- the end nearer zero, after the gap
          len :: Double
len = (Double
ybase Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double -> Double
yof Double
c1) Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
sign  -- positive when there is something to draw
          path :: AttributeValue
path = Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue (Text -> AttributeValue) -> Text -> AttributeValue
forall a b. (a -> b) -> a -> b
$ Bool -> Double -> Double -> Double -> Double -> Bool -> Text
barPath Bool
True Double
x Double
ybase Double
width (-Double
sign Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
len) Bool
False
      Bool -> Svg -> Svg
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Double
len Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
> Double
0.5) (Svg -> Svg) -> Svg -> Svg
forall a b. (a -> b) -> a -> b
$ Text -> Svg -> Svg
withTooltip Text
tip (Svg -> Svg) -> Svg -> Svg
forall a b. (a -> b) -> a -> b
$ do
        Svg
S.path Svg -> Attribute -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.class_ (AttributeValue
"fill " AttributeValue -> AttributeValue -> AttributeValue
forall a. Semigroup a => a -> a -> a
<> Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue (DrawSeries -> Text
dsClass DrawSeries
d)) Svg -> Attribute -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.d AttributeValue
path
        -- a contra amount or shortfall is hatched
        Bool -> Svg -> Svg
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Series -> Quantity -> Bool
isHatched (DrawSeries -> Series
dsSeries DrawSeries
d) Quantity
q) (Svg -> Svg) -> Svg -> Svg
forall a b. (a -> b) -> a -> b
$ Svg
S.path Svg -> Attribute -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.fill (Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue (Text -> AttributeValue) -> Text -> AttributeValue
forall a b. (a -> b) -> a -> b
$ Text
"url(#" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text -> Text
hatchId Text
ids Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
")") Svg -> Attribute -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.d AttributeValue
path
  Bool -> Svg -> Svg
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when Bool
clipped (Svg -> Svg) -> Svg -> Svg
forall a b. (a -> b) -> a -> b
$ Bool -> Double -> Double -> Double -> Double -> Svg
clipMark Bool
True (Double -> Double
yof (Double -> Double) -> Double -> Double
forall a b. (a -> b) -> a -> b
$ if Double
sign Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
> Double
0 then Double
hi else Double
lo) (Double
x Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
width Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Double
2) Double
width Double
sign

-- | Draw a bar's segments (if rounded) clipped to the bar's outline with a rounded outer end, so that the end
-- is rounded the same however thin its outermost segment. The bar is vertical or horizontal, with its base at a, b
-- (its left or top edge, at the axis), the given size across, and length (negative for up or left).
-- The clipping path's id, unique within the chart, is made from these, after the chart's prefix (see 'svgIds').
roundedEnd :: Text -> Bool -> Bool -> Double -> Double -> Double -> Double -> Svg -> Svg
roundedEnd :: Text
-> Bool
-> Bool
-> Double
-> Double
-> Double
-> Double
-> Svg
-> Svg
roundedEnd Text
ids Bool
rounded Bool
vertical Double
a Double
b Double
size Double
len Svg
segments
  | Bool -> Bool
not Bool
rounded Bool -> Bool -> Bool
|| Double -> Double
forall a. Num a => a -> a
abs Double
len Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
< Double
0.5 = Svg
segments
  | Bool
otherwise = do
      Svg -> Svg
S.defs (Svg -> Svg) -> Svg -> Svg
forall a b. (a -> b) -> a -> b
$ Svg -> Svg
S.clippath (Svg -> Svg) -> Attribute -> Svg -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.id_ (Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue Text
clipid) (Svg -> Svg) -> Svg -> Svg
forall a b. (a -> b) -> a -> b
$ Svg
S.path Svg -> Attribute -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.d (Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue (Text -> AttributeValue) -> Text -> AttributeValue
forall a b. (a -> b) -> a -> b
$ Bool -> Double -> Double -> Double -> Double -> Bool -> Text
barPath Bool
vertical Double
a Double
b Double
size Double
len Bool
True)
      Svg -> Svg
S.g (Svg -> Svg) -> Attribute -> Svg -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.clipPath (Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue (Text -> AttributeValue) -> Text -> AttributeValue
forall a b. (a -> b) -> a -> b
$ Text
"url(#" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
clipid Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
")") (Svg -> Svg) -> Svg -> Svg
forall a b. (a -> b) -> a -> b
$ Svg
segments
  where
    clipid :: Text
clipid = Text
ids Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"-clip-bar" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text -> [Text] -> Text
T.intercalate Text
"_" [if Bool
vertical then Text
"v" else Text
"h", Double -> Text
num Double
a, Double -> Text
num Double
b, Double -> Text
num Double
len]

-- | The id of the hatching pattern for contra amounts and shortfalls, after the chart's prefix (see 'svgIds').
hatchId :: Text -> Text
hatchId :: Text -> Text
hatchId Text
ids = Text
ids Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"-hatch"

-- | A small triangle pointing outward from the edge of a plot, marking a bar that was clipped there:
-- for a vertical bar (at y position edge, centred on x position mid) or a horizontal one (at x position edge, centred on y position mid).
-- sign is the direction in which the bar grows (1: up or right, -1: down or left).
clipMark :: Bool -> Double -> Double -> Double -> Double -> Svg
clipMark :: Bool -> Double -> Double -> Double -> Double -> Svg
clipMark Bool
vertical Double
edge Double
mid Double
barsize Double
sign =
  Svg
S.polygon Svg -> Attribute -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.class_ AttributeValue
"clip" Svg -> Attribute -> Svg
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.points (Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
toValue (Text -> AttributeValue) -> Text -> AttributeValue
forall a b. (a -> b) -> a -> b
$ [Text] -> Text
T.unwords [Double -> Text
num Double
a Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"," Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Double -> Text
num Double
b | (Double
a, Double
b) <- ((Double, Double) -> (Double, Double))
-> [(Double, Double)] -> [(Double, Double)]
forall a b. (a -> b) -> [a] -> [b]
map (Double, Double) -> (Double, Double)
orient [(Double, Double)]
pts])
  where
    w :: Double
w = Double -> Double -> Double
forall a. Ord a => a -> a -> a
max Double
6 (Double -> Double) -> Double -> Double
forall a b. (a -> b) -> a -> b
$ Double -> Double -> Double
forall a. Ord a => a -> a -> a
min Double
10 Double
barsize
    -- the points as (position across the bar, distance outward from the edge)
    pts :: [(Double, Double)]
pts = [(Double
mid Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
w Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Double
2, Double
0), (Double
mid Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
w Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Double
2, Double
0), (Double
mid, Double
w Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Double
2)]
    orient :: (Double, Double) -> (Double, Double)
orient (Double
across, Double
out)
      | Bool
vertical  = (Double
across, Double
edge Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
sign Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
out)
      | Bool
otherwise = (Double
edge Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
sign Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
out, Double
across)

-- | The points of a step line through these points: each value holds until the next point's x position.
steps :: [(Double, Double)] -> [(Double, Double)]
steps :: [(Double, Double)] -> [(Double, Double)]
steps [(Double, Double)]
ps = Int -> [(Double, Double)] -> [(Double, Double)]
forall a. Int -> [a] -> [a]
take Int
1 [(Double, Double)]
ps [(Double, Double)] -> [(Double, Double)] -> [(Double, Double)]
forall a. [a] -> [a] -> [a]
++ [[(Double, Double)]] -> [(Double, Double)]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat (((Double, Double) -> (Double, Double) -> [(Double, Double)])
-> [(Double, Double)] -> [(Double, Double)] -> [[(Double, Double)]]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith (\(Double
_, Double
py) (Double
x, Double
y) -> [(Double
x, Double
py), (Double
x, Double
y)]) [(Double, Double)]
ps (Int -> [(Double, Double)] -> [(Double, Double)]
forall a. Int -> [a] -> [a]
drop Int
1 [(Double, Double)]
ps))

-- | Leave out the points inside a run of points at the same height, which a line draws the same without:
-- eg a step line's long flat stretches, as when most of a register's transactions are in other commodities.
dropFlat :: [(Double, Double)] -> [(Double, Double)]
dropFlat :: [(Double, Double)] -> [(Double, Double)]
dropFlat ((Double, Double)
a : (Double, Double)
b : (Double, Double)
c : [(Double, Double)]
rest)
  | (Double, Double) -> Double
forall a b. (a, b) -> b
snd (Double, Double)
a Double -> Double -> Bool
forall a. Eq a => a -> a -> Bool
== (Double, Double) -> Double
forall a b. (a, b) -> b
snd (Double, Double)
b Bool -> Bool -> Bool
&& (Double, Double) -> Double
forall a b. (a, b) -> b
snd (Double, Double)
b Double -> Double -> Bool
forall a. Eq a => a -> a -> Bool
== (Double, Double) -> Double
forall a b. (a, b) -> b
snd (Double, Double)
c = [(Double, Double)] -> [(Double, Double)]
dropFlat ((Double, Double)
a (Double, Double) -> [(Double, Double)] -> [(Double, Double)]
forall a. a -> [a] -> [a]
: (Double, Double)
c (Double, Double) -> [(Double, Double)] -> [(Double, Double)]
forall a. a -> [a] -> [a]
: [(Double, Double)]
rest)
  | Bool
otherwise = (Double, Double)
a (Double, Double) -> [(Double, Double)] -> [(Double, Double)]
forall a. a -> [a] -> [a]
: [(Double, Double)] -> [(Double, Double)]
dropFlat ((Double, Double)
b (Double, Double) -> [(Double, Double)] -> [(Double, Double)]
forall a. a -> [a] -> [a]
: (Double, Double)
c (Double, Double) -> [(Double, Double)] -> [(Double, Double)]
forall a. a -> [a] -> [a]
: [(Double, Double)]
rest)
dropFlat [(Double, Double)]
ps = [(Double, Double)]
ps

-- | Reduce a line's points to at most four per pixel column (the first, lowest, highest and last, in their original order),
-- which draws the same line, but keeps the SVG small when there are many more points than pixels (eg a long register).
thinPoints :: [(Double, Double)] -> [(Double, Double)]
thinPoints :: [(Double, Double)] -> [(Double, Double)]
thinPoints = ([(Double, Double)] -> [(Double, Double)])
-> [[(Double, Double)]] -> [(Double, Double)]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap [(Double, Double)] -> [(Double, Double)]
forall {t} {a}. Ord t => [(a, t)] -> [(a, t)]
thin ([[(Double, Double)]] -> [(Double, Double)])
-> ([(Double, Double)] -> [[(Double, Double)]])
-> [(Double, Double)]
-> [(Double, Double)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((Double, Double) -> (Double, Double) -> Bool)
-> [(Double, Double)] -> [[(Double, Double)]]
forall a. (a -> a -> Bool) -> [a] -> [[a]]
groupBy (Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
(==) (Integer -> Integer -> Bool)
-> ((Double, Double) -> Integer)
-> (Double, Double)
-> (Double, Double)
-> Bool
forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
`on` (Double -> Integer
column (Double -> Integer)
-> ((Double, Double) -> Double) -> (Double, Double) -> Integer
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Double, Double) -> Double
forall a b. (a, b) -> a
fst))
  where
    column :: Double -> Integer
    column :: Double -> Integer
column = Double -> Integer
forall b. Integral b => Double -> b
forall a b. (RealFrac a, Integral b) => a -> b
round
    thin :: [(a, t)] -> [(a, t)]
thin [(a, t)]
ps
      | [(a, t)] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [(a, t)]
ps Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
4 = [(a, t)]
ps
      | Bool
otherwise = Map Int (a, t) -> [(a, t)]
forall k a. Map k a -> [a]
M.elems (Map Int (a, t) -> [(a, t)]) -> Map Int (a, t) -> [(a, t)]
forall a b. (a -> b) -> a -> b
$ [(Int, (a, t))] -> Map Int (a, t)
forall k a. Ord k => [(k, a)] -> Map k a
M.fromList ([(Int, (a, t))] -> Map Int (a, t))
-> [(Int, (a, t))] -> Map Int (a, t)
forall a b. (a -> b) -> a -> b
$ [Maybe (Int, (a, t))] -> [(Int, (a, t))]
forall a. [Maybe a] -> [a]
catMaybes [[(Int, (a, t))] -> Maybe (Int, (a, t))
forall a. [a] -> Maybe a
headMay [(Int, (a, t))]
indexed, (t -> t -> Bool) -> Maybe (Int, (a, t))
extreme t -> t -> Bool
forall a. Ord a => a -> a -> Bool
(<), (t -> t -> Bool) -> Maybe (Int, (a, t))
extreme t -> t -> Bool
forall a. Ord a => a -> a -> Bool
(>), [(Int, (a, t))] -> Maybe (Int, (a, t))
forall a. [a] -> Maybe a
lastMay [(Int, (a, t))]
indexed]
      where
        indexed :: [(Int, (a, t))]
indexed = [Int] -> [(a, t)] -> [(Int, (a, t))]
forall a b. [a] -> [b] -> [(a, b)]
zip [Int
0 :: Int ..] [(a, t)]
ps
        -- the first point whose y is better than all others' by this comparison
        extreme :: (t -> t -> Bool) -> Maybe (Int, (a, t))
extreme t -> t -> Bool
better = (Maybe (Int, (a, t)) -> (Int, (a, t)) -> Maybe (Int, (a, t)))
-> Maybe (Int, (a, t)) -> [(Int, (a, t))] -> Maybe (Int, (a, t))
forall b a. (b -> a -> b) -> b -> [a] -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' (\Maybe (Int, (a, t))
acc (Int, (a, t))
p -> case Maybe (Int, (a, t))
acc of
                                   Just (Int
_, (a
_, t
y')) | Bool -> Bool
not ((a, t) -> t
forall a b. (a, b) -> b
snd ((Int, (a, t)) -> (a, t)
forall a b. (a, b) -> b
snd (Int, (a, t))
p) t -> t -> Bool
`better` t
y') -> Maybe (Int, (a, t))
acc
                                   Maybe (Int, (a, t))
_ -> (Int, (a, t)) -> Maybe (Int, (a, t))
forall a. a -> Maybe a
Just (Int, (a, t))
p) Maybe (Int, (a, t))
forall a. Maybe a
Nothing [(Int, (a, t))]
indexed

-- | Up to 8 ticks for a date axis from the first to the last date, at calendar boundaries
-- (every 1, 2, 7 or 14 days, every 1, 2, 3 or 6 months, or every 1, 2, 5, 10.. years), with labels.
dateTicks :: Day -> Day -> [(Day, Text)]
dateTicks :: Day -> Day -> [(Day, Text)]
dateTicks Day
d0 Day
d1 = [(Day, Text)] -> Maybe [(Day, Text)] -> [(Day, Text)]
forall a. a -> Maybe a -> a
fromMaybe [] (Maybe [(Day, Text)] -> [(Day, Text)])
-> Maybe [(Day, Text)] -> [(Day, Text)]
forall a b. (a -> b) -> a -> b
$ ([(Day, Text)] -> Bool) -> [[(Day, Text)]] -> Maybe [(Day, Text)]
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Maybe a
find ((Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
8) (Int -> Bool) -> ([(Day, Text)] -> Int) -> [(Day, Text)] -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [(Day, Text)] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length) [[(Day, Text)]]
candidates
  where
    (Integer
y0, Int
_, Int
_) = Day -> (Integer, Int, Int)
toGregorian Day
d0
    (Integer
y1, Int
_, Int
_) = Day -> (Integer, Int, Int)
toGregorian Day
d1
    candidates :: [[(Day, Text)]]
candidates = [Integer -> [(Day, Text)]
dayticks Integer
k | Integer
k <- [Integer
1, Integer
2, Integer
7, Integer
14]] [[(Day, Text)]] -> [[(Day, Text)]] -> [[(Day, Text)]]
forall a. [a] -> [a] -> [a]
++ [Int -> [(Day, Text)]
monthticks Int
k | Int
k <- [Int
1, Int
2, Int
3, Int
6]] [[(Day, Text)]] -> [[(Day, Text)]] -> [[(Day, Text)]]
forall a. [a] -> [a] -> [a]
++ [Integer -> [(Day, Text)]
yearticks Integer
k | Integer
k <- [Integer
1, Integer
2, Integer
5, Integer
10, Integer
20, Integer
50, Integer
100, Integer
1000]]
    inrange :: Day -> Bool
inrange Day
d = Day
d Day -> Day -> Bool
forall a. Ord a => a -> a -> Bool
>= Day
d0 Bool -> Bool -> Bool
&& Day
d Day -> Day -> Bool
forall a. Ord a => a -> a -> Bool
<= Day
d1
    dayticks :: Integer -> [(Day, Text)]
dayticks Integer
k = [(Day
d, Day -> Text
showDate Day
d) | Day
d <- (Day -> Bool) -> [Day] -> [Day]
forall a. (a -> Bool) -> [a] -> [a]
takeWhile (Day -> Day -> Bool
forall a. Ord a => a -> a -> Bool
<= Day
d1) ([Day] -> [Day]) -> [Day] -> [Day]
forall a b. (a -> b) -> a -> b
$ (Day -> Day) -> Day -> [Day]
forall a. (a -> a) -> a -> [a]
iterate (Integer -> Day -> Day
addDays Integer
k) Day
d0]
    monthticks :: Int -> [(Day, Text)]
monthticks Int
k = [ (Day
d, [Char] -> Text
T.pack ([Char] -> Text) -> [Char] -> Text
forall a b. (a -> b) -> a -> b
$ [Char] -> Integer -> Int -> [Char]
forall r. PrintfType r => [Char] -> r
printf [Char]
"%04d-%02d" Integer
y Int
m)
                   | Integer
y <- [Integer
y0 .. Integer
y1], Int
m <- [Int
1 .. Int
12], (Int
m Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) Int -> Int -> Int
forall a. Integral a => a -> a -> a
`mod` Int
k Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0, let d :: Day
d = Integer -> Int -> Int -> Day
fromGregorian Integer
y Int
m Int
1, Day -> Bool
inrange Day
d ]
    yearticks :: Integer -> [(Day, Text)]
yearticks Integer
k = [ (Day
d, [Char] -> Text
T.pack ([Char] -> Text) -> [Char] -> Text
forall a b. (a -> b) -> a -> b
$ Integer -> [Char]
forall a. Show a => a -> [Char]
show Integer
y) | Integer
y <- [Integer
y0 .. Integer
y1], Integer
y Integer -> Integer -> Integer
forall a. Integral a => a -> a -> a
`mod` Integer
k Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
== Integer
0, let d :: Day
d = Integer -> Int -> Int -> Day
fromGregorian Integer
y Int
1 Int
1, Day -> Bool
inrange Day
d ]

-- | A series' more lines for its tooltips (eg the accounts an "other" series combines), each after a newline.
tipLines :: Series -> Text
tipLines :: Series -> Text
tipLines Series
s = [Text] -> Text
T.concat [Text
"\n" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
l | Text
l <- ChartLabel -> [Text]
lbTipLines (ChartLabel -> [Text]) -> ChartLabel -> [Text]
forall a b. (a -> b) -> a -> b
$ Series -> ChartLabel
seLabel Series
s]

-- | Give some markup a tooltip, by grouping it with an SVG title.
withTooltip :: Text -> Svg -> Svg
withTooltip :: Text -> Svg -> Svg
withTooltip Text
t Svg
m = Svg -> Svg
S.g (Svg -> Svg) -> Svg -> Svg
forall a b. (a -> b) -> a -> b
$ Svg -> Svg
S.title (Text -> Svg
forall a. ToMarkup a => a -> Svg
S.toMarkup Text
t) Svg -> Svg -> Svg
forall a b. MarkupM a -> MarkupM b -> MarkupM b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Svg
m

-- | An SVG path for a bar (segment) growing from a base line, with its far end rounded (radius up to 4px) if requested.
-- Vertical: base line at y=b, from x=a with the given width, growing by len pixels (negative is upward, since y grows downward).
-- Horizontal: base line at x=a, from y=b with the given thickness, growing by len pixels (positive is rightward).
barPath :: Bool -> Double -> Double -> Double -> Double -> Bool -> Text
barPath :: Bool -> Double -> Double -> Double -> Double -> Bool -> Text
barPath Bool
vertical Double
a Double
b Double
size Double
len Bool
rounded
  | Bool
vertical =
      let e :: Double
e = Double
b Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
len in [Text] -> Text
T.unwords
      [ Text
"M", Double -> Text
num Double
a, Double -> Text
num Double
b
      , Text
"V", Double -> Text
num (Double
e Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
sgn Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
r)
      , Text
"Q", Double -> Text
num Double
a, Double -> Text
num Double
e, Double -> Text
num (Double
a Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
r), Double -> Text
num Double
e
      , Text
"H", Double -> Text
num (Double
a Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
size Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
r)
      , Text
"Q", Double -> Text
num (Double
a Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
size), Double -> Text
num Double
e, Double -> Text
num (Double
a Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
size), Double -> Text
num (Double
e Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
sgn Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
r)
      , Text
"V", Double -> Text
num Double
b, Text
"Z" ]
  | Bool
otherwise =
      let e :: Double
e = Double
a Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
len in [Text] -> Text
T.unwords
      [ Text
"M", Double -> Text
num Double
a, Double -> Text
num Double
b
      , Text
"H", Double -> Text
num (Double
e Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
sgn Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
r)
      , Text
"Q", Double -> Text
num Double
e, Double -> Text
num Double
b, Double -> Text
num Double
e, Double -> Text
num (Double
b Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
r)
      , Text
"V", Double -> Text
num (Double
b Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
size Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
r)
      , Text
"Q", Double -> Text
num Double
e, Double -> Text
num (Double
b Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
size), Double -> Text
num (Double
e Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
sgn Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
r), Double -> Text
num (Double
b Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
size)
      , Text
"H", Double -> Text
num Double
a, Text
"Z" ]
  where
    sgn :: Double
sgn = if Double
len Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
< Double
0 then -Double
1 else Double
1
    r :: Double
r = if Bool
rounded then [Double] -> Double
forall a. Ord a => [a] -> a
forall (t :: * -> *) a. (Foldable t, Ord a) => t a -> a
minimum [Double
3, Double
size Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Double
2, Double -> Double
forall a. Num a => a -> a
abs Double
len] else Double
0

-- | The scale for a panel's values, given their range (which should include 0), divided into about this many intervals:
-- a nice scale covering it ('niceScale'), or with a fixed scale, exactly the range it spans ('scaleSpans'),
-- with the nice scale's ticks that are within it.
panelScale :: Int -> Maybe Quantity -> Double -> Double -> (Double, Double, [Double], Int)
panelScale :: Int
-> Maybe Quantity
-> Double
-> Double
-> (Double, Double, [Double], Int)
panelScale Int
intervals Maybe Quantity
mscale Double
datamin Double
datamax = case Maybe Quantity
mscale of
  Just Quantity
s | Quantity
s Quantity -> Quantity -> Bool
forall a. Ord a => a -> a -> Bool
> Quantity
0 ->
    let (Double
hi, Double
neg) = Double -> Double -> Double -> (Double, Double)
forall a. (Ord a, Fractional a) => a -> a -> a -> (a, a)
scaleSpans (Quantity -> Double
toD Quantity
s) (Double -> Double -> Double
forall a. Ord a => a -> a -> a
max Double
0 Double
datamax) (Double -> Double
forall a. Num a => a -> a
abs (Double -> Double) -> Double -> Double
forall a b. (a -> b) -> a -> b
$ Double -> Double -> Double
forall a. Ord a => a -> a -> a
min Double
0 Double
datamin)
        lo :: Double
lo = Double -> Double
forall a. Num a => a -> a
negate Double
neg
        (Double
_, Double
_, [Double]
ticks, Int
decimals) = Int -> Double -> Double -> (Double, Double, [Double], Int)
niceScale Int
intervals Double
lo Double
hi
        eps :: Double
eps = (Double
hi Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
lo) Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
1e-9
    in (Double
lo, Double
hi, (Double -> Bool) -> [Double] -> [Double]
forall a. (a -> Bool) -> [a] -> [a]
filter (\Double
t -> Double
t Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
>= Double
lo Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
eps Bool -> Bool -> Bool
&& Double
t Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
<= Double
hi Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
eps) [Double]
ticks, Int
decimals)
  Maybe Quantity
_ -> Int -> Double -> Double -> (Double, Double, [Double], Int)
niceScale Int
intervals Double
datamin Double
datamax

-- | Limit a value to a range.
clamp :: Double -> Double -> Double -> Double
clamp :: Double -> Double -> Double -> Double
clamp Double
lo Double
hi = Double -> Double -> Double
forall a. Ord a => a -> a -> a
max Double
lo (Double -> Double) -> (Double -> Double) -> Double -> Double
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Double -> Double -> Double
forall a. Ord a => a -> a -> a
min Double
hi

-- | A "nice" linear scale covering the given range (which should include 0), in about this many intervals:
-- its low and high ends, its ticks (at multiples of 1, 2 or 5 times a power of ten),
-- and the number of decimal places needed to show the ticks.
niceScale :: Int -> Double -> Double -> (Double, Double, [Double], Int)
niceScale :: Int -> Double -> Double -> (Double, Double, [Double], Int)
niceScale Int
intervals Double
lo0 Double
hi0 = (Double
lo, Double
hi, [Double]
ticks, Int
decimals)
  where
    (Double
a, Double
b) = if Double
hi0 Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
lo0 Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
<= Double
0 then (Double
lo0, Double
lo0 Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
1) else (Double
lo0, Double
hi0)
    -- choose the 1, 2 or 5 times a power of ten nearest the step that would give that many intervals (as d3 does)
    rawstep :: Double
rawstep = (Double
b Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
a) Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Int -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
intervals
    magnitude :: Double
magnitude = Double
10 Double -> Double -> Double
forall a. Floating a => a -> a -> a
** Int -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Double -> Int
forall b. Integral b => Double -> b
forall a b. (RealFrac a, Integral b) => a -> b
floor (Double -> Double -> Double
forall a. Floating a => a -> a -> a
logBase Double
10 Double
rawstep) :: Int)
    err :: Double
err = Double
rawstep Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Double
magnitude
    step :: Double
step | Double
err Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
>= Double -> Double
forall a. Floating a => a -> a
sqrt Double
50 = Double
10 Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
magnitude
         | Double
err Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
>= Double -> Double
forall a. Floating a => a -> a
sqrt Double
10 = Double
5 Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
magnitude
         | Double
err Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
>= Double -> Double
forall a. Floating a => a -> a
sqrt Double
2  = Double
2 Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
magnitude
         | Bool
otherwise      = Double
magnitude
    kmin :: Integer
kmin = Double -> Integer
forall b. Integral b => Double -> b
forall a b. (RealFrac a, Integral b) => a -> b
floor (Double
a Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Double
step) :: Integer
    kmax :: Integer
kmax = Double -> Integer
forall b. Integral b => Double -> b
forall a b. (RealFrac a, Integral b) => a -> b
ceiling (Double
b Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Double
step) :: Integer
    ticks :: [Double]
ticks = [Integer -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral Integer
k Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
step | Integer
k <- [Integer
kmin .. Integer
kmax]]
    lo :: Double
lo = Integer -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral Integer
kmin Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
step
    hi :: Double
hi = Integer -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral Integer
kmax Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
step
    decimals :: Int
decimals = Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
0 (Int -> Int) -> Int -> Int
forall a b. (a -> b) -> a -> b
$ Int -> Int
forall a. Num a => a -> a
negate (Double -> Int
forall b. Integral b => Double -> b
forall a b. (RealFrac a, Integral b) => a -> b
floor (Double -> Double -> Double
forall a. Floating a => a -> a -> a
logBase Double
10 Double
step Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
1e-9) :: Int)

-- | The value of a tick, as a quantity with the given number of decimal places.
-- | A y axis tick's label: its value in the unit's style, with this many decimal places.
-- To reduce clutter, the unit's commodity is shown only on the outermost of these ticks
-- (the top one, or the bottom one if the top one is zero).
tickLabel :: Amount -> Int -> [Double] -> Double -> Text
tickLabel :: Amount -> Int -> [Double] -> Double -> Text
tickLabel Amount
unit Int
decimals [Double]
ticks Double
t = Amount -> Quantity -> Text
showValue (Int -> Amount -> Amount
withPrecision Int
decimals Amount
u) (Int -> Double -> Quantity
tickQuantity Int
decimals Double
t)
  where
    u :: Amount
u = if Double -> Maybe Double
forall a. a -> Maybe a
Just Double
t Maybe Double -> Maybe Double -> Bool
forall a. Eq a => a -> a -> Bool
== Maybe Double
unittick then Amount
unit else Amount
unit{acommodity = ""}
    unittick :: Maybe Double
unittick = case [Double] -> Maybe Double
forall a. [a] -> Maybe a
lastMay [Double]
ticks of
      Just Double
top | Double
top Double -> Double -> Bool
forall a. Eq a => a -> a -> Bool
/= Double
0 -> Double -> Maybe Double
forall a. a -> Maybe a
Just Double
top
      Maybe Double
_                   -> [Double] -> Maybe Double
forall a. [a] -> Maybe a
headMay [Double]
ticks

tickQuantity :: Int -> Double -> Quantity
tickQuantity :: Int -> Double -> Quantity
tickQuantity Int
decimals = Word8 -> Double -> Quantity
forall i r. (Integral i, RealFrac r) => Word8 -> r -> DecimalRaw i
realFracToDecimal (Int -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
decimals)

-- | Show a quantity as an amount in the given unit (commodity and style).
showValue :: Amount -> Quantity -> Text
showValue :: Amount -> Quantity -> Text
showValue Amount
unit Quantity
q = [Char] -> Text
T.pack ([Char] -> Text) -> [Char] -> Text
forall a b. (a -> b) -> a -> b
$ AmountFormat -> Amount -> [Char]
showAmountWith AmountFormat
oneLineNoCostFmt Amount
unit{aquantity = q}

-- | The unit with its display precision set to this many decimal places.
withPrecision :: Int -> Amount -> Amount
withPrecision :: Int -> Amount -> Amount
withPrecision Int
d Amount
unit = Amount
unit{astyle = (astyle unit){asprecision = Precision (fromIntegral d)}}

toD :: Quantity -> Double
toD :: Quantity -> Double
toD = Quantity -> Double
forall a b. (Real a, Fractional b) => a -> b
realToFrac

-- | A coordinate for SVG, rounded to one decimal place, without a trailing ".0".
num :: Double -> Text
num :: Double -> Text
num Double
x
  | Double
r Double -> Double -> Bool
forall a. Eq a => a -> a -> Bool
== Double
0    = Text
"0"
  | Bool
otherwise = let t :: Text
t = [Char] -> Text
T.pack ([Char] -> Double -> [Char]
forall r. PrintfType r => [Char] -> r
printf [Char]
"%.1f" Double
r) in Text -> Maybe Text -> Text
forall a. a -> Maybe a -> a
fromMaybe Text
t (Text -> Text -> Maybe Text
T.stripSuffix Text
".0" Text
t)
  where r :: Double
r = Integer -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Double -> Integer
forall b. Integral b => Double -> b
forall a b. (RealFrac a, Integral b) => a -> b
round (Double
x Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
10) :: Integer) Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Double
10 :: Double

-- | The runs of consecutive Just values in a list.
runs :: [Maybe a] -> [[a]]
runs :: forall a. [Maybe a] -> [[a]]
runs [Maybe a]
ms = case (Maybe a -> Bool) -> [Maybe a] -> [Maybe a]
forall a. (a -> Bool) -> [a] -> [a]
dropWhile Maybe a -> Bool
forall a. Maybe a -> Bool
isNothing [Maybe a]
ms of
  [] -> []
  [Maybe a]
rest -> let ([Maybe a]
run, [Maybe a]
more) = (Maybe a -> Bool) -> [Maybe a] -> ([Maybe a], [Maybe a])
forall a. (a -> Bool) -> [a] -> ([a], [a])
span (Bool -> Bool
not (Bool -> Bool) -> (Maybe a -> Bool) -> Maybe a -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Maybe a -> Bool
forall a. Maybe a -> Bool
isNothing) [Maybe a]
rest in [Maybe a] -> [a]
forall a. [Maybe a] -> [a]
catMaybes [Maybe a]
run [a] -> [[a]] -> [[a]]
forall a. a -> [a] -> [a]
: [Maybe a] -> [[a]]
forall a. [Maybe a] -> [[a]]
runs [Maybe a]
more

-- | The CSS class for a colour. (The rules are in 'chartStylesheet'.)
colourClass :: Colour -> Text
colourClass :: Colour -> Text
colourClass Colour
c = case Colour
c of
  TypeColour AccountType
at Int
k -> Text
"t" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> AccountType -> Text
typeCode (AccountType -> AccountType
mainAccountType AccountType
at) Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> [Char] -> Text
T.pack (Int -> [Char]
forall a. Show a => a -> [Char]
show (Int
k Int -> Int -> Int
forall a. Integral a => a -> a -> a
`mod` Int
maxTints))
  IndexColour Int
k   -> Text
"s" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> [Char] -> Text
T.pack (Int -> [Char]
forall a. Show a => a -> [Char]
show (Int
k Int -> Int -> Int
forall a. Integral a => a -> a -> a
`mod` Int
8))
  Colour
LoneColour      -> Text
"lone"
  Colour
NeutralColour   -> Text
"neutral"
  OtherColour Maybe AccountType
Nothing   -> Text
"other"
  OtherColour (Just AccountType
at) -> Text
"o" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> AccountType -> Text
typeCode (AccountType -> AccountType
mainAccountType AccountType
at)
  Colour
TotalColour     -> Text
"total"

-- | A short code for each main account type, for CSS class names.
typeCode :: AccountType -> Text
typeCode :: AccountType -> Text
typeCode AccountType
at = case AccountType
at of
  AccountType
Asset     -> Text
"a"
  AccountType
Liability -> Text
"l"
  AccountType
Equity    -> Text
"e"
  AccountType
Revenue   -> Text
"r"
  AccountType
Expense   -> Text
"x"
  AccountType
_         -> Text
"e"

-- | The chart's styles: a colour-blind-safe categorical palette (8 slots) and chart chrome colours,
-- with a dark variant used when the viewer prefers a dark colour scheme.
-- Colours are written literally in each rule (rather than as CSS custom properties),
-- since some SVG renderers (eg librsvg, used by many image viewers) don't support those.
-- Charts include these styles, unless drawn without them for a page that supplies them (see 'SvgOptions').
chartStylesheet :: Bool -> Text
chartStylesheet :: Bool -> Text
chartStylesheet = Bool -> Bool -> Text
stylesheet Bool
True

-- | The chart's styles, with the shading's or without them, as a chart includes them
-- when it is shaded as of today or not.
stylesheet :: Bool -> Bool -> Text
stylesheet :: Bool -> Bool -> Text
stylesheet Bool
shaded Bool
dark = [Text] -> Text
T.unlines ([Text] -> Text) -> [Text] -> Text
forall a b. (a -> b) -> a -> b
$
  [ Text
".hledger-chart{max-width:100%;height:auto;font-family:system-ui,-apple-system,\"Segoe UI\",sans-serif;font-size:14px}"
  , Text
".hledger-chart .title{font-size:17px;font-weight:600}"
  , Text
".hledger-chart .heading{font-size:15px;font-weight:600}"
  , Text
".hledger-chart .value,.hledger-chart .tick{font-variant-numeric:tabular-nums}"
  , Text
".hledger-chart .vlabel{font-size:12px}"
  , Text
".hledger-chart .grid,.hledger-chart .axis{stroke-width:1}"
  , Text
".hledger-chart .line{fill:none;stroke-width:3;stroke-linejoin:round;stroke-linecap:round}"
  , Text
".hledger-chart .mark{stroke-width:2.5}"
  , Text
".hledger-chart .average{stroke-width:2;stroke-dasharray:5 4}"
  , Text
".hledger-chart .hit,.hledger-chart .plot{fill:transparent}"
  , Text
".hledger-chart .hit-line{fill:none;stroke:transparent;stroke-width:8;stroke-linecap:round}"
  , Text
".hledger-chart .hatch{stroke-width:2.5}"
  ]
  [Text] -> [Text] -> [Text]
forall a. [a] -> [a] -> [a]
++ Bool -> [(Text, Text)] -> [Text]
colourRules Bool
False [(Text, Text)]
lightcolours
  [Text] -> [Text] -> [Text]
forall a. [a] -> [a] -> [a]
++ (if Bool
dark then [Text
"@media (prefers-color-scheme:dark){"] [Text] -> [Text] -> [Text]
forall a. [a] -> [a] -> [a]
++ Bool -> [(Text, Text)] -> [Text]
colourRules Bool
True [(Text, Text)]
darkcolours [Text] -> [Text] -> [Text]
forall a. [a] -> [a] -> [a]
++ [Text
"}"] else [])
  where
    -- the chart chrome's colours, and the series' colours (from "Hledger.Write.Chart.Palette")
    colourRules :: Bool -> [(Text, Text)] -> [Text]
colourRules Bool
isdark [(Text, Text)]
cs =
      [ Text
".hledger-chart " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
sel Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"{" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
prop Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
":" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
colour Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"}"
      | (Text
sel, Text
prop, Text
role) <- [(Text, Text, Text)]
rules, Just Text
colour <- [Text -> [(Text, Text)] -> Maybe Text
forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup Text
role [(Text, Text)]
cs] ]
      [Text] -> [Text] -> [Text]
forall a. [a] -> [a] -> [a]
++ [[Text]] -> [Text]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [ [ Text
".hledger-chart .fill." Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
cls Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"{fill:" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
hex Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"}", Text
".hledger-chart .line." Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
cls Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"{stroke:" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
hex Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"}" ]
                | Colour
c <- [Colour]
seriescolours, let cls :: Text
cls = Colour -> Text
colourClass Colour
c, let hex :: Text
hex = Bool -> Colour -> Text
colourHex Bool
isdark Colour
c ]
      -- value labels' gains and losses (see 'vlabelClass')
      [Text] -> [Text] -> [Text]
forall a. [a] -> [a] -> [a]
++ [ Text
".hledger-chart .vlabel." Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
cls Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"{fill:" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Bool -> Colour -> Text
colourHex Bool
isdark (AccountType -> Int -> Colour
TypeColour AccountType
at Int
0) Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"}"
         | (Text
cls, AccountType
at) <- [(Text
"gain", AccountType
Revenue), (Text
"loss", AccountType
Expense)] ]
    seriescolours :: [Colour]
seriescolours =
      [AccountType -> Int -> Colour
TypeColour AccountType
at Int
k | AccountType
at <- [AccountType
Asset, AccountType
Liability, AccountType
Equity, AccountType
Revenue, AccountType
Expense], Int
k <- [Int
0 .. Int
maxTints Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1]]
      [Colour] -> [Colour] -> [Colour]
forall a. [a] -> [a] -> [a]
++ (Int -> Colour) -> [Int] -> [Colour]
forall a b. (a -> b) -> [a] -> [b]
map Int -> Colour
IndexColour [Int
0 .. Int
7] [Colour] -> [Colour] -> [Colour]
forall a. [a] -> [a] -> [a]
++ [Maybe AccountType -> Colour
OtherColour Maybe AccountType
t | Maybe AccountType
t <- Maybe AccountType
forall a. Maybe a
Nothing Maybe AccountType -> [Maybe AccountType] -> [Maybe AccountType]
forall a. a -> [a] -> [a]
: (AccountType -> Maybe AccountType)
-> [AccountType] -> [Maybe AccountType]
forall a b. (a -> b) -> [a] -> [b]
map AccountType -> Maybe AccountType
forall a. a -> Maybe a
Just [AccountType
Asset, AccountType
Liability, AccountType
Equity, AccountType
Revenue, AccountType
Expense]] [Colour] -> [Colour] -> [Colour]
forall a. [a] -> [a] -> [a]
++ [Colour
LoneColour, Colour
NeutralColour]
    rules :: [(Text, Text, Text)]
rules =
      [ (Text
".bg", Text
"fill", Text
"surface"), (Text
".title", Text
"fill", Text
"ink"), (Text
".heading", Text
"fill", Text
"ink")
      , (Text
".label", Text
"fill", Text
"ink2"), (Text
".value", Text
"fill", Text
"ink2"), (Text
".tick", Text
"fill", Text
"muted"), (Text
".note", Text
"fill", Text
"muted")
      , (Text
".grid", Text
"stroke", Text
"grid"), (Text
".axis", Text
"stroke", Text
"axis")
      , (Text
".line.total", Text
"stroke", Text
"ink"), (Text
".mark.total", Text
"stroke", Text
"ink"), (Text
".average", Text
"stroke", Text
"ink"), (Text
".clip", Text
"fill", Text
"ink"), (Text
".hatch", Text
"stroke", Text
"surface")
      ]
      -- the shading as of today (see 'SvgOptions'), tinted with the expenses' red where values are below zero
      [(Text, Text, Text)]
-> [(Text, Text, Text)] -> [(Text, Text, Text)]
forall a. [a] -> [a] -> [a]
++ (if Bool
shaded
          then [ (Text
".shade.past-negative", Text
"fill", Text
"pastnegative"), (Text
".shade.future", Text
"fill", Text
"future"), (Text
".shade.future-negative", Text
"fill", Text
"futurenegative")
               , (Text
".axis.zero", Text
"stroke", Text
"zero") ]
          else [])
    lightcolours :: [(Text, Text)]
    lightcolours :: [(Text, Text)]
lightcolours =
      [ (Text
"surface", Text
"#fcfcfb"), (Text
"ink", Text
"#0b0b0b"), (Text
"ink2", Text
"#52514e"), (Text
"muted", Text
"#898781"), (Text
"grid", Text
"#e1e0d9"), (Text
"axis", Text
"#c3c2b7")
      , (Text
"pastnegative", Text
"#fbe4e4"), (Text
"future", Text
"#f1f0ec"), (Text
"futurenegative", Text
"#f0dcda"), (Text
"zero", Text
"#e34948") ]
    darkcolours :: [(Text, Text)]
darkcolours =
      [ (Text
"surface", Text
"#1a1a19"), (Text
"ink", Text
"#ffffff"), (Text
"ink2", Text
"#c3c2b7"), (Text
"muted", Text
"#898781"), (Text
"grid", Text
"#2c2c2a"), (Text
"axis", Text
"#383835")
      , (Text
"pastnegative", Text
"#432929"), (Text
"future", Text
"#252524"), (Text
"futurenegative", Text
"#3a2a28"), (Text
"zero", Text
"#e66767") ]

tests_Hledger_Write_Chart_Svg :: TestTree
tests_Hledger_Write_Chart_Svg :: TestTree
tests_Hledger_Write_Chart_Svg = [Char] -> [TestTree] -> TestTree
testGroup [Char]
"Write.Chart.Svg" [
   [Char] -> Assertion -> TestTree
testCase [Char]
"num" (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$ do
     (Double -> Text) -> [Double] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map Double -> Text
num [Double
0, -Double
0.04, Double
1, Double
1.26, -Double
3.14159, Double
760] [Text] -> [Text] -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= [Text
"0", Text
"0", Text
"1", Text
"1.3", Text
"-3.1", Text
"760"]
  ,[Char] -> Assertion -> TestTree
testCase [Char]
"niceScale" (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$ do
     Int -> Double -> Double -> (Double, Double, [Double], Int)
niceScale Int
5 Double
0 Double
87 (Double, Double, [Double], Int)
-> (Double, Double, [Double], Int) -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= (Double
0, Double
100, [Double
0, Double
20, Double
40, Double
60, Double
80, Double
100], Int
0)
     Int -> Double -> Double -> (Double, Double, [Double], Int)
niceScale Int
5 (-Double
15) Double
100 (Double, Double, [Double], Int)
-> (Double, Double, [Double], Int) -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= (-Double
20, Double
100, [-Double
20, Double
0, Double
20, Double
40, Double
60, Double
80, Double
100], Int
0)
     Int -> Double -> Double -> (Double, Double, [Double], Int)
niceScale Int
5 Double
0 Double
0 (Double, Double, [Double], Int)
-> (Double, Double, [Double], Int) -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= (Double
0, Double
1, [Double
0, Double
0.2, Double
0.4, Double
0.6000000000000001, Double
0.8, Double
1], Int
1)
     Int -> Double -> Double -> (Double, Double, [Double], Int)
niceScale Int
5 Double
0 Double
7.3 (Double, Double, [Double], Int)
-> (Double, Double, [Double], Int) -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= (Double
0, Double
8, [Double
0, Double
2, Double
4, Double
6, Double
8], Int
0)
     -- fewer intervals, for a shorter plot
     Int -> Double -> Double -> (Double, Double, [Double], Int)
niceScale Int
5 Double
0 Double
117 (Double, Double, [Double], Int)
-> (Double, Double, [Double], Int) -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= (Double
0, Double
120, [Double
0, Double
20, Double
40, Double
60, Double
80, Double
100, Double
120], Int
0)
     Int -> Double -> Double -> (Double, Double, [Double], Int)
niceScale Int
3 Double
0 Double
117 (Double, Double, [Double], Int)
-> (Double, Double, [Double], Int) -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= (Double
0, Double
150, [Double
0, Double
50, Double
100, Double
150], Int
0)
  ,[Char] -> Assertion -> TestTree
testCase [Char]
"plotIntervals" (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$ do
     (Double -> Int) -> [Double] -> [Int]
forall a b. (a -> b) -> [a] -> [b]
map Double -> Int
plotIntervals [Double
260, Double
150, Double
100, Double
40] [Int] -> [Int] -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= [Int
5, Int
3, Int
2, Int
2]
  ,[Char] -> Assertion -> TestTree
testCase [Char]
"barPath" (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$ do
     Bool -> Double -> Double -> Double -> Double -> Bool -> Text
barPath Bool
True Double
10 Double
100 Double
20 (-Double
50) Bool
True Text -> Text -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= Text
"M 10 100 V 53 Q 10 50 13 50 H 27 Q 30 50 30 53 V 100 Z"
     Bool -> Double -> Double -> Double -> Double -> Bool -> Text
barPath Bool
False Double
10 Double
5 Double
12 (-Double
30) Bool
False Text -> Text -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= Text
"M 10 5 H -20 Q -20 5 -20 5 V 17 Q -20 17 -20 17 H 10 Z"
  ,[Char] -> Assertion -> TestTree
testCase [Char]
"a long line has tooltips all along it, at most one per 4px column" (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$ do
     let hits :: Int
hits = HasCallStack => Text -> Text -> Int
Text -> Text -> Int
T.count Text
"<circle class=\"hit" (Text -> Int) -> Text -> Int
forall a b. (a -> b) -> a -> b
$ SvgOptions -> Chart -> Text
svgText SvgOptions
defaultSvgOptions (Chart -> Text) -> Chart -> Text
forall a b. (a -> b) -> a -> b
$ Integer -> Chart
forall {a}. Integral a => a -> Chart
lineChart Integer
1000
     HasCallStack => [Char] -> Bool -> Assertion
[Char] -> Bool -> Assertion
assertBool ([Char]
"tooltip targets: " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ Int -> [Char]
forall a. Show a => a -> [Char]
show Int
hits) (Int
hits Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
150 Bool -> Bool -> Bool
&& Int
hits Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
220)
  ,[Char] -> Assertion -> TestTree
testCase [Char]
"a line by date has tooltips where it changes" (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$ do
     HasCallStack => Text -> Text -> Int
Text -> Text -> Int
T.count Text
"<circle class=\"hit" (SvgOptions -> Chart -> Text
svgText SvgOptions
defaultSvgOptions (Chart -> Text) -> Chart -> Text
forall a b. (a -> b) -> a -> b
$ [Quantity] -> Chart
lineChartOf [Quantity
1, Quantity
1, Quantity
1, Quantity
2, Quantity
2, Quantity
2]) Int -> Int -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= Int
2
  ,[Char] -> Assertion -> TestTree
testCase [Char]
"dropFlat" (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$ do
     [(Double, Double)] -> [(Double, Double)]
dropFlat [(Double
0, Double
1), (Double
1, Double
1), (Double
1, Double
1), (Double
2, Double
1), (Double
2, Double
2), (Double
3, Double
2)] [(Double, Double)] -> [(Double, Double)] -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= [(Double
0, Double
1), (Double
2, Double
1), (Double
2, Double
2), (Double
3, Double
2)]
  ,[Char] -> Assertion -> TestTree
testCase [Char]
"a line's points can link somewhere, out of the tab order" (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$ do
     let svg :: Text
svg = SvgOptions -> Chart -> Text
svgText SvgOptions
defaultSvgOptions{svgPointLink = \Int
j -> if Int -> Bool
forall a. Integral a => a -> Bool
even Int
j then Text -> Maybe Text
forall a. a -> Maybe a
Just (Text
"#t" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> [Char] -> Text
T.pack (Int -> [Char]
forall a. Show a => a -> [Char]
show Int
j)) else Maybe Text
forall a. Maybe a
Nothing} (Chart -> Text) -> Chart -> Text
forall a b. (a -> b) -> a -> b
$ Integer -> Chart
forall {a}. Integral a => a -> Chart
lineChart Integer
3
     HasCallStack => [Char] -> Bool -> Assertion
[Char] -> Bool -> Assertion
assertBool [Char]
"the third point links" (Bool -> Assertion) -> Bool -> Assertion
forall a b. (a -> b) -> a -> b
$ Text
"<a href=\"#t2\" tabindex=\"-1\">" Text -> Text -> Bool
`T.isInfixOf` Text
svg
     HasCallStack => [Char] -> Bool -> Assertion
[Char] -> Bool -> Assertion
assertBool [Char]
"the second point does not" (Bool -> Assertion) -> Bool -> Assertion
forall a b. (a -> b) -> a -> b
$ Bool -> Bool
not (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b
$ Text
"#t1" Text -> Text -> Bool
`T.isInfixOf` Text
svg
  ,[Char] -> Assertion -> TestTree
testCase [Char]
"a plot by date can give its date scale" (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$ do
     let svg :: Text
svg = SvgOptions -> Chart -> Text
svgText SvgOptions
defaultSvgOptions{svgDateScale = True} (Chart -> Text) -> Chart -> Text
forall a b. (a -> b) -> a -> b
$ Integer -> Chart
forall {a}. Integral a => a -> Chart
lineChart Integer
3
     HasCallStack => [Char] -> Bool -> Assertion
[Char] -> Bool -> Assertion
assertBool [Char]
"its first and last dates" (Bool -> Assertion) -> Bool -> Assertion
forall a b. (a -> b) -> a -> b
$ Text
"data-d0=\"2024-01-01\" data-d1=\"2024-01-03\"" Text -> Text -> Bool
`T.isInfixOf` Text
svg
  ,[Char] -> Assertion -> TestTree
testCase [Char]
"a chart can be drawn at another width, its plot filling it" (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$ do
     let svg :: Text
svg = SvgOptions -> Chart -> Text
svgText SvgOptions
defaultSvgOptions{svgWidth = 600, svgDateScale = True} (Chart -> Text) -> Chart -> Text
forall a b. (a -> b) -> a -> b
$ Integer -> Chart
forall {a}. Integral a => a -> Chart
lineChart Integer
3
     HasCallStack => [Char] -> Bool -> Assertion
[Char] -> Bool -> Assertion
assertBool [Char]
"its width" (Bool -> Assertion) -> Bool -> Assertion
forall a b. (a -> b) -> a -> b
$ Text
"viewBox=\"0 0 600 " Text -> Text -> Bool
`T.isInfixOf` Text
svg
     -- the last date is drawn 8px inside the plot's right edge, which is 16px inside the chart's
     HasCallStack => [Char] -> Bool -> Assertion
[Char] -> Bool -> Assertion
assertBool [Char]
"its plot" (Bool -> Assertion) -> Bool -> Assertion
forall a b. (a -> b) -> a -> b
$ Text
"data-x1=\"576\"" Text -> Text -> Bool
`T.isInfixOf` Text
svg
  ,[Char] -> Assertion -> TestTree
testCase [Char]
"a plot by date can be shaded as of today: the future, and the past below zero" (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$ do
     -- a line falling below zero, today being its second day
     let shading :: Day -> Text
shading Day
today = SvgOptions -> Chart -> Text
svgText SvgOptions
defaultSvgOptions{svgToday = Just today} (Chart -> Text) -> Chart -> Text
forall a b. (a -> b) -> a -> b
$ [Quantity] -> Chart
lineChartOf [Quantity
1, -Quantity
1, -Quantity
2]
         svg :: Text
svg = Day -> Text
shading (Integer -> Int -> Int -> Day
fromGregorian Integer
2024 Int
1 Int
2)
     HasCallStack => [Char] -> Bool -> Assertion
[Char] -> Bool -> Assertion
assertBool [Char]
"the past below zero" (Bool -> Assertion) -> Bool -> Assertion
forall a b. (a -> b) -> a -> b
$ Text
"class=\"shade past-negative\"" Text -> Text -> Bool
`T.isInfixOf` Text
svg
     HasCallStack => [Char] -> Bool -> Assertion
[Char] -> Bool -> Assertion
assertBool [Char]
"the future" (Bool -> Assertion) -> Bool -> Assertion
forall a b. (a -> b) -> a -> b
$ Text
"class=\"shade future\"" Text -> Text -> Bool
`T.isInfixOf` Text
svg
     HasCallStack => [Char] -> Bool -> Assertion
[Char] -> Bool -> Assertion
assertBool [Char]
"the future below zero" (Bool -> Assertion) -> Bool -> Assertion
forall a b. (a -> b) -> a -> b
$ Text
"class=\"shade future-negative\"" Text -> Text -> Bool
`T.isInfixOf` Text
svg
     HasCallStack => [Char] -> Bool -> Assertion
[Char] -> Bool -> Assertion
assertBool [Char]
"the zero line" (Bool -> Assertion) -> Bool -> Assertion
forall a b. (a -> b) -> a -> b
$ Text
"class=\"axis zero\"" Text -> Text -> Bool
`T.isInfixOf` Text
svg
     -- with today after the last day, it's all past
     HasCallStack => [Char] -> Bool -> Assertion
[Char] -> Bool -> Assertion
assertBool [Char]
"no future" (Bool -> Assertion) -> Bool -> Assertion
forall a b. (a -> b) -> a -> b
$ Bool -> Bool
not (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b
$ Text
"shade future" Text -> Text -> Bool
`T.isInfixOf` Day -> Text
shading (Integer -> Int -> Int -> Day
fromGregorian Integer
2024 Int
1 Int
9)
  ,[Char] -> Assertion -> TestTree
testCase [Char]
"a net total is marked across each period's bars, unless the periods are too narrow" (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$ do
     let marks :: Int -> Int
marks = HasCallStack => Text -> Text -> Int
Text -> Text -> Int
T.count Text
"class=\"mark total\"" (Text -> Int) -> (Int -> Text) -> Int -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SvgOptions -> Chart -> Text
svgText SvgOptions
defaultSvgOptions (Chart -> Text) -> (Int -> Chart) -> Int -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Mark -> Int -> Chart
netChart Mark
MarkMarker
     -- one per period, and one in the legend
     Int -> Int
marks Int
12 Int -> Int -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= Int
13
     -- at 880px, 100 periods are 8.8px each, too narrow: neither the marks nor their legend entry
     Int -> Int
marks Int
100 Int -> Int -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= Int
0
  ,[Char] -> Assertion -> TestTree
testCase [Char]
"a single period's net line is marked across its bars, since a line of one point would not show" (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$ do
     let svg :: Text
svg = SvgOptions -> Chart -> Text
svgText SvgOptions
defaultSvgOptions (Chart -> Text) -> Chart -> Text
forall a b. (a -> b) -> a -> b
$ Mark -> Int -> Chart
netChart Mark
MarkLine Int
1
     -- one across the bars, and one in the legend
     HasCallStack => Text -> Text -> Int
Text -> Text -> Int
T.count Text
"class=\"mark total\"" Text
svg Int -> Int -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= Int
2
     HasCallStack => [Char] -> Bool -> Assertion
[Char] -> Bool -> Assertion
assertBool [Char]
"no line" (Bool -> Assertion) -> Bool -> Assertion
forall a b. (a -> b) -> a -> b
$ Bool -> Bool
not (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b
$ Text
"line total" Text -> Text -> Bool
`T.isInfixOf` Text
svg
  ,[Char] -> Assertion -> TestTree
testCase [Char]
"a line over periods has tooltips all along it, each point's from halfway to its neighbours" (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$ do
     let svg :: Text
svg = SvgOptions -> Chart -> Text
svgText SvgOptions
defaultSvgOptions (Chart -> Text) -> Chart -> Text
forall a b. (a -> b) -> a -> b
$ Mark -> Int -> Chart
netChart Mark
MarkLine Int
3
         -- each target's path: from its point, a line to each end
         targets :: [Text]
targets = (Text -> Text) -> [Text] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map ((Char -> Bool) -> Text -> Text
T.takeWhile (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
/= Char
'"') (Text -> Text) -> (Text -> Text) -> Text -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Text -> Text
T.drop (Text -> Int
T.length Text
"d=\"")) ([Text] -> [Text]) -> [Text] -> [Text]
forall a b. (a -> b) -> a -> b
$ Int -> [Text] -> [Text]
forall a. Int -> [a] -> [a]
drop Int
1 ([Text] -> [Text]) -> [Text] -> [Text]
forall a b. (a -> b) -> a -> b
$ HasCallStack => Text -> Text -> [Text]
Text -> Text -> [Text]
T.splitOn Text
"class=\"hit-line total\" " Text
svg
     (Text -> Int) -> [Text] -> [Int]
forall a b. (a -> b) -> [a] -> [b]
map (HasCallStack => Text -> Text -> Int
Text -> Text -> Int
T.count Text
"M") [Text]
targets [Int] -> [Int] -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= [Int
1, Int
2, Int
1]
     HasCallStack => [Char] -> Bool -> Assertion
[Char] -> Bool -> Assertion
assertBool [Char]
"no circles" (Bool -> Assertion) -> Bool -> Assertion
forall a b. (a -> b) -> a -> b
$ Bool -> Bool
not (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b
$ Text
"class=\"hit" Text -> Text -> Bool
`T.isInfixOf` HasCallStack => Text -> Text -> Text -> Text
Text -> Text -> Text -> Text
T.replace Text
"hit-line" Text
"" Text
svg
  ,[Char] -> Assertion -> TestTree
testCase [Char]
"a chart's ids have the given prefix, and each of its panels drawn separately its own" (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$ do
     let svg :: Text
svg = SvgOptions -> Chart -> Text
svgText SvgOptions
defaultSvgOptions{svgIds = "x"} (Chart -> Text) -> Chart -> Text
forall a b. (a -> b) -> a -> b
$ Mark -> Int -> Chart
netChart Mark
MarkLine Int
2
     HasCallStack => [Char] -> Bool -> Assertion
[Char] -> Bool -> Assertion
assertBool [Char]
"the prefix" (Bool -> Assertion) -> Bool -> Assertion
forall a b. (a -> b) -> a -> b
$ Text
"id=\"x-clip-bar" Text -> Text -> Bool
`T.isInfixOf` Text
svg
     HasCallStack => [Char] -> Bool -> Assertion
[Char] -> Bool -> Assertion
assertBool [Char]
"not the default" (Bool -> Assertion) -> Bool -> Assertion
forall a b. (a -> b) -> a -> b
$ Bool -> Bool
not (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b
$ Text
"hledger-chart-clip" Text -> Text -> Bool
`T.isInfixOf` Text
svg
     let chart :: Chart
chart = Mark -> Int -> Chart
netChart Mark
MarkLine Int
2
         panels :: [Text]
panels = (Svg -> Text) -> [Svg] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map (Text -> Text
TL.toStrict (Text -> Text) -> (Svg -> Text) -> Svg -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Svg -> Text
renderSvg) ([Svg] -> [Text]) -> [Svg] -> [Text]
forall a b. (a -> b) -> a -> b
$ SvgOptions -> Chart -> [Svg]
chartPanelsSvgMarkupWith SvgOptions
defaultSvgOptions Chart
chart{chPanels = chPanels chart ++ chPanels chart}
     (Text -> Bool) -> [Text] -> [Bool]
forall a b. (a -> b) -> [a] -> [b]
map (Text
"id=\"hledger-chart-0-clip-bar" Text -> Text -> Bool
`T.isInfixOf`) [Text]
panels [Bool] -> [Bool] -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= [Bool
True, Bool
False]
     (Text -> Bool) -> [Text] -> [Bool]
forall a b. (a -> b) -> [a] -> [b]
map (Text
"id=\"hledger-chart-1-clip-bar" Text -> Text -> Bool
`T.isInfixOf`) [Text]
panels [Bool] -> [Bool] -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= [Bool
False, Bool
True]
  ,[Char] -> Assertion -> TestTree
testCase [Char]
"a line of one point is drawn as a short dash, so that it shows" (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$ do
     let points :: Chart -> Text
points = (Char -> Bool) -> Text -> Text
T.takeWhile (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
/= Char
'"') (Text -> Text) -> (Chart -> Text) -> Chart -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Text -> Text
T.drop (Text -> Int
T.length Text
"points=\"") (Text -> Text) -> (Chart -> Text) -> Chart -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Text, Text) -> Text
forall a b. (a, b) -> b
snd ((Text, Text) -> Text) -> (Chart -> (Text, Text)) -> Chart -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HasCallStack => Text -> Text -> (Text, Text)
Text -> Text -> (Text, Text)
T.breakOn Text
"points=\"" (Text -> (Text, Text)) -> (Chart -> Text) -> Chart -> (Text, Text)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SvgOptions -> Chart -> Text
svgText SvgOptions
defaultSvgOptions
     [Text] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length (Text -> [Text]
T.words (Text -> [Text]) -> Text -> [Text]
forall a b. (a -> b) -> a -> b
$ Chart -> Text
points (Chart -> Text) -> Chart -> Text
forall a b. (a -> b) -> a -> b
$ [Quantity] -> Chart
lineChartOf [Quantity
5]) Int -> Int -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= Int
2
  ]
  where
    svgText :: SvgOptions -> Chart -> Text
svgText SvgOptions
opts = Text -> Text
TL.toStrict (Text -> Text) -> (Chart -> Text) -> Chart -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Svg -> Text
renderSvg (Svg -> Text) -> (Chart -> Svg) -> Chart -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SvgOptions -> Chart -> Svg
chartSvgMarkupWith SvgOptions
opts
    -- a chart of one line, rising by 1 a day for n days from 2024-01-01
    lineChart :: a -> Chart
lineChart a
n = [Quantity] -> Chart
lineChartOf [a -> Quantity
forall a b. (Integral a, Num b) => a -> b
fromIntegral a
k | a
k <- [a
1 .. a
n]]
    -- a chart of one line, with these values on successive days from 2024-01-01
    lineChartOf :: [Quantity] -> Chart
lineChartOf [Quantity]
qs = Text -> XAxis -> [Panel] -> [Text] -> Chart
Chart Text
"" ([Day] -> XAxis
XDates ([Day] -> XAxis) -> [Day] -> XAxis
forall a b. (a -> b) -> a -> b
$ Int -> [Day] -> [Day]
forall a. Int -> [a] -> [a]
take ([Quantity] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Quantity]
qs) ([Day] -> [Day]) -> [Day] -> [Day]
forall a b. (a -> b) -> a -> b
$ (Day -> Day) -> Day -> [Day]
forall a. (a -> a) -> a -> [a]
iterate (Integer -> Day -> Day
addDays Integer
1) (Integer -> Int -> Int -> Day
fromGregorian Integer
2024 Int
1 Int
1))
      [Amount
-> [Series]
-> [Text]
-> Maybe Quantity
-> BarLayout
-> Bool
-> [Summary]
-> [Maybe Text]
-> Panel
Panel Amount
nullamt [ChartLabel
-> SeriesRole -> Mark -> [Maybe Quantity] -> Int -> Int -> Series
Series (Text
-> Text -> Int -> Maybe AccountType -> Bool -> [Text] -> ChartLabel
ChartLabel Text
"a" Text
"a" Int
0 Maybe AccountType
forall a. Maybe a
Nothing Bool
True []) SeriesRole
SeriesItem Mark
MarkLine ((Quantity -> Maybe Quantity) -> [Quantity] -> [Maybe Quantity]
forall a b. (a -> b) -> [a] -> [b]
map Quantity -> Maybe Quantity
forall a. a -> Maybe a
Just [Quantity]
qs) Int
0 Int
0] [] Maybe Quantity
forall a. Maybe a
Nothing BarLayout
Diverging Bool
False [] []] []
    -- a chart of n periods, each with a positive and a negative bar, and their net total, drawn with the given mark
    netChart :: Mark -> Int -> Chart
netChart Mark
mk Int
n = Text -> XAxis -> [Panel] -> [Text] -> Chart
Chart Text
"" ([(DateSpan, Text)] -> XAxis
XPeriods ([(DateSpan, Text)] -> XAxis) -> [(DateSpan, Text)] -> XAxis
forall a b. (a -> b) -> a -> b
$ Int -> (DateSpan, Text) -> [(DateSpan, Text)]
forall a. Int -> a -> [a]
replicate Int
n (DateSpan
nulldatespan, Text
"p"))
      [Amount
-> [Series]
-> [Text]
-> Maybe Quantity
-> BarLayout
-> Bool
-> [Summary]
-> [Maybe Text]
-> Panel
Panel Amount
nullamt [ ChartLabel
-> SeriesRole -> Mark -> [Maybe Quantity] -> Int -> Int -> Series
Series (Text
-> Text -> Int -> Maybe AccountType -> Bool -> [Text] -> ChartLabel
ChartLabel Text
"a" Text
"a" Int
0 Maybe AccountType
forall a. Maybe a
Nothing Bool
True []) SeriesRole
SeriesItem Mark
MarkBar (Int -> Maybe Quantity -> [Maybe Quantity]
forall a. Int -> a -> [a]
replicate Int
n (Quantity -> Maybe Quantity
forall a. a -> Maybe a
Just Quantity
3)) Int
0 Int
0
                     , ChartLabel
-> SeriesRole -> Mark -> [Maybe Quantity] -> Int -> Int -> Series
Series (Text
-> Text -> Int -> Maybe AccountType -> Bool -> [Text] -> ChartLabel
ChartLabel Text
"b" Text
"b" Int
0 Maybe AccountType
forall a. Maybe a
Nothing Bool
True []) SeriesRole
SeriesItem Mark
MarkBar (Int -> Maybe Quantity -> [Maybe Quantity]
forall a. Int -> a -> [a]
replicate Int
n (Quantity -> Maybe Quantity
forall a. a -> Maybe a
Just (-Quantity
1))) Int
0 Int
0
                     , ChartLabel
-> SeriesRole -> Mark -> [Maybe Quantity] -> Int -> Int -> Series
Series (Text
-> Text -> Int -> Maybe AccountType -> Bool -> [Text] -> ChartLabel
ChartLabel Text
"net" Text
"net" Int
0 Maybe AccountType
forall a. Maybe a
Nothing Bool
True []) SeriesRole
SeriesTotal Mark
mk (Int -> Maybe Quantity -> [Maybe Quantity]
forall a. Int -> a -> [a]
replicate Int
n (Quantity -> Maybe Quantity
forall a. a -> Maybe a
Just Quantity
2)) Int
0 Int
0
                     ] [] Maybe Quantity
forall a. Maybe a
Nothing BarLayout
Diverging Bool
False [] []] []