diff --git a/.vscode/settings.json b/.vscode/settings.json index 42470c1..9af7baa 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -99,6 +99,7 @@ "xticklabels", "xticks", "yaxis", + "yerr", "ylabel", "ymax", "ymin", diff --git a/README.md b/README.md index 868422a..9a34a32 100644 --- a/README.md +++ b/README.md @@ -116,7 +116,7 @@ fn main() -> Result<(), StrError> { // barplot object and options let mut bar = Barplot::new(); - bar.set_x_errors(&errors) + bar.set_errors(&errors) .set_horizontal(true) .set_with_text("edge") .draw_with_str(&fruits, &prices); diff --git a/src/barplot.rs b/src/barplot.rs index c4a4a53..6c5de17 100644 --- a/src/barplot.rs +++ b/src/barplot.rs @@ -89,7 +89,7 @@ use std::fmt::Write; /// /// // barplot object and options /// let mut bar = Barplot::new(); -/// bar.set_x_errors(&errors) +/// bar.set_errors(&errors) /// .set_horizontal(true) /// .set_with_text("edge") /// .draw_with_str(&fruits, &prices); @@ -117,7 +117,7 @@ pub struct Barplot { bottom: Vec, // bottom coordinates to stack bars with_text: Option, // Text to be added to each bar (aka, bar_label) horizontal: bool, // Horizontal barplot - x_errors: Vec, // Shows x-error icons on horizontal bars + errors: Vec, // Shows error icons on bars extra: String, // Extra commands (comma separated) buffer: String, // buffer } @@ -132,7 +132,7 @@ impl Barplot { bottom: Vec::new(), with_text: None, horizontal: false, - x_errors: Vec::new(), + errors: Vec::new(), extra: String::new(), buffer: String::new(), } @@ -153,8 +153,8 @@ impl Barplot { if self.bottom.len() > 0 { vector_to_array(&mut self.buffer, "bottom", &self.bottom); } - if self.x_errors.len() > 0 { - vector_to_array(&mut self.buffer, "xerr", &self.x_errors); + if self.errors.len() > 0 { + vector_to_array(&mut self.buffer, "err", &self.errors); } if self.horizontal { write!(&mut self.buffer, "p=plt.barh(x,y{})\n", &opt).unwrap(); @@ -181,8 +181,8 @@ impl Barplot { if self.bottom.len() > 0 { vector_to_array(&mut self.buffer, "bottom", &self.bottom); } - if self.x_errors.len() > 0 { - vector_to_array(&mut self.buffer, "xerr", &self.x_errors); + if self.errors.len() > 0 { + vector_to_array(&mut self.buffer, "err", &self.errors); } if self.horizontal { write!(&mut self.buffer, "p=plt.barh(x,y{})\n", &opt).unwrap(); @@ -240,9 +240,9 @@ impl Barplot { self } - /// Enables the error indicators - pub fn set_x_errors(&mut self, errors: &[f64]) -> &mut Self { - self.x_errors = errors.to_vec(); + /// Enables error indicators + pub fn set_errors(&mut self, errors: &[f64]) -> &mut Self { + self.errors = errors.to_vec(); self } @@ -275,8 +275,12 @@ impl Barplot { if self.bottom.len() > 0 { write!(&mut opt, ",bottom=bottom").unwrap(); } - if self.x_errors.len() > 0 { - write!(&mut opt, ",xerr=xerr").unwrap(); + if self.errors.len() > 0 { + if self.horizontal { + write!(&mut opt, ",xerr=err").unwrap(); + } else { + write!(&mut opt, ",yerr=err").unwrap(); + } } if self.extra != "" { write!(&mut opt, ",{}", self.extra).unwrap(); diff --git a/tests/test_barplot.rs b/tests/test_barplot.rs index e56a65f..21b7dc2 100644 --- a/tests/test_barplot.rs +++ b/tests/test_barplot.rs @@ -85,7 +85,7 @@ fn test_barplot_3() -> Result<(), StrError> { // barplot object and options let mut bar = Barplot::new(); - bar.set_x_errors(&errors) + bar.set_errors(&errors) .set_horizontal(true) .set_with_text("edge") .draw(&fruits, &prices); @@ -116,7 +116,7 @@ fn test_barplot_4() -> Result<(), StrError> { // barplot object and options let mut bar = Barplot::new(); - bar.set_x_errors(&errors) + bar.set_errors(&errors) .set_horizontal(true) .set_with_text("edge") .draw_with_str(&fruits, &prices); @@ -137,3 +137,34 @@ fn test_barplot_4() -> Result<(), StrError> { assert!(c > 770 && c < 810); Ok(()) } + +#[test] +fn test_barplot_5() -> Result<(), StrError> { + // data + let y = vec![-2.5, -15.0, -5.0, -7.5, -2.5, -5.0, -17.5]; + let e = vec![0.5, 0.4, 0.1, 0.7, 0.2, 0.0, 1.7]; + let _x: Vec = (0..y.len()).map(|a| a as f64).collect(); + let _x_str = vec!["Uno", "Dos", "Tres", "Cuatro", "Cinco", "Seis", "Siete"]; + + // barplot + let mut bar = Barplot::new(); + bar.set_errors(&e) + // .draw(&_x, &y); // requires numbers, as expected + .draw_with_str(&_x_str, &y); // requires string, as expected + + // plot + let mut plot = Plot::new(); + plot.add(&bar); + + // save figure + let path = Path::new(OUT_DIR).join("integ_barplot_5.svg"); + plot.save(&path)?; + + // check number of lines + let file = File::open(path).map_err(|_| "cannot open file")?; + let buffered = BufReader::new(file); + let lines_iter = buffered.lines(); + let c = lines_iter.count(); + assert!(c > 830 && c < 900); + Ok(()) +}