diff --git a/demo/src/plot_demo.rs b/demo/src/plot_demo.rs index 15ae9967..9f9252e4 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, @@ -819,6 +820,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 f211e581..bc9968be 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,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 { @@ -704,7 +715,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(