plotting/
quick.rs

1use crate::figure::Figure;
2use crate::format::FormatBuilder;
3use crate::trace::Trace;
4
5/// Quickly create a 2D plot.
6///
7/// # Arguments
8///
9/// * `x` - x-axis data.
10/// * `y` - y-axis data.
11///
12/// # Returns
13///
14/// Figure.
15///
16/// # Example
17///
18/// ```
19/// use plotting::{quick_plot_2d, Figure};
20///
21/// // Plot y = x^2.
22/// let fig: Figure = quick_plot_2d([1.0, 2.0, 3.0], [1.0, 4.0, 9.0]);
23/// ```
24///
25/// ## Additional examples
26///
27/// Additional examples can be found at
28/// <https://tamaskis.github.io/plotting/plot_2d/quick_2d_plot.html>.
29pub fn quick_plot_2d(x: impl Into<Vec<f64>>, y: impl Into<Vec<f64>>) -> Figure {
30    // Create the trace.
31    let trace = Trace::new_2d(x.into(), y.into());
32
33    // Formatting.
34    let format = FormatBuilder::default().build().unwrap();
35
36    // Create and return the figure.
37    Figure::new(vec![trace], format)
38}
39
40/// Quickly create a 2D plot with axis labels and a title.
41///
42/// # Arguments
43///
44/// * `x` - x-axis data.
45/// * `y` - y-axis data.
46/// # `x_label` - x-axis label.
47/// * `y_label` - y-axis label.
48/// * `title` - Title.
49///
50/// # Returns
51///
52/// Figure.
53///
54/// # Example
55///
56/// ```
57/// use plotting::{quick_plot_2d_with_labels, Figure};
58///
59/// // Plot y = x^2.
60/// let fig: Figure = quick_plot_2d_with_labels(
61///     [1.0, 2.0, 3.0],
62///     [1.0, 4.0, 9.0],
63///     "x",
64///     "y",
65///     "y = x^2",
66/// );
67/// ```
68///
69/// ## Additional examples
70///
71/// Additional examples can be found at
72/// <https://tamaskis.github.io/plotting/plot_2d/quick_2d_plot_with_labels.html>.
73pub fn quick_plot_2d_with_labels(
74    x: impl Into<Vec<f64>>,
75    y: impl Into<Vec<f64>>,
76    x_label: impl Into<String>,
77    y_label: impl Into<String>,
78    title: impl Into<String>,
79) -> Figure {
80    // Create the trace.
81    let trace = Trace::new_2d(x.into(), y.into());
82
83    // Formatting.
84    let format = FormatBuilder::default()
85        .x_label(x_label.into())
86        .y_label(y_label.into())
87        .title(title.into())
88        .build()
89        .unwrap();
90
91    // Create and return the figure.
92    Figure::new(vec![trace], format)
93}
94
95/// Quickly create a 3D plot.
96///
97/// # Arguments
98///
99/// * `x` - x-axis data.
100/// * `y` - y-axis data.
101/// * `z` - z-axis data.
102///
103/// # Returns
104///
105/// Figure.
106///
107/// # Example
108///
109/// ```
110/// use plotting::{quick_plot_3d, Figure};
111///
112/// let fig: Figure = quick_plot_3d([1.0, 2.0, 10.0], [1.0, 4.0, 9.0], [2.0, 5.0, 10.0]);
113/// ```
114///
115/// ## Additional examples
116///
117/// Additional examples can be found at
118/// <https://tamaskis.github.io/plotting/plot_3d/quick_3d_plot.html>.
119pub fn quick_plot_3d(
120    x: impl Into<Vec<f64>>,
121    y: impl Into<Vec<f64>>,
122    z: impl Into<Vec<f64>>,
123) -> Figure {
124    // Create the trace.
125    let trace = Trace::new_3d(x.into(), y.into(), z.into());
126
127    // Formatting.
128    let format = FormatBuilder::default().build().unwrap();
129
130    // Create and return the figure.
131    Figure::new(vec![trace], format)
132}
133
134/// Quickly create a 3D plot with axis labels and a title.
135///
136/// # Arguments
137///
138/// * `x` - x-axis data.
139/// * `y` - y-axis data.
140/// * `z` - z-axis data.
141/// # `x_label` - x-axis label.
142/// * `y_label` - y-axis label.
143/// * `z_label` - z-axis label.
144/// * `title` - Title.
145///
146/// # Returns
147///
148/// Figure.
149///
150/// ## Example
151///
152/// ```
153/// use plotting::{quick_plot_3d_with_labels, Figure};
154///
155/// let fig: Figure = quick_plot_3d_with_labels(
156///     [1.0, 2.0, 10.0],
157///     [1.0, 4.0, 9.0],
158///     [2.0, 5.0, 10.0],
159///     "x",
160///     "y",
161///     "z",
162///     "z vs. x and y",
163/// );
164/// ```
165///
166/// ## Additional examples
167///
168/// Additional examples can be found at
169/// <https://tamaskis.github.io/plotting/plot_3d/quick_3d_plot_with_labels.html>.
170pub fn quick_plot_3d_with_labels(
171    x: impl Into<Vec<f64>>,
172    y: impl Into<Vec<f64>>,
173    z: impl Into<Vec<f64>>,
174    x_label: impl Into<String>,
175    y_label: impl Into<String>,
176    z_label: impl Into<String>,
177    title: impl Into<String>,
178) -> Figure {
179    // Create the trace.
180    let trace = Trace::new_3d(x.into(), y.into(), z.into());
181
182    // Formatting.
183    let format = FormatBuilder::default()
184        .x_label(x_label.into())
185        .y_label(y_label.into())
186        .z_label(z_label.into())
187        .title(title.into())
188        .build()
189        .unwrap();
190
191    // Create and return the figure.
192    Figure::new(vec![trace], format)
193}
194
195#[cfg(test)]
196mod tests {
197    use super::*;
198
199    #[test]
200    fn test_quick_plot_2d() {
201        let fig: Figure = quick_plot_2d([1.0, 2.0, 3.0], [1.0, 4.0, 9.0]);
202        assert_eq!(fig.traces[0].x, [1.0, 2.0, 3.0]);
203        assert_eq!(fig.traces[0].y, [1.0, 4.0, 9.0]);
204        assert!(fig.format.title.is_none());
205        assert!(fig.format.x_label.is_none());
206        assert!(fig.format.y_label.is_none());
207        assert!(fig.format.width.is_none());
208        assert!(fig.format.height.is_none());
209    }
210
211    #[test]
212    fn test_quick_plot_2d_with_labels() {
213        let fig: Figure =
214            quick_plot_2d_with_labels([1.0, 2.0, 3.0], [1.0, 4.0, 9.0], "x", "y", "y vs. x");
215        assert_eq!(fig.traces[0].x, [1.0, 2.0, 3.0]);
216        assert_eq!(fig.traces[0].y, [1.0, 4.0, 9.0]);
217        assert_eq!(fig.format.title.unwrap(), "y vs. x");
218        assert_eq!(fig.format.x_label.unwrap(), "x");
219        assert_eq!(fig.format.y_label.unwrap(), "y");
220        assert!(fig.format.width.is_none());
221        assert!(fig.format.height.is_none());
222    }
223
224    #[test]
225    fn test_quick_plot_3d() {
226        let fig: Figure = quick_plot_3d([1.0, 2.0, 10.0], [1.0, 4.0, 9.0], [2.0, 5.0, 10.0]);
227        assert_eq!(fig.traces[0].x, [1.0, 2.0, 10.0]);
228        assert_eq!(fig.traces[0].y, [1.0, 4.0, 9.0]);
229        assert_eq!(fig.traces[0].z.clone().unwrap(), [2.0, 5.0, 10.0]);
230        assert!(fig.format.title.is_none());
231        assert!(fig.format.x_label.is_none());
232        assert!(fig.format.y_label.is_none());
233        assert!(fig.format.z_label.is_none());
234        assert!(fig.format.width.is_none());
235        assert!(fig.format.height.is_none());
236    }
237
238    #[test]
239    fn test_quick_plot_3d_with_labels() {
240        let fig: Figure = quick_plot_3d_with_labels(
241            [1.0, 2.0, 10.0],
242            [1.0, 4.0, 9.0],
243            [2.0, 5.0, 10.0],
244            "x",
245            "y",
246            "z",
247            "z vs. x and y",
248        );
249        assert_eq!(fig.traces[0].x, [1.0, 2.0, 10.0]);
250        assert_eq!(fig.traces[0].y, [1.0, 4.0, 9.0]);
251        assert_eq!(fig.traces[0].z.clone().unwrap(), [2.0, 5.0, 10.0]);
252        assert_eq!(fig.format.title.unwrap(), "z vs. x and y");
253        assert_eq!(fig.format.x_label.unwrap(), "x");
254        assert_eq!(fig.format.y_label.unwrap(), "y");
255        assert_eq!(fig.format.z_label.unwrap(), "z");
256        assert!(fig.format.width.is_none());
257        assert!(fig.format.height.is_none());
258    }
259}