add charts

This commit is contained in:
Scott Pruett 2022-06-11 15:20:57 -04:00
parent e524758a42
commit 26be78209a
1 changed files with 71 additions and 7 deletions

View File

@ -25,7 +25,7 @@ use crate::tts::Tts;
use analysis::save_frames_from; use analysis::save_frames_from;
use config::Config; use config::Config;
use eframe::{ use eframe::{
egui::{self, FontTweak, Ui, Visuals}, egui::{self, plot::{LinkedAxisGroup}, FontTweak, Ui, Visuals},
emath::Vec2, emath::Vec2,
epaint::Color32, epaint::Color32,
}; };
@ -78,12 +78,7 @@ fn main() -> anyhow::Result<()> {
); );
} }
fn show_optional_u8_with_diff( fn show_optional_u8_with_diff(ui: &mut Ui, color: Color32, old: &Option<u8>, v: &Option<u8>) {
ui: &mut Ui,
color: Color32,
old: &Option<u8>,
v: &Option<u8>,
) {
let diff_string = match (old, v) { let diff_string = match (old, v) {
(Some(old), Some(v)) => { (Some(old), Some(v)) => {
let diff = *v as isize - *old as isize; let diff = *v as isize - *old as isize;
@ -298,6 +293,75 @@ fn show_race_state(
prev_lap = Some(lap); prev_lap = Some(lap);
} }
}); });
let axis_group = LinkedAxisGroup::x();
let plot = egui::plot::Plot::new("health-gas-tyres")
.width(600.)
.height(200.)
.link_axis(axis_group.clone());
fn push_all_values<T: Copy + Into<f64>>(values: &mut Vec<egui::plot::Value>, data: &[T], lap_number: usize) {
for (i, d) in data.iter().enumerate() {
let x = (lap_number as f64) + (i as f64 / data.len() as f64);
values.push(egui::plot::Value { x, y: (*d).into() })
}
}
plot.show(ui, |plot| {
let mut health_values = Vec::new();
let mut gas_values = Vec::new();
let mut tyre_values = Vec::new();
for (i, lap) in race.laps.iter().enumerate() {
push_all_values(&mut health_values, &lap.metrics.health, i);
push_all_values(&mut gas_values, &lap.metrics.gas, i);
push_all_values(&mut tyre_values, &lap.metrics.tyres, i);
}
plot.line(
egui::plot::Line::new(egui::plot::Values::from_values(health_values))
.name("health")
.color(Color32::RED),
);
plot.line(
egui::plot::Line::new(egui::plot::Values::from_values(gas_values))
.name("gas")
.color(Color32::LIGHT_BLUE),
);
plot.line(
egui::plot::Line::new(egui::plot::Values::from_values(tyre_values))
.name("tyres")
.color(Color32::GREEN),
);
});
let plot = egui::plot::Plot::new("position")
.width(600.)
.height(200.)
.link_axis(axis_group.clone());
plot.show(ui, |plot| {
let mut pos_values = Vec::new();
for (i, lap) in race.laps.iter().enumerate() {
push_all_values(&mut pos_values, &lap.metrics.position, i);
}
plot.line(
egui::plot::Line::new(egui::plot::Values::from_values(pos_values))
.name("position")
.color(Color32::LIGHT_GRAY),
);
});
let plot = egui::plot::Plot::new("pace")
.width(600.)
.height(200.)
.link_axis(axis_group);
plot.show(ui, |plot| {
let mut lap_values = Vec::new();
for (i, lap) in race.laps.iter().enumerate() {
if let Some(lap_time) = lap.lap_time {
lap_values.push(egui::plot::Value { x: (i + 1) as f64, y: lap_time.as_secs_f64() })
}
}
plot.line(
egui::plot::Line::new(egui::plot::Values::from_values(lap_values))
.name("pace")
.color(Color32::LIGHT_GREEN),
);
});
} }
fn show_debug_frames( fn show_debug_frames(