Skip to content

Commit

Permalink
[Important] Make set super title to receive a reference to SuperTitle…
Browse files Browse the repository at this point in the history
…Params. Impl clone for SuperTitleParams
  • Loading branch information
cpmech committed Sep 19, 2024
1 parent ec8196f commit 0819c4c
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/plot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ impl Plot {
/// 2. The TeX handling in the super-title string is more limited than in the title string
/// because we cannot use Python's raw string notation (`r''`) here. The reason is that we want
/// the super-title to be wrapped if it is too long and only non-raw strings can do that.
pub fn set_super_title(&mut self, title: &str, params: Option<SuperTitleParams>) -> &mut Self {
pub fn set_super_title(&mut self, title: &str, params: Option<&SuperTitleParams>) -> &mut Self {
let t = title.replace("'", "’");
match params {
Some(p) => write!(&mut self.buffer, "st=plt.suptitle('{}'{})\n", t, p.options()).unwrap(),
Expand Down Expand Up @@ -1265,7 +1265,7 @@ mod tests {
.set_fontsize(12.0)
.set_fontweight(10.0);
let mut plot = Plot::new();
plot.set_super_title("all subplots", Some(params));
plot.set_super_title("all subplots", Some(&params));
let b: &str =
"st=plt.suptitle('all subplots',x=123.3,y=456.7,ha='left',va='bottom',fontsize=12,fontweight=10)\n\
add_to_ea(st)\n";
Expand Down
8 changes: 8 additions & 0 deletions src/super_title_params.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::fmt::Write;

/// Holds parameters for the SuperTitle
#[derive(Clone)]
pub struct SuperTitleParams {
/// The x location of the text in figure coordinates (default = 0.5)
x: Option<f64>,
Expand Down Expand Up @@ -120,6 +121,13 @@ mod tests {
assert_eq!(params.fontweight, 0.0);
}

#[test]
fn clone_works() {
let params = SuperTitleParams::new();
let clone = params.clone();
assert_eq!(clone.fontsize, 0.0);
}

#[test]
fn options_works() {
let mut params = SuperTitleParams::new();
Expand Down
2 changes: 1 addition & 1 deletion tests/test_plot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ fn test_plot_subplots() -> Result<(), StrError> {

// configure plot
let mut plot = Plot::new();
plot.set_super_title("\"$\\int$ Plot's Owner Says $\\mathrm{d}\\sigma$\": This is the \"super title\", \\n followed by a very long text to see \\n if this whole thing will be wrapped or not \\n we hope that it gets wrapped and beautifully formatted.\"", Some(params))
plot.set_super_title("\"$\\int$ Plot's Owner Says $\\mathrm{d}\\sigma$\": This is the \"super title\", \\n followed by a very long text to see \\n if this whole thing will be wrapped or not \\n we hope that it gets wrapped and beautifully formatted.\"", Some(&params))
.set_horizontal_gap(0.5)
.set_vertical_gap(0.5)
.set_gaps(0.3, 0.3);
Expand Down

0 comments on commit 0819c4c

Please sign in to comment.