plotters_unsable/series/
point_series.rs

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