Skip to content

Commit a17cd42

Browse files
committed
Docs
1 parent 2122d11 commit a17cd42

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-0
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55

66
- Adjust Canvas backend size with DPR (Thanks to Marius-Mueller)
77

8+
### Improvement
9+
10+
- Enhanced key point algorithms: New key point hint trait are used for key point algorithms && many improvment on key point algorithms for different types
11+
- Renamed `MeshStyle::line\_style\_1\ and `MeshStyle::line\_style\_2` to `bold\_line\_style` and `light\_line\_style`
12+
813
## Plotters 0.2.15 (2020-05-26)
914
### Fixed
1015

src/coord/ckps.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,44 @@ use std::ops::Range;
55

66
use super::{AsRangedCoord, DiscreteRanged, KeyPointHint, Ranged};
77

8+
/// The coordinate decorator that binds a key point vector.
9+
/// Normally, all the ranged coordinate implements its own keypoint algorithm
10+
/// to determine how to render the tick mark and mesh grid.
11+
/// This decorator allows customized tick mark specifiied by vector.
12+
/// See [BindKeyPoints::with_key_points](trait.BindKeyPoints.html#tymethod.with_key_points)
13+
/// for details.
14+
/// Note: For any coordinate spec wrapped by this decorator, the maxium number of labels configured by
15+
/// MeshStyle will be ignored and the key point function will always returns the entire vector
816
pub struct WithKeyPoints<Inner: Ranged> {
917
inner: Inner,
1018
bold_points: Vec<Inner::ValueType>,
1119
light_points: Vec<Inner::ValueType>,
1220
}
1321

1422
impl<I: Ranged> WithKeyPoints<I> {
23+
/// Specify the light key points, which is used to render the light mesh line
1524
pub fn with_light_points<T: IntoIterator<Item = I::ValueType>>(mut self, iter: T) -> Self {
1625
self.light_points.clear();
1726
self.light_points.extend(iter);
1827
self
1928
}
2029

30+
/// Get a reference to the bold points
2131
pub fn bold_points(&self) -> &[I::ValueType] {
2232
self.bold_points.as_ref()
2333
}
2434

35+
/// Get a mut reference to the bold points
2536
pub fn bold_points_mut(&mut self) -> &mut [I::ValueType] {
2637
self.bold_points.as_mut()
2738
}
2839

40+
/// Get a reference to light key points
2941
pub fn light_points(&self) -> &[I::ValueType] {
3042
self.light_points.as_ref()
3143
}
3244

45+
/// Get a mut reference to the light key points
3346
pub fn light_points_mut(&mut self) -> &mut [I::ValueType] {
3447
self.light_points.as_mut()
3548
}
@@ -82,6 +95,20 @@ pub trait BindKeyPoints
8295
where
8396
Self: AsRangedCoord,
8497
{
98+
/// Bind a existing coordinate spec with a given key points vector. See [WithKeyPoints](struct.WithKeyPoints.html ) for more details.
99+
/// Example:
100+
/// ```
101+
///use plotters::prelude::*;
102+
///use plotters_bitmap::BitMapBackend;
103+
///let mut buffer = vec![0;1024*768*3];
104+
/// let root = BitMapBackend::with_buffer(&mut buffer, (1024, 768)).into_drawing_area();
105+
/// let mut chart = ChartBuilder::on(&root)
106+
/// .build_ranged(
107+
/// (0..100).with_key_points(vec![1,20,50,90]), // <= This line will make the plot shows 4 tick marks at 1, 20, 50, 90
108+
/// 0..10
109+
/// ).unwrap();
110+
/// chart.configure_mesh().draw().unwrap();
111+
///```
85112
fn with_key_points(self, points: Vec<Self::Value>) -> WithKeyPoints<Self::CoordDescType> {
86113
WithKeyPoints {
87114
inner: self.into(),
@@ -93,6 +120,11 @@ where
93120

94121
impl<T: AsRangedCoord> BindKeyPoints for T {}
95122

123+
/// The coordinate decorator that allows customized keypoint algorithms.
124+
/// Normally, all the coordinate spec implements its own key point algorith
125+
/// But this decorator allows you override the pre-defined key point algorithm.
126+
///
127+
/// To use this decorator, see [BindKeyPointMethod::with_key_point_func](trait.BindKeyPointMethod.html#tymethod.with_key_point_func)
96128
pub struct WithKeyPointMethod<R: Ranged> {
97129
inner: R,
98130
bold_func: Box<dyn Fn(usize) -> Vec<R::ValueType>>,
@@ -103,6 +135,20 @@ pub trait BindKeyPointMethod
103135
where
104136
Self: AsRangedCoord,
105137
{
138+
/// Bind a existing coordinate spec with a given key points algorithm. See [WithKeyPointMethod](struct.WithKeyMethod.html ) for more details.
139+
/// Example:
140+
/// ```
141+
///use plotters::prelude::*;
142+
///use plotters_bitmap::BitMapBackend;
143+
///let mut buffer = vec![0;1024*768*3];
144+
/// let root = BitMapBackend::with_buffer(&mut buffer, (1024, 768)).into_drawing_area();
145+
/// let mut chart = ChartBuilder::on(&root)
146+
/// .build_ranged(
147+
/// (0..100).with_key_point_func(|n| (0..100 / n as i32).map(|x| x * 100 / n as i32).collect()),
148+
/// 0..10
149+
/// ).unwrap();
150+
/// chart.configure_mesh().draw().unwrap();
151+
///```
106152
fn with_key_point_func<F: Fn(usize) -> Vec<Self::Value> + 'static>(
107153
self,
108154
func: F,
@@ -118,6 +164,7 @@ where
118164
impl<T: AsRangedCoord> BindKeyPointMethod for T {}
119165

120166
impl<R: Ranged> WithKeyPointMethod<R> {
167+
/// Define the light key point algorithm, by default this returns an empty set
121168
pub fn with_light_point_func<F: Fn(usize) -> Vec<R::ValueType> + 'static>(
122169
mut self,
123170
func: F,

src/coord/ranged.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ pub enum KeyPointWeight {
4343
}
4444

4545
impl KeyPointWeight {
46+
/// Check if this key point weight setting allows light point
4647
pub fn allow_light_points(&self) -> bool {
4748
match self {
4849
Self::Bold => false,
@@ -51,9 +52,16 @@ impl KeyPointWeight {
5152
}
5253
}
5354

55+
/// The trait for a hint provided to the key point algorithm used by the coordinate specs.
56+
/// The most important constraint is the `max_num_points` which means the algorithm could emit no more than specific number of key points
57+
/// `weight` is used to determine if this is used as a bold grid line or light grid line
58+
/// `bold_points` returns the max number of coresponding bold grid lines
5459
pub trait KeyPointHint {
60+
/// Returns the max number of key points
5561
fn max_num_points(&self) -> usize;
62+
/// Returns the weight for this hint
5663
fn weight(&self) -> KeyPointWeight;
64+
/// Returns the point number constraint for the bold points
5765
fn bold_points(&self) -> usize {
5866
self.max_num_points()
5967
}
@@ -69,6 +77,7 @@ impl KeyPointHint for usize {
6977
}
7078
}
7179

80+
/// The key point hint indicates we only need key point for the bold grid lines
7281
pub struct BoldPoints(pub usize);
7382

7483
impl KeyPointHint for BoldPoints {
@@ -81,12 +90,14 @@ impl KeyPointHint for BoldPoints {
8190
}
8291
}
8392

93+
/// The key point hint indicates that we are using the key points for the light grid lines
8494
pub struct LightPoints {
8595
bold_points_num: usize,
8696
light_limit: usize,
8797
}
8898

8999
impl LightPoints {
100+
/// Create a new light key point hind
90101
pub fn new(bold_count: usize, requested: usize) -> Self {
91102
Self {
92103
bold_points_num: bold_count,

0 commit comments

Comments
 (0)