Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
"xticklabels",
"xticks",
"yaxis",
"yerr",
"ylabel",
"ymax",
"ymin",
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
28 changes: 16 additions & 12 deletions src/barplot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -117,7 +117,7 @@ pub struct Barplot {
bottom: Vec<f64>, // bottom coordinates to stack bars
with_text: Option<String>, // Text to be added to each bar (aka, bar_label)
horizontal: bool, // Horizontal barplot
x_errors: Vec<f64>, // Shows x-error icons on horizontal bars
errors: Vec<f64>, // Shows error icons on bars
extra: String, // Extra commands (comma separated)
buffer: String, // buffer
}
Expand All @@ -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(),
}
Expand All @@ -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();
Expand All @@ -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();
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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();
Expand Down
35 changes: 33 additions & 2 deletions tests/test_barplot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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<f64> = (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(())
}
Loading