Skip to main content

annotated/
annotated.rs

1use bland::{Figure, PaperSize, Stroke, TextAnchor};
2
3fn main() {
4    let xs: Vec<f64> = (0..=200).map(|i| i as f64 / 20.0).collect();
5    let ys: Vec<f64> = xs.iter().map(|t| (-t / 4.0).exp() * t.cos()).collect();
6
7    // Find the global maximum.
8    let (idx, &peak_y) = ys
9        .iter()
10        .enumerate()
11        .max_by(|a, b| a.1.partial_cmp(b.1).unwrap())
12        .unwrap();
13    let peak_x = xs[idx];
14
15    let fig = Figure::new()
16        .size(PaperSize::A5Landscape)
17        .title("Annotated peak")
18        .xlabel("t [s]")
19        .ylabel("x(t)")
20        .line(&xs, &ys, |s| s.stroke(Stroke::Solid))
21        .annotate_arrow((peak_x + 1.0, peak_y - 0.4), (peak_x, peak_y))
22        .annotate_text(peak_x + 1.05, peak_y - 0.45, "global peak", TextAnchor::Start)
23        .hline(0.0, |h| h.stroke(Stroke::Dotted));
24
25    std::fs::create_dir_all("out").expect("create out/");
26    std::fs::write("out/annotated.svg", fig.to_svg()).expect("write svg");
27    println!("wrote out/annotated.svg");
28}