Skip to content

Commit b00065e

Browse files
committed
Support different grid types depending on the plot
Categorical views only show horizontal lines, whereas continuous views show lines in both directions.
1 parent ba8c44a commit b00065e

File tree

5 files changed

+95
-27
lines changed

5 files changed

+95
-27
lines changed

examples/with_grid.rs

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,42 @@
11
extern crate plotlib;
22

33
use plotlib::grid::Grid;
4+
use plotlib::style::BarChart;
45
use plotlib::style::Line;
56
use plotlib::view::View;
67

78
fn main() {
9+
render_line_chart("line_with_grid.svg");
10+
render_barchart("barchart_with_grid.svg");
11+
}
12+
13+
fn render_line_chart<S>(filename: S)
14+
where
15+
S: AsRef<str>,
16+
{
817
let l1 = plotlib::line::Line::new(&[(0., 1.), (2., 1.5), (3., 1.2), (4., 1.1)])
918
.style(plotlib::line::Style::new().colour("burlywood"));
10-
let grid = Grid::new(3, 8);
1119
let mut v = plotlib::view::ContinuousView::new().add(&l1);
12-
v.add_grid(grid);
20+
v.add_grid(Grid::new(3, 8));
21+
plotlib::page::Page::single(&v)
22+
.save(filename.as_ref())
23+
.expect("saving svg");
24+
}
25+
26+
fn render_barchart<S>(filename: S)
27+
where
28+
S: AsRef<str>,
29+
{
30+
let b1 = plotlib::barchart::BarChart::new(5.3).label("1");
31+
let b2 = plotlib::barchart::BarChart::new(2.6)
32+
.label("2")
33+
.style(plotlib::barchart::Style::new().fill("darkolivegreen"));
34+
let mut v = plotlib::view::CategoricalView::new()
35+
.add(&b1)
36+
.add(&b2)
37+
.x_label("Experiment");
38+
v.add_grid(Grid::new(3, 8));
1339
plotlib::page::Page::single(&v)
14-
.save("line.svg")
40+
.save(filename.as_ref())
1541
.expect("saving svg");
1642
}

src/grid.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
pub(crate) enum GridType<'a> {
2+
HorizontalOnly(&'a Grid),
3+
Both(&'a Grid),
4+
}
5+
16
pub struct Grid {
27
pub nx: u32,
38
pub ny: u32,

src/page.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,23 @@ pub struct Page<'a> {
2525

2626
impl<'a> Page<'a> {
2727
/**
28-
Creates a plot containing a single view
28+
Creates an empty page container for plots to be added to
2929
*/
30-
pub fn single(view: &'a View) -> Self {
30+
pub fn empty() -> Self {
3131
Page {
32-
views: vec![view],
33-
num_views: 1,
32+
views: Vec::new(),
33+
num_views: 0,
3434
dimensions: (600, 400),
3535
}
3636
}
3737

38+
/**
39+
Creates a plot containing a single view
40+
*/
41+
pub fn single(view: &'a View) -> Self {
42+
Page::empty().add_plot(view)
43+
}
44+
3845
/// Set the dimensions of the plot.
3946
pub fn dimensions(mut self, x: u32, y: u32) -> Self {
4047
self.dimensions = (x, y);

src/svg_render.rs

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use svg::node;
44
use svg::Node;
55

66
use crate::axis;
7-
use crate::grid::Grid;
7+
use crate::grid::GridType;
88
use crate::histogram;
99
use crate::style;
1010
use crate::utils;
@@ -448,28 +448,45 @@ where
448448
group
449449
}
450450

451-
pub(crate) fn draw_grid(grid: &Grid, face_width: f64, face_height: f64) -> node::element::Group {
452-
let (xmin, xmax) = (0f64, face_width);
453-
let (ymin, ymax) = (0f64, face_height);
451+
pub(crate) fn draw_grid(grid: GridType, face_width: f64, face_height: f64) -> node::element::Group {
452+
match grid {
453+
GridType::HorizontalOnly(grid) => {
454+
let (ymin, ymax) = (0f64, face_height);
455+
let y_step = (ymax - ymin) / f64::from(grid.ny);
456+
let mut lines = node::element::Group::new();
457+
458+
for iy in 0..=grid.ny {
459+
let y = f64::from(iy) * y_step + ymin;
460+
let line = horizontal_line(-y, 0.0, face_width, grid.color.as_str());
461+
lines = lines.add(line);
462+
}
454463

455-
let x_step = (xmax - xmin) / f64::from(grid.nx);
456-
let y_step = (ymax - ymin) / f64::from(grid.ny);
464+
lines
465+
}
466+
GridType::Both(grid) => {
467+
let (xmin, xmax) = (0f64, face_width);
468+
let (ymin, ymax) = (0f64, face_height);
457469

458-
let mut lines = node::element::Group::new();
470+
let x_step = (xmax - xmin) / f64::from(grid.nx);
471+
let y_step = (ymax - ymin) / f64::from(grid.ny);
459472

460-
for iy in 0..=grid.ny {
461-
let y = f64::from(iy) * y_step + ymin;
462-
let line = horizontal_line(-y, 0.0, face_width, grid.color.as_str());
463-
lines = lines.add(line);
464-
}
473+
let mut lines = node::element::Group::new();
465474

466-
for ix in 0..=grid.nx {
467-
let x = f64::from(ix) * x_step + xmin;
468-
let line = vertical_line(x, 0.0, -face_height, grid.color.as_str());
469-
lines = lines.add(line);
470-
}
475+
for iy in 0..=grid.ny {
476+
let y = f64::from(iy) * y_step + ymin;
477+
let line = horizontal_line(-y, 0.0, face_width, grid.color.as_str());
478+
lines = lines.add(line);
479+
}
471480

472-
lines
481+
for ix in 0..=grid.nx {
482+
let x = f64::from(ix) * x_step + xmin;
483+
let line = vertical_line(x, 0.0, -face_height, grid.color.as_str());
484+
lines = lines.add(line);
485+
}
486+
487+
lines
488+
}
489+
}
473490
}
474491

475492
#[cfg(test)]

src/view.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use svg::Node;
1414

1515
use crate::axis;
1616
use crate::errors::Result;
17-
use crate::grid::Grid;
17+
use crate::grid::{Grid, GridType};
1818
use crate::representation::{CategoricalRepresentation, ContinuousRepresentation};
1919
use crate::svg_render;
2020
use crate::text_render;
@@ -171,7 +171,11 @@ impl<'a> View for ContinuousView<'a> {
171171
view_group.append(svg_render::draw_y_axis(&y_axis, face_height));
172172

173173
if let Some(grid) = &self.grid {
174-
view_group.append(svg_render::draw_grid(grid, face_width, face_height));
174+
view_group.append(svg_render::draw_grid(
175+
GridType::Both(grid),
176+
face_width,
177+
face_height,
178+
));
175179
}
176180

177181
Ok(view_group)
@@ -369,6 +373,15 @@ impl<'a> View for CategoricalView<'a> {
369373
// Add in the axes
370374
view_group.append(svg_render::draw_categorical_x_axis(&x_axis, face_width));
371375
view_group.append(svg_render::draw_y_axis(&y_axis, face_height));
376+
377+
if let Some(grid) = &self.grid {
378+
view_group.append(svg_render::draw_grid(
379+
GridType::HorizontalOnly(grid),
380+
face_width,
381+
face_height,
382+
));
383+
}
384+
372385
Ok(view_group)
373386
}
374387

0 commit comments

Comments
 (0)