Skip to content

Commit 5602f6a

Browse files
committed
Make all discrete coordinate implements the unmap && improve documents
1 parent 1df31e3 commit 5602f6a

File tree

7 files changed

+43
-21
lines changed

7 files changed

+43
-21
lines changed

src/chart/builder.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ use crate::style::{IntoTextStyle, SizeDesc, TextStyle};
77
use plotters_backend::DrawingBackend;
88

99
/// The enum used to specify the position of label area.
10-
/// This is used when we configure the label area size with the API `set_label_area_size`
10+
/// This is used when we configure the label area size with the API
11+
/// [ChartBuilder::set_label_area_size](struct ChartBuilder.html#method.set_label_area_size)
1112
#[derive(Copy, Clone)]
1213
pub enum LabelAreaPosition {
1314
Top = 0,

src/coord/discrete.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use super::{AsRangedCoord, Ranged, ValueFormatter};
1+
use super::{AsRangedCoord, Ranged, ReversibleRanged, ValueFormatter};
22
use std::ops::Range;
33

44
/// The trait indicates the coordinate is discrete
@@ -205,6 +205,14 @@ impl<T> From<T> for SegmentValue<T> {
205205
}
206206
}
207207

208+
impl<DC: DiscreteRanged> ReversibleRanged for DC {
209+
fn unmap(&self, input: i32, limit: (i32, i32)) -> Option<Self::ValueType> {
210+
let idx = (f64::from(input - limit.0) * (self.size() as f64) / f64::from(limit.1 - limit.0))
211+
.floor() as usize;
212+
self.from_index(idx)
213+
}
214+
}
215+
208216
/// The iterator that can be used to iterate all the values defined by a discrete coordinate
209217
pub struct DiscreteValueIter<'a, T: DiscreteRanged>(&'a T, usize, usize);
210218

src/coord/group_by.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::ops::Range;
66
/// This is useful, for example, when we have an X axis is a integer and denotes days.
77
/// And we are expecting the tick mark denotes weeks, in this way we can make the range
88
/// spec grouping by 7 elements.
9-
/// With the help of the GroupBy decorator, this can be archived quite easily:
9+
/// With the help of the GroupBy decorator, this can be archived quite easily:
1010
///```rust
1111
///use plotters::prelude::*;
1212
///let mut buf = vec![0;1024*768*3];

src/coord/linspace.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,13 +153,19 @@ where
153153
/// to a discrete coordinate by a giving step.
154154
///
155155
/// For example, range `0f32..100f32` is a continuous coordinate, thus this prevent us having a
156-
/// histogram on it. In this case, to get a histogram, we need to split the original range to a
156+
/// histogram on it since Plotters doesn't know how to segment the range into buckets.
157+
/// In this case, to get a histogram, we need to split the original range to a
157158
/// set of discrete buckets (for example, 0.5 per bucket).
158159
///
159160
/// The linspace decorate abstracting this method. For example, we can have a discrete coordinate:
160161
/// `(0f32..100f32).step(0.5)`.
161162
///
162-
/// Linspace also supports different types of bucket matching method: ceil, floor, round or exact.
163+
/// Linspace also supports different types of bucket matching method - This configuration alters the behavior of
164+
/// [DiscreteCoord::index_of](../trait.DiscreteCoord.html#tymethod.index_of) for Linspace coord spec
165+
/// - **Flooring**, the value falls into the nearst bucket smaller than it. See [Linspace::use_floor](struct.Linspace.html#method.use_floor)
166+
/// - **Round**, the value falls into the nearst bucket. See [Linearspace::use_round](struct.Linspace.html#method.use_round)
167+
/// - **Ceiling**, the value falls into the nearst bucket larger than itself. See [Linspace::use_ceil](struct.Linspace.html#method.use_ceil)
168+
/// - **Exact Matchting**, the value must be exactly same as the butcket value. See [Linspace::use_exact](struct.Linspace.html#method.use_exact)
163169
#[derive(Clone)]
164170
pub struct Linspace<T: Ranged, S: Clone, R: LinspaceRoundingMethod<T::ValueType>>
165171
where

