Browse Source

Replaced the rectangles with a proper histogram

master
Julio Biason 2 years ago
parent
commit
3cb60f505d
  1. 30
      plotterstest/src/main.rs

30
plotterstest/src/main.rs

@ -2,8 +2,8 @@ use plotters::prelude::BitMapBackend;
use plotters::prelude::ChartBuilder; use plotters::prelude::ChartBuilder;
use plotters::prelude::IntoDrawingArea; use plotters::prelude::IntoDrawingArea;
use plotters::prelude::IntoSegmentedCoord; use plotters::prelude::IntoSegmentedCoord;
use plotters::prelude::Rectangle;
use plotters::prelude::SegmentValue; use plotters::prelude::SegmentValue;
use plotters::series::Histogram;
use plotters::style::full_palette::PURPLE; use plotters::style::full_palette::PURPLE;
use plotters::style::text_anchor::HPos; use plotters::style::text_anchor::HPos;
use plotters::style::text_anchor::Pos; use plotters::style::text_anchor::Pos;
@ -164,18 +164,22 @@ fn main() {
let root = BitMapBackend::new("cases.png", (1920, 720)).into_drawing_area(); let root = BitMapBackend::new("cases.png", (1920, 720)).into_drawing_area();
root.fill(&WHITE).unwrap(); 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) let mut chart = ChartBuilder::on(&root)
.x_label_area_size(150) .x_label_area_size(150)
.y_label_area_size(50) .y_label_area_size(50)
.margin(5) .margin(5)
.caption("Cases", ("sans-serif bold", 40.0)) // .caption("Cases", ("sans-serif bold", 40.0))
.build_cartesian_2d((0..values.len()).into_segmented(), 0.0f64..350.0f64) .build_cartesian_2d((0..values.len()).into_segmented(), 0.0f64..max)
.unwrap(); .unwrap();
let pos = Pos::new(HPos::Left, VPos::Bottom); let pos = Pos::new(HPos::Left, VPos::Bottom);
let x_label_style = TextStyle::from(("Ubuntu Medium", 10).into_font()) let x_label_style = TextStyle::from(("Ubuntu Medium", 10).into_font())
.pos(pos) .pos(pos)
.transform(FontTransform::Rotate90); .transform(FontTransform::Rotate270);
chart chart
.configure_mesh() .configure_mesh()
.x_labels(values.len()) .x_labels(values.len())
@ -195,23 +199,17 @@ fn main() {
label label
}) })
.x_label_style(x_label_style) .x_label_style(x_label_style)
.disable_x_mesh()
.draw() .draw()
.unwrap(); .unwrap();
let just_values = values.map(|(_name, value)| value);
chart chart
.draw_series( .draw_series(
(0usize..) Histogram::vertical(&chart)
.zip(values.iter().map(|(_name, value)| { .style(PURPLE.filled())
// println!("{_name} is {value}"); .margin(0)
*value .data((0usize..).zip(just_values)),
}))
.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
}),
) )
.unwrap(); .unwrap();

Loading…
Cancel
Save