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
//! Example: Sending sample chunks for efficiency
//!
//! What it demonstrates
//! - Producing and sending chunks of `PlotPoint` entries via `send_points` for better throughput.
//! - How to timestamp samples within a chunk so x-values remain monotonic.
//!
//! How to run
//! ```bash
//! cargo run --example sine_cosine_chunks
//! ```
//! The UI renders two traces (`sine` and `cosine`) where each update sends a 200-sample chunk.
use liveplot::{channel_plot, run_liveplot, LivePlotConfig, PlotPoint};
use std::time::{Duration, SystemTime, UNIX_EPOCH};
fn main() -> eframe::Result<()> {
// Create multi-trace plot channel
let (sink, rx) = channel_plot();
let tr_sine = sink.create_trace("sine", None);
let tr_cos = sink.create_trace("cosine", None);
// Producer: generate chunks of 200 samples at 1 kHz for a 3 Hz sine/cosine
std::thread::spawn(move || {
const FS_HZ: f64 = 1000.0; // 1 kHz sampling rate
const F_HZ: f64 = 3.0; // 3 Hz
const CHUNK: usize = 200;
let dt = Duration::from_millis((CHUNK as f64 / FS_HZ * 1000.0) as u64);
let mut n: u64 = 0;
loop {
// Base timestamp for this chunk (seconds since epoch)
let base_t = SystemTime::now()
.duration_since(UNIX_EPOCH)
.map(|d| d.as_secs_f64())
.unwrap_or(0.0);
// Build two vectors of PlotPoint for the chunk
let mut pts_sine: Vec<PlotPoint> = Vec::with_capacity(CHUNK);
let mut pts_cos: Vec<PlotPoint> = Vec::with_capacity(CHUNK);
for i in 0..CHUNK {
let t = (n as f64 + i as f64) / FS_HZ; // relative time in seconds
let s_val = (2.0 * std::f64::consts::PI * F_HZ * t).sin();
let c_val = (2.0 * std::f64::consts::PI * F_HZ * t).cos();
// Use the base timestamp plus the per-sample offset to produce realistic x values
let t_s = base_t + (i as f64) / FS_HZ;
pts_sine.push(PlotPoint { x: t_s, y: s_val });
pts_cos.push(PlotPoint { x: t_s, y: c_val });
}
// Ignore errors if the UI closed (receiver dropped)
let _ = sink.send_points(&tr_sine, pts_sine);
let _ = sink.send_points(&tr_cos, pts_cos);
n = n.wrapping_add(CHUNK as u64);
std::thread::sleep(dt);
}
});
// Run the UI until closed. Uses the unified multi-trace engine.
let mut cfg = LivePlotConfig::default();
cfg.headline = Some("Sine/cosine example".to_string());
cfg.subheadline = Some("(with chunks)".to_string());
run_liveplot(rx, cfg)
}