plotters/element/
composable.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
use super::*;
use plotters_backend::DrawingBackend;
use std::borrow::Borrow;
use std::iter::{once, Once};
use std::marker::PhantomData;
use std::ops::Add;

/**
An empty composable element. This is the starting point of a composed element.

# Example

```
use plotters::prelude::*;
let data = [(1.0, 3.3), (2., 2.1), (3., 1.5), (4., 1.9), (5., 1.0)];
let drawing_area = SVGBackend::new("composable.svg", (300, 200)).into_drawing_area();
drawing_area.fill(&WHITE).unwrap();
let mut chart_builder = ChartBuilder::on(&drawing_area);
chart_builder.margin(7).set_left_and_bottom_label_area_size(20);
let mut chart_context = chart_builder.build_cartesian_2d(0.0..5.5, 0.0..5.5).unwrap();
chart_context.configure_mesh().draw().unwrap();
chart_context.draw_series(data.map(|(x, y)| {
    EmptyElement::at((x, y)) // Use the guest coordinate system with EmptyElement
    + Circle::new((0, 0), 10, BLUE) // Use backend coordinates with the rest
    + Cross::new((4, 4), 3, RED)
    + Pixel::new((4, -4), RED)
    + TriangleMarker::new((-4, -4), 4, RED)
})).unwrap();
```

The result is a data series where each point consists of a circle, a cross, a pixel, and a triangle:

![](https://cdn.jsdelivr.net/gh/facorread/plotters-doc-data@06d370f/apidoc/composable.svg)

*/
pub struct EmptyElement<Coord, DB: DrawingBackend> {
    coord: Coord,
    phantom: PhantomData<DB>,
}

impl<Coord, DB: DrawingBackend> EmptyElement<Coord, DB> {
    /**
    An empty composable element. This is the starting point of a composed element.

    See [`EmptyElement`] for more information and examples.
    */
    pub fn at(coord: Coord) -> Self {
        Self {
            coord,
            phantom: PhantomData,
        }
    }
}

impl<Coord, Other, DB: DrawingBackend> Add<Other> for EmptyElement<Coord, DB>
where
    Other: Drawable<DB>,
    for<'a> &'a Other: PointCollection<'a, BackendCoord>,
{
    type Output = BoxedElement<Coord, DB, Other>;
    fn add(self, other: Other) -> Self::Output {
        BoxedElement {
            offset: self.coord,
            inner: other,
            phantom: PhantomData,
        }
    }
}

impl<'a, Coord, DB: DrawingBackend> PointCollection<'a, Coord> for &'a EmptyElement<Coord, DB> {
    type Point = &'a Coord;
    type IntoIter = Once<&'a Coord>;
    fn point_iter(self) -> Self::IntoIter {
        once(&self.coord)
    }
}

impl<Coord, DB: DrawingBackend> Drawable<DB> for EmptyElement<Coord, DB> {
    fn draw<I: Iterator<Item = BackendCoord>>(
        &self,
        _pos: I,
        _backend: &mut DB,
        _: (u32, u32),
    ) -> Result<(), DrawingErrorKind<DB::ErrorType>> {
        Ok(())
    }
}

/**
A container for one drawable element, used for composition.

This is used internally by Plotters and should probably not be included in user code.
See [`EmptyElement`] for more information and examples.
*/
pub struct BoxedElement<Coord, DB: DrawingBackend, A: Drawable<DB>> {
    inner: A,
    offset: Coord,
    phantom: PhantomData<DB>,
}

impl<'b, Coord, DB: DrawingBackend, A: Drawable<DB>> PointCollection<'b, Coord>
    for &'b BoxedElement<Coord, DB, A>
{
    type Point = &'b Coord;
    type IntoIter = Once<&'b Coord>;
    fn point_iter(self) -> Self::IntoIter {
        once(&self.offset)
    }
}

