Skip to main content

ggplot_rs/
annotate.rs

1/// Annotation types for adding static marks to a plot.
2#[derive(Clone, Debug)]
3pub enum Annotation {
4    /// Text annotation at a data position.
5    Text {
6        label: String,
7        x: f64,
8        y: f64,
9        size: f64,
10        color: (u8, u8, u8),
11    },
12    /// Rectangle annotation between two data positions.
13    Rect {
14        xmin: f64,
15        xmax: f64,
16        ymin: f64,
17        ymax: f64,
18        fill: (u8, u8, u8),
19        alpha: f64,
20    },
21    /// Line segment annotation between two data positions.
22    Segment {
23        x: f64,
24        y: f64,
25        xend: f64,
26        yend: f64,
27        color: (u8, u8, u8),
28        width: f64,
29    },
30}
31
32impl Annotation {
33    /// Create a text annotation.
34    pub fn text(label: &str, x: f64, y: f64) -> Self {
35        Annotation::Text {
36            label: label.to_string(),
37            x,
38            y,
39            size: 12.0,
40            color: (50, 50, 50),
41        }
42    }
43
44    /// Create a rectangle annotation.
45    pub fn rect(xmin: f64, xmax: f64, ymin: f64, ymax: f64) -> Self {
46        Annotation::Rect {
47            xmin,
48            xmax,
49            ymin,
50            ymax,
51            fill: (200, 200, 200),
52            alpha: 0.3,
53        }
54    }
55
56    /// Create a segment annotation.
57    pub fn segment(x: f64, y: f64, xend: f64, yend: f64) -> Self {
58        Annotation::Segment {
59            x,
60            y,
61            xend,
62            yend,
63            color: (50, 50, 50),
64            width: 1.0,
65        }
66    }
67}