plotlars/components/
text.rs

1use plotly::common::{Font, Title};
2
3use crate::components::Rgb;
4
5/// A structure representing text with customizable content, font, size, and color.
6///
7/// # Example
8///
9/// ```rust
10/// use polars::prelude::*;
11/// use plotlars::{Axis, BarPlot, Plot, Text, Rgb};
12///
13/// let dataset = df![
14///         "label" => &[""],
15///         "value" => &[0],
16///     ]
17///     .unwrap();
18///
19/// let axis = Axis::new()
20///     .tick_values(vec![]);
21///
22/// BarPlot::builder()
23///     .data(&dataset)
24///     .labels("label")
25///     .values("value")
26///     .plot_title(
27///         Text::from("Title")
28///             .x(0.1)
29///             .color(Rgb(178, 34, 34))
30///             .size(30)
31///             .font("Zapfino")
32///     )
33///     .x_title(
34///         Text::from("X")
35///             .color(Rgb(65, 105, 225))
36///             .size(20)
37///             .font("Marker Felt")
38///     )
39///     .y_title(
40///         Text::from("Y")
41///             .color(Rgb(255, 140, 0))
42///             .size(20)
43///             .font("Arial Black")
44///     )
45///     .x_axis(&axis)
46///     .y_axis(&axis)
47///     .build()
48///     .plot();
49/// ```
50/// ![Example](https://imgur.com/4outoUQ.png)
51#[derive(Clone)]
52pub struct Text {
53    pub(crate) content: String,
54    pub(crate) font: String,
55    pub(crate) size: usize,
56    pub(crate) color: Rgb,
57    pub(crate) x: f64,
58    pub(crate) y: f64,
59}
60
61impl Default for Text {
62    /// Provides default values for the `Text` struct.
63    ///
64    /// - `content`: An empty string.
65    /// - `font`: An empty string.
66    /// - `size`: `0`.
67    /// - `color`: Default `Rgb` value.
68    /// - `x`: `0.5`.
69    /// - `y`: `0.9`.
70    fn default() -> Self {
71        Text {
72            content: String::new(),
73            font: String::new(),
74            size: 0,
75            color: Rgb::default(),
76            x: 0.5,
77            y: 0.9,
78        }
79    }
80}
81
82impl Text {
83    /// Creates a new `Text` instance from the given content.
84    ///
85    /// # Argument
86    ///
87    /// * `content` - A value that can be converted into a `String`, representing the textual content.
88    pub fn from(content: impl Into<String>) -> Self {
89        Self {
90            content: content.into(),
91            ..Default::default()
92        }
93    }
94
95    /// Sets the font of the text.
96    ///
97    /// # Argument
98    ///
99    /// * `font` - A value that can be converted into a `String`, representing the font name.
100    pub fn font(mut self, font: impl Into<String>) -> Self {
101        self.font = font.into();
102        self
103    }
104
105    /// Sets the size of the text.
106    ///
107    /// # Argument
108    ///
109    /// * `size` - A `usize` value specifying the font size.
110    pub fn size(mut self, size: usize) -> Self {
111        self.size = size;
112        self
113    }
114
115    /// Sets the color of the text.
116    ///
117    /// # Argument
118    ///
119    /// * `color` - An `Rgb` value specifying the color of the text.
120    pub fn color(mut self, color: Rgb) -> Self {
121        self.color = color;
122        self
123    }
124
125    /// Sets the x-coordinate position of the text.
126    ///
127    /// # Argument
128    ///
129    /// * `x` - A `f64` value specifying the horizontal position.
130    pub fn x(mut self, x: f64) -> Self {
131        self.x = x;
132        self
133    }
134
135    /// Sets the y-coordinate position of the text.
136    ///
137    /// # Argument
138    ///
139    /// * `y` - A `f64` value specifying the vertical position.
140    pub fn y(mut self, y: f64) -> Self {
141        self.y = y;
142        self
143    }
144
145    pub(crate) fn to_plotly(&self) -> Title {
146        Title::with_text(&self.content)
147            .font(
148                Font::new()
149                    .family(self.font.as_str())
150                    .size(self.size)
151                    .color(self.color.to_plotly()),
152            )
153            .x(self.x)
154            .y(self.y)
155    }
156}
157
158impl From<&str> for Text {
159    fn from(content: &str) -> Self {
160        Self::from(content.to_string())
161    }
162}
163
164impl From<String> for Text {
165    fn from(content: String) -> Self {
166        Self::from(content)
167    }
168}
169
170impl From<&String> for Text {
171    fn from(content: &String) -> Self {
172        Self::from(content)
173    }
174}