Skip to content

Commit

Permalink
fix [BUG] Rotated labels overlap with the chart #551 - Do not overrid…
Browse files Browse the repository at this point in the history
…e the user-defined `label_style.pos`
  • Loading branch information
AliMMehr committed Feb 28, 2024
1 parent b42a78e commit 313f2a9
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 25 deletions.
6 changes: 4 additions & 2 deletions plotters-backend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,12 +235,14 @@ pub trait DrawingBackend: Sized {
let ((min_x, min_y), (max_x, max_y)) = layout;
let width = (max_x - min_x) as i32;
let height = (max_y - min_y) as i32;
let dx = match style.anchor().h_pos {

let anchor = style.anchor().unwrap_or_default();
let dx = match anchor.h_pos {
HPos::Left => 0,
HPos::Right => -width,
HPos::Center => -width / 2,
};
let dy = match style.anchor().v_pos {
let dy = match anchor.v_pos {
VPos::Top => 0,
VPos::Center => -height / 2,
VPos::Bottom => -height,
Expand Down
5 changes: 2 additions & 3 deletions plotters-backend/src/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ pub mod text_anchor {
pub fn new(h_pos: HPos, v_pos: VPos) -> Self {
Pos { h_pos, v_pos }
}

}
}

Expand Down Expand Up @@ -214,8 +213,8 @@ pub trait BackendTextStyle {
FontStyle::Normal
}

fn anchor(&self) -> text_anchor::Pos {
text_anchor::Pos::default()
fn anchor(&self) -> Option<text_anchor::Pos> {
None
}

fn family(&self) -> FontFamily;
Expand Down
9 changes: 5 additions & 4 deletions plotters-svg/src/svg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -394,13 +394,14 @@ impl<'a> DrawingBackend for SVGBackend<'a> {
}

let (x0, y0) = pos;
let text_anchor = match style.anchor().h_pos {
let anchor = style.anchor().unwrap_or_default();
let text_anchor = match anchor.h_pos {
HPos::Left => "start",
HPos::Right => "end",
HPos::Center => "middle",
};

let dy = match style.anchor().v_pos {
let dy = match anchor.v_pos {
VPos::Top => "0.76em",
VPos::Center => "0.5ex",
VPos::Bottom => "-0.5ex",
Expand All @@ -410,12 +411,12 @@ impl<'a> DrawingBackend for SVGBackend<'a> {
{
let ((fx0, fy0), (fx1, fy1)) =
font.layout_box(text).map_err(DrawingErrorKind::FontError)?;
let x0 = match style.anchor().h_pos {
let x0 = match anchor.h_pos {
HPos::Left => x0,
HPos::Center => x0 - fx1 / 2 + fx0 / 2,
HPos::Right => x0 - fx1 + fx0,
};
let y0 = match style.anchor().v_pos {
let y0 = match anchor.v_pos {
VPos::Top => y0,
VPos::Center => y0 - fy1 / 2 + fy0 / 2,
VPos::Bottom => y0 - fy1 + fy0,
Expand Down
8 changes: 6 additions & 2 deletions plotters/src/chart/context/cartesian2d/draw_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,8 +247,12 @@ impl<'a, DB: DrawingBackend, X: Ranged, Y: Ranged> ChartContext<'a, DB, Cartesia
(cx, cy + label_offset)
};

let label_style = &label_style.pos(Pos::new(h_pos, v_pos));
area.draw_text(t, label_style, (text_x, text_y))?;
let label_style_with_new_pos = label_style.pos(Pos::new(h_pos, v_pos));
let label_style = match label_style.anchor() {
Some(_) => label_style,
None => &label_style_with_new_pos,
};
area.draw_text(t, &label_style, (text_x, text_y))?;

if tick_size != 0 {
if let Some(style) = axis_style {
Expand Down
20 changes: 11 additions & 9 deletions plotters/src/chart/context/cartesian3d/draw_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,16 +95,18 @@ where
let logic_pos = Coord3D::build_coord([&pos[0], &pos[1], &pos[2]]);
let mut font = font.clone();

match dir.0.cmp(&0) {
Ordering::Less => font.pos = Pos::new(HPos::Right, VPos::Center),
Ordering::Greater => font.pos = Pos::new(HPos::Left, VPos::Center),
_ => (),
}
if font.pos.is_none() {
match dir.0.cmp(&0) {
Ordering::Less => font.pos = Some(Pos::new(HPos::Right, VPos::Center)),
Ordering::Greater => font.pos = Some(Pos::new(HPos::Left, VPos::Center)),
_ => (),
}

match dir.1.cmp(&0) {
Ordering::Less => font.pos = Pos::new(HPos::Center, VPos::Bottom),
Ordering::Greater => font.pos = Pos::new(HPos::Center, VPos::Top),
_ => (),
match dir.1.cmp(&0) {
Ordering::Less => font.pos = Some(Pos::new(HPos::Center, VPos::Bottom)),
Ordering::Greater => font.pos = Some(Pos::new(HPos::Center, VPos::Top)),
_ => (),
}
}

let element = EmptyElement::at(logic_pos)
Expand Down
2 changes: 1 addition & 1 deletion plotters/src/style/font/font_desc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ impl<'a> FontDesc<'a> {
TextStyle {
font: self.clone(),
color: color.to_backend_color(),
pos: Pos::default(),
pos: None,
}
}

Expand Down
8 changes: 4 additions & 4 deletions plotters/src/style/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub struct TextStyle<'a> {
/// The text color
pub color: BackendColor,
/// The anchor point position
pub pos: text_anchor::Pos,
pub pos: Option<text_anchor::Pos>,
}

/// Trait for values that can be converted into `TextStyle` values
Expand Down Expand Up @@ -191,7 +191,7 @@ impl<'a> TextStyle<'a> {
Self {
font: self.font.clone(),
color: self.color,
pos,
pos: Some(pos),
}
}
}
Expand Down Expand Up @@ -276,7 +276,7 @@ impl<'a, T: Into<FontDesc<'a>>> From<T> for TextStyle<'a> {
Self {
font: font.into(),
color: BLACK.to_backend_color(),
pos: text_anchor::Pos::default(),
pos: Some(text_anchor::Pos::default()),
}
}
}
Expand Down Expand Up @@ -304,7 +304,7 @@ impl<'a> BackendTextStyle for TextStyle<'a> {
self.font.layout_box(text)
}

fn anchor(&self) -> text_anchor::Pos {
fn anchor(&self) -> Option<text_anchor::Pos> {
self.pos
}

Expand Down

0 comments on commit 313f2a9

Please sign in to comment.