use crisol_plot_core::series_bounds as core;
use super::axis::{Bounds, Scale};
use super::series::{Kind, Series};
impl Series<'_> {
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
}
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())
}
pub(super) fn sorted_x(&self) -> bool {
core::sorted_x(&self.data)
}
pub(super) fn ohlc(&self) -> Option<&[[f64; 5]]> {
self.ohlc.as_deref()
}
pub(super) fn cells(&self) -> Option<&[[f64; 3]]> {
self.cells.as_deref()
}
}