plotlars-core 0.12.2

Core types and traits for plotlars
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
use crate::components::Rgb;

/// A structure representing text with customizable content, font, size, and color.
///
/// # Example
///
/// ```rust
/// use polars::prelude::*;
/// use plotlars::{Axis, BarPlot, Plot, Text, Rgb};
///
/// let dataset = df![
///         "label" => &[""],
///         "value" => &[0],
///     ]
///     .unwrap();
///
/// let axis = Axis::new()
///     .tick_values(vec![]);
///
/// BarPlot::builder()
///     .data(&dataset)
///     .labels("label")
///     .values("value")
///     .plot_title(
///         Text::from("Title")
///             .x(0.1)
///             .color(Rgb(178, 34, 34))
///             .size(30)
///             .font("Zapfino")
///     )
///     .x_title(
///         Text::from("X")
///             .color(Rgb(65, 105, 225))
///             .size(20)
///             .font("Marker Felt")
///     )
///     .y_title(
///         Text::from("Y")
///             .color(Rgb(255, 140, 0))
///             .size(20)
///             .font("Arial Black")
///     )
///     .x_axis(&axis)
///     .y_axis(&axis)
///     .build()
///     .plot();
/// ```
/// ![Example](https://imgur.com/4outoUQ.png)
#[derive(Clone)]
pub struct Text {
    pub content: String,
    pub font: String,
    pub size: usize,
    pub color: Rgb,
    pub x: f64,
    pub y: f64,
}

impl Default for Text {
    /// Provides default values for the `Text` struct.
    ///
    /// - `content`: An empty string.
    /// - `font`: An empty string.
    /// - `size`: `12` (reasonable default for visibility).
    /// - `color`: Default `Rgb` value.
    /// - `x`: `0.5`.
    /// - `y`: `0.9`.
    fn default() -> Self {
        Text {
            content: String::new(),
            font: String::new(),
            size: 12,
            color: Rgb::default(),
            x: 0.5,
            y: 0.9,
        }
    }
}

impl Text {
    /// Creates a new `Text` instance from the given content.
    ///
    /// # Argument
    ///
    /// * `content` - A value that can be converted into a `String`, representing the textual content.
    pub fn from(content: impl Into<String>) -> Self {
        Self {
            content: content.into(),
            ..Default::default()
        }
    }

    /// Sets the font of the text.
    ///
    /// # Argument
    ///
    /// * `font` - A value that can be converted into a `String`, representing the font name.
    pub fn font(mut self, font: impl Into<String>) -> Self {
        self.font = font.into();
        self
    }

    /// Sets the size of the text.
    ///
    /// # Argument
    ///
    /// * `size` - A `usize` value specifying the font size.
    pub fn size(mut self, size: usize) -> Self {
        self.size = size;
        self
    }

    /// Sets the color of the text.
    ///
    /// # Argument
    ///
    /// * `color` - An `Rgb` value specifying the color of the text.
    pub fn color(mut self, color: Rgb) -> Self {
        self.color = color;
        self
    }

    /// Sets the x-coordinate position of the text.
    ///
    /// # Argument
    ///
    /// * `x` - A `f64` value specifying the horizontal position.
    pub fn x(mut self, x: f64) -> Self {
        self.x = x;
        self
    }

    /// Sets the y-coordinate position of the text.
    ///
    /// # Argument
    ///
    /// * `y` - A `f64` value specifying the vertical position.
    pub fn y(mut self, y: f64) -> Self {
        self.y = y;
        self
    }

    pub fn has_custom_position(&self) -> bool {
        const EPSILON: f64 = 1e-6;
        (self.x - 0.5).abs() > EPSILON || (self.y - 0.9).abs() > EPSILON
    }

    /// Apply default positioning for plot titles (x=0.5, y=0.95 - centered above)
    pub fn with_plot_title_defaults(mut self) -> Self {
        const EPSILON: f64 = 1e-6;
        let y_is_default = (self.y - 0.9).abs() < EPSILON;

        if y_is_default {
            self.y = 0.95;
        }

        self
    }

    /// Apply default positioning for subplot titles (x=0.5, y=1.1 - centered above, higher than overall)
    pub fn with_subplot_title_defaults(mut self) -> Self {
        const EPSILON: f64 = 1e-6;
        let y_is_default = (self.y - 0.9).abs() < EPSILON;
        let y_is_plot_default = (self.y - 0.95).abs() < EPSILON;

        // Override both Text::default (0.9) and plot_title default (0.95)
        if y_is_default || y_is_plot_default {
            self.y = 1.1;
        }

        self
    }

    /// Apply default positioning for x-axis titles (x=0.5, y=-0.15 - centered below)
    pub fn with_x_title_defaults(mut self) -> Self {
        const EPSILON: f64 = 1e-6;
        let y_is_default = (self.y - 0.9).abs() < EPSILON;

        if y_is_default {
            self.y = -0.15;
        }

        self
    }

    /// Apply default positioning for y-axis titles (x=-0.08, y=0.5 - left side, vertically centered)
    pub fn with_y_title_defaults(mut self) -> Self {
        const EPSILON: f64 = 1e-6;
        let x_is_default = (self.x - 0.5).abs() < EPSILON;
        let y_is_default = (self.y - 0.9).abs() < EPSILON;

        if x_is_default {
            self.x = -0.08;
        }

        if y_is_default {
            self.y = 0.5;
        }

        self
    }

