From 99439e44d58eac743d28cb6d84b9474fa056b726 Mon Sep 17 00:00:00 2001 From: alemi Date: Sat, 11 Jun 2022 05:51:40 +0200 Subject: [PATCH] feat: improved plot marks made timeserie plot marks align to days and hours. Also added some collapsable empty space at the bottom of sources panel to allow to shift the last ones up some space and still be able to edit their color. --- src/app/mod.rs | 26 +++++++++++++++++++++++--- src/app/util.rs | 41 +++++++++++++++++++++++++---------------- 2 files changed, 48 insertions(+), 19 deletions(-) diff --git a/src/app/mod.rs b/src/app/mod.rs index 50c03b6..34875da 100644 --- a/src/app/mod.rs +++ b/src/app/mod.rs @@ -5,6 +5,7 @@ pub mod util; use std::sync::Arc; use chrono::Utc; use eframe::egui; +use eframe::egui::plot::GridMark; use eframe::egui::{RichText, plot::{Line, Plot}, Color32}; use self::data::ApplicationState; @@ -152,6 +153,10 @@ impl eframe::App for App { }); }); } + ui.collapsing("extra space", |ui| { + ui.add_space(300.0); + ui.separator(); + }) }); }); } @@ -201,14 +206,29 @@ impl eframe::App for App { } if panel.timeserie { - p = p.x_axis_formatter(|x, _range| timestamp_to_str(x as i64)); + p = p.x_axis_formatter(|x, _range| timestamp_to_str(x as i64, true, false)); p = p.label_formatter(|name, value| { if !name.is_empty() { - return format!("{}\nx = {}\ny = {:.1}", name, timestamp_to_str(value.x as i64), value.y) + return format!("{}\nx = {}\ny = {:.1}", name, timestamp_to_str(value.x as i64, false, true), value.y) } else { - return format!("x = {}\ny = {:.1}", timestamp_to_str(value.x as i64), value.y); + return format!("x = {}\ny = {:.1}", timestamp_to_str(value.x as i64, false, true), value.y); } }); + p = p.x_grid_spacer(|grid| { + let (start, end) = grid.bounds; + let mut counter = (start as i64) - ((start as i64) % 3600); + let mut out : Vec = Vec::new(); + loop { + counter += 3600; + if counter > end as i64 { break; } + if counter % 86400 == 0 { + out.push(GridMark { value: counter as f64, step_size: 86400 as f64 }) + } else if counter % 3600 == 0 { + out.push(GridMark { value: counter as f64, step_size: 3600 as f64 }); + } + } + return out; + }); } p.show(ui, |plot_ui| { diff --git a/src/app/util.rs b/src/app/util.rs index f9721b5..55325d7 100644 --- a/src/app/util.rs +++ b/src/app/util.rs @@ -1,42 +1,51 @@ // if you're handling more than terabytes of data, it's the future and you ought to update this code! -use chrono::{DateTime, Utc, NaiveDateTime}; +use chrono::{DateTime, NaiveDateTime, Utc, Local}; use eframe::egui::Color32; -const PREFIXES: &'static [&'static str] = &["", "k", "M", "G", "T"]; +const PREFIXES: &'static [&'static str] = &["", "k", "M", "G", "T"]; -pub fn human_size(size:u64) -> String { - let mut buf : f64 = size as f64; - let mut prefix : usize = 0; - while buf > 1024.0 && prefix < PREFIXES.len() -1 { +pub fn human_size(size: u64) -> String { + let mut buf: f64 = size as f64; + let mut prefix: usize = 0; + while buf > 1024.0 && prefix < PREFIXES.len() - 1 { buf /= 1024.0; prefix += 1; } - + return format!("{:.3} {}B", buf, PREFIXES[prefix]); } -pub fn timestamp_to_str(t:i64) -> String { +pub fn timestamp_to_str(t: i64, date: bool, time: bool) -> String { format!( "{}", - DateTime::::from_utc(NaiveDateTime::from_timestamp(t, 0), Utc) - .format("%Y/%m/%d %H:%M:%S") + DateTime::::from(DateTime::::from_utc(NaiveDateTime::from_timestamp(t, 0), Utc)).format( + if date && time { + "%Y/%m/%d %H:%M:%S" + } else if date { + "%Y/%m/%d" + } else if time { + "%H:%M:%S" + } else { + "%s" + } + ) ) } pub fn unpack_color(c: u32) -> Color32 { - let r : u8 = (c >> 0) as u8; - let g : u8 = (c >> 8) as u8; - let b : u8 = (c >> 16) as u8; - let a : u8 = (c >> 24) as u8; + let r: u8 = (c >> 0) as u8; + let g: u8 = (c >> 8) as u8; + let b: u8 = (c >> 16) as u8; + let a: u8 = (c >> 24) as u8; return Color32::from_rgba_unmultiplied(r, g, b, a); } pub fn repack_color(c: Color32) -> u32 { - let mut out : u32 = 0; + let mut out: u32 = 0; let mut offset = 0; for el in c.to_array() { out |= ((el & 0xFF) as u32) << offset; offset += 8; } return out; -} \ No newline at end of file +}