src/coord/logarithmic.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ use super::{AsRangedCoord, Ranged, RangedCoordf64};
22
use std::marker::PhantomData;
33
use std::ops::Range;
44

5-
/// The trait for the type that is able to be presented in the log scale
5+
/// The trait for the type that is able to be presented in the log scale.
6+
/// This trait is primarily used by [LogRange](struct.LogRange.html).
67
pub trait LogScalable: Clone {
78
/// Make the conversion from the type to the floating point number
89
fn as_f64(&self) -> f64;
@@ -47,7 +48,8 @@ impl_log_scalable!(i, u64);
4748
impl_log_scalable!(f, f32);
4849
impl_log_scalable!(f, f64);
4950

50-
/// The decorator type for a range of a log-scaled value
51+
/// The logarithmic coodinate decorator.
52+
/// This decorator is used to make the axis rendered as logarithmically.
5153
pub struct LogRange<V: LogScalable>(pub Range<V>);
5254

5355
impl<V: LogScalable + Clone> Clone for LogRange<V> {

src/coord/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ for a interactive figure.
1818
`RangedCoord` is the 2D cartesian coordinate system that has two `Ranged` axis.
1919
A ranged axis can be logarithmic and by applying an logarithmic axis, the figure is logarithmic scale.
2020
Also, the ranged axis can be deserted, and this is required by the histogram series.
21-
2221
*/
2322
use plotters_backend::BackendCoord;
2423

@@ -37,7 +36,9 @@ mod slice;
3736
pub use slice::RangedSlice;
3837

3938
#[cfg(feature = "chrono")]
40-
pub use datetime::{IntoMonthly, IntoYearly, RangedDate, RangedDateTime, RangedDuration};
39+
pub use datetime::{
40+
IntoMonthly, IntoYearly, Monthly, RangedDate, RangedDateTime, RangedDuration, Yearly,
41+
};
4142
pub use numeric::{
4243
RangedCoordf32, RangedCoordf64, RangedCoordi128, RangedCoordi32, RangedCoordi64,
4344
RangedCoordu128, RangedCoordu32, RangedCoordu64,

src/coord/numeric.rs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,21 @@ macro_rules! impl_ranged_type_trait {
4040
}
4141
};
4242
}
43+
macro_rules! impl_reverse_mapping_trait {
44+
($type:ty, $name: ident) => {
45+
impl ReversibleRanged for $name {
46+
fn unmap(&self, p: i32, (min, max): (i32, i32)) -> Option<$type> {
47+
if p < min.min(max) || p > max.max(min) || min == max {
48+
return None;
49+
}
4350

51+
let logical_offset = f64::from(p - min) / f64::from(max - min);
52+
53+
return Some(((self.1 - self.0) as f64 * logical_offset + self.0 as f64) as $type);
54+
}
55+
}
56+
};
57+
}
4458
macro_rules! make_numeric_coord {
4559
($type:ty, $name:ident, $key_points:ident, $doc: expr) => {
4660
#[doc = $doc]
@@ -78,18 +92,6 @@ macro_rules! make_numeric_coord {
7892
return self.0..self.1;
7993
}
8094
}
81-
82-
impl ReversibleRanged for $name {
83-
fn unmap(&self, p:i32, (min,max): (i32, i32)) -> Option<$type> {
84-
if p < min.min(max) || p > max.max(min) {
85-
return None;
86-
}
87-
88-
let logical_offset = (p - min) as f64 / (max - min) as f64;
89-
90-
return Some(((self.1 - self.0) as f64 * logical_offset + self.0 as f64) as $type);
91-
}
92-
}
9395
};
9496
}
9597

@@ -204,12 +206,14 @@ make_numeric_coord!(
204206
compute_f32_key_points,
205207
"The ranged coordinate for type f32"
206208
);
209+
impl_reverse_mapping_trait!(f32, RangedCoordf32);
207210
make_numeric_coord!(
208211
f64,
209212
RangedCoordf64,
210213
compute_f64_key_points,
211214
"The ranged coordinate for type f64"
212215
);
216+
impl_reverse_mapping_trait!(f64, RangedCoordf64);
213217
make_numeric_coord!(
214218
u32,
215219
RangedCoordu32,

0 commit comments

Comments
 (0)