• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 use plotters::prelude::*;
2 const OUT_FILE_NAME: &'static str = "plotters-doc-data/3d-plot.svg";
main() -> Result<(), Box<dyn std::error::Error>>3 fn main() -> Result<(), Box<dyn std::error::Error>> {
4     let area = SVGBackend::new(OUT_FILE_NAME, (1024, 760)).into_drawing_area();
5 
6     area.fill(&WHITE)?;
7 
8     let x_axis = (-3.0..3.0).step(0.1);
9     let z_axis = (-3.0..3.0).step(0.1);
10 
11     let mut chart = ChartBuilder::on(&area)
12         .caption(format!("3D Plot Test"), ("sans", 20))
13         .build_cartesian_3d(x_axis.clone(), -3.0..3.0, z_axis.clone())?;
14 
15     chart.with_projection(|mut pb| {
16         pb.yaw = 0.5;
17         pb.scale = 0.9;
18         pb.into_matrix()
19     });
20 
21     chart
22         .configure_axes()
23         .light_grid_style(BLACK.mix(0.15))
24         .max_light_lines(3)
25         .draw()?;
26 
27     chart
28         .draw_series(
29             SurfaceSeries::xoz(
30                 (-30..30).map(|f| f as f64 / 10.0),
31                 (-30..30).map(|f| f as f64 / 10.0),
32                 |x, z| (x * x + z * z).cos(),
33             )
34             .style(BLUE.mix(0.2).filled()),
35         )?
36         .label("Surface")
37         .legend(|(x, y)| Rectangle::new([(x + 5, y - 5), (x + 15, y + 5)], BLUE.mix(0.5).filled()));
38 
39     chart
40         .draw_series(LineSeries::new(
41             (-100..100)
42                 .map(|y| y as f64 / 40.0)
43                 .map(|y| ((y * 10.0).sin(), y, (y * 10.0).cos())),
44             &BLACK,
45         ))?
46         .label("Line")
47         .legend(|(x, y)| PathElement::new(vec![(x, y), (x + 20, y)], &BLACK));
48 
49     chart
50         .configure_series_labels()
51         .border_style(&BLACK)
52         .draw()?;
53 
54     // To avoid the IO failure being ignored silently, we manually call the present function
55     area.present().expect("Unable to write result to file, please make sure 'plotters-doc-data' dir exists under current dir");
56     println!("Result has been saved to {}", OUT_FILE_NAME);
57     Ok(())
58 }
59 #[test]
entry_point()60 fn entry_point() {
61     main().unwrap()
62 }
63