| 1 | use plotters::prelude::*; |
| 2 | const OUT_FILE_NAME: &'static str = "plotters-doc-data/histogram.png" ; |
| 3 | fn main() -> Result<(), Box<dyn std::error::Error>> { |
| 4 | let root = BitMapBackend::new(OUT_FILE_NAME, (640, 480)).into_drawing_area(); |
| 5 | |
| 6 | root.fill(&WHITE)?; |
| 7 | |
| 8 | let mut chart = ChartBuilder::on(&root) |
| 9 | .x_label_area_size(35) |
| 10 | .y_label_area_size(40) |
| 11 | .margin(5) |
| 12 | .caption("Histogram Test" , ("sans-serif" , 50.0)) |
| 13 | .build_cartesian_2d((0u32..10u32).into_segmented(), 0u32..10u32)?; |
| 14 | |
| 15 | chart |
| 16 | .configure_mesh() |
| 17 | .disable_x_mesh() |
| 18 | .bold_line_style(&WHITE.mix(0.3)) |
| 19 | .y_desc("Count" ) |
| 20 | .x_desc("Bucket" ) |
| 21 | .axis_desc_style(("sans-serif" , 15)) |
| 22 | .draw()?; |
| 23 | |
| 24 | let data = [ |
| 25 | 0u32, 1, 1, 1, 4, 2, 5, 7, 8, 6, 4, 2, 1, 8, 3, 3, 3, 4, 4, 3, 3, 3, |
| 26 | ]; |
| 27 | |
| 28 | chart.draw_series( |
| 29 | Histogram::vertical(&chart) |
| 30 | .style(RED.mix(0.5).filled()) |
| 31 | .data(data.iter().map(|x: &u32| (*x, 1))), |
| 32 | )?; |
| 33 | |
| 34 | // To avoid the IO failure being ignored silently, we manually call the present function |
| 35 | root.present().expect("Unable to write result to file, please make sure 'plotters-doc-data' dir exists under current dir" ); |
| 36 | println!("Result has been saved to {}" , OUT_FILE_NAME); |
| 37 | |
| 38 | Ok(()) |
| 39 | } |
| 40 | #[test] |
| 41 | fn entry_point() { |
| 42 | main().unwrap() |
| 43 | } |
| 44 | |