| 1 |
use std::collections::{hash_map::IntoIter as HashMapIter, HashMap}; |
| 2 |
use std::marker::PhantomData; |
| 3 |
use std::ops::AddAssign; |
| 4 |
|
| 5 |
use crate::chart::ChartContext; |
| 6 |
use crate::coord::cartesian::Cartesian2d; |
| 7 |
use crate::coord::ranged1d::{DiscreteRanged, Ranged}; |
| 8 |
use crate::element::Rectangle; |
| 9 |
use crate::style::{Color, ShapeStyle, GREEN}; |
| 10 |
use plotters_backend::DrawingBackend; |
| 11 |
|
| 12 |
pub trait HistogramType {} |
| 13 |
pub struct Vertical; |
| 14 |
pub struct Horizontal; |
| 15 |
|
| 16 |
impl HistogramType for Vertical {} |
| 17 |
impl HistogramType for Horizontal {} |
| 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 |
pub struct Histogram<'a, BR, A, Tag = Vertical> |
| 81 |
where |
| 82 |
BR: DiscreteRanged, |
| 83 |
A: AddAssign<A> + Default, |
| 84 |
Tag: HistogramType, |
| 85 |
{ |
| 86 |
style: Box<dyn Fn(&BR::ValueType, &A) -> ShapeStyle + 'a>, |
| 87 |
margin: u32, |
| 88 |
iter: HashMapIter<usize, A>, |
| 89 |
baseline: Box<dyn Fn(&BR::ValueType) -> A + 'a>, |
| 90 |
br: BR, |
| 91 |
_p: PhantomData<Tag>, |
| 92 |
} |
| 93 |
|
| 94 |
impl<'a, BR, A, Tag> Histogram<'a, BR, A, Tag> |
| 95 |
where |
| 96 |
BR: DiscreteRanged + Clone, |
| 97 |
A: AddAssign<A> + Default + 'a, |
| 98 |
Tag: HistogramType, |
| 99 |
{ |
| 100 |
fn empty(br: &BR) -> Self { |
| 101 |
Self { |
| 102 |
style: Box::new(|_, _| GREEN.filled()), |
| 103 |
margin: 5, |
| 104 |
iter: HashMap::new().into_iter(), |
| 105 |
baseline: Box::new(|_| A::default()), |
| 106 |
br: br.clone(), |
| 107 |
_p: PhantomData, |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
pub fn style<S: Into<ShapeStyle>>(mut self, style: S) -> Self { |
| 116 |
let style = style.into(); |
| 117 |
self.style = Box::new(move |_, _| style); |
| 118 |
self |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
pub fn style_func( |
| 129 |
mut self, |
| 130 |
style_func: impl Fn(&BR::ValueType, &A) -> ShapeStyle + 'a, |
| 131 |
) -> Self { |
| 132 |
self.style = Box::new(style_func); |
| 133 |
self |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
pub fn baseline(mut self, baseline: A) -> Self |
| 142 |
where |
| 143 |
A: Clone, |
| 144 |
{ |
| 145 |
self.baseline = Box::new(move |_| baseline.clone()); |
| 146 |
self |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
pub fn baseline_func(mut self, func: impl Fn(&BR::ValueType) -> A + 'a) -> Self { |
| 157 |
self.baseline = Box::new(func); |
| 158 |
self |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
pub fn margin(mut self, value: u32) -> Self { |
| 167 |
self.margin = value; |
| 168 |
self |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
pub fn data<TB: Into<BR::ValueType>, I: IntoIterator<Item = (TB, A)>>( |
| 177 |
mut self, |
| 178 |
iter: I, |
| 179 |
) -> Self { |
| 180 |
let mut buffer = HashMap::<usize, A>::new(); |
| 181 |
for (x, y) in iter.into_iter() { |
| 182 |
if let Some(x) = self.br.index_of(&x.into()) { |
| 183 |
*buffer.entry(x).or_default() += y; |
| 184 |
} |
| 185 |
} |
| 186 |
self.iter = buffer.into_iter(); |
| 187 |
self |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
impl<'a, BR, A> Histogram<'a, BR, A, Vertical> |
| 192 |
where |
| 193 |
BR: DiscreteRanged + Clone, |
| 194 |
A: AddAssign<A> + Default + 'a, |
| 195 |
{ |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
pub fn vertical<ACoord, DB: DrawingBackend + 'a>( |
| 202 |
parent: &ChartContext<DB, Cartesian2d<BR, ACoord>>, |
| 203 |
) -> Self |
| 204 |
where |
| 205 |
ACoord: Ranged<ValueType = A>, |
| 206 |
{ |
| 207 |
let dp = parent.as_coord_spec().x_spec(); |
| 208 |
|
| 209 |
Self::empty(dp) |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
impl<'a, BR, A> Histogram<'a, BR, A, Horizontal> |
| 214 |
where |
| 215 |
BR: DiscreteRanged + Clone, |
| 216 |
A: AddAssign<A> + Default + 'a, |
| 217 |
{ |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
pub fn horizontal<ACoord, DB: DrawingBackend>( |
| 224 |
parent: &ChartContext<DB, Cartesian2d<ACoord, BR>>, |
| 225 |
) -> Self |
| 226 |
where |
| 227 |
ACoord: Ranged<ValueType = A>, |
| 228 |
{ |
| 229 |
let dp = parent.as_coord_spec().y_spec(); |
| 230 |
Self::empty(dp) |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
impl<'a, BR, A> Iterator for Histogram<'a, BR, A, Vertical> |
| 235 |
where |
| 236 |
BR: DiscreteRanged, |
| 237 |
A: AddAssign<A> + Default, |
| 238 |
{ |
| 239 |
type Item = Rectangle<(BR::ValueType, A)>; |
| 240 |
fn next(&mut self) -> Option<Self::Item> { |
| 241 |
while let Some((x, y)) = self.iter.next() { |
| 242 |
if let Some((x, Some(nx))) = self |
| 243 |
.br |
| 244 |
.from_index(x) |
| 245 |
.map(|v| (v, self.br.from_index(x + 1))) |
| 246 |
{ |
| 247 |
let base = (self.baseline)(&x); |
| 248 |
let style = (self.style)(&x, &y); |
| 249 |
let mut rect = Rectangle::new([(x, y), (nx, base)], style); |
| 250 |
rect.set_margin(0, 0, self.margin, self.margin); |
| 251 |
return Some(rect); |
| 252 |
} |
| 253 |
} |
| 254 |
None |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
impl<'a, BR, A> Iterator for Histogram<'a, BR, A, Horizontal> |
| 259 |
where |
| 260 |
BR: DiscreteRanged, |
| 261 |
A: AddAssign<A> + Default, |
| 262 |
{ |
| 263 |
type Item = Rectangle<(A, BR::ValueType)>; |
| 264 |
fn next(&mut self) -> Option<Self::Item> { |
| 265 |
while let Some((y, x)) = self.iter.next() { |
| 266 |
if let Some((y, Some(ny))) = self |
| 267 |
.br |
| 268 |
.from_index(y) |
| 269 |
.map(|v| (v, self.br.from_index(y + 1))) |
| 270 |
{ |
| 271 |
let base = (self.baseline)(&y); |
| 272 |
let style = (self.style)(&y, &x); |
| 273 |
let mut rect = Rectangle::new([(x, y), (base, ny)], style); |
| 274 |
rect.set_margin(self.margin, self.margin, 0, 0); |
| 275 |
return Some(rect); |
| 276 |
} |
| 277 |
} |
| 278 |
None |
| 279 |
} |
| 280 |
} |