demo_basic/demo_basic.rs
1//! # Basic example
2//!
3//! See [the basic_plot() function source](../src/demo_basic/demo_basic.rs.html#49-77) for a demo
4//! on how to use this library after declaring the components of a plot.
5//!
6//! A basic plot may be configured by providing a struct detailing its components and deriving it
7//! using the `Plot` macro:
8//!
9//! ```
10//! #[derive(Clone, PartialEq, Debug, Default, Plot)]
11//! pub struct BasicPlot {
12//! config: Config,
13//! title: Maybe<Title>,
14//! x: XAxis,
15//! y: YAxis,
16//! series: Series<f64>,
17//! }
18//! ```
19//!
20//! Axis can be similarly derived with an `Axis` macro:
21//!
22//! ```
23//! #[derive(Clone, PartialEq, Eq, Debug, Default, Axis)]
24//! pub struct XAxis
25//! {
26//! /// declares a label on the X axis as required (without it, the gnuplot commands can't be
27//! /// accessed)
28//! label: Required<Label<X>>
29//! }
30//! ```
31//!
32//! Individual properties must implement the `GnuCommandFactory` trait.
33
34use gnuplotter::maybe::Maybe;
35use gnuplotter::prelude::{Axis, Config, Label, Plot, Required, Series, Title, X, Y};
36
37/// Derives an axis using the `Axis` macro. This produces all necessary getters and gnuplot
38/// command producing functionalities.
39#[derive(Clone, PartialEq, Eq, Debug, Default, Axis)]
40pub struct XAxis
41{
42 /// declares a label on the X axis as required (without it, the gnuplot commands can't be
43 /// accessed)
44 label: Required<Label<X>>
45}
46
47/// Derives an axis using the `Axis` macro. This produces all necessary getters and gnuplot
48/// command producing functionalities.
49#[derive(Clone, PartialEq, Eq, Debug, Default, Axis)]
50pub struct YAxis
51{
52 /// Declares an optional label
53 label: Maybe<Label<Y>>
54}
55
56/// Produces a plot by declaring its components, and then deriving both getters and rendering
57/// functionality (usage demonstrated in basic_plot() function)
58#[derive(Clone, PartialEq, Debug, Default, Plot)]
59pub struct BasicPlot {
60 config: Config,
61 title: Maybe<Title>,
62 x: XAxis,
63 y: YAxis,
64 series: Series<f64>,
65}
66
67impl BasicPlot {
68 pub fn new() -> Self {
69 BasicPlot::default()
70 }
71}
72
73pub fn main() {
74 basic_plot().unwrap();
75}
76
77/// Produces a basic plot by instantiating and configuring the structure, before adding data to it.
78pub fn basic_plot() -> RenderResult<()> {
79
80 // initialize the plot
81 let mut plot = BasicPlot::new();
82
83 // configure where to save the output file (must be PNG at the moment)
84 plot.config().terminal().output().update("./.tmp/output.png");
85 plot.config().terminal().font().update("Helvetica", 9);
86 plot.config().terminal().size().update(1200, 800);
87
88 // update various fields
89 plot.x().label().update("X");
90 plot.y().label().update("Y");
91 plot.title().update("A basic plot");
92
93 // we need to generate some data into a `Serie` struct. At the moment, only f64 falues are allowed.
94 let mut data_1 = Serie::with_title("Data series 1");
95 let mut data_2 = Serie::with_title("Data series 2");
96 for i in 1..=20 {
97 data_1.add((i*i) as f64);
98 data_2.add(i as f64);
99 }
100
101 // data series are added individually at this time
102 plot.series().add(data_1);
103 plot.series().add(data_2);
104
105 plot.render()
106}
107
108#[cfg(test)]
109mod tests {
110 use super::*;
111
112 #[test]
113 fn test_basic_plot(){
114 assert!(basic_plot().is_ok());
115 }
116}