Skip to content

Commit

Permalink
add option to specify axis range
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabian Bösiger committed Nov 9, 2021
1 parent 6a469df commit 732dec5
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
4 changes: 3 additions & 1 deletion examples/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ fn main() {
i where
caption = "Options",
size = (400, 300),
path = "plots/Options.jpg",
x_desc = "X Description",
y_desc = "Y Description",
path = "plots/Options.jpg"
x_range = 0f64..500f64,
y_range = 0f64..500f64
);
}
}
Binary file modified plots/Options.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 9 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@
/// |`x_desc`|`"x description"`|Sets the description of the x axis.|
/// |`y_desc`|`"y description"`|Sets the description of the y axis.|
/// |`path`|`"/plots/my_plot.jpg"`|Defines where the plot is saved.|
/// |`x_range`|`0f64..100f64`|Defines start and end of the x axis.|
/// |`y_range`|`0f64..100f64`|Defines start and end of the y axis.|
#[macro_export]
macro_rules! plot {
(
Expand Down Expand Up @@ -118,7 +120,7 @@ pub use debug::*;
mod debug {
use num_traits::cast::ToPrimitive;
use plotters::prelude::*;
use std::{cell::RefCell, collections::HashMap, fmt};
use std::{cell::RefCell, collections::HashMap, fmt, ops::Range};

thread_local! {
pub static PLOTS: Plots = Plots::new();
Expand Down Expand Up @@ -263,7 +265,10 @@ mod debug {
.margin(30)
.x_label_area_size(30)
.y_label_area_size(60)
.build_cartesian_2d(self.x_min()..self.x_max(), self.y_min()..self.y_max())?;
.build_cartesian_2d(
self.options.x_range.clone().unwrap_or(self.x_min()..self.x_max()),
self.options.y_range.clone().unwrap_or(self.y_min()..self.y_max()),
)?;

let mut mesh = chart.configure_mesh();
if let Some(x_desc) = &self.options.x_desc {
Expand Down Expand Up @@ -300,5 +305,7 @@ mod debug {
pub x_desc: Option<String>,
pub y_desc: Option<String>,
pub path: Option<String>,
pub x_range: Option<Range<PlotType>>,
pub y_range: Option<Range<PlotType>>,
}
}

0 comments on commit 732dec5

Please sign in to comment.