plotters/coord/
translate.rs

1use plotters_backend::BackendCoord;
2use std::ops::Deref;
3
4/// The trait that translates some customized object to the backend coordinate
5pub trait CoordTranslate {
6    type From;
7
8    /// Translate the guest coordinate to the guest coordinate
9    fn translate(&self, from: &Self::From) -> BackendCoord;
10
11    /// Get the Z-value of current coordinate
12    fn depth(&self, _from: &Self::From) -> i32 {
13        0
14    }
15}
16
17impl<C, T> CoordTranslate for T
18where
19    C: CoordTranslate,
20    T: Deref<Target = C>,
21{
22    type From = C::From;
23    fn translate(&self, from: &Self::From) -> BackendCoord {
24        self.deref().translate(from)
25    }
26}
27
28/// The trait indicates that the coordinate system supports reverse transform
29/// This is useful when we need an interactive plot, thus we need to map the event
30/// from the backend coordinate to the logical coordinate
31pub trait ReverseCoordTranslate: CoordTranslate {
32    /// Reverse translate the coordinate from the drawing coordinate to the
33    /// logic coordinate.
34    /// Note: the return value is an option, because it's possible that the drawing
35    /// coordinate isn't able to be represented in te guest coordinate system
36    fn reverse_translate(&self, input: BackendCoord) -> Option<Self::From>;
37}