Skip to content

Commit 3137913

Browse files
authored
Merge pull request #464 from VincentFoulon80/bugfix/463/axes-label-style
Add HPos and VPos::Default to allow the user to override axes label anchors
2 parents 453b614 + c4bead2 commit 3137913

File tree

5 files changed

+27
-25
lines changed

5 files changed

+27
-25
lines changed

plotters-backend/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,12 +236,12 @@ pub trait DrawingBackend: Sized {
236236
let width = (max_x - min_x) as i32;
237237
let height = (max_y - min_y) as i32;
238238
let dx = match style.anchor().h_pos {
239-
HPos::Left => 0,
239+
HPos::Left | HPos::Default => 0,
240240
HPos::Right => -width,
241241
HPos::Center => -width / 2,
242242
};
243243
let dy = match style.anchor().v_pos {
244-
VPos::Top => 0,
244+
VPos::Top | VPos::Default => 0,
245245
VPos::Center => -height / 2,
246246
VPos::Bottom => -height,
247247
};

plotters-backend/src/text.rs

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,12 @@ impl<'a> From<&'a str> for FontFamily<'a> {
6060
/// ```
6161
pub mod text_anchor {
6262
/// The horizontal position of the anchor point relative to the text.
63-
#[derive(Clone, Copy)]
63+
#[derive(Default, Clone, Copy)]
6464
pub enum HPos {
65+
/// Default value (Left), except chart axes that might provide a different pos.
66+
/// Use another variant to override the default positionning
67+
#[default]
68+
Default,
6569
/// Anchor point is on the left side of the text
6670
Left,
6771
/// Anchor point is on the right side of the text
@@ -71,8 +75,12 @@ pub mod text_anchor {
7175
}
7276

7377
/// The vertical position of the anchor point relative to the text.
74-
#[derive(Clone, Copy)]
78+
#[derive(Default, Clone, Copy)]
7579
pub enum VPos {
80+
/// Default value (Top), except chart axes that might provide a different pos
81+
/// Use another variant to override the default positionning
82+
#[default]
83+
Default,
7684
/// Anchor point is on the top of the text
7785
Top,
7886
/// Anchor point is in the vertical center of the text
@@ -82,7 +90,7 @@ pub mod text_anchor {
8290
}
8391

8492
/// The text anchor position.
85-
#[derive(Clone, Copy)]
93+
#[derive(Default, Clone, Copy)]
8694
pub struct Pos {
8795
/// The horizontal position of the anchor point
8896
pub h_pos: HPos,
@@ -105,22 +113,6 @@ pub mod text_anchor {
105113
pub fn new(h_pos: HPos, v_pos: VPos) -> Self {
106114
Pos { h_pos, v_pos }
107115
}
108-
109-
/// Create a default text anchor position (top left).
110-
///
111-
/// - **returns** The default text anchor position
112-
///
113-
/// ```rust
114-
/// use plotters_backend::text_anchor::{Pos, HPos, VPos};
115-
///
116-
/// let pos = Pos::default();
117-
/// ```
118-
pub fn default() -> Self {
119-
Pos {
120-
h_pos: HPos::Left,
121-
v_pos: VPos::Top,
122-
}
123-
}
124116
}
125117
}
126118

plotters-svg/src/svg.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -395,13 +395,13 @@ impl<'a> DrawingBackend for SVGBackend<'a> {
395395

396396
let (x0, y0) = pos;
397397
let text_anchor = match style.anchor().h_pos {
398-
HPos::Left => "start",
398+
HPos::Left | HPos::Default => "start",
399399
HPos::Right => "end",
400400
HPos::Center => "middle",
401401
};
402402

403403
let dy = match style.anchor().v_pos {
404-
VPos::Top => "0.76em",
404+
VPos::Top | VPos::Default => "0.76em",
405405
VPos::Center => "0.5ex",
406406
VPos::Bottom => "-0.5ex",
407407
};

plotters/examples/console.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,12 +132,12 @@ impl DrawingBackend for TextDrawingBackend {
132132
let (width, height) = self.estimate_text_size(text, style)?;
133133
let (width, height) = (width as i32, height as i32);
134134
let dx = match style.anchor().h_pos {
135-
HPos::Left => 0,
135+
HPos::Left | HPos::Default => 0,
136136
HPos::Right => -width,
137137
HPos::Center => -width / 2,
138138
};
139139
let dy = match style.anchor().v_pos {
140-
VPos::Top => 0,
140+
VPos::Top | VPos::Default => 0,
141141
VPos::Center => -height / 2,
142142
VPos::Bottom => -height,
143143
};

plotters/src/chart/context/cartesian2d/draw_impl.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,16 @@ impl<'a, DB: DrawingBackend, X: Ranged, Y: Ranged> ChartContext<'a, DB, Cartesia
250250
(cx, cy + label_offset)
251251
};
252252

253+
let h_pos = if matches!(label_style.pos.h_pos, HPos::Default) {
254+
h_pos
255+
} else {
256+
label_style.pos.h_pos
257+
};
258+
let v_pos = if matches!(label_style.pos.v_pos, VPos::Default) {
259+
v_pos
260+
} else {
261+
label_style.pos.v_pos
262+
};
253263
let label_style = &label_style.pos(Pos::new(h_pos, v_pos));
254264
area.draw_text(t, label_style, (text_x, text_y))?;
255265

0 commit comments

Comments
 (0)