Skip to content

Commit 6ebe510

Browse files
committed
Update some documentations
1 parent b4381bf commit 6ebe510

File tree

11 files changed

+75
-24
lines changed

11 files changed

+75
-24
lines changed

src/chart/context.rs

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,26 @@ pub struct ChartContext<'a, DB: DrawingBackend, CT: CoordTranslate> {
8484
///
8585
/// For each frame, instead of updating the entire backend, we are able to keep the keep the figure
8686
/// component like axis, labels untouched and make updates only in the plotting drawing area.
87+
/// This is very useful for incremental render.
88+
/// ```rust
89+
/// use plotters::prelude::*;
90+
/// let mut buffer = vec![0u8;1024*768*3];
91+
/// let area = BitMapBackend::with_buffer(&mut buffer[..], (1024, 768))
92+
/// .into_drawing_area()
93+
/// .split_evenly((1,2));
94+
/// let chart = ChartBuilder::on(&area[0])
95+
/// .caption("Incremental Example", ("sans-serif", 20))
96+
/// .set_all_label_area_size(30)
97+
/// .build_ranged(0..10, 0..10)
98+
/// .expect("Unable to build ChartContext");
99+
/// // Draw the first frame at this point
100+
/// area[0].present().expect("Present");
101+
/// let state = chart.into_chart_state();
102+
/// // Let's draw the second frame
103+
/// let chart = state.restore(&area[0]);
104+
/// chart.plotting_area().fill(&WHITE).unwrap(); // Clear the previously drawn graph
105+
/// // At this point, you are able to draw next frame
106+
///```
87107
pub struct ChartState<CT: CoordTranslate> {
88108
drawing_area_pos: (i32, i32),
89109
drawing_area_size: (u32, u32),
@@ -112,7 +132,7 @@ impl<'a, DB: DrawingBackend, CT: CoordTranslate> From<ChartContext<'a, DB, CT>>
112132

113133
impl<'a, DB: DrawingBackend, CT: CoordTranslate> ChartContext<'a, DB, CT> {
114134
/// Convert a chart context into a chart state, by doing so, the chart context is consumed and
115-
/// a saved chart state is created for later use.
135+
/// a saved chart state is created for later use. This is typically used in incrmental rendering. See documentation of `ChartState` for more detailed example.
116136
pub fn into_chart_state(self) -> ChartState<CT> {
117137
self.into()
118138
}
@@ -201,7 +221,7 @@ impl<
201221
}
202222

203223
/// Initialize a mesh configuration object and mesh drawing can be finalized by calling
204-
/// the function `MeshStyle::draw`
224+
/// the function `MeshStyle::draw`.
205225
pub fn configure_mesh<'b>(&'b mut self) -> MeshStyle<'a, 'b, X, Y, DB> {
206226
let base_tick_size = (5u32).percent().max(5).in_pixels(&self.drawing_area);
207227

@@ -258,6 +278,7 @@ impl<'a, DB: DrawingBackend + 'a, CT: CoordTranslate> ChartContext<'a, DB, CT> {
258278
}
259279

260280
impl<'a, DB: DrawingBackend, CT: CoordTranslate> ChartContext<'a, DB, CT> {
281+
/// Cast the reference to a chart context to a reference to underlying coordinate specification.
261282
pub fn as_coord_spec(&self) -> &CT {
262283
self.drawing_area.as_coord_spec()
263284
}
@@ -354,7 +375,8 @@ impl<'a, DB: DrawingBackend, X: Ranged, Y: Ranged> ChartContext<'a, DB, RangedCo
354375
&mut self.series_anno[idx]
355376
}
356377

357-
/// Draw a data series. A data series in Plotters is abstracted as an iterator of elements
378+
/// Draw a data series. A data series in Plotters is abstracted as an iterator of elements.
379+
/// - **Returns**: Either drawing error or a series annotation object thus we can put annotation to current series (e.g. legend)
358380
pub fn draw_series<E, R, S>(
359381
&mut self,
360382
series: S,
@@ -724,7 +746,8 @@ impl<'a, DB: DrawingBackend, X: Ranged, Y: Ranged> ChartContext<'a, DB, RangedCo
724746
Ok(())
725747
}
726748

727-
/// Convert this chart context into a dual axis chart context
749+
/// Convert this chart context into a dual axis chart context and attach a second coordinate spec
750+
/// on the chart context. For more detailed information, see documentation for [struct DualCoordChartContext](struct.DualCoordChartContext.html)
728751
///
729752
/// - `x_coord`: The coordinate spec for the X axis
730753
/// - `y_coord`: The coordinate spec for the Y axis

