simple_plot/lib.rs
1//! # simple-plot
2//! Provides a macro plot!() that plots a one-dimensional vector (impl IntoIterator<Item={number}>) using Plotly.
3//!
4//! There is no need for complicated settings; simply pass to plot!() the title of the graph and the vectors you wish to plot, and the graph will be displayed.
5//!
6//! The passed vector is plotted with index on the x-axis and elements as values on the y-axis.
7//!
8//! # What this library cannot do
9//! - Changing the value of the x-axis
10//! - Changing the color of a graph
11//! etc.
12//!
13//! If you need these functions, use plotly
14//!
15//! # Examples
16//! ```
17//! let range = 0..100;
18//! let sin_iter = (0..150).map(|x| (x as f32 / 10.0).sin() * 20.0);
19//! let parabola_vec:Vec<i32> = (-10..10).map(|x| x*x - 30).collect();
20//! let cos_vec: Vec<f32> = (0..150).map(|x| (x as f32 / 20.0).cos() * 10.0).collect();
21//! let cos_slcie: &[f32] = &cos_vec;
22//!
23//! simple_plot::plot!("title", range, sin_iter, parabola_vec, cos_slcie);
24//! ```
25
26pub mod _plotly {
27 pub use plotly::{
28 common::{Mode, Title},
29 layout::Layout,
30 Plot, Scatter,
31 };
32 /// Plot a one-dimensional vector using Plotly.
33 ///
34 /// The passed vector is plotted with index on the x-axis and elements as values on the y-axis.
35 ///
36 /// First argument : `&str` Title of the graph
37 ///
38 /// Second and subsequent arguments: `impl IntoIterator<Item={number}>` Vector(s) to plot.
39 ///
40 /// # Examples
41 /// ```
42 /// let range = 0..100;
43 /// let sin_iter = (0..150).map(|x| (x as f32 / 10.0).sin() * 20.0);
44 /// simple_plot::plot!("title", range, sin_iter);
45 /// ```
46 #[macro_export]
47 macro_rules! plot {
48 ($title:expr, $($ys:expr), +) => {{
49 use simple_plot::_plotly::{Mode, Title, Plot, Scatter,Layout};
50 let layout = Layout::new().title(Title::new($title));
51 let mut plot = Plot::new();
52 $(
53 let name = stringify!($ys);
54 let (xs, ys):(Vec<_>, Vec<_>) = $ys.into_iter().enumerate().map(|(i, x)| (i as f32, x.clone() as f32)).unzip();
55 let trace = Scatter::new(xs, ys)
56 .mode(Mode::Lines)
57 .name(name);
58 plot.add_trace(trace);
59 )+
60 plot.set_layout(layout);
61 plot.show();
62 }}
63 }
64}