Skip to content

Commit 0a68720

Browse files
committed
Start to make layout dynamic
This starts by calculating the max size of the y-axis labels.
1 parent 0e80bb1 commit 0a68720

File tree

3 files changed

+6
-4
lines changed

3 files changed

+6
-4
lines changed

scatter.png

37 Bytes
Loading

src/page.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ impl<'a> Page<'a> {
6262
let (width, height) = self.dimensions;
6363
let mut document = Document::new().set("viewBox", (0, 0, width, height));
6464

65-
let x_margin = 90; // should actually depend on y-axis label font size
65+
let x_margin = 120; // should actually depend on y-axis label font size
6666
let y_margin = 60;
6767
let x_offset = 0.6 * f64::from(x_margin);
6868
let y_offset = 0.6 * f64::from(y_margin);

src/svg_render.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,17 +110,19 @@ pub fn draw_y_axis(a: &axis::ContinuousAxis, face_height: f64) -> node::element:
110110
labels.append(tick_label);
111111
}
112112

113-
const X_OFFSET: i32 = -30;
113+
let max_tick_length = a.ticks().iter().map(|&t| t.to_string().len()).max().expect("Could not calculate max tick length");
114+
115+
let x_offset = -(y_tick_font_size*max_tick_length as i32);
114116
let y_label_offset = -(face_height / 2.);
115117
let y_label_font_size = 12;
116118
let label = node::element::Text::new()
117-
.set("x", X_OFFSET)
119+
.set("x", x_offset)
118120
.set("y", y_label_offset - f64::from(y_label_font_size))
119121
.set("text-anchor", "middle")
120122
.set("font-size", y_label_font_size)
121123
.set(
122124
"transform",
123-
format!("rotate(-90 {} {})", X_OFFSET, y_label_offset),
125+
format!("rotate(-90 {} {})", x_offset, y_label_offset),
124126
)
125127
.add(node::Text::new(a.get_label()));
126128

0 commit comments

Comments
 (0)