Skip to content

Commit

Permalink
Merge pull request #6 from bananaturtlesandwich/main
Browse files Browse the repository at this point in the history
fix axis label drawing (#5)
  • Loading branch information
Gip-Gip authored Jul 23, 2023
2 parents a6326a3 + 748781e commit 2a4b060
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 8 deletions.
53 changes: 47 additions & 6 deletions src/backend.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
//! Plotter backend for egui

use std::error::Error as ErrorTrait;
use std::f32::consts::FRAC_PI_2;
use std::fmt::{Display, Formatter, Result as FmtResult};
use std::ops::{Add, AddAssign, MulAssign, Sub, SubAssign};

use egui::{
epaint::PathShape, Align2, Color32, FontFamily as EguiFontFamily, FontId, Pos2, Rect, Stroke,
Ui,
epaint::{PathShape, TextShape},
Align, Align2, Color32, FontFamily as EguiFontFamily, FontId, Pos2, Rect, Stroke, Ui,
};
use plotters_backend::{
text_anchor::{HPos, Pos, VPos},
BackendColor, BackendCoord, BackendStyle, BackendTextStyle, DrawingBackend, DrawingErrorKind,
FontFamily as PlottersFontFamily,
};

#[derive(Debug, Clone, Copy)]
/// Error to be returned by the backend. Since egui doesn't return any errors
/// on any painter operations, this is a stub type.
pub enum EguiBackendError {
None,
}
pub struct EguiBackendError;

impl Display for EguiBackendError {
#[inline]
Expand Down Expand Up @@ -322,7 +322,48 @@ impl<'a> DrawingBackend for EguiBackend<'a> {

let color: Color32 = EguiBackendColor::from(style.color()).into();

painter.text(pos.into(), Align2::LEFT_TOP, text, font, color);
let rotations = style.transform() as usize;
let angle = rotations as f32 * FRAC_PI_2;

let Pos { h_pos, v_pos } = style.anchor();

// !TODO! Find a slightly more eligant rotation function.
let mut anchor = Align2([
match h_pos {
HPos::Left => Align::LEFT,
HPos::Right => Align::RIGHT,
HPos::Center => Align::Center,
},
match v_pos {
VPos::Top => Align::TOP,
VPos::Center => Align::Center,
VPos::Bottom => Align::BOTTOM,
},
]);
fn rotate(anchor: &mut Align2) {
*anchor = match anchor {
&mut Align2::LEFT_TOP => Align2::RIGHT_TOP,
&mut Align2::RIGHT_TOP => Align2::RIGHT_BOTTOM,
&mut Align2::RIGHT_BOTTOM => Align2::LEFT_BOTTOM,
&mut Align2::LEFT_BOTTOM => Align2::LEFT_TOP,
&mut Align2::LEFT_CENTER => Align2::CENTER_TOP,
&mut Align2::CENTER_TOP => Align2::RIGHT_CENTER,
&mut Align2::RIGHT_CENTER => Align2::CENTER_BOTTOM,
&mut Align2::CENTER_BOTTOM => Align2::LEFT_CENTER,
&mut Align2::CENTER_CENTER => Align2::CENTER_CENTER,
}
}
for _ in 0..rotations {
rotate(&mut anchor)
}
let galley = painter.layout_no_wrap(text.to_string(), font, color);
let rect = anchor.anchor_rect(Rect::from_min_size(pos.into(), galley.size()));
if !galley.is_empty() {
painter.add(TextShape {
angle,
..TextShape::new(rect.min, galley)
});
}

Ok(())
}
Expand Down
2 changes: 0 additions & 2 deletions src/charts/xytime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,6 @@ impl XyTimeData {
ranges.push((range_x, range_y));
}

let y_unit: String = y_unit.split("").map(|c| format!("{}\n", c)).collect();

// Turn all the vecs and strings into arcs since they are more or less read-only at
// this point

Expand Down

0 comments on commit 2a4b060

Please sign in to comment.