    /// Apply default positioning for x-axis title annotations
    /// Used when user sets custom position and annotation mode is triggered
    /// Ensures unset coordinates get appropriate axis defaults
    pub fn with_x_title_defaults_for_annotation(mut self) -> Self {
        const EPSILON: f64 = 1e-6;
        let x_is_default = (self.x - 0.5).abs() < EPSILON;
        let y_is_default = (self.y - 0.9).abs() < EPSILON;

        if x_is_default {
            self.x = 0.5;
        }

        if y_is_default {
            self.y = -0.15;
        }

        self
    }

    /// Apply default positioning for y-axis title annotations
    /// Used when user sets custom position and annotation mode is triggered
    /// Ensures unset coordinates get appropriate axis defaults
    pub fn with_y_title_defaults_for_annotation(mut self) -> Self {
        const EPSILON: f64 = 1e-6;
        let x_is_default = (self.x - 0.5).abs() < EPSILON;
        let y_is_default = (self.y - 0.9).abs() < EPSILON;

        if x_is_default {
            self.x = -0.08;
        }

        if y_is_default {
            self.y = 0.5;
        }

        self
    }
}

impl From<&str> for Text {
    fn from(content: &str) -> Self {
        Self::from(content.to_string())
    }
}

impl From<String> for Text {
    fn from(content: String) -> Self {
        Self::from(content)
    }
}

impl From<&String> for Text {
    fn from(content: &String) -> Self {
        Self::from(content)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::components::Rgb;

    fn assert_float_eq(a: f64, b: f64) {
        assert!(
            (a - b).abs() < 1e-6,
            "expected {b}, got {a} (diff {})",
            (a - b).abs()
        );
    }

    #[test]
    fn test_from_str() {
        let t = Text::from("hello");
        assert_eq!(t.content, "hello");
        assert_eq!(t.font, "");
        assert_eq!(t.size, 12);
        assert_eq!(t.color.0, 0);
        assert_eq!(t.color.1, 0);
        assert_eq!(t.color.2, 0);
        assert_float_eq(t.x, 0.5);
        assert_float_eq(t.y, 0.9);
    }

    #[test]
    fn test_from_string() {
        let t: Text = String::from("world").into();
        assert_eq!(t.content, "world");
    }

    #[test]
    fn test_from_ref_string() {
        let s = String::from("ref");
        let t: Text = (&s).into();
        assert_eq!(t.content, "ref");
    }

    #[test]
    fn test_default_values() {
        let t = Text::default();
        assert_eq!(t.content, "");
        assert_eq!(t.font, "");
        assert_eq!(t.size, 12);
        assert_eq!(t.color.0, 0);
        assert_eq!(t.color.1, 0);
        assert_eq!(t.color.2, 0);
        assert_float_eq(t.x, 0.5);
        assert_float_eq(t.y, 0.9);
    }

    #[test]
    fn test_font() {
        let t = Text::from("x").font("Arial");
        assert_eq!(t.font, "Arial");
        assert_eq!(t.size, 12);
    }

    #[test]
    fn test_size() {
        let t = Text::from("x").size(20);
        assert_eq!(t.size, 20);
        assert_eq!(t.font, "");
    }

    #[test]
    fn test_color() {
        let t = Text::from("x").color(Rgb(1, 2, 3));
        assert_eq!(t.color.0, 1);
        assert_eq!(t.color.1, 2);
        assert_eq!(t.color.2, 3);
    }

    #[test]
    fn test_x() {
        let t = Text::from("x").x(0.1);
        assert_float_eq(t.x, 0.1);
        assert_float_eq(t.y, 0.9);
    }

    #[test]
    fn test_y() {
        let t = Text::from("x").y(0.2);
        assert_float_eq(t.y, 0.2);
        assert_float_eq(t.x, 0.5);
    }

    #[test]
    fn test_builder_chaining() {
        let t = Text::from("chained")
            .font("Courier")
            .size(24)
            .color(Rgb(10, 20, 30))
            .x(0.3)
            .y(0.7);
        assert_eq!(t.content, "chained");
        assert_eq!(t.font, "Courier");
        assert_eq!(t.size, 24);
        assert_eq!(t.color.0, 10);
        assert_eq!(t.color.1, 20);
        assert_eq!(t.color.2, 30);
        assert_float_eq(t.x, 0.3);
        assert_float_eq(t.y, 0.7);
    }

    #[test]
    fn test_has_custom_position_default() {
        let t = Text::default();
        assert!(!t.has_custom_position());
    }

    #[test]
    fn test_has_custom_position_x_changed() {
        let t = Text::from("x").x(0.3);
        assert!(t.has_custom_position());
    }

    #[test]
    fn test_has_custom_position_epsilon() {
        let t = Text::from("x").x(0.5 + 1e-7);
        assert!(!t.has_custom_position());
    }

    #[test]
    fn test_with_plot_title_defaults() {
        let t = Text::from("title").with_plot_title_defaults();
        assert_float_eq(t.y, 0.95);

        let t2 = Text::from("title").y(0.7).with_plot_title_defaults();
        assert_float_eq(t2.y, 0.7);
    }

    #[test]
    fn test_with_x_title_defaults() {
        let t = Text::from("x").with_x_title_defaults();
        assert_float_eq(t.y, -0.15);
    }

    #[test]
    fn test_with_y_title_defaults() {
        let t = Text::from("y").with_y_title_defaults();
        assert_float_eq(t.x, -0.08);
        assert_float_eq(t.y, 0.5);

        let t2 = Text::from("y").x(0.2).y(0.3).with_y_title_defaults();
        assert_float_eq(t2.x, 0.2);
        assert_float_eq(t2.y, 0.3);
    }
}