plotters_unsable/coord/mod.rs
1/*!
2Coordinate system abstractions.
3
4Coordinate systems can be attached to drawing areas. By doing so,
5the drawing area can draw elements in the guest coordinate system.
6`DrawingArea::apply_coord_spec` is used to attach new coordinate system
7to the drawing area.
8
9`CoordTranslate` is the trait required by `DrawingArea::apply_coord_spec`. It provides
10the forward coordinate translation: from the logic coordinate to the pixel-based absolute
11backend coordinate system.
12
13When the coordinate type implements `ReverseCoordTranslate`,
14the backward translation is possible, which allows mapping pixel-based coordinate into
15the logic coordinate. It's not usually used for static figure rendering, but may be useful
16for a interactive figure.
17
18`RangedCoord` is the 2D cartesian coordinate system that has two `Ranged` axis.
19A ranged axis can be logarithmic and by applying an logarithmic axis, the figure is logarithmic scale.
20Also, the ranged axis can be decereted, and this is required by the histogram series.
21
22*/
23use crate::drawing::backend::BackendCoord;
24
25#[cfg(feature = "chrono")]
26mod datetime;
27mod logarithmic;
28mod numeric;
29mod ranged;
30
31#[cfg(feature = "chrono")]
32pub use datetime::{RangedDate, RangedDateTime};
33pub use numeric::{
34 RangedCoordf32, RangedCoordf64, RangedCoordi32, RangedCoordi64, RangedCoordu32, RangedCoordu64,
35};
36pub use ranged::{AsRangedCoord, DescreteRanged, MeshLine, Ranged, RangedCoord, ReversableRanged};
37
38pub use logarithmic::{LogCoord, LogRange, LogScalable};
39
40/// The trait that translates some customized object to the backend coordinate
41pub trait CoordTranslate {
42 type From;
43
44 /// Translate the guest coordinate to the guest coordinate
45 fn translate(&self, from: &Self::From) -> BackendCoord;
46}
47
48/// The trait indicates that the coordinate system supports reverse transform
49/// This is useful when we need an interactive plot, thus we need to map the event
50/// from the backend coordinate to the logical coordinate
51pub trait ReverseCoordTranslate: CoordTranslate {
52 /// Reverse translate the coordinate from the drawing coordinate to the
53 /// logic coordinate.
54 /// Note: the return value is an option, because it's possible that the drawing
55 /// coordinate isn't able to be represented in te guest cooredinate system
56 fn reverse_translate(&self, input: BackendCoord) -> Option<Self::From>;
57}
58
59/// The coordinate translation that only impose shift
60#[derive(Debug, Clone)]
61pub struct Shift(pub BackendCoord);
62
63impl CoordTranslate for Shift {
64 type From = BackendCoord;
65 fn translate(&self, from: &Self::From) -> BackendCoord {
66 (from.0 + (self.0).0, from.1 + (self.0).1)
67 }
68}
69
70impl ReverseCoordTranslate for Shift {
71 fn reverse_translate(&self, input: BackendCoord) -> Option<BackendCoord> {
72 Some((input.0 - (self.0).0, input.1 - (self.0).1))
73 }
74}
75
76/// We can compose an abitray transformation with a shift
77pub struct ShiftAndTrans<T: CoordTranslate>(Shift, T);
78
79impl<T: CoordTranslate> CoordTranslate for ShiftAndTrans<T> {
80 type From = T::From;
81 fn translate(&self, from: &Self::From) -> BackendCoord {
82 let temp = self.1.translate(from);
83 self.0.translate(&temp)
84 }
85}
86
87impl<T: ReverseCoordTranslate> ReverseCoordTranslate for ShiftAndTrans<T> {
88 fn reverse_translate(&self, input: BackendCoord) -> Option<T::From> {
89 Some(self.1.reverse_translate(self.0.reverse_translate(input)?)?)
90 }
91}