with_grid/
with_grid.rs

1use plotlib::grid::Grid;
2use plotlib::page::Page;
3use plotlib::repr::{BarChart, Plot};
4use plotlib::style::{BoxStyle, LineStyle};
5use plotlib::view::{CategoricalView, ContinuousView, View};
6
7fn main() {
8    render_line_chart("line_with_grid.svg");
9    render_barchart("barchart_with_grid.svg");
10}
11
12fn render_line_chart<S>(filename: S)
13where
14    S: AsRef<str>,
15{
16    let l1 = Plot::new(vec![(0., 1.), (2., 1.5), (3., 1.2), (4., 1.1)])
17        .line_style(LineStyle::new().colour("burlywood"));
18    let mut v = ContinuousView::new().add(l1);
19    v.add_grid(Grid::new(3, 8));
20    Page::single(&v)
21        .save(filename.as_ref())
22        .expect("saving svg");
23}
24
25fn render_barchart<S>(filename: S)
26where
27    S: AsRef<str>,
28{
29    let b1 = BarChart::new(5.3).label("1");
30    let b2 = BarChart::new(2.6)
31        .label("2")
32        .style(&BoxStyle::new().fill("darkolivegreen"));
33    let mut v = CategoricalView::new().add(b1).add(b2).x_label("Experiment");
34    v.add_grid(Grid::new(3, 8));
35    Page::single(&v)
36        .save(filename.as_ref())
37        .expect("saving svg");
38}