crisol-plot 1.12.0

Crisol plot: interactive 2D charts and a 3D sibling with SVG, PNG, and PDF export.
Documentation
//! Data extents for every series kind (the automatic view and caching use them).
//!
//! The extent math lives in `crisol-plot-core`; this file feeds it each series' kind.

use crisol_plot_core::series_bounds as core;

use super::axis::{Bounds, Scale};
use super::series::{Kind, Series};

impl Series<'_> {
    /// Data extent in scaled space (log axes ignore values ≤ 0).
    pub(super) fn bounds(&self, x_scale: Scale, y_scale: Scale) -> Bounds {
        match &self.kind {
            Kind::Candles { width } => return self.candle_bounds(x_scale, y_scale, *width),
            Kind::Heatmap => return self.heatmap_bounds(x_scale, y_scale),
            Kind::Waterfall { width } => return self.waterfall_bounds(x_scale, y_scale, *width),
            Kind::Pie { .. } | Kind::Radar | Kind::Gauge { .. } => return Bounds::NOTHING,
            _ => {}
        }
        let mut bounds = core::point_bounds(&self.data, self.y_errors.as_deref(), x_scale, y_scale);
        if !bounds.is_valid() {
            return bounds;
        }
        match self.kind {
            Kind::Bars { base, width } => {
                bounds.extend([bounds.min_x, y_scale.forward(base)]);
                let half = width.unwrap_or_else(|| self.min_spacing() * 0.8) * 0.5;
                if x_scale == Scale::Linear {
                    bounds.min_x -= half;
                    bounds.max_x += half;
                }
            }
            Kind::Line {
                fill_to: Some(base), ..
            } => bounds.extend([bounds.min_x, y_scale.forward(base)]),
            _ => {}
        }
        bounds
    }

    /// The smallest positive x step (1 for a single point).
    pub(super) fn min_spacing(&self) -> f64 {
        core::smallest_step(self.data.iter().map(|point| point[0]))
    }

    fn candle_bounds(&self, x_scale: Scale, y_scale: Scale, width: Option<f64>) -> Bounds {
        let Some(rows) = &self.ohlc else {
            return Bounds::NOTHING;
        };
        core::candle_bounds(rows, x_scale, y_scale, width, self.min_spacing())
    }

    fn heatmap_bounds(&self, x_scale: Scale, y_scale: Scale) -> Bounds {
        let Some(cells) = &self.cells else {
            return Bounds::NOTHING;
        };
        core::heatmap_bounds(cells, x_scale, y_scale)
    }

    fn waterfall_bounds(&self, x_scale: Scale, y_scale: Scale, width: Option<f64>) -> Bounds {
        core::waterfall_bounds(&self.data, x_scale, y_scale, width, self.min_spacing())
    }

    /// Whether x never decreases (enables binary search and decimation).
    pub(super) fn sorted_x(&self) -> bool {
        core::sorted_x(&self.data)
    }

    /// The OHLC rows, when this is a candlestick series.
    pub(super) fn ohlc(&self) -> Option<&[[f64; 5]]> {
        self.ohlc.as_deref()
    }

    /// The heatmap cells, when this is a heatmap.
    pub(super) fn cells(&self) -> Option<&[[f64; 3]]> {
        self.cells.as_deref()
    }
}