| 1 |
use crate::element::{ |
| 2 |
Circle, DashedPathElement, DottedPathElement, DynElement, IntoDynElement, PathElement, |
| 3 |
}; |
| 4 |
use crate::style::{ShapeStyle, SizeDesc}; |
| 5 |
use plotters_backend::{BackendCoord, DrawingBackend}; |
| 6 |
use std::marker::PhantomData; |
| 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 |
pub struct LineSeries<DB: DrawingBackend, Coord> { |
| 35 |
style: ShapeStyle, |
| 36 |
data: Vec<Coord>, |
| 37 |
point_idx: usize, |
| 38 |
point_size: u32, |
| 39 |
phantom: PhantomData<DB>, |
| 40 |
} |
| 41 |
|
| 42 |
impl<DB: DrawingBackend, Coord: Clone + 'static> Iterator for LineSeries<DB, Coord> { |
| 43 |
type Item = DynElement<'static, DB, Coord>; |
| 44 |
fn next(&mut self) -> Option<Self::Item> { |
| 45 |
if !self.data.is_empty() { |
| 46 |
if self.point_size > 0 && self.point_idx < self.data.len() { |
| 47 |
let idx = self.point_idx; |
| 48 |
self.point_idx += 1; |
| 49 |
return Some( |
| 50 |
Circle::new(self.data[idx].clone(), self.point_size, self.style).into_dyn(), |
| 51 |
); |
| 52 |
} |
| 53 |
let mut data = vec![]; |
| 54 |
std::mem::swap(&mut self.data, &mut data); |
| 55 |
Some(PathElement::new(data, self.style).into_dyn()) |
| 56 |
} else { |
| 57 |
None |
| 58 |
} |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
impl<DB: DrawingBackend, Coord> LineSeries<DB, Coord> { |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
pub fn new<I: IntoIterator<Item = Coord>, S: Into<ShapeStyle>>(iter: I, style: S) -> Self { |
| 69 |
Self { |
| 70 |
style: style.into(), |
| 71 |
data: iter.into_iter().collect(), |
| 72 |
point_size: 0, |
| 73 |
point_idx: 0, |
| 74 |
phantom: PhantomData, |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
pub fn point_size(mut self, size: u32) -> Self { |
| 84 |
self.point_size = size; |
| 85 |
self |
| 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 |
pub struct DashedLineSeries<I: Iterator + Clone, Size: SizeDesc> { |
| 127 |
points: I, |
| 128 |
size: Size, |
| 129 |
spacing: Size, |
| 130 |
style: ShapeStyle, |
| 131 |
} |
| 132 |
|
| 133 |
impl<I: Iterator + Clone, Size: SizeDesc> DashedLineSeries<I, Size> { |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
pub fn new<I0>(points: I0, size: Size, spacing: Size, style: ShapeStyle) -> Self |
| 141 |
where |
| 142 |
I0: IntoIterator<IntoIter = I>, |
| 143 |
{ |
| 144 |
Self { |
| 145 |
points: points.into_iter(), |
| 146 |
size, |
| 147 |
spacing, |
| 148 |
style, |
| 149 |
} |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
impl<I: Iterator + Clone, Size: SizeDesc> IntoIterator for DashedLineSeries<I, Size> { |
| 154 |
type Item = DashedPathElement<I, Size>; |
| 155 |
type IntoIter = std::iter::Once<Self::Item>; |
| 156 |
|
| 157 |
fn into_iter(self) -> Self::IntoIter { |
| 158 |
std::iter::once(DashedPathElement::new( |
| 159 |
self.points, |
| 160 |
self.size, |
| 161 |
self.spacing, |
| 162 |
self.style, |
| 163 |
)) |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
pub struct DottedLineSeries<I: Iterator + Clone, Size: SizeDesc, Marker> { |
| 169 |
points: I, |
| 170 |
shift: Size, |
| 171 |
spacing: Size, |
| 172 |
func: Box<dyn Fn(BackendCoord) -> Marker>, |
| 173 |
} |
| 174 |
|
| 175 |
impl<I: Iterator + Clone, Size: SizeDesc, Marker> DottedLineSeries<I, Size, Marker> { |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
pub fn new<I0, F>(points: I0, shift: Size, spacing: Size, func: F) -> Self |
| 183 |
where |
| 184 |
I0: IntoIterator<IntoIter = I>, |
| 185 |
F: Fn(BackendCoord) -> Marker + 'static, |
| 186 |
{ |
| 187 |
Self { |
| 188 |
points: points.into_iter(), |
| 189 |
shift, |
| 190 |
spacing, |
| 191 |
func: Box::new(func), |
| 192 |
} |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
impl<I: Iterator + Clone, Size: SizeDesc, Marker: 'static> IntoIterator |
| 197 |
for DottedLineSeries<I, Size, Marker> |
| 198 |
{ |
| 199 |
type Item = DottedPathElement<I, Size, Marker>; |
| 200 |
type IntoIter = std::iter::Once<Self::Item>; |
| 201 |
|
| 202 |
fn into_iter(self) -> Self::IntoIter { |
| 203 |
std::iter::once(DottedPathElement::new( |
| 204 |
self.points, |
| 205 |
self.shift, |
| 206 |
self.spacing, |
| 207 |
self.func, |
| 208 |
)) |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
#[cfg(test)] |
| 213 |
mod test { |
| 214 |
use crate::prelude::*; |
| 215 |
|
| 216 |
#[test] |
| 217 |
fn test_line_series() { |
| 218 |
let drawing_area = create_mocked_drawing_area(200, 200, |m| { |
| 219 |
m.check_draw_path(|c, s, _path| { |
| 220 |
assert_eq!(c, RED.to_rgba()); |
| 221 |
assert_eq!(s, 3); |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
}); |
| 228 |
|
| 229 |
m.drop_check(|b| { |
| 230 |
assert_eq!(b.num_draw_path_call, 8); |
| 231 |
assert_eq!(b.draw_count, 27); |
| 232 |
}); |
| 233 |
}); |
| 234 |
|
| 235 |
let mut chart = ChartBuilder::on(&drawing_area) |
| 236 |
.build_cartesian_2d(0..100, 0..100) |
| 237 |
.expect("Build chart error"); |
| 238 |
|
| 239 |
chart |
| 240 |
.draw_series(LineSeries::new( |
| 241 |
(0..100).map(|x| (x, x)), |
| 242 |
Into::<ShapeStyle>::into(RED).stroke_width(3), |
| 243 |
)) |
| 244 |
.expect("Drawing Error"); |
| 245 |
chart |
| 246 |
.draw_series(DashedLineSeries::new( |
| 247 |
(0..=50).map(|x| (0, x)), |
| 248 |
10, |
| 249 |
5, |
| 250 |
Into::<ShapeStyle>::into(RED).stroke_width(3), |
| 251 |
)) |
| 252 |
.expect("Drawing Error"); |
| 253 |
let mk_f = |c| Circle::new(c, 3, Into::<ShapeStyle>::into(RED).filled()); |
| 254 |
chart |
| 255 |
.draw_series(DottedLineSeries::new((0..=50).map(|x| (x, 0)), 5, 5, mk_f)) |
| 256 |
.expect("Drawing Error"); |
| 257 |
} |
| 258 |
} |