Skip to content

Commit fa604ae

Browse files
Add HPos and VPos::Default to allow the user to override axes label anchors
1 parent b804891 commit fa604ae

File tree

5 files changed

+24
-8
lines changed

5 files changed

+24
-8
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: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ pub mod text_anchor {
6262
/// The horizontal position of the anchor point relative to the text.
6363
#[derive(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,
6568
/// Anchor point is on the left side of the text
6669
Left,
6770
/// Anchor point is on the right side of the text
@@ -73,6 +76,9 @@ pub mod text_anchor {
7376
/// The vertical position of the anchor point relative to the text.
7477
#[derive(Clone, Copy)]
7578
pub enum VPos {
79+
/// Default value (Top), except chart axes that might provide a different pos
80+
/// Use another variant to override the default positionning
81+
Default,
7682
/// Anchor point is on the top of the text
7783
Top,
7884
/// Anchor point is in the vertical center of the text
@@ -117,8 +123,8 @@ pub mod text_anchor {
117123
/// ```
118124
pub fn default() -> Self {
119125
Pos {
120-
h_pos: HPos::Left,
121-
v_pos: VPos::Top,
126+
h_pos: HPos::Default,
127+
v_pos: VPos::Default,
122128
}
123129
}
124130
}

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)