diff --git a/implot-examples/examples-shared/src/line_plots.rs b/implot-examples/examples-shared/src/line_plots.rs index c3342e7..782e6d6 100644 --- a/implot-examples/examples-shared/src/line_plots.rs +++ b/implot-examples/examples-shared/src/line_plots.rs @@ -39,12 +39,13 @@ pub fn show_two_yaxis_plot(ui: &Ui, plot_ui: &PlotUi) { .size([content_width, 300.0]) .with_plot_flags(&(PlotFlags::NONE | PlotFlags::Y_AXIS_2)) .y_limits( - &ImPlotRange { Min: 0.0, Max: 1.0 }, + ImPlotRange { Min: 0.0, Max: 1.0 }, YAxisChoice::First, Condition::Always, ) .y_limits( - &ImPlotRange { Min: 1.0, Max: 3.5 }, + // One can also use [f32; 2], (f32, f32) and ImVec2 for limit setting + [1.0, 3.5], YAxisChoice::Second, Condition::Always, ) @@ -114,7 +115,7 @@ pub fn show_configurable_plot(ui: &Ui, plot_ui: &PlotUi) { .x_label(&x_label) .y_label(&y_label) .x_limits( - &ImPlotRange { + ImPlotRange { Min: x_min, Max: x_max, }, @@ -125,7 +126,7 @@ pub fn show_configurable_plot(ui: &Ui, plot_ui: &PlotUi) { Condition::Always, ) .y_limits( - &ImPlotRange { + ImPlotRange { Min: y_min, Max: y_max, }, @@ -162,9 +163,9 @@ pub fn show_query_features_plot(ui: &Ui, plot_ui: &PlotUi) { // Draw a plot Plot::new("Plot querying") .size([content_width, 300.0]) - .x_limits(&ImPlotRange { Min: 0.0, Max: 5.0 }, Condition::FirstUseEver) + .x_limits(ImPlotRange { Min: 0.0, Max: 5.0 }, Condition::FirstUseEver) .y_limits( - &ImPlotRange { Min: 0.0, Max: 5.0 }, + ImPlotRange { Min: 0.0, Max: 5.0 }, YAxisChoice::First, Condition::FirstUseEver, ) @@ -247,9 +248,9 @@ pub fn show_style_plot(ui: &Ui, plot_ui: &PlotUi) { let style = push_style_color(&PlotColorElement::PlotBg, 1.0, 1.0, 1.0, 0.2); Plot::new("Style demo plot") .size([content_width, 300.0]) - .x_limits(&ImPlotRange { Min: 0.0, Max: 6.0 }, Condition::Always) + .x_limits(ImPlotRange { Min: 0.0, Max: 6.0 }, Condition::Always) .y_limits( - &ImPlotRange { + ImPlotRange { Min: -1.0, Max: 3.0, }, @@ -338,8 +339,8 @@ pub fn show_conversions_plot(ui: &Ui, plot_ui: &PlotUi) { let content_width = ui.window_content_region_width(); Plot::new("Simple line plot, conversion 1") .size([content_width, 300.0]) - .x_limits(&ImVec2 { x: 0.0, y: 1.0 }.into(), Condition::Always) - .y_limits(&[0.0, 1.0].into(), YAxisChoice::First, Condition::Always) + .x_limits(ImVec2 { x: 0.0, y: 1.0 }, Condition::Always) + .y_limits([0.0, 1.0], YAxisChoice::First, Condition::Always) .build(plot_ui, || { // If this is called outside a plot build callback, the program will panic. let x_positions = vec![0.1, 0.9]; diff --git a/src/plot.rs b/src/plot.rs index 5c6c311..129caad 100644 --- a/src/plot.rs +++ b/src/plot.rs @@ -204,27 +204,49 @@ impl Plot { /// Set the x limits of the plot #[inline] - pub fn x_limits(mut self, limits: &ImPlotRange, condition: Condition) -> Self { - self.x_limits = Some(*limits); + pub fn x_limits>(mut self, limits: L, condition: Condition) -> Self { + self.x_limits = Some(limits.into()); self.x_limit_condition = Some(condition); self } - /// Set the Y limits of the plot for the given Y axis. Call multiple times - /// to set for multiple axes. + /// Set the Y limits of the plot for the given Y axis. Call multiple times with different + /// `y_axis_choice` values to set for multiple axes, or use the convenience methods such as + /// [`Plot::y1_limits`]. #[inline] - pub fn y_limits( + pub fn y_limits>( mut self, - limits: &ImPlotRange, + limits: L, y_axis_choice: YAxisChoice, condition: Condition, ) -> Self { let axis_index = y_axis_choice as usize; - self.y_limits[axis_index] = Some(*limits); + self.y_limits[axis_index] = Some(limits.into()); self.y_limit_condition[axis_index] = Some(condition); self } + /// Convenience function to directly set the Y limits for the first Y axis. To programmatically + /// (or on demand) decide which axie to set limits for, use [`Plot::y_limits`] + #[inline] + pub fn y1_limits>(self, limits: L, condition: Condition) -> Self { + self.y_limits(limits, YAxisChoice::First, condition) + } + + /// Convenience function to directly set the Y limits for the second Y axis. To + /// programmatically (or on demand) decide which axie to set limits for, use [`Plot::y_limits`] + #[inline] + pub fn y2_limits>(self, limits: L, condition: Condition) -> Self { + self.y_limits(limits, YAxisChoice::Second, condition) + } + + /// Convenience function to directly set the Y limits for the third Y axis. To programmatically + /// (or on demand) decide which axie to set limits for, use [`Plot::y_limits`] + #[inline] + pub fn y3_limits>(self, limits: L, condition: Condition) -> Self { + self.y_limits(limits, YAxisChoice::Third, condition) + } + /// Set X ticks without labels for the plot. The vector contains one label each in /// the form of a tuple `(label_position, label_string)`. The `show_default` setting /// determines whether the default ticks are also shown. @@ -352,7 +374,7 @@ impl Plot { /// "set next plot ticks" wrapper functions for both X and Y. fn maybe_set_tick_labels(&self) { // Show x ticks if they are available - if self.x_tick_positions.is_some() && self.x_tick_positions.as_ref().unwrap().len() > 0 { + if self.x_tick_positions.is_some() && !self.x_tick_positions.as_ref().unwrap().is_empty() { let mut pointer_vec; // The vector of pointers we create has to have a longer lifetime let labels_pointer = if let Some(labels_value) = &self.x_tick_labels { pointer_vec = labels_value @@ -380,7 +402,7 @@ impl Plot { .zip(self.show_y_default_ticks.iter()) .enumerate() .for_each(|(k, ((positions, labels), show_defaults))| { - if positions.is_some() && positions.as_ref().unwrap().len() > 0 { + if positions.is_some() && !positions.as_ref().unwrap().is_empty() { // The vector of pointers we create has to have a longer lifetime let mut pointer_vec; let labels_pointer = if let Some(labels_value) = &labels { diff --git a/src/plot_elements.rs b/src/plot_elements.rs index 4742ee3..5525a50 100644 --- a/src/plot_elements.rs +++ b/src/plot_elements.rs @@ -222,7 +222,7 @@ impl PlotText { /// closures passed to [`Plot::build()`](struct.Plot.html#method.build) pub fn plot(&self, x: f64, y: f64, vertical: bool) { // If there is nothing to show, don't do anything - if self.label == "" { + if self.label.is_empty() { return; }