From a3529772cd0d9329ed40f34ec9beeae2f767ab05 Mon Sep 17 00:00:00 2001 From: Olivier 'reivilibre Date: Sun, 28 Nov 2021 16:08:52 +0000 Subject: [PATCH] Add vertical axis ticks to graph --- Cargo.lock | 10 ++++++++++ bare-metrics-gui/Cargo.toml | 1 + bare-metrics-gui/src/graph.rs | 37 ++++++++++++++++++++++++++++++++--- 3 files changed, 45 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7d62c7f..285dca1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -124,6 +124,15 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" +[[package]] +name = "axis-ticks" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b8d71f0cd2d8ee5cdf22cc6405287931603f7aa312f6eee0f356320d72f08b1" +dependencies = [ + "num-traits", +] + [[package]] name = "backtrace" version = "0.3.63" @@ -154,6 +163,7 @@ version = "0.1.0" dependencies = [ "anyhow", "appdirs", + "axis-ticks", "bare-metrics-core", "bare-metrics-reader", "eframe", diff --git a/bare-metrics-gui/Cargo.toml b/bare-metrics-gui/Cargo.toml index fe1f156..a8bcb74 100644 --- a/bare-metrics-gui/Cargo.toml +++ b/bare-metrics-gui/Cargo.toml @@ -21,3 +21,4 @@ rfd = "0.6.1" ron = "0.7.0" serde = { version = "1.0.130", features = ["derive"] } appdirs = "0.2.0" +axis-ticks = "0.1.0" diff --git a/bare-metrics-gui/src/graph.rs b/bare-metrics-gui/src/graph.rs index 3fd26e4..e4a9af7 100644 --- a/bare-metrics-gui/src/graph.rs +++ b/bare-metrics-gui/src/graph.rs @@ -4,7 +4,8 @@ use bare_metrics_core::structures::{ }; use bare_metrics_reader::{MetricsLogReader, SeekToken}; use eframe::egui::{ - Color32, Frame as EguiFrame, PointerButton, Pos2, Rect, Sense, Stroke, Ui, Vec2, + Color32, Frame as EguiFrame, Galley, PointerButton, Pos2, Rect, Sense, Stroke, TextStyle, Ui, + Vec2, }; use hdrhistogram::Histogram; use log::{debug, error, info}; @@ -762,13 +763,43 @@ impl Graph { // let display_transform = // emath::RectTransform::from_to(Rect::from_x_y_ranges(x_axis, y_axis), display_rect); - let display_transform = - GraphTransform::from_ranges_and_display_rect(x_axis, y_axis, display_rect); + let display_transform = GraphTransform::from_ranges_and_display_rect( + x_axis.clone(), + y_axis.clone(), + display_rect, + ); let stroke = Stroke::new(2.0, Color32::GREEN); let mut last_point = None; + let ticks = axis_ticks::ticks(y_axis.start().clone(), y_axis.end().clone(), 10); + let tick_stroke = Stroke { + width: 0.5, + color: Color32::LIGHT_GRAY, + }; + for tick in ticks { + let text = ui.fonts().layout_no_wrap( + format!("{}", tick), + TextStyle::Small, + Color32::LIGHT_GRAY, + ); + + let mut text_rect = text.rect; + text_rect.set_center(display_transform * (*x_axis.start(), tick)); + text_rect.set_left(display_rect.left()); + ui.painter().line_segment( + [ + display_transform * (*x_axis.start(), tick) + + Vec2::new(5.0 + text.rect.width(), 0.0), + display_transform * (*x_axis.end(), tick), + ], + tick_stroke, + ); + + ui.painter().galley(text_rect.left_top(), text); + } + if let Some(scalar) = current_window .counters .get(&metric_id)