liveplot 1.0.0

Realtime interactive plotting library using egui/eframe, with optional gRPC and Parquet export support.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
//! Data source types and channels for feeding points into the plotter UI.
//!
//! New API (breaking change):
//! - First create a `Trace` (with name and optional info). The library assigns a numeric ID.
//! - Send `PlotPoint { x, y }` to a given trace, either singly or in chunks for efficiency.

use std::sync::atomic::{AtomicU32, Ordering};
use std::sync::mpsc::{Receiver, Sender};

/// Numeric identifier for a trace, assigned by the library when creating a `Trace`.
pub type TraceId = u32;

/// A single point on a plot: x is typically time (in seconds), y is the value.
#[derive(Debug, Clone, Copy)]
pub struct PlotPoint {
    pub x: f64,
    pub y: f64,
}

/// Declaration of a trace; returned to the caller after registration.
#[derive(Debug, Clone)]
pub struct Trace {
    pub id: TraceId,
    pub name: String,
    pub info: Option<String>,
}

/// Messages sent over the channel to drive the UI.
pub enum PlotCommand {
    /// Register a new trace with a numeric ID and optional info string.
    RegisterTrace {
        id: TraceId,
        name: String,
        info: Option<String>,
    },
    /// Append a single point to the given trace ID.
    Point { trace_id: TraceId, point: PlotPoint },
    /// Append a chunk of points to the given trace ID.
    Points {
        trace_id: TraceId,
        points: Vec<PlotPoint>,
    },
    /// Set the Y value for specific points identified by their exact X coordinates.
    SetPointsY {
        trace_id: TraceId,
        xs: Vec<f64>,
        y: f64,
    },
    /// Delete specific points identified by their exact X coordinates.
    DeletePointsX { trace_id: TraceId, xs: Vec<f64> },
    /// Delete all points within the inclusive X range [x_min, x_max].
    DeleteXRange {
        trace_id: TraceId,
        x_min: f64,
        x_max: f64,
    },
    /// Apply a Y-transform function to specific points (by exact X coordinates).
    ApplyYFnAtX {
        trace_id: TraceId,
        xs: Vec<f64>,
        f: YTransform,
    },
    /// Apply a Y-transform function to all points within an inclusive X range.
    ApplyYFnInXRange {
        trace_id: TraceId,
        x_min: f64,
        x_max: f64,
        f: YTransform,
    },
    /// Remove all data points for the given trace (resulting trace is empty).
    ClearData { trace_id: TraceId },
    /// Replace the entire data vector for the given trace with the provided points.
    ///
    /// This is intended as an efficient overwrite operation: any existing points
    /// for the trace are discarded and replaced atomically with `points`.
    SetData { trace_id: TraceId, points: Vec<PlotPoint> },
}

/// Convenience sender for feeding points into the multi-trace plotter.
#[derive(Clone)]
pub struct PlotSink {
    tx: Sender<PlotCommand>,
}

/// A function that transforms a point's Y value.
///
/// Note: This function will be applied in the UI thread that processes incoming plot commands.
pub type YTransform = Box<dyn Fn(f64) -> f64 + Send + 'static>;

impl PlotSink {
    /// Create and register a new `Trace` with a unique numeric ID.
    pub fn create_trace<S: Into<String>>(&self, name: S, info: Option<S>) -> Trace {
        static NEXT_ID: AtomicU32 = AtomicU32::new(1);
        let id = NEXT_ID.fetch_add(1, Ordering::Relaxed);
        let name = name.into();
        let info_str = info.map(|s| s.into());
        // Inform the UI about the new trace
        let _ = self.tx.send(PlotCommand::RegisterTrace {
            id,
            name: name.clone(),
            info: info_str.clone(),
        });
        Trace {
            id,
            name,
            info: info_str,
        }
    }

    /// Send a single `PlotPoint` for a given `Trace`.
    pub fn send_point(
        &self,
        trace: &Trace,
        point: PlotPoint,
    ) -> Result<(), std::sync::mpsc::SendError<PlotCommand>> {
        self.tx.send(PlotCommand::Point {
            trace_id: trace.id,
            point,
        })
    }

    /// Send a single `PlotPoint` for a given trace ID.
    pub fn send_point_by_id(
        &self,
        trace_id: TraceId,
        point: PlotPoint,
    ) -> Result<(), std::sync::mpsc::SendError<PlotCommand>> {
        self.tx.send(PlotCommand::Point { trace_id, point })
    }

    /// Send a chunk of points for a given `Trace` (more efficient than point-by-point).
    pub fn send_points<I>(
        &self,
        trace: &Trace,
        points: I,
    ) -> Result<(), std::sync::mpsc::SendError<PlotCommand>>
    where
        I: Into<Vec<PlotPoint>>,
    {
        self.tx.send(PlotCommand::Points {
            trace_id: trace.id,
            points: points.into(),
        })
    }

