Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions demo/src/plot_demo.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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"));
Expand Down
17 changes: 16 additions & 1 deletion egui_plot/src/items/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -666,6 +667,7 @@ impl Text {
position,
color: Color32::TRANSPARENT,
anchor: Align2::CENTER_CENTER,
angle: 0.0,
}
}

Expand All @@ -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 {
Expand Down Expand Up @@ -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(
Expand Down