1
//! The one chart of a report: the adapter of the outcome's report, with the
2
//! options the web draws its charts with.
3

            
4
mod backend;
5
#[cfg(test)]
6
mod backend_tests;
7
mod render;
8
#[cfg(test)]
9
mod tests;
10

            
11
use finance::error::ReportError;
12
use plotting::adapters::{
13
    ActivityChartOpts, BalanceChartOpts, BreakdownChartOpts, activity_chart, balance_chart,
14
    breakdown_chart,
15
};
16
use plotting::{ChartKind, ChartSpec};
17
use server::command::report::view::{
18
    flatten_activity_data, flatten_breakdown_data, flatten_report_data,
19
};
20

            
21
use crate::forms::ReportOutcome;
22

            
23
pub use plotting::adapters::SortOrder;
24
pub const DEFAULT_WIDTH: u32 = 720;
25
pub const DEFAULT_HEIGHT: u32 = 360;
26

            
27
pub use backend::{ChartBackend, chart_backend, env_speaks_kitty, term_speaks_kitty};
28
pub use render::{ChartFormat, ChartLook, MAX_SIDE, kind_name, render_chart, render_svg_chart};
29

            
30
#[derive(Debug, Clone, Copy)]
31
pub struct ChartOpts {
32
    pub kind: ChartKind,
33
    pub sort: SortOrder,
34
    pub top_n: usize,
35
    pub include_net: bool,
36
}
37

            
38
impl Default for ChartOpts {
39
6585
    fn default() -> Self {
40
6585
        Self {
41
6585
            kind: ChartKind::Bar,
42
6585
            sort: SortOrder::MagnitudeDesc,
43
6585
            top_n: 10,
44
6585
            include_net: true,
45
6585
        }
46
6585
    }
47
}
48

            
49
/// The chart kind named `name`: `bar`, `line` or `stacked`.
50
#[must_use]
51
10288
pub fn parse_kind(name: &str) -> Option<ChartKind> {
52
10288
    match name.to_ascii_lowercase().as_str() {
53
6434
        "bar" => Some(ChartKind::Bar),
54
3365
        "line" => Some(ChartKind::Line),
55
326
        "stacked" | "stackedbar" => Some(ChartKind::StackedBar),
56
163
        _ => None,
57
    }
58
10288
}
59

            
60
/// `name`'s kind; a missing or unknown name is `Bar`.
61
#[must_use]
62
6564
pub fn kind_or_default(name: Option<&str>) -> ChartKind {
63
6564
    name.and_then(parse_kind).unwrap_or(ChartKind::Bar)
64
6564
}
65

            
66
/// Only `chart_series=groups` drops the Activity chart's Net series.
67
#[must_use]
68
963
pub fn series_includes_net(series: Option<&str>) -> bool {
69
963
    !series.is_some_and(|name| name.eq_ignore_ascii_case("groups"))
70
963
}
71

            
72
/// The chart of a report outcome.
73
///
74
/// # Errors
75
/// [`ReportError::AmountOverflow`] when an activity amount cannot be negated.
76
6585
pub fn chart_spec(outcome: &ReportOutcome, opts: &ChartOpts) -> Result<ChartSpec, ReportError> {
77
6585
    Ok(match outcome {
78
4011
        ReportOutcome::Balance(data) => balance_chart(
79
4011
            &flatten_report_data(data),
80
4011
            BalanceChartOpts {
81
4011
                kind: opts.kind,
82
4011
                top_n: opts.top_n,
83
4011
                sort_order: opts.sort,
84
4011
            },
85
4011
        ),
86
1927
        ReportOutcome::Activity(data) => activity_chart(
87
1927
            &flatten_activity_data(data)?,
88
1927
            ActivityChartOpts {
89
1927
                kind: opts.kind,
90
1927
                include_net: opts.include_net,
91
1927
            },
92
        ),
93
647
        ReportOutcome::Breakdown(data) => breakdown_chart(
94
647
            &flatten_breakdown_data(data),
95
647
            BreakdownChartOpts {
96
647
                kind: opts.kind,
97
647
                top_n: opts.top_n,
98
647
            },
99
647
        )?,
100
    })
101
6585
}