src/chart/dual_coord.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,22 @@ use crate::element::{Drawable, PointCollection};
1515

1616
use plotters_backend::{BackendCoord, DrawingBackend};
1717

18-
/// The chart context that has two coordinate system attached
18+
/// The chart context that has two coordinate system attached.
19+
/// This situation is quite common, for example, we with two different coodinate system.
20+
/// For instance this example <img src="https://plotters-rs.github.io/plotters-doc-data/twoscale.png"></img>
21+
/// This is done by attaching a second coordinate system to ChartContext by method [ChartContext::set_secondary_coord](struct.ChartContext.html#method.set_secondary_coord).
22+
/// For instance of dual coordinate charts, see [this example](https://github.com/38/plotters/blob/master/examples/two-scales.rs#L15).
23+
/// Note: `DualCoordChartContext` is always deref to the chart context.
24+
/// - If you want to configure the secondary axis, method [DualCoordChartContext::configure_secondary_axes](struct.DualCoordChartContext.html#method.configure_secondary_axes)
25+
/// - If you want to draw a series using secondary coordinate system, use [DualCoordChartContext::draw_secondary_series](struct.DualCoordChartContext.html#method.draw_secondary_series). And method [ChartContext::draw_series](struct.ChartContext.html#method.draw_series) will always use primary coordinate spec.
1926
pub struct DualCoordChartContext<'a, DB: DrawingBackend, CT1: CoordTranslate, CT2: CoordTranslate> {
2027
pub(super) primary: ChartContext<'a, DB, CT1>,
2128
pub(super) secondary: ChartContext<'a, DB, CT2>,
2229
}
2330