impl<Coord, DB: DrawingBackend, A> Drawable<DB> for BoxedElement<Coord, DB, A>
where
    for<'a> &'a A: PointCollection<'a, BackendCoord>,
    A: Drawable<DB>,
{
    fn draw<I: Iterator<Item = BackendCoord>>(
        &self,
        mut pos: I,
        backend: &mut DB,
        ps: (u32, u32),
    ) -> Result<(), DrawingErrorKind<DB::ErrorType>> {
        if let Some((x0, y0)) = pos.next() {
            self.inner.draw(
                self.inner.point_iter().into_iter().map(|p| {
                    let p = p.borrow();
                    (p.0 + x0, p.1 + y0)
                }),
                backend,
                ps,
            )?;
        }
        Ok(())
    }
}

impl<Coord, DB: DrawingBackend, My, Yours> Add<Yours> for BoxedElement<Coord, DB, My>
where
    My: Drawable<DB>,
    for<'a> &'a My: PointCollection<'a, BackendCoord>,
    Yours: Drawable<DB>,
    for<'a> &'a Yours: PointCollection<'a, BackendCoord>,
{
    type Output = ComposedElement<Coord, DB, My, Yours>;
    fn add(self, yours: Yours) -> Self::Output {
        ComposedElement {
            offset: self.offset,
            first: self.inner,
            second: yours,
            phantom: PhantomData,
        }
    }
}

/**
A container for two drawable elements, used for composition.

This is used internally by Plotters and should probably not be included in user code.
See [`EmptyElement`] for more information and examples.
*/
pub struct ComposedElement<Coord, DB: DrawingBackend, A, B>
where
    A: Drawable<DB>,
    B: Drawable<DB>,
{
    first: A,
    second: B,
    offset: Coord,
    phantom: PhantomData<DB>,
}

impl<'b, Coord, DB: DrawingBackend, A, B> PointCollection<'b, Coord>
    for &'b ComposedElement<Coord, DB, A, B>
where
    A: Drawable<DB>,
    B: Drawable<DB>,
{
    type Point = &'b Coord;
    type IntoIter = Once<&'b Coord>;
    fn point_iter(self) -> Self::IntoIter {
        once(&self.offset)
    }
}

impl<Coord, DB: DrawingBackend, A, B> Drawable<DB> for ComposedElement<Coord, DB, A, B>
where
    for<'a> &'a A: PointCollection<'a, BackendCoord>,
    for<'b> &'b B: PointCollection<'b, BackendCoord>,
    A: Drawable<DB>,
    B: Drawable<DB>,
{
    fn draw<I: Iterator<Item = BackendCoord>>(
        &self,
        mut pos: I,
        backend: &mut DB,
        ps: (u32, u32),
    ) -> Result<(), DrawingErrorKind<DB::ErrorType>> {
        if let Some((x0, y0)) = pos.next() {
            self.first.draw(
                self.first.point_iter().into_iter().map(|p| {
                    let p = p.borrow();
                    (p.0 + x0, p.1 + y0)
                }),
                backend,
                ps,
            )?;
            self.second.draw(
                self.second.point_iter().into_iter().map(|p| {
                    let p = p.borrow();
                    (p.0 + x0, p.1 + y0)
                }),
                backend,
                ps,
            )?;
        }
        Ok(())
    }
}

impl<Coord, DB: DrawingBackend, A, B, C> Add<C> for ComposedElement<Coord, DB, A, B>
where
    A: Drawable<DB>,
    for<'a> &'a A: PointCollection<'a, BackendCoord>,
    B: Drawable<DB>,
    for<'a> &'a B: PointCollection<'a, BackendCoord>,
    C: Drawable<DB>,
    for<'a> &'a C: PointCollection<'a, BackendCoord>,
{
    type Output = ComposedElement<Coord, DB, A, ComposedElement<BackendCoord, DB, B, C>>;
    fn add(self, rhs: C) -> Self::Output {
        ComposedElement {
            offset: self.offset,
            first: self.first,
            second: ComposedElement {
                offset: (0, 0),
                first: self.second,
                second: rhs,
                phantom: PhantomData,
            },
            phantom: PhantomData,
        }
    }
}