hledger-lib
Safe HaskellNone
LanguageGHC2021

Hledger.Write.Chart.Svg

Description

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).

Synopsis

Documentation

chartAsSvg :: Chart -> Text Source #

Render a chart as a complete SVG document, with a dark variant for viewers preferring a dark colour scheme.

chartAsSvgWith :: Bool -> Chart -> Text Source #

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.

chartSvgMarkup :: Bool -> Chart -> Svg Source #

A chart as SVG markup, which can also be embedded in HTML; with or without the dark variant.

data SvgOptions Source #

How to draw a chart as SVG.

Constructors

SvgOptions 

Fields

  • 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.

  • svgDark :: Bool

    include the styles' dark variant, for viewers preferring a dark colour scheme ?

  • 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.

  • 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.)

  • 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.

  • 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 ?

  • 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.

  • 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.

defaultSvgOptions :: SvgOptions Source #

A self-contained chart: with its styles, including their dark variant, 880px wide, plots 260px high, no links, no date scale, and no shading.

chartSvgMarkupWith :: SvgOptions -> Chart -> Svg Source #

A chart as SVG markup, which can also be embedded in HTML, drawn with these options.

chartPanelsSvgMarkupWith :: SvgOptions -> Chart -> [Svg] Source #

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.

chartStylesheet :: Bool -> Text Source #

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).