Skip to main content

crisol_plot_core/
series_bounds.rs

1//! Data extents for every plot series kind (the automatic view and caching use them).
2
3use crisol_core::data::Bounds;
4
5use crate::axis::Scale;
6
7/// The extent of a line or scatter series in scaled space (log axes ignore values ≤ 0).
8pub fn point_bounds(data: &[[f64; 2]], y_errors: Option<&[f64]>, x_scale: Scale, y_scale: Scale) -> Bounds {
9    let mut bounds = Bounds::NOTHING;
10    let error = |index: usize| y_errors.and_then(|errors| errors.get(index)).copied();
11    for (index, point) in data.iter().enumerate() {
12        let x = x_scale.forward(point[0]);
13        match error(index) {
14            Some(error) => {
15                bounds.extend([x, y_scale.forward(point[1] - error)]);
16                bounds.extend([x, y_scale.forward(point[1] + error)]);
17            }
18            None => bounds.extend([x, y_scale.forward(point[1])]),
19        }
20    }
21    bounds
22}
23
24/// The extent of the open–high–low–close rows.
25pub fn candle_bounds(
26    rows: &[[f64; 5]],
27    x_scale: Scale,
28    y_scale: Scale,
29    width: Option<f64>,
30    min_spacing: f64,
31) -> Bounds {
32    let mut bounds = Bounds::NOTHING;
33    for row in rows {
34        let x = x_scale.forward(row[0]);
35        for value in &row[1..] {
36            bounds.extend([x, y_scale.forward(*value)]);
37        }
38    }
39    if bounds.is_valid() && x_scale == Scale::Linear {
40        let half = width.unwrap_or(min_spacing * 0.65) * 0.5;
41        bounds.min_x -= half;
42        bounds.max_x += half;
43    }
44    bounds
45}
46
47/// The extent of the x/y/value heatmap cells, expanded by half a cell.
48pub fn heatmap_bounds(cells: &[[f64; 3]], x_scale: Scale, y_scale: Scale) -> Bounds {
49    let mut bounds = Bounds::NOTHING;
50    for cell in cells {
51        bounds.extend([x_scale.forward(cell[0]), y_scale.forward(cell[1])]);
52    }
53    if bounds.is_valid() {
54        let x_half = smallest_step(cells.iter().map(|cell| cell[0])) * 0.5;
55        let y_half = smallest_step(cells.iter().map(|cell| cell[1])) * 0.5;
56        bounds.min_x -= x_half;
57        bounds.max_x += x_half;
58        bounds.min_y -= y_half;
59        bounds.max_y += y_half;
60    }
61    bounds
62}
63
64/// The extent of a cumulative waterfall of signed changes.
65pub fn waterfall_bounds(
66    data: &[[f64; 2]],
67    x_scale: Scale,
68    y_scale: Scale,
69    width: Option<f64>,
70    min_spacing: f64,
71) -> Bounds {
72    let mut bounds = Bounds::NOTHING;
73    let mut base = 0.0;
74    for point in data {
75        let next = base + point[1];
76        let x = x_scale.forward(point[0]);
77        bounds.extend([x, y_scale.forward(base)]);
78        bounds.extend([x, y_scale.forward(next)]);
79        base = next;
80    }
81    if bounds.is_valid() && x_scale == Scale::Linear {
82        bounds.extend([bounds.min_x, y_scale.forward(0.0)]);
83        let half = width.unwrap_or(min_spacing * 0.65) * 0.5;
84        bounds.min_x -= half;
85        bounds.max_x += half;
86    }
87    bounds
88}
89
90/// Whether x never decreases (enables binary search and decimation).
91pub fn sorted_x(data: &[[f64; 2]]) -> bool {
92    data.windows(2)
93        .all(|pair| pair[1][0] >= pair[0][0] || pair[1][0].is_nan() || pair[0][0].is_nan())
94}
95
96/// The smallest positive step between values (1 when there is none).
97pub fn smallest_step(values: impl Iterator<Item = f64>) -> f64 {
98    let mut previous: Option<f64> = None;
99    let mut smallest = f64::INFINITY;
100    for value in values {
101        if !value.is_finite() {
102            continue;
103        }
104        if let Some(previous) = previous {
105            let delta = (value - previous).abs();
106            if delta > 0.0 {
107                smallest = smallest.min(delta);
108            }
109        }
110        previous = Some(value);
111    }
112    if smallest.is_finite() { smallest } else { 1.0 }
113}