• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 use plotters::prelude::*;
2 
3 use rand::SeedableRng;
4 use rand_distr::{Distribution, Normal};
5 use rand_xorshift::XorShiftRng;
6 
7 const OUT_FILE_NAME: &'static str = "plotters-doc-data/area-chart.png";
main() -> Result<(), Box<dyn std::error::Error>>8 fn main() -> Result<(), Box<dyn std::error::Error>> {
9     let data: Vec<_> = {
10         let norm_dist = Normal::new(500.0, 100.0).unwrap();
11         let mut x_rand = XorShiftRng::from_seed(*b"MyFragileSeed123");
12         let x_iter = norm_dist.sample_iter(&mut x_rand);
13         x_iter
14             .filter(|x| *x < 1500.0)
15             .take(100)
16             .zip(0..)
17             .map(|(x, b)| x + (b as f64).powf(1.2))
18             .collect()
19     };
20 
21     let root = BitMapBackend::new(OUT_FILE_NAME, (1024, 768)).into_drawing_area();
22 
23     root.fill(&WHITE)?;
24 
25     let mut chart = ChartBuilder::on(&root)
26         .set_label_area_size(LabelAreaPosition::Left, 60)
27         .set_label_area_size(LabelAreaPosition::Bottom, 60)
28         .caption("Area Chart Demo", ("sans-serif", 40))
29         .build_cartesian_2d(0..(data.len() - 1), 0.0..1500.0)?;
30 
31     chart
32         .configure_mesh()
33         .disable_x_mesh()
34         .disable_y_mesh()
35         .draw()?;
36 
37     chart.draw_series(
38         AreaSeries::new(
39             (0..).zip(data.iter()).map(|(x, y)| (x, *y)),
40             0.0,
41             &RED.mix(0.2),
42         )
43         .border_style(&RED),
44     )?;
45 
46     // To avoid the IO failure being ignored silently, we manually call the present function
47     root.present().expect("Unable to write result to file, please make sure 'plotters-doc-data' dir exists under current dir");
48     println!("Result has been saved to {}", OUT_FILE_NAME);
49     Ok(())
50 }
51 #[test]
entry_point()52 fn entry_point() {
53     main().unwrap()
54 }
55