    /// Send a chunk of points for a given trace ID (more efficient than point-by-point).
    pub fn send_points_by_id<I>(
        &self,
        trace_id: TraceId,
        points: I,
    ) -> Result<(), std::sync::mpsc::SendError<PlotCommand>>
    where
        I: Into<Vec<PlotPoint>>,
    {
        self.tx.send(PlotCommand::Points {
            trace_id,
            points: points.into(),
        })
    }

    /// Set the Y value for a specific point (by exact X) on a given `Trace`.
    #[inline]
    pub fn set_point_y(
        &self,
        trace: &Trace,
        x: f64,
        y: f64,
    ) -> Result<(), std::sync::mpsc::SendError<PlotCommand>> {
        self.tx.send(PlotCommand::SetPointsY {
            trace_id: trace.id,
            xs: vec![x],
            y,
        })
    }

    /// Set the Y value for a specific point (by exact X) by trace ID.
    #[inline]
    pub fn set_point_y_by_id(
        &self,
        trace_id: TraceId,
        x: f64,
        y: f64,
    ) -> Result<(), std::sync::mpsc::SendError<PlotCommand>> {
        self.tx.send(PlotCommand::SetPointsY {
            trace_id,
            xs: vec![x],
            y,
        })
    }

    /// Set the Y value for multiple specific points (by exact X) on a given `Trace`.
    #[inline]
    pub fn set_points_y<I>(
        &self,
        trace: &Trace,
        xs: I,
        y: f64,
    ) -> Result<(), std::sync::mpsc::SendError<PlotCommand>>
    where
        I: Into<Vec<f64>>,
    {
        self.tx.send(PlotCommand::SetPointsY {
            trace_id: trace.id,
            xs: xs.into(),
            y,
        })
    }

    /// Set the Y value for multiple specific points (by exact X) by trace ID.
    #[inline]
    pub fn set_points_y_by_id<I>(
        &self,
        trace_id: TraceId,
        xs: I,
        y: f64,
    ) -> Result<(), std::sync::mpsc::SendError<PlotCommand>>
    where
        I: Into<Vec<f64>>,
    {
        self.tx.send(PlotCommand::SetPointsY {
            trace_id,
            xs: xs.into(),
            y,
        })
    }

    /// Delete a specific point (by exact X) on a given `Trace`.
    #[inline]
    pub fn delete_point_x(
        &self,
        trace: &Trace,
        x: f64,
    ) -> Result<(), std::sync::mpsc::SendError<PlotCommand>> {
        self.tx.send(PlotCommand::DeletePointsX {
            trace_id: trace.id,
            xs: vec![x],
        })
    }

    /// Delete a specific point (by exact X) by trace ID.
    #[inline]
    pub fn delete_point_x_by_id(
        &self,
        trace_id: TraceId,
        x: f64,
    ) -> Result<(), std::sync::mpsc::SendError<PlotCommand>> {
        self.tx.send(PlotCommand::DeletePointsX {
            trace_id,
            xs: vec![x],
        })
    }

    /// Delete multiple specific points (by exact X) on a given `Trace`.
    #[inline]
    pub fn delete_points_x<I>(
        &self,
        trace: &Trace,
        xs: I,
    ) -> Result<(), std::sync::mpsc::SendError<PlotCommand>>
    where
        I: Into<Vec<f64>>,
    {
        self.tx.send(PlotCommand::DeletePointsX {
            trace_id: trace.id,
            xs: xs.into(),
        })
    }

    /// Delete multiple specific points (by exact X) by trace ID.
    #[inline]
    pub fn delete_points_x_by_id<I>(
        &self,
        trace_id: TraceId,
        xs: I,
    ) -> Result<(), std::sync::mpsc::SendError<PlotCommand>>
    where
        I: Into<Vec<f64>>,
    {
        self.tx.send(PlotCommand::DeletePointsX {
            trace_id,
            xs: xs.into(),
        })
    }

    /// Delete all points in the inclusive X range [x_min, x_max] on a given `Trace`.
    ///
    /// Special values: pass `f64::NAN` for `x_min` or `x_max` to mean the start or end of the
    /// trace's current data vector respectively (i.e. `x_min = NaN` → earliest sample, `x_max = NaN` → latest sample).
    #[inline]
    pub fn delete_x_range(
        &self,
        trace: &Trace,
        x_min: f64,
        x_max: f64,
    ) -> Result<(), std::sync::mpsc::SendError<PlotCommand>> {
        self.tx.send(PlotCommand::DeleteXRange {
            trace_id: trace.id,
            x_min,
            x_max,
        })
    }

    /// Delete all points in the inclusive X range [x_min, x_max] by trace ID.
    #[inline]
    pub fn delete_x_range_by_id(
        &self,
        trace_id: TraceId,
        x_min: f64,
        x_max: f64,
    ) -> Result<(), std::sync::mpsc::SendError<PlotCommand>> {
        self.tx.send(PlotCommand::DeleteXRange {
            trace_id,
            x_min,
            x_max,
        })
    }

