-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathtest_histogram.rs
More file actions
72 lines (61 loc) · 2.14 KB
/
test_histogram.rs
File metadata and controls
72 lines (61 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
use plotpy::{Histogram, Plot, StrError};
use std::fs::File;
use std::io::{BufRead, BufReader};
use std::path::Path;
const OUT_DIR: &str = "/tmp/plotpy/integ_tests";
#[test]
fn test_histogram_1() -> Result<(), StrError> {
let mut histogram = Histogram::new();
histogram
.set_colors(&vec!["#cd0000", "#1862ab", "#cd8c00"])
.set_style("barstacked");
// draw histogram
let values = vec![
vec![1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 4, 5, 6], // first series
vec![-1, -1, 0, 1, 2, 3], // second series
vec![5, 6, 7, 8], // third series
];
let labels = ["first".to_string(), "second".to_string(), "third".to_string()];
histogram.draw(&values, &labels);
// add surface to plot
let mut plot = Plot::new();
plot.add(&histogram);
// save figure
let path = Path::new(OUT_DIR).join("integ_histogram_1.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();
assert!(lines_iter.count() > 670);
Ok(())
}
#[test]
fn test_histogram_2() -> Result<(), StrError> {
let mut histogram = Histogram::new();
histogram
.set_no_fill(true)
.set_number_bins(16)
.set_stacked(true)
.set_extra("orientation='horizontal'");
// draw histogram
let values = vec![
vec![1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 4, 5, 6], // first series
vec![-1, -1, 0, 1, 2, 3], // second series
vec![5, 6, 7, 8], // third series
];
let labels = ["first", "second", "third"];
histogram.draw(&values, &labels);
// add histogram to plot
let mut plot = Plot::new();
plot.add(&histogram);
// save figure
let path = Path::new(OUT_DIR).join("integ_histogram_2.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();
assert!(lines_iter.count() > 810);
Ok(())
}