From 3cb60f505db0e8cd13d599be0e3a84b12d464986 Mon Sep 17 00:00:00 2001 From: Julio Biason Date: Wed, 26 Apr 2023 09:55:21 -0300 Subject: [PATCH] Replaced the rectangles with a proper histogram --- plotterstest/src/main.rs | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/plotterstest/src/main.rs b/plotterstest/src/main.rs index b6b3c3d..8d0f5cc 100644 --- a/plotterstest/src/main.rs +++ b/plotterstest/src/main.rs @@ -2,8 +2,8 @@ use plotters::prelude::BitMapBackend; use plotters::prelude::ChartBuilder; use plotters::prelude::IntoDrawingArea; use plotters::prelude::IntoSegmentedCoord; -use plotters::prelude::Rectangle; use plotters::prelude::SegmentValue; +use plotters::series::Histogram; use plotters::style::full_palette::PURPLE; use plotters::style::text_anchor::HPos; use plotters::style::text_anchor::Pos; @@ -164,18 +164,22 @@ fn main() { let root = BitMapBackend::new("cases.png", (1920, 720)).into_drawing_area(); root.fill(&WHITE).unwrap(); + let max = values + .iter() + .map(|(_name, value)| value) + .fold(0.0f64, |acc, x| if acc > *x { acc } else { *x }); let mut chart = ChartBuilder::on(&root) .x_label_area_size(150) .y_label_area_size(50) .margin(5) - .caption("Cases", ("sans-serif bold", 40.0)) - .build_cartesian_2d((0..values.len()).into_segmented(), 0.0f64..350.0f64) + // .caption("Cases", ("sans-serif bold", 40.0)) + .build_cartesian_2d((0..values.len()).into_segmented(), 0.0f64..max) .unwrap(); let pos = Pos::new(HPos::Left, VPos::Bottom); let x_label_style = TextStyle::from(("Ubuntu Medium", 10).into_font()) .pos(pos) - .transform(FontTransform::Rotate90); + .transform(FontTransform::Rotate270); chart .configure_mesh() .x_labels(values.len()) @@ -195,23 +199,17 @@ fn main() { label }) .x_label_style(x_label_style) + .disable_x_mesh() .draw() .unwrap(); + let just_values = values.map(|(_name, value)| value); chart .draw_series( - (0usize..) - .zip(values.iter().map(|(_name, value)| { - // println!("{_name} is {value}"); - *value - })) - .map(|(pos, value)| { - let x0 = SegmentValue::Exact(pos); - let x1 = SegmentValue::Exact(pos + 1); - let mut rect = Rectangle::new([(x0, 0.0), (x1, value)], PURPLE.filled()); - rect.set_margin(0, 0, 1, 1); - rect - }), + Histogram::vertical(&chart) + .style(PURPLE.filled()) + .margin(0) + .data((0usize..).zip(just_values)), ) .unwrap();