1
//! Text-relative sizes and bar slot geometry shared by every backend.
2

            
3
use plotters::prelude::RGBColor;
4

            
5
use crate::spec::ChartSpec;
6

            
7
/// The rendered height, in pixels, of label text at the sizes the layout was designed for.
8
/// plotters draws a font of size `n` about four fifths of `n` tall, so the designed
9
/// size 11 is 9 px of glyph.
10
pub const DEFAULT_TEXT_PX: u32 = 9;
11

            
12
/// Converts sizes designed for [`DEFAULT_TEXT_PX`] text to the text height drawn,
13
/// and holds the colour the axes, grid and text are drawn in.
14
#[derive(Debug, Clone, Copy)]
15
pub(crate) struct Metrics {
16
    text: u32,
17
    pub ink: RGBColor,
18
}
19

            
20
impl Metrics {
21
5534
    pub fn new(text_px: u32, ink: RGBColor) -> Self {
22
5534
        Self {
23
5534
            text: text_px.max(1),
24
5534
            ink,
25
5534
        }
26
5534
    }
27

            
28
    /// `base` pixels at the default text height, scaled to this one.
29
168796
    pub fn px(self, base: u32) -> u32 {
30
168796
        base.saturating_mul(self.text)
31
168796
            .saturating_add(DEFAULT_TEXT_PX / 2)
32
168796
            / DEFAULT_TEXT_PX
33
168796
    }
34

            
35
    /// The space between the axis and the top of the label under it: 0.4 of the text height.
36
12637
    pub fn gap(self) -> i32 {
37
12637
        i32::try_from(self.text.saturating_mul(2) / 5).unwrap_or(i32::MAX)
38
12637
    }
39

            
40
78976
    pub fn px_i32(self, base: u32) -> i32 {
41
78976
        i32::try_from(self.px(base)).unwrap_or(i32::MAX)
42
78976
    }
43
}
44

            
45
/// The share of a slot left empty on each side of its bars.
46
const SLOT_GAP: f64 = 1.0 / 7.0;
47

            
48
44365
fn units(count: usize) -> f64 {
49
44365
    f64::from(u32::try_from(count).unwrap_or(u32::MAX))
50
44365
}
51

            
52
/// How many bars each x label holds. A label is one unit wide on the x axis, so a label's
53
/// bars share the slot between its gaps and a lone bar fills it whatever the other labels hold.
54
pub(crate) struct Slots {
55
    counts: Vec<usize>,
56
}
57

            
58
impl Slots {
59
    /// One bar per label, as a line or a stacked bar has.
60
1007
    pub fn single(labels: usize) -> Self {
61
1007
        Self {
62
1007
            counts: vec![1; labels],
63
1007
        }
64
1007
    }
65

            
66
    /// The bars of each label are the series that have a point at it.
67
3527
    pub fn grouped(spec: &ChartSpec, labels: &[String]) -> Self {
68
3527
        let counts = labels
69
3527
            .iter()
70
9978
            .map(|label| {
71
9978
                spec.series
72
9978
                    .iter()
73
48771
                    .filter(|series| series.points.iter().any(|p| &p.x == label))
74
                    .count()
75
9978
                    .max(1)
76
9978
            })
77
            .collect();
78
3527
        Self { counts }
79
3527
    }
80

            
81
    /// The x axis length: one unit per label.
82
4533
    pub fn len(&self) -> f64 {
83
4533
        units(self.counts.len())
84
4533
    }
85

            
86
    /// The middle of the slot of `label`.
87
15304
    pub fn centre(label: usize) -> f64 {
88
15304
        units(label) + 0.5
89
15304
    }
90

            
91
    /// The x range of bar `rank` of label `label`.
92
8176
    pub fn span(&self, label: usize, rank: usize) -> (f64, f64) {
93
8176
        let bars = self.counts.get(label).copied().unwrap_or(1).max(1);
94
8176
        let each = (1.0 - 2.0 * SLOT_GAP) / units(bars);
95
8176
        let left = units(label) + SLOT_GAP + units(rank) * each;
96
8176
        (left, left + each)
97
8176
    }
98
}