@@ -5,31 +5,44 @@ use std::ops::Range;
55
66use 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
816pub struct WithKeyPoints < Inner : Ranged > {
917 inner : Inner ,
1018 bold_points : Vec < Inner :: ValueType > ,
1119 light_points : Vec < Inner :: ValueType > ,
1220}
1321
1422impl < 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
8295where
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
94121impl < 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)
96128pub struct WithKeyPointMethod < R : Ranged > {
97129 inner : R ,
98130 bold_func : Box < dyn Fn ( usize ) -> Vec < R :: ValueType > > ,
@@ -103,6 +135,20 @@ pub trait BindKeyPointMethod
103135where
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
118164impl < T : AsRangedCoord > BindKeyPointMethod for T { }
119165
120166impl < 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 ,
0 commit comments