1use crate::theme::Gradient;
6use presentar_core::{
7 Brick, BrickAssertion, BrickBudget, BrickVerification, Canvas, Color, Constraints, Event,
8 LayoutResult, Point, Rect, Size, TextStyle, TypeId, Widget,
9};
10use std::any::Any;
11use std::time::Duration;
12
13#[derive(Debug, Clone, Copy, Default)]
15pub enum BinStrategy {
16 Count(usize),
18 Width(f64),
20 #[default]
22 Sturges,
23 Scott,
25 FreedmanDiaconis,
27}
28
29#[derive(Debug, Clone, Copy, Default)]
31pub enum HistogramOrientation {
32 #[default]
34 Vertical,
35 Horizontal,
37}
38
39#[derive(Debug, Clone, Copy, Default)]
41pub enum BarStyle {
42 #[default]
44 Solid,
45 Blocks,
47 Ascii,
49}
50
51#[derive(Debug, Clone)]
53pub struct Histogram {
54 data: Vec<f64>,
55 bins: BinStrategy,
56 orientation: HistogramOrientation,
57 bar_style: BarStyle,
58 color: Color,
59 gradient: Option<Gradient>,
60 show_labels: bool,
61 bounds: Rect,
62 computed_bins: Vec<(f64, f64, usize)>, }
65
66impl Histogram {
67 #[must_use]
69 pub fn new(data: Vec<f64>) -> Self {
70 let mut hist = Self {
71 data,
72 bins: BinStrategy::default(),
73 orientation: HistogramOrientation::default(),
74 bar_style: BarStyle::default(),
75 color: Color::new(0.3, 0.7, 1.0, 1.0),
76 gradient: None,
77 show_labels: true,
78 bounds: Rect::default(),
79 computed_bins: Vec::new(),
80 };
81 hist.compute_bins();
82 hist
83 }
84
85 #[must_use]
87 pub fn with_bins(mut self, strategy: BinStrategy) -> Self {
88 self.bins = strategy;
89 self.compute_bins();
90 self
91 }
92
93 #[must_use]
95 pub fn with_orientation(mut self, orientation: HistogramOrientation) -> Self {
96 self.orientation = orientation;
97 self
98 }
99
100 #[must_use]
102 pub fn with_bar_style(mut self, style: BarStyle) -> Self {
103 self.bar_style = style;
104 self
105 }
106
107 #[must_use]
109 pub fn with_color(mut self, color: Color) -> Self {
110 self.color = color;
111 self
112 }
113
114 #[must_use]
116 pub fn with_gradient(mut self, gradient: Gradient) -> Self {
117 self.gradient = Some(gradient);
118 self
119 }
120
121 #[must_use]
123 pub fn with_labels(mut self, show: bool) -> Self {
124 self.show_labels = show;
125 self
126 }
127
128 pub fn set_data(&mut self, data: Vec<f64>) {
130 self.data = data;
131 self.compute_bins();
132 }
133
134 #[allow(clippy::manual_clamp)]
136 fn compute_bin_count(&self) -> usize {
137 let n = self.data.len();
138 if n == 0 {
139 return 1;
140 }
141
142 match self.bins {
143 BinStrategy::Count(k) => k.max(1),
144 BinStrategy::Width(w) => {
145 let (min, max) = self.data_range();
146 ((max - min) / w).ceil() as usize
147 }
148 BinStrategy::Sturges => {
149 ((n as f64).log2().ceil() as usize + 1).max(1)
151 }
152 BinStrategy::Scott => {
153 let std = self.std_dev();
155 if std < 1e-10 {
156 return 1;
157 }
158 let (min, max) = self.data_range();
159 let width = 3.49 * std / (n as f64).cbrt();
160 ((max - min) / width).ceil() as usize
161 }
162 BinStrategy::FreedmanDiaconis => {
163 let iqr = self.iqr();
165 if iqr < 1e-10 {
166 return 1;
167 }
168 let (min, max) = self.data_range();
169 let width = 2.0 * iqr / (n as f64).cbrt();
170 ((max - min) / width).ceil() as usize
171 }
172 }
173 .max(1)
174 .min(100) }
176
177 fn data_range(&self) -> (f64, f64) {
179 let mut min = f64::INFINITY;
180 let mut max = f64::NEG_INFINITY;
181
182 for &v in &self.data {
183 if v.is_finite() {
184 min = min.min(v);
185 max = max.max(v);
186 }
187 }
188
189 if min == f64::INFINITY {
190 (0.0, 1.0)
191 } else if (max - min).abs() < 1e-10 {
192 (min - 0.5, max + 0.5)
193 } else {
194 (min, max)
195 }
196 }
197
198 fn std_dev(&self) -> f64 {
200 let n = self.data.len();
201 if n < 2 {
202 return 0.0;
203 }
204
205 let mean: f64 = self.data.iter().filter(|x| x.is_finite()).sum::<f64>()
206 / self.data.iter().filter(|x| x.is_finite()).count() as f64;
207
208 let variance: f64 = self
209 .data
210 .iter()
211 .filter(|x| x.is_finite())
212 .map(|x| (x - mean).powi(2))
213 .sum::<f64>()
214 / (n - 1) as f64;
215
216 variance.sqrt()
217 }
218
219 fn iqr(&self) -> f64 {
221 let mut sorted: Vec<f64> = self
222 .data
223 .iter()
224 .filter(|x| x.is_finite())
225 .copied()
226 .collect();
227 sorted.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));
228
229 if sorted.len() < 4 {
230 return self.std_dev(); }
232
233 let q1_idx = sorted.len() / 4;
234 let q3_idx = 3 * sorted.len() / 4;
235
236 sorted[q3_idx] - sorted[q1_idx]
237 }
238
239 fn compute_bins(&mut self) {
241 let n_bins = self.compute_bin_count();
242 let (min, max) = self.data_range();
243 let bin_width = (max - min) / n_bins as f64;
244
245 self.computed_bins = (0..n_bins)
246 .map(|i| {
247 let start = min + i as f64 * bin_width;
248 let end = start + bin_width;
249 let count = self
250 .data
251 .iter()
252 .filter(|&&v| {
253 if i == n_bins - 1 {
254 v >= start && v <= end
255 } else {
256 v >= start && v < end
257 }
258 })
259 .count();
260 (start, end, count)
261 })
262 .collect();
263 }
264}
265
266impl Default for Histogram {
267 fn default() -> Self {
268 Self::new(Vec::new())
269 }
270}
271
272impl Widget for Histogram {
273 fn type_id(&self) -> TypeId {
274 TypeId::of::<Self>()
275 }
276
277 fn measure(&self, constraints: Constraints) -> Size {
278 Size::new(
279 constraints.max_width.min(60.0),
280 constraints.max_height.min(15.0),
281 )
282 }
283
284 fn layout(&mut self, bounds: Rect) -> LayoutResult {
285 self.bounds = bounds;
286 LayoutResult {
287 size: Size::new(bounds.width, bounds.height),
288 }
289 }
290
291 fn paint(&self, canvas: &mut dyn Canvas) {
292 if self.bounds.width < 5.0 || self.bounds.height < 3.0 || self.computed_bins.is_empty() {
293 return;
294 }
295
296 let max_count = self
297 .computed_bins
298 .iter()
299 .map(|(_, _, c)| *c)
300 .max()
301 .unwrap_or(1)
302 .max(1);
303
304 match self.orientation {
305 HistogramOrientation::Vertical => self.paint_vertical(canvas, max_count),
306 HistogramOrientation::Horizontal => self.paint_horizontal(canvas, max_count),
307 }
308 }
309
310 fn event(&mut self, _event: &Event) -> Option<Box<dyn Any + Send>> {
311 None
312 }
313
314 fn children(&self) -> &[Box<dyn Widget>] {
315 &[]
316 }
317
318 fn children_mut(&mut self) -> &mut [Box<dyn Widget>] {
319 &mut []
320 }
321}
322
323impl Histogram {
324 fn paint_vertical(&self, canvas: &mut dyn Canvas, max_count: usize) {
325 let label_height = if self.show_labels { 1.0 } else { 0.0 };
326 let label_width = if self.show_labels { 5.0 } else { 0.0 };
327
328 let plot_x = self.bounds.x + label_width;
329 let plot_y = self.bounds.y;
330 let plot_width = self.bounds.width - label_width;
331 let plot_height = self.bounds.height - label_height;
332
333 let n_bins = self.computed_bins.len();
334 let bar_width = (plot_width / n_bins as f32).max(1.0);
335
336 if self.show_labels {
338 let label_style = TextStyle {
339 color: Color::new(0.6, 0.6, 0.6, 1.0),
340 ..Default::default()
341 };
342
343 canvas.draw_text(
344 &format!("{max_count:>4}"),
345 Point::new(self.bounds.x, plot_y),
346 &label_style,
347 );
348 canvas.draw_text(
349 " 0",
350 Point::new(self.bounds.x, plot_y + plot_height - 1.0),
351 &label_style,
352 );
353 }
354
355 for (i, &(start, _end, count)) in self.computed_bins.iter().enumerate() {
357 let bar_height = if max_count > 0 {
358 (count as f32 / max_count as f32) * plot_height
359 } else {
360 0.0
361 };
362
363 let x = plot_x + i as f32 * bar_width;
364 let y = plot_y + plot_height - bar_height;
365
366 let color = if let Some(ref gradient) = self.gradient {
368 gradient.sample(count as f64 / max_count as f64)
369 } else {
370 self.color
371 };
372
373 let style = TextStyle {
374 color,
375 ..Default::default()
376 };
377
378 match self.bar_style {
380 BarStyle::Solid => {
381 for row in 0..(bar_height.ceil() as usize) {
382 let bar_chars: String =
383 (0..(bar_width as usize).max(1)).map(|_| '█').collect();
384 canvas.draw_text(&bar_chars, Point::new(x, y + row as f32), &style);
385 }
386 }
387 BarStyle::Blocks => {
388 const BLOCKS: [char; 8] = ['▁', '▂', '▃', '▄', '▅', '▆', '▇', '█'];
389 let full_rows = bar_height as usize;
390 let frac = bar_height.fract();
391 let frac_idx = ((frac * 8.0) as usize).min(7);
392
393 for row in 0..full_rows {
394 let bar_chars: String =
395 (0..(bar_width as usize).max(1)).map(|_| '█').collect();
396 canvas.draw_text(&bar_chars, Point::new(x, y + row as f32), &style);
397 }
398
399 if frac > 0.1 {
400 let bar_chars: String = (0..(bar_width as usize).max(1))
401 .map(|_| BLOCKS[frac_idx])
402 .collect();
403 canvas.draw_text(&bar_chars, Point::new(x, y + full_rows as f32), &style);
404 }
405 }
406 BarStyle::Ascii => {
407 for row in 0..(bar_height.ceil() as usize) {
408 let bar_chars: String =
409 (0..(bar_width as usize).max(1)).map(|_| '#').collect();
410 canvas.draw_text(&bar_chars, Point::new(x, y + row as f32), &style);
411 }
412 }
413 }
414
415 if self.show_labels && i % 2 == 0 {
417 let label = format!("{start:.0}");
418 let label_x = x + bar_width / 2.0 - label.len() as f32 / 2.0;
419 canvas.draw_text(
420 &label,
421 Point::new(label_x, plot_y + plot_height),
422 &TextStyle {
423 color: Color::new(0.6, 0.6, 0.6, 1.0),
424 ..Default::default()
425 },
426 );
427 }
428 }
429 }
430
431 fn paint_horizontal(&self, canvas: &mut dyn Canvas, max_count: usize) {
432 let label_width = if self.show_labels { 6.0 } else { 0.0 };
433
434 let plot_x = self.bounds.x + label_width;
435 let plot_y = self.bounds.y;
436 let plot_width = self.bounds.width - label_width;
437 let plot_height = self.bounds.height;
438
439 let n_bins = self.computed_bins.len();
440 let bar_height = (plot_height / n_bins as f32).max(1.0);
441
442 for (i, &(start, _end, count)) in self.computed_bins.iter().enumerate() {
443 let bar_width = if max_count > 0 {
444 (count as f32 / max_count as f32) * plot_width
445 } else {
446 0.0
447 };
448
449 let x = plot_x;
450 let y = plot_y + i as f32 * bar_height;
451
452 let color = if let Some(ref gradient) = self.gradient {
454 gradient.sample(count as f64 / max_count as f64)
455 } else {
456 self.color
457 };
458
459 let style = TextStyle {
460 color,
461 ..Default::default()
462 };
463
464 if self.show_labels {
466 let label = format!("{start:>5.0}");
467 canvas.draw_text(
468 &label,
469 Point::new(self.bounds.x, y),
470 &TextStyle {
471 color: Color::new(0.6, 0.6, 0.6, 1.0),
472 ..Default::default()
473 },
474 );
475 }
476
477 let bar_chars: String = (0..(bar_width.ceil() as usize)).map(|_| '█').collect();
479 if !bar_chars.is_empty() {
480 canvas.draw_text(&bar_chars, Point::new(x, y), &style);
481 }
482 }
483 }
484}
485
486impl Brick for Histogram {
487 fn brick_name(&self) -> &'static str {
488 "Histogram"
489 }
490
491 fn assertions(&self) -> &[BrickAssertion] {
492 static ASSERTIONS: &[BrickAssertion] = &[BrickAssertion::max_latency_ms(8)];
493 ASSERTIONS
494 }
495
496 fn budget(&self) -> BrickBudget {
497 BrickBudget::uniform(8)
498 }
499
500 fn verify(&self) -> BrickVerification {
501 let mut passed = Vec::new();
502 let mut failed = Vec::new();
503
504 if self.bounds.width >= 5.0 && self.bounds.height >= 3.0 {
505 passed.push(BrickAssertion::max_latency_ms(8));
506 } else {
507 failed.push((
508 BrickAssertion::max_latency_ms(8),
509 "Size too small".to_string(),
510 ));
511 }
512
513 BrickVerification {
514 passed,
515 failed,
516 verification_time: Duration::from_micros(5),
517 }
518 }
519
520 fn to_html(&self) -> String {
521 String::new()
522 }
523
524 fn to_css(&self) -> String {
525 String::new()
526 }
527}
528
529#[cfg(test)]
530mod tests {
531 use super::*;
532
533 #[test]
534 fn test_histogram_creation() {
535 let data = vec![1.0, 2.0, 3.0, 4.0, 5.0];
536 let hist = Histogram::new(data);
537 assert!(!hist.computed_bins.is_empty());
538 }
539
540 #[test]
541 fn test_bin_strategies() {
542 let data: Vec<f64> = (0..100).map(|i| i as f64).collect();
543
544 let sturges = Histogram::new(data.clone()).with_bins(BinStrategy::Sturges);
545 assert!(!sturges.computed_bins.is_empty());
546
547 let scott = Histogram::new(data.clone()).with_bins(BinStrategy::Scott);
548 assert!(!scott.computed_bins.is_empty());
549
550 let fd = Histogram::new(data).with_bins(BinStrategy::FreedmanDiaconis);
551 assert!(!fd.computed_bins.is_empty());
552 }
553
554 #[test]
555 fn test_empty_data() {
556 let hist = Histogram::new(vec![]);
557 assert_eq!(hist.computed_bins.len(), 1);
558 }
559
560 #[test]
561 fn test_single_value() {
562 let hist = Histogram::new(vec![5.0, 5.0, 5.0]);
563 assert!(!hist.computed_bins.is_empty());
564 }
565
566 #[test]
567 fn test_histogram_assertions() {
568 let hist = Histogram::default();
569 assert!(!hist.assertions().is_empty());
570 }
571
572 #[test]
573 #[allow(clippy::field_reassign_with_default)]
574 fn test_histogram_verify() {
575 let mut hist = Histogram::default();
576 hist.bounds = Rect::new(0.0, 0.0, 60.0, 15.0);
577 assert!(hist.verify().is_valid());
578 }
579
580 #[test]
581 fn test_histogram_children() {
582 let hist = Histogram::default();
583 assert!(hist.children().is_empty());
584 }
585
586 #[test]
587 fn test_histogram_children_mut() {
588 let mut hist = Histogram::default();
589 assert!(hist.children_mut().is_empty());
590 }
591
592 #[test]
593 fn test_histogram_type_id() {
594 let hist = Histogram::default();
595 let tid = Widget::type_id(&hist);
596 assert_eq!(tid, TypeId::of::<Histogram>());
597 }
598
599 #[test]
600 fn test_histogram_measure() {
601 let hist = Histogram::new(vec![1.0, 2.0, 3.0]);
602 let size = hist.measure(Constraints::new(0.0, 100.0, 0.0, 50.0));
603 assert!(size.width > 0.0);
604 assert!(size.height > 0.0);
605 }
606
607 #[test]
608 fn test_histogram_layout() {
609 let mut hist = Histogram::new(vec![1.0, 2.0, 3.0]);
610 let result = hist.layout(Rect::new(0.0, 0.0, 60.0, 15.0));
611 assert_eq!(result.size.width, 60.0);
612 assert_eq!(result.size.height, 15.0);
613 }
614
615 #[test]
616 fn test_histogram_event() {
617 let mut hist = Histogram::default();
618 let event = Event::Resize {
619 width: 80.0,
620 height: 24.0,
621 };
622 assert!(hist.event(&event).is_none());
623 }
624
625 #[test]
626 fn test_histogram_brick_name() {
627 let hist = Histogram::default();
628 assert_eq!(hist.brick_name(), "Histogram");
629 }
630
631 #[test]
632 fn test_histogram_budget() {
633 let hist = Histogram::default();
634 let budget = hist.budget();
635 assert!(budget.layout_ms > 0);
636 }
637
638 #[test]
639 fn test_histogram_to_html() {
640 let hist = Histogram::default();
641 assert!(hist.to_html().is_empty());
642 }
643
644 #[test]
645 fn test_histogram_to_css() {
646 let hist = Histogram::default();
647 assert!(hist.to_css().is_empty());
648 }
649
650 #[test]
651 fn test_histogram_with_orientation() {
652 let hist =
653 Histogram::new(vec![1.0, 2.0]).with_orientation(HistogramOrientation::Horizontal);
654 assert!(matches!(hist.orientation, HistogramOrientation::Horizontal));
655 }
656
657 #[test]
658 fn test_histogram_with_bar_style() {
659 let hist = Histogram::new(vec![1.0, 2.0]).with_bar_style(BarStyle::Blocks);
660 assert!(matches!(hist.bar_style, BarStyle::Blocks));
661 }
662
663 #[test]
664 fn test_histogram_with_color() {
665 let hist = Histogram::new(vec![1.0, 2.0]).with_color(Color::RED);
666 assert_eq!(hist.color, Color::RED);
667 }
668
669 #[test]
670 fn test_histogram_with_gradient() {
671 let gradient = Gradient::from_hex(&["#00FF00", "#FF0000"]);
672 let hist = Histogram::new(vec![1.0, 2.0]).with_gradient(gradient);
673 assert!(hist.gradient.is_some());
674 }
675
676 #[test]
677 fn test_histogram_with_labels() {
678 let hist = Histogram::new(vec![1.0, 2.0]).with_labels(false);
679 assert!(!hist.show_labels);
680 }
681
682 #[test]
683 fn test_histogram_set_data() {
684 let mut hist = Histogram::new(vec![1.0, 2.0]);
685 hist.set_data(vec![10.0, 20.0, 30.0, 40.0, 50.0]);
686 assert!(!hist.computed_bins.is_empty());
687 }
688
689 #[test]
690 fn test_histogram_bin_count() {
691 let hist = Histogram::new(vec![1.0, 2.0, 3.0]).with_bins(BinStrategy::Count(5));
692 assert!(!hist.computed_bins.is_empty());
693 }
694
695 #[test]
696 fn test_histogram_bin_width() {
697 let data: Vec<f64> = (0..10).map(|i| i as f64).collect();
698 let hist = Histogram::new(data).with_bins(BinStrategy::Width(2.0));
699 assert!(!hist.computed_bins.is_empty());
700 }
701
702 #[test]
703 fn test_histogram_paint_vertical() {
704 use crate::{CellBuffer, DirectTerminalCanvas};
705
706 let mut hist = Histogram::new(vec![1.0, 2.0, 3.0, 4.0, 5.0]);
707 let mut buffer = CellBuffer::new(60, 15);
708 let mut canvas = DirectTerminalCanvas::new(&mut buffer);
709
710 hist.layout(Rect::new(0.0, 0.0, 60.0, 15.0));
711 hist.paint(&mut canvas);
712 }
713
714 #[test]
715 fn test_histogram_paint_horizontal() {
716 use crate::{CellBuffer, DirectTerminalCanvas};
717
718 let mut hist = Histogram::new(vec![1.0, 2.0, 3.0, 4.0, 5.0])
719 .with_orientation(HistogramOrientation::Horizontal);
720 let mut buffer = CellBuffer::new(60, 15);
721 let mut canvas = DirectTerminalCanvas::new(&mut buffer);
722
723 hist.layout(Rect::new(0.0, 0.0, 60.0, 15.0));
724 hist.paint(&mut canvas);
725 }
726
727 #[test]
728 fn test_histogram_paint_blocks() {
729 use crate::{CellBuffer, DirectTerminalCanvas};
730
731 let mut hist =
732 Histogram::new(vec![1.0, 2.0, 3.0, 4.0, 5.0]).with_bar_style(BarStyle::Blocks);
733 let mut buffer = CellBuffer::new(60, 15);
734 let mut canvas = DirectTerminalCanvas::new(&mut buffer);
735
736 hist.layout(Rect::new(0.0, 0.0, 60.0, 15.0));
737 hist.paint(&mut canvas);
738 }
739
740 #[test]
741 fn test_histogram_paint_ascii() {
742 use crate::{CellBuffer, DirectTerminalCanvas};
743
744 let mut hist =
745 Histogram::new(vec![1.0, 2.0, 3.0, 4.0, 5.0]).with_bar_style(BarStyle::Ascii);
746 let mut buffer = CellBuffer::new(60, 15);
747 let mut canvas = DirectTerminalCanvas::new(&mut buffer);
748
749 hist.layout(Rect::new(0.0, 0.0, 60.0, 15.0));
750 hist.paint(&mut canvas);
751 }
752
753 #[test]
754 fn test_histogram_paint_with_gradient() {
755 use crate::{CellBuffer, DirectTerminalCanvas};
756
757 let gradient = Gradient::from_hex(&["#00FF00", "#FF0000"]);
758 let mut hist = Histogram::new(vec![1.0, 2.0, 3.0, 4.0, 5.0]).with_gradient(gradient);
759 let mut buffer = CellBuffer::new(60, 15);
760 let mut canvas = DirectTerminalCanvas::new(&mut buffer);
761
762 hist.layout(Rect::new(0.0, 0.0, 60.0, 15.0));
763 hist.paint(&mut canvas);
764 }
765
766 #[test]
767 fn test_histogram_paint_without_labels() {
768 use crate::{CellBuffer, DirectTerminalCanvas};
769
770 let mut hist = Histogram::new(vec![1.0, 2.0, 3.0, 4.0, 5.0]).with_labels(false);
771 let mut buffer = CellBuffer::new(60, 15);
772 let mut canvas = DirectTerminalCanvas::new(&mut buffer);
773
774 hist.layout(Rect::new(0.0, 0.0, 60.0, 15.0));
775 hist.paint(&mut canvas);
776 }
777
778 #[test]
779 fn test_histogram_paint_small_bounds() {
780 use crate::{CellBuffer, DirectTerminalCanvas};
781
782 let mut hist = Histogram::new(vec![1.0, 2.0, 3.0]);
783 let mut buffer = CellBuffer::new(4, 2);
784 let mut canvas = DirectTerminalCanvas::new(&mut buffer);
785
786 hist.layout(Rect::new(0.0, 0.0, 4.0, 2.0));
787 hist.paint(&mut canvas);
788 }
790
791 #[test]
792 #[allow(clippy::field_reassign_with_default)]
793 fn test_histogram_verify_small_bounds() {
794 let mut hist = Histogram::default();
795 hist.bounds = Rect::new(0.0, 0.0, 2.0, 1.0);
796 assert!(!hist.verify().is_valid());
797 }
798
799 #[test]
800 fn test_histogram_data_with_nan() {
801 let hist = Histogram::new(vec![1.0, f64::NAN, 3.0, f64::INFINITY, 5.0]);
802 assert!(!hist.computed_bins.is_empty());
803 }
804
805 #[test]
806 fn test_histogram_iqr_small_data() {
807 let hist = Histogram::new(vec![1.0, 2.0]); assert!(!hist.computed_bins.is_empty());
809 }
810
811 #[test]
812 fn test_histogram_std_dev_single() {
813 let hist = Histogram::new(vec![5.0]);
814 assert!(!hist.computed_bins.is_empty());
815 }
816
817 #[test]
818 fn test_histogram_clone() {
819 let hist = Histogram::new(vec![1.0, 2.0, 3.0]);
820 let cloned = hist.clone();
821 assert_eq!(cloned.computed_bins.len(), hist.computed_bins.len());
822 }
823
824 #[test]
825 fn test_histogram_debug() {
826 let hist = Histogram::new(vec![1.0, 2.0, 3.0]);
827 let debug = format!("{hist:?}");
828 assert!(debug.contains("Histogram"));
829 }
830
831 #[test]
832 fn test_bin_strategy_debug() {
833 let strategy = BinStrategy::Sturges;
834 let debug = format!("{strategy:?}");
835 assert!(debug.contains("Sturges"));
836 }
837
838 #[test]
839 fn test_histogram_orientation_debug() {
840 let orientation = HistogramOrientation::Vertical;
841 let debug = format!("{orientation:?}");
842 assert!(debug.contains("Vertical"));
843 }
844
845 #[test]
846 fn test_bar_style_debug() {
847 let style = BarStyle::Solid;
848 let debug = format!("{style:?}");
849 assert!(debug.contains("Solid"));
850 }
851
852 #[test]
853 fn test_histogram_horizontal_with_gradient() {
854 use crate::{CellBuffer, DirectTerminalCanvas};
855
856 let gradient = Gradient::from_hex(&["#00FF00", "#FF0000"]);
857 let mut hist = Histogram::new(vec![1.0, 2.0, 3.0, 4.0, 5.0])
858 .with_orientation(HistogramOrientation::Horizontal)
859 .with_gradient(gradient);
860 let mut buffer = CellBuffer::new(60, 15);
861 let mut canvas = DirectTerminalCanvas::new(&mut buffer);
862
863 hist.layout(Rect::new(0.0, 0.0, 60.0, 15.0));
864 hist.paint(&mut canvas);
865 }
866
867 #[test]
868 fn test_histogram_horizontal_without_labels() {
869 use crate::{CellBuffer, DirectTerminalCanvas};
870
871 let mut hist = Histogram::new(vec![1.0, 2.0, 3.0, 4.0, 5.0])
872 .with_orientation(HistogramOrientation::Horizontal)
873 .with_labels(false);
874 let mut buffer = CellBuffer::new(60, 15);
875 let mut canvas = DirectTerminalCanvas::new(&mut buffer);
876
877 hist.layout(Rect::new(0.0, 0.0, 60.0, 15.0));
878 hist.paint(&mut canvas);
879 }
880
881 #[test]
882 fn test_histogram_large_data() {
883 let data: Vec<f64> = (0..1000).map(|i| (i as f64 * 0.37) % 100.0).collect();
884 let hist = Histogram::new(data);
885 assert!(!hist.computed_bins.is_empty());
886 }
887}