From 46540aeb73c7400ea14939a4ef84c781490ec651 Mon Sep 17 00:00:00 2001 From: Justin Bell Date: Wed, 2 Jul 2025 15:46:53 -0500 Subject: [PATCH 1/5] Adding rotation to text --- demo/src/plot_demo.rs | 9 +++++++-- egui_plot/src/items/mod.rs | 11 ++++++++++- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/demo/src/plot_demo.rs b/demo/src/plot_demo.rs index 15ae9967..047b7ba3 100644 --- a/demo/src/plot_demo.rs +++ b/demo/src/plot_demo.rs @@ -1,6 +1,6 @@ use std::f64::consts::TAU; use std::ops::RangeInclusive; - +use eframe::emath::Align2; use egui::{ Color32, ComboBox, NumExt as _, Pos2, Response, ScrollArea, Stroke, TextWrapMode, Vec2b, WidgetInfo, WidgetType, remap, vec2, @@ -818,7 +818,12 @@ impl ItemsDemo { plot_ui.polygon(polygon.name("Convex polygon").id("convex_polygon")); plot_ui.points(points.name("Points with stems").id("points_with_stems")); plot_ui.text(Text::new("Text", PlotPoint::new(-3.0, -3.0), "wow").id("text0")); - plot_ui.text(Text::new("Text", PlotPoint::new(-2.0, 2.5), "so graph").id("text1")); + plot_ui.text( + Text::new("Text", PlotPoint::new(-2.0, 2.5), "so graph") + .angle(-std::f32::consts::FRAC_PI_4) + .anchor(Align2::CENTER_TOP) + .id("text1") + ); plot_ui.text(Text::new("Text", PlotPoint::new(3.0, 3.0), "much color").id("text2")); plot_ui.text(Text::new("Text", PlotPoint::new(2.5, -2.0), "such plot").id("text3")); plot_ui.image(image.name("Image")); diff --git a/egui_plot/src/items/mod.rs b/egui_plot/src/items/mod.rs index f211e581..2aca06a1 100644 --- a/egui_plot/src/items/mod.rs +++ b/egui_plot/src/items/mod.rs @@ -656,6 +656,7 @@ pub struct Text { pub(super) position: PlotPoint, pub(super) color: Color32, pub(super) anchor: Align2, + pub(super) angle: f32, } impl Text { @@ -666,6 +667,7 @@ impl Text { position, color: Color32::TRANSPARENT, anchor: Align2::CENTER_CENTER, + angle: 0.0, } } @@ -676,6 +678,13 @@ impl Text { self } + /// Text rotation angle. + #[inline] + pub fn angle(mut self, angle: f32) -> Self { + self.angle = angle; + self + } + /// Anchor position of the text. Default is `Align2::CENTER_CENTER`. #[inline] pub fn anchor(mut self, anchor: Align2) -> Self { @@ -704,7 +713,7 @@ impl PlotItem for Text { let pos = transform.position_from_point(&self.position); let rect = self.anchor.anchor_size(pos, galley.size()); - shapes.push(TextShape::new(rect.min, galley, color).into()); + shapes.push(TextShape::new(rect.min, galley, color).with_angle(self.angle).into()); if self.base.highlight { shapes.push(Shape::rect_stroke( From ad8213c811fb8ef050260b1e391076b3a3db5103 Mon Sep 17 00:00:00 2001 From: Justin Bell Date: Wed, 2 Jul 2025 16:03:23 -0500 Subject: [PATCH 2/5] Addressing linter recommendations --- demo/src/plot_demo.rs | 7 ++++--- egui_plot/src/items/mod.rs | 6 +++++- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/demo/src/plot_demo.rs b/demo/src/plot_demo.rs index 047b7ba3..7c9cf08d 100644 --- a/demo/src/plot_demo.rs +++ b/demo/src/plot_demo.rs @@ -1,11 +1,12 @@ -use std::f64::consts::TAU; -use std::ops::RangeInclusive; use eframe::emath::Align2; use egui::{ Color32, ComboBox, NumExt as _, Pos2, Response, ScrollArea, Stroke, TextWrapMode, Vec2b, WidgetInfo, WidgetType, remap, vec2, }; +use std::f64::consts::TAU; +use std::ops::RangeInclusive; + use egui_plot::{ Arrows, AxisHints, Bar, BarChart, BoxElem, BoxPlot, BoxSpread, CoordinatesFormatter, Corner, GridInput, GridMark, HLine, Legend, Line, LineStyle, MarkerShape, Plot, PlotImage, PlotPoint, @@ -822,7 +823,7 @@ impl ItemsDemo { Text::new("Text", PlotPoint::new(-2.0, 2.5), "so graph") .angle(-std::f32::consts::FRAC_PI_4) .anchor(Align2::CENTER_TOP) - .id("text1") + .id("text1"), ); plot_ui.text(Text::new("Text", PlotPoint::new(3.0, 3.0), "much color").id("text2")); plot_ui.text(Text::new("Text", PlotPoint::new(2.5, -2.0), "such plot").id("text3")); diff --git a/egui_plot/src/items/mod.rs b/egui_plot/src/items/mod.rs index 2aca06a1..cea83331 100644 --- a/egui_plot/src/items/mod.rs +++ b/egui_plot/src/items/mod.rs @@ -713,7 +713,11 @@ impl PlotItem for Text { let pos = transform.position_from_point(&self.position); let rect = self.anchor.anchor_size(pos, galley.size()); - shapes.push(TextShape::new(rect.min, galley, color).with_angle(self.angle).into()); + shapes.push( + TextShape::new(rect.min, galley, color) + .with_angle(self.angle) + .into(), + ); if self.base.highlight { shapes.push(Shape::rect_stroke( From 678f412ef4e9481e718f165d25a6e4a2ca62d3bd Mon Sep 17 00:00:00 2001 From: Justin Bell Date: Mon, 7 Jul 2025 06:27:16 -0500 Subject: [PATCH 3/5] Reintroducing original "so graph" text label, adding name attribute for plot differentiation --- demo/src/plot_demo.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/demo/src/plot_demo.rs b/demo/src/plot_demo.rs index 7c9cf08d..09d38b93 100644 --- a/demo/src/plot_demo.rs +++ b/demo/src/plot_demo.rs @@ -819,11 +819,13 @@ impl ItemsDemo { plot_ui.polygon(polygon.name("Convex polygon").id("convex_polygon")); plot_ui.points(points.name("Points with stems").id("points_with_stems")); plot_ui.text(Text::new("Text", PlotPoint::new(-3.0, -3.0), "wow").id("text0")); + plot_ui.text(Text::new("Text", PlotPoint::new(-2.0, 2.5), "so graph").id("text1")); plot_ui.text( - Text::new("Text", PlotPoint::new(-2.0, 2.5), "so graph") + Text::new("Text", PlotPoint::new(-2.0, 2.5), "Very angle") .angle(-std::f32::consts::FRAC_PI_4) .anchor(Align2::CENTER_TOP) - .id("text1"), + .name("Angled text") + .id("text_angled"), ); plot_ui.text(Text::new("Text", PlotPoint::new(3.0, 3.0), "much color").id("text2")); plot_ui.text(Text::new("Text", PlotPoint::new(2.5, -2.0), "such plot").id("text3")); From 873be17bf90ab2839e4aa6d10c56c55924b46f06 Mon Sep 17 00:00:00 2001 From: Justin Bell Date: Mon, 7 Jul 2025 06:29:10 -0500 Subject: [PATCH 4/5] Adding CENTER_CENTER pivot; documenting usage and effective behavior of rotation angle --- egui_plot/src/items/mod.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/egui_plot/src/items/mod.rs b/egui_plot/src/items/mod.rs index cea83331..bc9968be 100644 --- a/egui_plot/src/items/mod.rs +++ b/egui_plot/src/items/mod.rs @@ -678,7 +678,9 @@ impl Text { self } - /// Text rotation angle. + /// Sets the text rotation angle. Angles are defined in radians, with positive values + /// resulting in a clockwise direction. Rotations are performed about the center of the + /// bounding box. #[inline] pub fn angle(mut self, angle: f32) -> Self { self.angle = angle; @@ -715,7 +717,7 @@ impl PlotItem for Text { shapes.push( TextShape::new(rect.min, galley, color) - .with_angle(self.angle) + .with_angle_and_anchor(self.angle, Align2::CENTER_CENTER) .into(), ); From 1ab4ac17ff7289d5dde9687d5a1979020d5941f4 Mon Sep 17 00:00:00 2001 From: Justin Bell Date: Mon, 7 Jul 2025 06:30:03 -0500 Subject: [PATCH 5/5] Positioning demo label to avoid existing "so graph" label, updating anchor to better demonstrate how it affects positioning --- demo/src/plot_demo.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/demo/src/plot_demo.rs b/demo/src/plot_demo.rs index 09d38b93..9f9252e4 100644 --- a/demo/src/plot_demo.rs +++ b/demo/src/plot_demo.rs @@ -821,9 +821,9 @@ impl ItemsDemo { plot_ui.text(Text::new("Text", PlotPoint::new(-3.0, -3.0), "wow").id("text0")); plot_ui.text(Text::new("Text", PlotPoint::new(-2.0, 2.5), "so graph").id("text1")); plot_ui.text( - Text::new("Text", PlotPoint::new(-2.0, 2.5), "Very angle") + Text::new("Text", PlotPoint::new(-1.5, 2.0), "Very angle") .angle(-std::f32::consts::FRAC_PI_4) - .anchor(Align2::CENTER_TOP) + .anchor(Align2::LEFT_TOP) .name("Angled text") .id("text_angled"), );