egui_plotter/charts/timedata.rs
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
//! Animatable chart with data on the Y and time on the X axis
use egui::Ui;
use plotters::style::{RGBAColor, ShapeStyle};
use crate::charts::XyTimeData;
/// Animatabe chart with time on the X axis and data on the Y axis.
///
/// ## Usage
///
/// Creating the chart is very simple. You only need to provide 4 parameters,
/// 3 of which are just strings.
///
/// * `points`: A slice of tuples, arranged so that the first float is the time
/// and the second is the data.
/// * `unit`: String describing the data on the Y axis.
/// * `caption`: String to be shown as the caption of the chart.
///
/// This will create a basic line chart with nothing fancy, which you can easily
/// add to your egui project. You can also animate this chart with `.toggle_playback()`
/// and adjust various parameters with the many `.set_` functions included.
pub struct TimeData {
chart: XyTimeData,
}
impl TimeData {
/// Create a new TimeData chart. See [Usage](#usage).
pub fn new(points: &[(f32, f32)], unit: &str, caption: &str) -> Self {
let points: Vec<(f32, f32, f32)> = points
.into_iter()
.map(|(data, time)| (*data, *time, *time))
.collect();
let chart = XyTimeData::new(&points, "seconds", unit, caption);
Self { chart }
}
/// Set the time to resume playback at. Time is in seconds.
#[inline]
pub fn set_time(&mut self, time: f32) {
self.chart.set_time(time)
}
/// Set the time to resume playback at. Time is in seconds. Consumes self.
#[inline]
pub fn time(mut self, time: f32) -> Self {
self.set_time(time);
self
}
/// Set the playback speed. 1.0 is normal speed, 2.0 is double, & 0.5 is half.
#[inline]
pub fn set_playback_speed(&mut self, speed: f32) {
self.chart.set_playback_speed(speed)
}
/// Set the playback speed. 1.0 is normal speed, 2.0 is double, & 0.5 is half. Consumes self.
#[inline]
pub fn playback_speed(mut self, speed: f32) -> Self {
self.set_playback_speed(speed);
self
}
#[inline]
/// Set the style of the plotted line.
pub fn set_line_style(&mut self, line_style: ShapeStyle) {
self.chart.set_line_style(line_style)
}
#[inline]
/// Set the style of the plotted line. Consumes self.
pub fn line_style(mut self, line_style: ShapeStyle) -> Self {
self.set_line_style(line_style);
self
}
#[inline]
/// Set the style of the grid.
pub fn set_grid_style(&mut self, grid_style: ShapeStyle) {
self.chart.set_grid_style(grid_style)
}
#[inline]
/// Set the style of the grid. Consumes self.
pub fn grid_style(mut self, grid_style: ShapeStyle) -> Self {
self.set_grid_style(grid_style);
self
}
#[inline]
/// Set the style of the subgrid.
pub fn set_subgrid_style(&mut self, subgrid_style: ShapeStyle) {
self.chart.set_subgrid_style(subgrid_style)
}
#[inline]
/// Set the style of the subgrid. Consumes self.
pub fn subgrid_style(mut self, subgrid_style: ShapeStyle) -> Self {
self.set_subgrid_style(subgrid_style);
self
}
#[inline]
/// Set the style of the axes.
pub fn set_axes_style(&mut self, axes_style: ShapeStyle) {
self.chart.set_axes_style(axes_style)
}
#[inline]
/// Set the style of the plotted line. Consumes self.
pub fn axes_style(mut self, axes_style: ShapeStyle) -> Self {
self.set_axes_style(axes_style);
self
}
#[inline]
/// Set the text color of the chart.
pub fn set_text_color<T>(&mut self, color: T)
where
T: Into<RGBAColor>,
{
self.chart.set_text_color(color)
}
#[inline]
/// Set the text color of the chart. Consumes self.
pub fn text_color<T>(mut self, color: T) -> Self
where
T: Into<RGBAColor>,
{
self.set_text_color(color);
self
}
#[inline]
/// Set the background color of the chart.
pub fn set_background_color<T>(&mut self, color: T)
where
T: Into<RGBAColor>,
{
self.chart.set_background_color(color);
}
#[inline]
/// Set the background color of the chart. Consumes self.
pub fn background_color<T>(mut self, color: T) -> Self
where
T: Into<RGBAColor>,
{
self.set_background_color(color);
self
}
#[inline]
/// Set the ratio between X and Y values, default being 1 x unit to 1 y unit.
pub fn set_ratio(&mut self, ratio: f32) {
self.chart.set_ratio(ratio);
}
#[inline]
/// Set the ratio between X and Y values, default being 1 x unit to 1 y unit. Consumes self.
pub fn ratio(mut self, ratio: f32) -> Self {
self.set_ratio(ratio);
self
}
/// Draw the chart to a Ui. Will also proceed to animate the chart if playback is currently
/// enabled.
pub fn draw(&mut self, ui: &Ui) {
self.chart.draw(ui)
}
/// Start/enable playback of the chart.
#[inline]
pub fn start_playback(&mut self) {
self.chart.start_playback()
}
/// Stop/disable playback of the chart.
#[inline]
pub fn stop_playback(&mut self) {
self.chart.stop_playback()
}
/// Toggle playback of the chart.
pub fn toggle_playback(&mut self) {
self.chart.toggle_playback()
}
/// Return true if playback is currently enabled & underway.
#[inline]
pub fn is_playing(&self) -> bool {
self.chart.is_playing()
}
/// Return the time the chart starts at when playback is enabled.
#[inline]
pub fn start_time(&self) -> f32 {
self.chart.start_time()
}
/// Return the current time to be animated when playback is enabled.
#[inline]
pub fn current_time(&mut self) -> f32 {
self.chart.current_time()
}
/// Return the time the chart finished animating at when playback is enabled.
#[inline]
pub fn end_time(&self) -> f32 {
self.chart.end_time()
}
/// Return the speed the chart is animated at.
#[inline]
pub fn get_playback_speed(&self) -> f32 {
self.chart.get_playback_speed()
}
}