plotlib/grid.rs
1#![deny(missing_docs)]
2
3//! Configure a grid on a plot.
4//!
5//! Grids allow for easier estimating of data values. This module allows the configuration of grids
6//! on plots.
7//!
8//! Grids are created by creating a `Grid` definition, and adding it to a plot:
9//!
10//! The grid lines for `plotlib` are rendered
11//! _underneath_ the data so as to not detract from the data.
12//!
13//! # Examples
14//!
15//! ```rust
16//! # use plotlib::view::ContinuousView;
17//! use plotlib::grid::Grid;
18//! # use plotlib::style::LineStyle;
19//! # use plotlib::view::View;
20//!
21//! # let l1 = plotlib::repr::Plot::new(vec![(0., 1.), (2., 1.5), (3., 1.2), (4., 1.1)])
22//! # .line_style(LineStyle::new().colour("burlywood"));
23//! // let l1 = Plot::new() ...
24//! let mut v = ContinuousView::new().add(l1);
25//!
26//! // 3 vertical lines and 8 horizontal lines
27//! v.add_grid(Grid::new(3, 8));
28//!
29//! // Render plot
30//! ```
31
32// Internal type representing the logic of when do we render only horizontal lines, and when do we
33// render a full grid
34pub(crate) enum GridType<'a> {
35 HorizontalOnly(&'a Grid),
36 Both(&'a Grid),
37}
38
39/// Configuration for the grid on a plot
40///
41/// Supports changing the number of grid lines for the x and y dimensions.
42/// **Note:** for categorical plots, only horizontal lines will be shown.
43pub struct Grid {
44 /// Number of vertical grid lines (defaults to 3)
45 pub nx: u32,
46 /// Number of horizontal grid lines (defaults to 3)
47 pub ny: u32,
48 /// Color of the grid lines (defaults to "darkgrey")
49 pub color: String,
50}
51
52impl Default for Grid {
53 fn default() -> Self {
54 Grid::new(3, 3)
55 }
56}
57
58impl Grid {
59 /// Create a new grid with `nx` vertical and `ny` horizontal grid lines
60 ///
61 /// The default colour is "darkgrey".
62 pub fn new(nx: u32, ny: u32) -> Grid {
63 Grid {
64 nx,
65 ny,
66 color: "darkgrey".to_owned(),
67 }
68 }
69}