TracesController

Struct TracesController 

Source
pub struct TracesController { /* private fields */ }
Expand description

Controller to observe and modify traces UI state (color/visibility/marker selection).

Implementations§

Source§

impl TracesController

Source

pub fn new() -> Self

Examples found in repository?
examples/custom_colors.rs (line 43)
15fn main() -> eframe::Result<()> {
16    // Create multi-trace plot channel
17    let (sink, rx) = channel_plot();
18    let tr_sine = sink.create_trace("sine", None);
19    let tr_cos = sink.create_trace("cosine", None);
20
21    // Producer: 1 kHz sample rate, 3 Hz sine and cosine
22    std::thread::spawn(move || {
23        const FS_HZ: f64 = 1000.0; // 1 kHz sampling rate
24        const F_HZ: f64 = 3.0; // 3 Hz
25        let dt = Duration::from_millis(1);
26        let mut n: u64 = 0;
27        loop {
28            let t = n as f64 / FS_HZ;
29            let s_val = (2.0 * std::f64::consts::PI * F_HZ * t).sin();
30            let c_val = (2.0 * std::f64::consts::PI * F_HZ * t).cos();
31            let t_s = SystemTime::now()
32                .duration_since(UNIX_EPOCH)
33                .map(|d| d.as_secs_f64())
34                .unwrap_or(0.0);
35            let _ = sink.send_point(&tr_sine, PlotPoint { x: t_s, y: s_val });
36            let _ = sink.send_point(&tr_cos, PlotPoint { x: t_s, y: c_val });
37            n = n.wrapping_add(1);
38            std::thread::sleep(dt);
39        }
40    });
41
42    // Traces controller to set custom colors once traces appear
43    let traces_ctrl = TracesController::new();
44    {
45        // In a background thread, subscribe to trace info and set colors when seen
46        let ctrl = traces_ctrl.clone();
47        let rx_info = ctrl.subscribe();
48        std::thread::spawn(move || {
49            while let Ok(info) = rx_info.recv() {
50                for tr in info.traces {
51                    match tr.name.as_str() {
52                        "sine" => ctrl.request_set_color("sine", [0xFF, 0x66, 0x00]), // orange
53                        "cosine" => ctrl.request_set_color("cosine", [0x00, 0x99, 0xFF]), // blue
54                        _ => {}
55                    }
56                }
57            }
58        });
59    }
60
61    // Run the UI with the controller attached via config
62    let mut cfg = LivePlotConfig::default();
63    cfg.title = "LivePlot (custom colors)".into();
64    cfg.traces_controller = Some(traces_ctrl);
65    run_liveplot(rx, cfg)
66}
Source

pub fn request_set_color<S: Into<String>>(&self, name: S, rgb: [u8; 3])

Request setting the RGB color of a trace by name.

Examples found in repository?
examples/custom_colors.rs (line 52)
15fn main() -> eframe::Result<()> {
16    // Create multi-trace plot channel
17    let (sink, rx) = channel_plot();
18    let tr_sine = sink.create_trace("sine", None);
19    let tr_cos = sink.create_trace("cosine", None);
20
21    // Producer: 1 kHz sample rate, 3 Hz sine and cosine
22    std::thread::spawn(move || {
23        const FS_HZ: f64 = 1000.0; // 1 kHz sampling rate
24        const F_HZ: f64 = 3.0; // 3 Hz
25        let dt = Duration::from_millis(1);
26        let mut n: u64 = 0;
27        loop {
28            let t = n as f64 / FS_HZ;
29            let s_val = (2.0 * std::f64::consts::PI * F_HZ * t).sin();
30            let c_val = (2.0 * std::f64::consts::PI * F_HZ * t).cos();
31            let t_s = SystemTime::now()
32                .duration_since(UNIX_EPOCH)
33                .map(|d| d.as_secs_f64())
34                .unwrap_or(0.0);
35            let _ = sink.send_point(&tr_sine, PlotPoint { x: t_s, y: s_val });
36            let _ = sink.send_point(&tr_cos, PlotPoint { x: t_s, y: c_val });
37            n = n.wrapping_add(1);
38            std::thread::sleep(dt);
39        }
40    });
41
42    // Traces controller to set custom colors once traces appear
43    let traces_ctrl = TracesController::new();
44    {
45        // In a background thread, subscribe to trace info and set colors when seen
46        let ctrl = traces_ctrl.clone();
47        let rx_info = ctrl.subscribe();
48        std::thread::spawn(move || {
49            while let Ok(info) = rx_info.recv() {
50                for tr in info.traces {
51                    match tr.name.as_str() {
52                        "sine" => ctrl.request_set_color("sine", [0xFF, 0x66, 0x00]), // orange
53                        "cosine" => ctrl.request_set_color("cosine", [0x00, 0x99, 0xFF]), // blue
54                        _ => {}
55                    }
56                }
57            }
58        });
59    }
60
61    // Run the UI with the controller attached via config
62    let mut cfg = LivePlotConfig::default();
63    cfg.title = "LivePlot (custom colors)".into();
64    cfg.traces_controller = Some(traces_ctrl);
65    run_liveplot(rx, cfg)
66}
Source

