dear-implot 0.12.0

High-level Rust bindings to ImPlot with dear-imgui-rs integration
Documentation
//! Stem plot implementation

use super::{
    Plot, PlotError, PlotItemStyle, plot_spec_with_style, validate_data_lengths,
    with_plot_str_or_empty,
};
use crate::{ItemFlags, StemsFlags, sys};

/// Builder for stem plots (lollipop charts)
pub struct StemPlot<'a> {
    label: &'a str,
    x_data: &'a [f64],
    y_data: &'a [f64],
    style: PlotItemStyle,
    y_ref: f64,
    flags: StemsFlags,
    item_flags: ItemFlags,
    offset: i32,
    stride: i32,
}

impl<'a> super::PlotItemStyled for StemPlot<'a> {
    fn style_mut(&mut self) -> &mut PlotItemStyle {
        &mut self.style
    }
}

impl<'a> StemPlot<'a> {
    /// Create a new stem plot with the given label and data
    pub fn new(label: &'a str, x_data: &'a [f64], y_data: &'a [f64]) -> Self {
        Self {
            label,
            x_data,
            y_data,
            style: PlotItemStyle::default(),
            y_ref: 0.0, // Default reference line at Y=0
            flags: StemsFlags::NONE,
            item_flags: ItemFlags::NONE,
            offset: 0,
            stride: std::mem::size_of::<f64>() as i32,
        }
    }

    /// Set the reference Y value for stems
    /// Stems will be drawn from this Y value to the data points
    pub fn with_y_ref(mut self, y_ref: f64) -> Self {
        self.y_ref = y_ref;
        self
    }

    /// Set stem flags for customization
    pub fn with_flags(mut self, flags: StemsFlags) -> Self {
        self.flags = flags;
        self
    }

    /// Set common item flags for this plot item (applies to all plot types)
    pub fn with_item_flags(mut self, flags: ItemFlags) -> Self {
        self.item_flags = flags;
        self
    }

    /// Set data offset for partial plotting
    pub fn with_offset(mut self, offset: i32) -> Self {
        self.offset = offset;
        self
    }

    /// Set data stride for non-contiguous data
    pub fn with_stride(mut self, stride: i32) -> Self {
        self.stride = stride;
        self
    }

    /// Validate the plot data
    pub fn validate(&self) -> Result<(), PlotError> {
        validate_data_lengths(self.x_data, self.y_data)
    }
}

impl<'a> Plot for StemPlot<'a> {
    fn plot(&self) {
        if self.validate().is_err() {
            return;
        }
        let Ok(count) = i32::try_from(self.x_data.len()) else {
            return;
        };

        with_plot_str_or_empty(self.label, |label_ptr| unsafe {
            let spec = plot_spec_with_style(
                self.style,
                self.flags.bits() | self.item_flags.bits(),
                self.offset,
                self.stride,
            );
            sys::ImPlot_PlotStems_doublePtrdoublePtr(
                label_ptr,
                self.x_data.as_ptr(),
                self.y_data.as_ptr(),
                count,
                self.y_ref,
                spec,
            );
        })
    }

    fn label(&self) -> &str {
        self.label
    }
}

/// Simple stem plot for quick plotting without builder pattern
pub struct SimpleStemPlot<'a> {
    label: &'a str,
    values: &'a [f64],
    style: PlotItemStyle,
    y_ref: f64,
    flags: StemsFlags,
    item_flags: ItemFlags,
    x_scale: f64,
    x_start: f64,
}

impl<'a> super::PlotItemStyled for SimpleStemPlot<'a> {
    fn style_mut(&mut self) -> &mut PlotItemStyle {
        &mut self.style
    }
}

impl<'a> SimpleStemPlot<'a> {
    /// Create a simple stem plot with Y values only (X will be indices)
    pub fn new(label: &'a str, values: &'a [f64]) -> Self {
        Self {
            label,
            values,
            style: PlotItemStyle::default(),
            y_ref: 0.0,
            flags: StemsFlags::NONE,
            item_flags: ItemFlags::NONE,
            x_scale: 1.0,
            x_start: 0.0,
        }
    }

    /// Set the reference Y value for stems
    pub fn with_y_ref(mut self, y_ref: f64) -> Self {
        self.y_ref = y_ref;
        self
    }

    /// Set stem flags for customization
    pub fn with_flags(mut self, flags: StemsFlags) -> Self {
        self.flags = flags;
        self
    }