    /// Apply a Y-transform function to a specific point (by exact X) on a given `Trace`.
    #[inline]
    pub fn apply_y_fn_at_x(
        &self,
        trace: &Trace,
        x: f64,
        f: YTransform,
    ) -> Result<(), std::sync::mpsc::SendError<PlotCommand>> {
        self.tx.send(PlotCommand::ApplyYFnAtX {
            trace_id: trace.id,
            xs: vec![x],
            f,
        })
    }

    /// Apply a Y-transform function to a specific point (by exact X) by trace ID.
    #[inline]
    pub fn apply_y_fn_at_x_by_id(
        &self,
        trace_id: TraceId,
        x: f64,
        f: YTransform,
    ) -> Result<(), std::sync::mpsc::SendError<PlotCommand>> {
        self.tx.send(PlotCommand::ApplyYFnAtX {
            trace_id,
            xs: vec![x],
            f,
        })
    }

    /// Apply a Y-transform function to multiple specific points (by exact X) on a given `Trace`.
    #[inline]
    pub fn apply_y_fn_at_xs<I>(
        &self,
        trace: &Trace,
        xs: I,
        f: YTransform,
    ) -> Result<(), std::sync::mpsc::SendError<PlotCommand>>
    where
        I: Into<Vec<f64>>,
    {
        self.tx.send(PlotCommand::ApplyYFnAtX {
            trace_id: trace.id,
            xs: xs.into(),
            f,
        })
    }

    /// Apply a Y-transform function to multiple specific points (by exact X) by trace ID.
    #[inline]
    pub fn apply_y_fn_at_xs_by_id<I>(
        &self,
        trace_id: TraceId,
        xs: I,
        f: YTransform,
    ) -> Result<(), std::sync::mpsc::SendError<PlotCommand>>
    where
        I: Into<Vec<f64>>,
    {
        self.tx.send(PlotCommand::ApplyYFnAtX {
            trace_id,
            xs: xs.into(),
            f,
        })
    }

    /// Apply a Y-transform function to all points in the inclusive X range [x_min, x_max] on a given `Trace`.
    ///
    /// Special values: pass `f64::NAN` for `x_min` or `x_max` to mean the start or end of the
    /// trace's current data vector respectively (i.e. `x_min = NaN` → earliest sample, `x_max = NaN` → latest sample).
    #[inline]
    pub fn apply_y_fn_in_x_range(
        &self,
        trace: &Trace,
        x_min: f64,
        x_max: f64,
        f: YTransform,
    ) -> Result<(), std::sync::mpsc::SendError<PlotCommand>> {
        self.tx.send(PlotCommand::ApplyYFnInXRange {
            trace_id: trace.id,
            x_min,
            x_max,
            f,
        })
    }

    /// Apply a Y-transform function to all points in the inclusive X range [x_min, x_max] by trace ID.
    #[inline]
    pub fn apply_y_fn_in_x_range_by_id(
        &self,
        trace_id: TraceId,
        x_min: f64,
        x_max: f64,
        f: YTransform,
    ) -> Result<(), std::sync::mpsc::SendError<PlotCommand>> {
        self.tx.send(PlotCommand::ApplyYFnInXRange {
            trace_id,
            x_min,
            x_max,
            f,
        })
    }

    /// Remove all data points for a given `Trace`.
    #[inline]
    pub fn clear_data(&self, trace: &Trace) -> Result<(), std::sync::mpsc::SendError<PlotCommand>> {
        self.tx.send(PlotCommand::ClearData { trace_id: trace.id })
    }

    /// Remove all data points for a given trace id.
    #[inline]
    pub fn clear_data_by_id(&self, trace_id: TraceId) -> Result<(), std::sync::mpsc::SendError<PlotCommand>> {
        self.tx.send(PlotCommand::ClearData { trace_id })
    }

    /// Replace the entire data vector for a given `Trace` with the provided points.
    /// This discards any existing points for the trace.
    pub fn set_data<I>(&self, trace: &Trace, points: I) -> Result<(), std::sync::mpsc::SendError<PlotCommand>>
    where
        I: Into<Vec<PlotPoint>>,
    {
        self.tx.send(PlotCommand::SetData {
            trace_id: trace.id,
            points: points.into(),
        })
    }

    /// Replace the entire data vector for a given trace id with the provided points.
    pub fn set_data_by_id<I>(&self, trace_id: TraceId, points: I) -> Result<(), std::sync::mpsc::SendError<PlotCommand>>
    where
        I: Into<Vec<PlotPoint>>,
    {
        self.tx.send(PlotCommand::SetData {
            trace_id,
            points: points.into(),
        })
    }
}

/// Create a new channel pair for plotting: `(PlotSink, Receiver<PlotCommand>)`.
pub fn channel_plot() -> (PlotSink, Receiver<PlotCommand>) {
    let (tx, rx) = std::sync::mpsc::channel();
    (PlotSink { tx }, rx)
}