pub fn request_set_visible<S: Into<String>>(&self, name: S, visible: bool)

Request setting the visibility of a trace by name.

Source

pub fn request_set_offset<S: Into<String>>(&self, name: S, offset: f64)

Request setting the Y offset of a trace by name.

Source

pub fn request_set_y_unit<S: Into<String>>(&self, unit: Option<S>)

Request setting the global Y unit label (None for no unit).

Source

pub fn request_set_y_log(&self, enable: bool)

Request toggling Y log scale.

Source

pub fn request_select_marker_free(&self)

Request selecting the marker to be “Free” (no snapping).

Source

pub fn request_select_marker_trace<S: Into<String>>(&self, name: S)

Request selecting a specific trace for markers.

Source

pub fn subscribe(&self) -> Receiver<TracesInfo>

Subscribe to receive updates about traces and current selection.

Examples found in repository?
examples/custom_colors.rs (line 47)
15fn main() -> eframe::Result<()> {
16    // Create multi-trace plot channel
17    let (sink, rx) = channel_plot();
18    let tr_sine = sink.create_trace("sine", None);
19    let tr_cos = sink.create_trace("cosine", None);
20
21    // Producer: 1 kHz sample rate, 3 Hz sine and cosine
22    std::thread::spawn(move || {
23        const FS_HZ: f64 = 1000.0; // 1 kHz sampling rate
24        const F_HZ: f64 = 3.0; // 3 Hz
25        let dt = Duration::from_millis(1);
26        let mut n: u64 = 0;
27        loop {
28            let t = n as f64 / FS_HZ;
29            let s_val = (2.0 * std::f64::consts::PI * F_HZ * t).sin();
30            let c_val = (2.0 * std::f64::consts::PI * F_HZ * t).cos();
31            let t_s = SystemTime::now()
32                .duration_since(UNIX_EPOCH)
33                .map(|d| d.as_secs_f64())
34                .unwrap_or(0.0);
35            let _ = sink.send_point(&tr_sine, PlotPoint { x: t_s, y: s_val });
36            let _ = sink.send_point(&tr_cos, PlotPoint { x: t_s, y: c_val });
37            n = n.wrapping_add(1);
38            std::thread::sleep(dt);
39        }
40    });
41
42    // Traces controller to set custom colors once traces appear
43    let traces_ctrl = TracesController::new();
44    {
45        // In a background thread, subscribe to trace info and set colors when seen
46        let ctrl = traces_ctrl.clone();
47        let rx_info = ctrl.subscribe();
48        std::thread::spawn(move || {
49            while let Ok(info) = rx_info.recv() {
50                for tr in info.traces {
51                    match tr.name.as_str() {
52                        "sine" => ctrl.request_set_color("sine", [0xFF, 0x66, 0x00]), // orange
53                        "cosine" => ctrl.request_set_color("cosine", [0x00, 0x99, 0xFF]), // blue
54                        _ => {}
55                    }
56                }
57            }
58        });
59    }
60
61    // Run the UI with the controller attached via config
62    let mut cfg = LivePlotConfig::default();
63    cfg.title = "LivePlot (custom colors)".into();
64    cfg.traces_controller = Some(traces_ctrl);
65    run_liveplot(rx, cfg)
66}

Trait Implementations§

Source§

impl Clone for TracesController

Source§

fn clone(&self) -> TracesController

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
Source§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

Source§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Sync + Send>

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> SerializableAny for T
where T: 'static + Any + Clone + for<'a> Send + Sync,