Skip to main content

box_plotters/box_plot/
orientation.rs

1use std::marker::PhantomData;
2
3use plotters_backend::BackendCoord;
4
5/// Trait implemented by box plot orientations ([Vertical] and [Horizontal])
6pub trait Orientation<K, V> {
7    type XType;
8    type YType;
9
10    fn make_coord(key: K, val: V) -> (Self::XType, Self::YType);
11    fn with_offset(coord: BackendCoord, offset: i32) -> BackendCoord;
12}
13
14/// Represents a vertical orientation.
15pub struct Vertical<K, V>(PhantomData<(K, V)>);
16/// Represents a horizontal orientation.
17pub struct Horizontal<K, V>(PhantomData<(K, V)>);
18
19impl<K, V> Orientation<K, V> for Vertical<K, V> {
20    type XType = K;
21    type YType = V;
22
23    fn make_coord(key: K, val: V) -> (K, V) {
24        (key, val)
25    }
26
27    fn with_offset(coord: BackendCoord, offset: i32) -> BackendCoord {
28        (coord.0 + offset, coord.1)
29    }
30}
31
32impl<K, V> Orientation<K, V> for Horizontal<K, V> {
33    type XType = V;
34    type YType = K;
35
36    fn make_coord(key: K, val: V) -> (V, K) {
37        (val, key)
38    }
39
40    fn with_offset(coord: BackendCoord, offset: i32) -> BackendCoord {
41        (coord.0, coord.1 + offset)
42    }
43}