rt_graph/lib.rs
1#![deny(warnings)]
2#![deny(missing_docs)]
3
4//! A real-time graphing experiment.
5//!
6//! rt-graph uses GTK (via the gtk-rs Rust bindings) for its UI and is
7//! designed to be embedded into any gtk::Container in your
8//! application.
9
10#[macro_use]
11extern crate derive_builder;
12
13#[macro_use]
14extern crate log;
15
16use std::fmt::Debug;
17
18mod graph;
19pub use graph::{Config, ConfigBuilder, Graph, PointStyle, View, ViewMode};
20
21mod graph_with_controls;
22pub use graph_with_controls::GraphWithControls;
23
24mod null_data_source;
25pub use null_data_source::NullDataSource;
26
27pub mod observable_value;
28
29mod signal;
30pub use signal::Signal;
31
32mod store;
33use store::Store;
34
35mod test_data_generator;
36pub use test_data_generator::TestDataGenerator;
37
38/// Represents an error that could occur using the crate
39#[derive(Debug)]
40pub enum Error {
41 /// An error described by a `String`.
42 String(String),
43}
44
45/// Represents either a value or an error from the crate.
46pub type Result<T> = std::result::Result<T, Error>;
47
48/// A point in time when a data point was emitted.
49pub type Time = u32;
50
51/// The value of a data point
52pub type Value = u16;
53
54/// A data point on a graph.
55#[derive(Debug, Clone)]
56pub struct Point {
57 /// The time when this data point was emitted.
58 pub t: Time,
59
60 /// The values this point holds.
61 pub vs: Vec<Value>,
62}
63
64impl Point {
65 /// Return the time when this data point was emitted.
66 pub fn t(&self) -> Time {
67 self.t
68 }
69
70 /// Return the values that this point holds.
71 pub fn vals(&self) -> &[Value] {
72 &self.vs
73 }
74}
75
76/// A color in RGB format.
77///
78/// The tuple values are the red, green, and blue components of the
79/// color respectively.
80#[derive(Clone, Copy)]
81pub struct Color(pub u8, pub u8, pub u8);
82
83impl Color {
84 /// Create a color from red, green, and blue components.
85 pub fn from_rgb(r: u8, g: u8, b: u8) -> Color {
86 Color(r, g, b)
87 }
88
89 /// Return the red component of the `Color`.
90 pub fn r(&self) -> u8 {
91 self.0
92 }
93
94 /// Return the green component of the `Color`.
95 pub fn g(&self) -> u8 {
96 self.1
97 }
98
99 /// Return the blue component of the `Color`.
100 pub fn b(&self) -> u8 {
101 self.2
102 }
103}
104
105#[cfg(test)]
106mod color_tests {
107 use super::Color;
108
109 #[test]
110 fn values() {
111 let c = Color::from_rgb(10, 20, 30);
112 assert_eq!(c.r(), 10);
113 assert_eq!(c.g(), 20);
114 assert_eq!(c.b(), 30);
115 }
116}
117
118/// Implement this to get your own data into a `Graph`.
119pub trait DataSource: Debug + Send {
120 /// Return whatever points you have available when this method is called.
121 ///
122 /// Each point must have a `t` field greater than the previous point.
123 ///
124 /// Each point must have a `vs` field with length equal to the
125 /// value returned by `get_num_values`.
126 ///
127 /// This is currently called once a frame.
128 fn get_data(&mut self) -> Result<Vec<Point>>;
129
130 /// The number of values that each Point will have.
131 fn get_num_values(&self) -> Result<usize>;
132
133 /// Return the colors you want to use to display each value of the graph.
134 ///
135 /// Some sample colors are returned by default.
136 ///
137 /// If you don't supply enough colors for the number of values
138 /// returned, these colors will be repeated.
139 fn get_colors(&self) -> Result<Vec<Color>> {
140 Ok(vec![Color(255u8, 0u8, 0u8),
141 Color(0u8, 255u8, 0u8),
142 Color(0u8, 0u8, 255u8)
143 ])
144 }
145}