1 use crate::element::PointElement;
2 use crate::style::{ShapeStyle, SizeDesc};
3 
4 /// The point plot object, which takes an iterator of points in guest coordinate system
5 /// and create an element for each point
6 pub struct PointSeries<'a, Coord, I: IntoIterator<Item = Coord>, E, Size: SizeDesc + Clone> {
7     style: ShapeStyle,
8     size: Size,
9     data_iter: I::IntoIter,
10     make_point: &'a dyn Fn(Coord, Size, ShapeStyle) -> E,
11 }
12 
13 impl<'a, Coord, I: IntoIterator<Item = Coord>, E, Size: SizeDesc + Clone> Iterator
14     for PointSeries<'a, Coord, I, E, Size>
15 {
16     type Item = E;
next(&mut self) -> Option<Self::Item>17     fn next(&mut self) -> Option<Self::Item> {
18         self.data_iter
19             .next()
20             .map(|x| (self.make_point)(x, self.size.clone(), self.style))
21     }
22 }
23 
24 impl<'a, Coord, I: IntoIterator<Item = Coord>, E, Size: SizeDesc + Clone>
25     PointSeries<'a, Coord, I, E, Size>
26 where
27     E: PointElement<Coord, Size>,
28 {
29     /// Create a new point series with the element that implements point trait.
30     /// You may also use a more general way to create a point series with `of_element`
31     /// function which allows a customized element construction function
new<S: Into<ShapeStyle>>(iter: I, size: Size, style: S) -> Self32     pub fn new<S: Into<ShapeStyle>>(iter: I, size: Size, style: S) -> Self {
33         Self {
34             data_iter: iter.into_iter(),
35             size,
36             style: style.into(),
37             make_point: &|a, b, c| E::make_point(a, b, c),
38         }
39     }
40 }
41 
42 impl<'a, Coord, I: IntoIterator<Item = Coord>, E, Size: SizeDesc + Clone>
43     PointSeries<'a, Coord, I, E, Size>
44 {
45     /// Create a new point series. Similar to `PointSeries::new` but it doesn't
46     /// requires the element implements point trait. So instead of using the point
47     /// constructor, it uses the customized function for element creation
of_element<S: Into<ShapeStyle>, F: Fn(Coord, Size, ShapeStyle) -> E>( iter: I, size: Size, style: S, cons: &'a F, ) -> Self48     pub fn of_element<S: Into<ShapeStyle>, F: Fn(Coord, Size, ShapeStyle) -> E>(
49         iter: I,
50         size: Size,
51         style: S,
52         cons: &'a F,
53     ) -> Self {
54         Self {
55             data_iter: iter.into_iter(),
56             size,
57             style: style.into(),
58             make_point: cons,
59         }
60     }
61 }
62