crisol-plot 1.8.1

Crisol plot: interactive 2D charts and a 3D sibling with SVG, PNG, and PDF export.
Documentation
//! The interactive state of one plot: the egui-free view state from
//! `crisol-plot-core` plus the annotations, measurements, and live drag this crate
//! paints.
//!
//! The application owns it, like every other component state.

use std::ops::{Deref, DerefMut};

use super::PlotAnnotation;
use super::axis::{Bounds, Scale};
use super::filter::FilterKind;
use super::measure::{MeasurementConfig, PlotMeasurement};
use super::pipeline::Pipeline;
use super::series::Series;
use super::transforms::TransformKind;
use super::types::Drag;
use crisol_plot_core::state::ViewState;

/// The interactive state of one plot. Keep it in the application model.
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Clone, Debug, Default)]
pub struct PlotState {
    #[cfg_attr(feature = "serde", serde(flatten))]
    inner: ViewState,
    /// Text notes, arrows, spans, and regions.
    #[cfg_attr(feature = "serde", serde(default))]
    pub annotations: Vec<PlotAnnotation>,
    /// Distance and area measurements drawn over the data.
    #[cfg_attr(feature = "serde", serde(default))]
    pub measurements: Vec<PlotMeasurement>,
    /// Live measurement settings. The chart config seeds them until the user edits them.
    #[cfg_attr(feature = "serde", serde(default))]
    pub measurement: MeasurementConfig,
    #[cfg_attr(feature = "serde", serde(skip))]
    pub(super) drag: Option<Drag>,
}

impl Deref for PlotState {
    type Target = ViewState;

    fn deref(&self) -> &Self::Target {
        &self.inner
    }
}

impl DerefMut for PlotState {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.inner
    }
}

impl PlotState {
    /// The cached extent of `series`, if it is current.
    pub(super) fn cached_bounds(&self, series: &Series<'_>, x_scale: Scale, y_scale: Scale) -> Bounds {
        let key = series.fingerprint(x_scale, y_scale);
        self.cached_extent(key)
            .unwrap_or_else(|| series.bounds(x_scale, y_scale))
    }

    /// Every distance and area measurement, in the order they were added.
    pub fn measurements(&self) -> &[PlotMeasurement] {
        &self.measurements
    }

    /// Add a measurement. The plot draws it on the next frame.
    pub fn add_measurement(&mut self, measurement: PlotMeasurement) {
        self.measurements.push(measurement);
    }

    /// Remove the measurement at `index`.
    pub fn remove_measurement(&mut self, index: usize) -> Option<PlotMeasurement> {
        (index < self.measurements.len()).then(|| self.measurements.remove(index))
    }

    /// Remove the newest measurement and return it.
    pub fn pop_measurement(&mut self) -> Option<PlotMeasurement> {
        self.measurements.pop()
    }

    /// Remove every measurement.
    pub fn clear_measurements(&mut self) {
        self.measurements.clear();
    }

    /// The filter/transform pipeline.
    pub fn pipeline(&self) -> &Pipeline {
        &self.inner.pipeline
    }

    /// The filter/transform pipeline, mutably.
    pub fn pipeline_mut(&mut self) -> &mut Pipeline {
        &mut self.inner.pipeline
    }

    /// Enable or disable one filter.
    pub fn toggle_filter(&mut self, filter: FilterKind) {
        self.inner.pipeline.toggle_filter(filter);
    }

    /// Enable or disable one filter explicitly.
    pub fn set_filter(&mut self, filter: FilterKind, enabled: bool) {
        self.inner.pipeline.set_filter(filter, enabled);
    }

    /// Replace the transform. A new transform clears measurements because the data
    /// domain changed.
    pub fn set_transform(&mut self, transform: Option<TransformKind>) {
        if self.inner.pipeline.transform != transform {
            self.inner.pipeline.set_transform(transform);
            self.measurements.clear();
        }
    }

    /// Disable every filter and transform.
    pub fn clear_analysis(&mut self) {
        self.inner.pipeline.clear();
    }

    /// Replace the live measurement settings and mark them user-edited.
    pub fn set_measurement(&mut self, measurement: MeasurementConfig) {
        self.measurement = measurement;
        self.inner.measurement_customized = true;
    }

    /// Clear every painted overlay: measurements, selection, annotations, and analysis.
    pub fn clear_overlays(&mut self) {
        self.measurements.clear();
        self.inner.selection = None;
        self.annotations.clear();
        self.inner.pipeline.clear();
    }

    /// Whether a clear action would remove anything.
    pub fn has_overlays(&self) -> bool {
        !self.measurements.is_empty() || !self.annotations.is_empty() || self.inner.has_overlays()
    }
}