diff --git a/demo/src/plot_demo.rs b/demo/src/plot_demo.rs index 01d8e219..7c39157e 100644 --- a/demo/src/plot_demo.rs +++ b/demo/src/plot_demo.rs @@ -1,3 +1,4 @@ +use eframe::emath::Align2; use std::ops::RangeInclusive; use std::{f64::consts::TAU, sync::Arc}; @@ -853,6 +854,13 @@ impl ItemsDemo { 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(-1.5, 2.0), "Very angle") + .angle(-std::f32::consts::FRAC_PI_4) + .anchor(Align2::LEFT_TOP) + .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")); plot_ui.image(image.name("Image")); diff --git a/egui_plot/src/items/mod.rs b/egui_plot/src/items/mod.rs index afdfc8ab..88a7dba5 100644 --- a/egui_plot/src/items/mod.rs +++ b/egui_plot/src/items/mod.rs @@ -729,6 +729,7 @@ pub struct Text { pub(super) position: PlotPoint, pub(super) color: Color32, pub(super) anchor: Align2, + pub(super) angle: f32, } impl Text { @@ -739,6 +740,7 @@ impl Text { position, color: Color32::TRANSPARENT, anchor: Align2::CENTER_CENTER, + angle: 0.0, } } @@ -749,6 +751,15 @@ impl Text { self } + /// 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; + self + } + /// Anchor position of the text. Default is `Align2::CENTER_CENTER`. #[inline] pub fn anchor(mut self, anchor: Align2) -> Self { @@ -777,7 +788,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).into()); + shapes.push( + TextShape::new(rect.min, galley, color) + .with_angle_and_anchor(self.angle, Align2::CENTER_CENTER) + .into(), + ); if self.base.highlight { shapes.push(Shape::rect_stroke(