2431
/// The chart state for a dual coord chart, see the detailed description for `ChartState` for more
25-
/// information about the purpose of a chart state
32+
/// information about the purpose of a chart state.
33+
/// Similar to [ChartState](struct.ChartState.html), but used for the dual coordinate charts.
2634
pub struct DualCoordChartState<CT1: CoordTranslate, CT2: CoordTranslate> {
2735
primary: ChartState<CT1>,
2836
secondary: ChartState<CT2>,
@@ -31,15 +39,15 @@ pub struct DualCoordChartState<CT1: CoordTranslate, CT2: CoordTranslate> {
3139
impl<'a, DB: DrawingBackend, CT1: CoordTranslate, CT2: CoordTranslate>
3240
DualCoordChartContext<'a, DB, CT1, CT2>
3341
{
34-
/// Convert the chart context into a chart state
42+
/// Convert the chart context into a chart state, similar to [ChartContext::into_chart_state](struct.ChartContext.html#method.into_chart_state)
3543
pub fn into_chart_state(self) -> DualCoordChartState<CT1, CT2> {
3644
DualCoordChartState {
3745
primary: self.primary.into(),
3846
secondary: self.secondary.into(),
3947
}
4048
}
4149

42-
/// Convert the chart context into a sharable chart state
50+
/// Convert the chart context into a sharable chart state.
4351
pub fn into_shared_chart_state(self) -> DualCoordChartState<Arc<CT1>, Arc<CT2>> {
4452
DualCoordChartState {
4553
primary: self.primary.into_shared_chart_state(),
@@ -54,7 +62,7 @@ where
5462
CT1: Clone,
5563
CT2: Clone,
5664
{
57-
/// Copy the coordinate specs and make the chart state
65+
/// Copy the coordinate specs and make a chart state
5866
pub fn to_chart_state(&self) -> DualCoordChartState<CT1, CT2> {
5967
DualCoordChartState {
6068
primary: self.primary.to_chart_state(),
@@ -183,7 +191,7 @@ where
183191
impl<'a, DB: DrawingBackend, X: Ranged, Y: Ranged, SX: Ranged, SY: Ranged>
184192
DualCoordChartContext<'a, DB, RangedCoord<X, Y>, RangedCoord<SX, SY>>
185193
{
186-
/// Draw a series use the secondary coordinate system
194+
/// Draw a series use the secondary coordinate system.
187195
/// - `series`: The series to draw
188196
/// - `Returns` the series annotation object or error code
189197
pub fn draw_secondary_series<E, R, S>(

src/chart/mesh.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,17 @@ where
3737
}
3838

3939
/// The offset of x labels. This is used when we want to place the label in the middle of
40-
/// the grid. This is useful if we are drawing a histogram
40+
/// the grid. This is used to adjust label position for histograms, but since plotters 0.3, this
41+
/// use case is deprecated, see [CentricDiscreteRanged coord decorator](../coord/trait.IntoCentric.html) for more details
4142
/// - `value`: The offset in pixel
4243
pub fn x_label_offset<S: SizeDesc>(&mut self, value: S) -> &mut Self {
4344
self.style.x_label_offset(value);
4445
self
4546
}
4647

4748
/// The offset of y labels. This is used when we want to place the label in the middle of
48-
/// the grid. This is useful if we are drawing a histogram
49+
/// the grid. This is used to adjust label position for histograms, but since plotters 0.3, this
50+
/// use case is deprecated, see [CentricDiscreteRanged coord decorator](../coord/trait.IntoCentric.html) for more details
4951
/// - `value`: The offset in pixel
5052
pub fn y_label_offset<S: SizeDesc>(&mut self, value: S) -> &mut Self {
5153
self.style.y_label_offset(value);
@@ -202,15 +204,17 @@ where
202204
}
203205

204206
/// The offset of x labels. This is used when we want to place the label in the middle of
205-
/// the grid. This is useful if we are drawing a histogram
207+
/// the grid. This is used to adjust label position for histograms, but since plotters 0.3, this
208+
/// use case is deprecated, see [CentricDiscreteRanged coord decorator](../coord/trait.IntoCentric.html) for more details
206209
/// - `value`: The offset in pixel
207210
pub fn x_label_offset<S: SizeDesc>(&mut self, value: S) -> &mut Self {
208211
self.x_label_offset = value.in_pixels(&self.parent_size);
209212
self
210213
}
211214

212215
/// The offset of y labels. This is used when we want to place the label in the middle of
213-
/// the grid. This is useful if we are drawing a histogram
216+
/// the grid. This is used to adjust label position for histograms, but since plotters 0.3, this
217+
/// use case is deprecated, see [CentricDiscreteRanged coord decorator](../coord/trait.IntoCentric.html) for more details
214218
/// - `value`: The offset in pixel
215219
pub fn y_label_offset<S: SizeDesc>(&mut self, value: S) -> &mut Self {
216220
self.y_label_offset = value.in_pixels(&self.parent_size);

src/chart/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,5 @@ mod series;
2121
pub use builder::{ChartBuilder, LabelAreaPosition};
2222
pub use context::{ChartContext, ChartState, SeriesAnno};
2323
pub use dual_coord::{DualCoordChartContext, DualCoordChartState};
24-
pub use mesh::MeshStyle;
24+
pub use mesh::{MeshStyle, SecondaryMeshStyle};
2525
pub use series::{SeriesLabelPosition, SeriesLabelStyle};

src/coord/datetime.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ use std::ops::{Add, Range, Sub};
44

55
use super::{AsRangedCoord, DiscreteRanged, Ranged, ValueFormatter};
66

7-
/// The trait that describe some time value
7+
/// The trait that describe some time value. This is the uniformed abstraction that works
8+
/// for both Date, DateTime and Duration, etc.
89
pub trait TimeValue: Eq {
910
type DateType: Datelike + PartialOrd;
1011

@@ -16,12 +17,12 @@ pub trait TimeValue: Eq {
1617
fn earliest_after_date(date: Self::DateType) -> Self;
1718
/// Returns the duration between two time value
1819
fn subtract(&self, other: &Self) -> Duration;
19-
/// Get the timezone information for current value
20+
/// Instantiate a date type for current time value;
2021
fn ymd(&self, year: i32, month: u32, date: u32) -> Self::DateType;
2122
/// Cast current date type into this type
2223
fn from_date(date: Self::DateType) -> Self;
2324

24-
/// Map the coord
25+
/// Map the coord spec
2526
fn map_coord(value: &Self, begin: &Self, end: &Self, limit: (i32, i32)) -> i32 {
2627
let total_span = end.subtract(begin);
2728
let value_span = value.subtract(begin);
@@ -34,6 +35,7 @@ pub trait TimeValue: Eq {
3435
}
3536
}
3637

38+
// Yes, converting them to floating point may lose precision, but this is Ok.
3739
// If it overflows, it means we have a time span nearly 300 years, we are safe to ignore the
3840
// portion less than 1 day.
3941
let total_days = total_span.num_days() as f64;

src/coord/discrete.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,12 @@ where
8181
}
8282
}
8383

84-
/// The axis decorator that makes key-point in the center of the value range
84+
/// For any discrete ranged coordiante, one possible transformation is instead of using the exact point to represent the value
85+
/// We are now using an interval [value, value + 1) as the representation of that value.
86+
/// A good example for this transformation is histogram's bucket, each bucket is actually represented by a interval instead of a single point.
87+
/// In addition to that, all the labels should be appreas in the middle of the representing intervals.
88+
///
89+
/// The `CentricDiscreteRange` decorator that makes key-point in the center of the value range
8590
/// This is useful when we draw a histogram, since we want the axis value label
8691
/// to be shown in the middle of the range rather than exactly the location where
8792
/// the value mapped to.
@@ -93,7 +98,7 @@ impl<D: DiscreteRanged + Clone> Clone for CentricDiscreteRange<D> {
9398
}
9499
}
95100

96-
/// The trait for types that can decorated by `CentricDiscreteRange` decorator
101+
/// The trait for types that can decorated by `CentricDiscreteRange` decorator. See [struct CentricDiscreteRange](struct.CentricDiscreteRange.html) for details.
97102
pub trait IntoCentric: AsRangedCoord
98103
where
99104
Self::CoordDescType: DiscreteRanged,
@@ -106,11 +111,14 @@ where
106111

107112
impl<R: AsRangedCoord> IntoCentric for R where R::CoordDescType: DiscreteRanged {}
108113

109-
/// The value that used by the centric coordinate
114+
/// The value that used by the centric coordinate.
110115
#[derive(Clone, Debug)]
111116
pub enum CentricValues<T> {
117+
/// Means we are referring the exact position of value `T`
112118
Exact(T),
119+
/// Means we are referring the center of position `T` and the successor of `T`
113120
CenterOf(T),
121+
/// Referring the last dummy element
114122
Last,
115123
}
116124

@@ -198,6 +206,7 @@ impl<T> From<T> for CentricValues<T> {
198206
}
199207
}
200208

209+
/// The iterator that can be used to iterate all the values defined by a discrete coordinate
201210
pub struct DiscreteValueIter<'a, T: DiscreteRanged>(&'a T, usize, usize);
202211

203212
impl<'a, T: DiscreteRanged> Iterator for DiscreteValueIter<'a, T> {

src/coord/group_by.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ impl<T: DiscreteRanged> Ranged for GroupBy<T> {
5353
fn range(&self) -> Range<T::ValueType> {
5454
self.0.range()
5555
}
56+
// TODO: See issue #88
5657
fn key_points(&self, max_points: usize) -> Vec<T::ValueType> {
5758
let range = 0..(self.0.size() + self.1 - 1) / self.1;
5859
let logic_range: RangedCoordusize = range.into();

src/coord/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ pub use ranged::{
4949

5050
pub use partial_axis::{make_partial_axis, IntoPartialAxis};
5151

52-
pub use discrete::{CentricValues, DiscreteRanged, IntoCentric};
52+
pub use discrete::{CentricDiscreteRange, CentricValues, DiscreteRanged, IntoCentric};
5353
pub use linspace::{IntoLinspace, Linspace};
5454

5555
pub use logarithmic::{LogCoord, LogRange, LogScalable};

src/coord/nested.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use super::{AsRangedCoord, DiscreteRanged, Ranged, ValueFormatter};
22
use std::ops::Range;
33

4-
/// Describe a value for a nested croodinate
4+
/// Describe a value for a nested coordinate
55
#[derive(PartialEq, Eq, Clone, Debug)]
66
pub enum NestedValue<C, V> {
77
/// Category value

src/coord/ranged.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,17 @@ pub trait DefaultValueFormatOption {}
1414
pub struct DefaultFormatting;
1515
impl DefaultValueFormatOption for DefaultFormatting {}
1616

17+
/// This markers prevent Plotters to implement the default `Debug` based formatting
1718
pub struct NoDefaultFormatting;
1819
impl DefaultValueFormatOption for NoDefaultFormatting {}
1920

21+
/// Determine how we can format a value in a coordinate system by default
2022
pub trait ValueFormatter<V> {
23+
/// Format the value
2124
fn format(value: &V) -> String;
2225
}
2326

27+
// By default the value is formatted by the debug trait
2428
impl<R: Ranged<FormatOption = DefaultFormatting>> ValueFormatter<R::ValueType> for R
2529
where
2630
R::ValueType: Debug,

0 commit comments

Comments
 (0)