Skip to content

Commit 35c376a

Browse files
committed
Merge branch 'Th3Whit3Wolf-master'
2 parents c32dbe0 + 4adc489 commit 35c376a

1 file changed

Lines changed: 31 additions & 0 deletions

File tree

examples/letter_counter.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
use std::collections::btree_map::BTreeMap;
2+
use plotlib::style::BoxStyle;
3+
4+
fn main() {
5+
let mut data = Vec::new();
6+
let message: &str = "This is a long message";
7+
let mut count = BTreeMap::new();
8+
9+
for c in message.trim().to_lowercase().chars() {
10+
if c.is_alphabetic() {
11+
*count.entry(c).or_insert(0) += 1
12+
}
13+
}
14+
15+
println!("Number of occurences per character");
16+
for (ch, count) in &count {
17+
println!("{:?}: {}", ch, count);
18+
let count = *count as f64;
19+
data.push(plotlib::repr::BarChart::new(count).label(ch.to_string()));
20+
}
21+
// Add data to the view
22+
let v = data
23+
.into_iter()
24+
.fold(plotlib::view::CategoricalView::new(), |view, datum| {
25+
view.add(datum)
26+
});
27+
28+
plotlib::page::Page::single(&v)
29+
.save("barchart.svg")
30+
.expect("saving svg");
31+
}

0 commit comments

Comments
 (0)