    /// Set common item flags for this plot item (applies to all plot types)
    pub fn with_item_flags(mut self, flags: ItemFlags) -> Self {
        self.item_flags = flags;
        self
    }

    /// Set X scale factor
    pub fn with_x_scale(mut self, scale: f64) -> Self {
        self.x_scale = scale;
        self
    }

    /// Set X start value
    pub fn with_x_start(mut self, start: f64) -> Self {
        self.x_start = start;
        self
    }
}

impl<'a> Plot for SimpleStemPlot<'a> {
    fn plot(&self) {
        if self.values.is_empty() {
            return;
        }
        let Ok(count) = i32::try_from(self.values.len()) else {
            return;
        };

        with_plot_str_or_empty(self.label, |label_ptr| unsafe {
            let spec = plot_spec_with_style(
                self.style,
                self.flags.bits() | self.item_flags.bits(),
                0,
                std::mem::size_of::<f64>() as i32,
            );
            sys::ImPlot_PlotStems_doublePtrInt(
                label_ptr,
                self.values.as_ptr(),
                count,
                self.y_ref,
                self.x_scale,
                self.x_start,
                spec,
            );
        })
    }

    fn label(&self) -> &str {
        self.label
    }
}

/// Convenience functions for quick stem plotting
impl<'ui> crate::PlotUi<'ui> {
    /// Plot a stem plot with X and Y data
    pub fn stem_plot(&self, label: &str, x_data: &[f64], y_data: &[f64]) -> Result<(), PlotError> {
        let plot = StemPlot::new(label, x_data, y_data);
        plot.validate()?;
        plot.plot();
        Ok(())
    }

    /// Plot a stem plot with custom reference Y value
    pub fn stem_plot_with_ref(
        &self,
        label: &str,
        x_data: &[f64],
        y_data: &[f64],
        y_ref: f64,
    ) -> Result<(), PlotError> {
        let plot = StemPlot::new(label, x_data, y_data).with_y_ref(y_ref);
        plot.validate()?;
        plot.plot();
        Ok(())
    }

    /// Plot a simple stem plot with Y values only (X will be indices)
    pub fn simple_stem_plot(&self, label: &str, values: &[f64]) -> Result<(), PlotError> {
        if values.is_empty() {
            return Err(PlotError::EmptyData);
        }
        let plot = SimpleStemPlot::new(label, values);
        plot.plot();
        Ok(())
    }

    /// Plot a simple stem plot with custom reference Y value
    pub fn simple_stem_plot_with_ref(
        &self,
        label: &str,
        values: &[f64],
        y_ref: f64,
    ) -> Result<(), PlotError> {
        if values.is_empty() {
            return Err(PlotError::EmptyData);
        }
        let plot = SimpleStemPlot::new(label, values).with_y_ref(y_ref);
        plot.plot();
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_stem_plot_creation() {
        let x_data = [1.0, 2.0, 3.0, 4.0];
        let y_data = [1.0, 4.0, 2.0, 3.0];

        let plot = StemPlot::new("test", &x_data, &y_data);
        assert_eq!(plot.label(), "test");
        assert!(plot.validate().is_ok());
    }

    #[test]
    fn test_stem_plot_validation() {
        let x_data = [1.0, 2.0, 3.0];
        let y_data = [1.0, 4.0]; // Different length

        let plot = StemPlot::new("test", &x_data, &y_data);
        assert!(plot.validate().is_err());
    }

    #[test]
    fn test_simple_stem_plot() {
        let values = [1.0, 2.0, 3.0, 4.0];
        let plot = SimpleStemPlot::new("test", &values)
            .with_flags(StemsFlags::HORIZONTAL)
            .with_item_flags(ItemFlags::NO_LEGEND);
        assert_eq!(plot.label(), "test");
        assert_eq!(plot.flags.bits(), StemsFlags::HORIZONTAL.bits());
        assert_eq!(plot.item_flags, ItemFlags::NO_LEGEND);
    }

    #[test]
    fn test_stem_plot_with_ref() {
        let x_data = [1.0, 2.0, 3.0, 4.0];
        let y_data = [1.0, 4.0, 2.0, 3.0];

        let plot = StemPlot::new("test", &x_data, &y_data).with_y_ref(1.0);
        assert_eq!(plot.y_ref, 1.0);
        assert!(plot.validate().is_ok());
    }
}