1 | use plotters::prelude::*; |
2 | |
3 | const OUT_FILE_NAME: &'static str = "plotters-doc-data/sample.png" ; |
4 | fn main() -> Result<(), Box<dyn std::error::Error>> { |
5 | let root_area = BitMapBackend::new(OUT_FILE_NAME, (1024, 768)).into_drawing_area(); |
6 | |
7 | root_area.fill(&WHITE)?; |
8 | |
9 | let root_area = root_area.titled("Image Title" , ("sans-serif" , 60))?; |
10 | |
11 | let (upper, lower) = root_area.split_vertically(512); |
12 | |
13 | let x_axis = (-3.4f32..3.4).step(0.1); |
14 | |
15 | let mut cc = ChartBuilder::on(&upper) |
16 | .margin(5) |
17 | .set_all_label_area_size(50) |
18 | .caption("Sine and Cosine" , ("sans-serif" , 40)) |
19 | .build_cartesian_2d(-3.4f32..3.4, -1.2f32..1.2f32)?; |
20 | |
21 | cc.configure_mesh() |
22 | .x_labels(20) |
23 | .y_labels(10) |
24 | .disable_mesh() |
25 | .x_label_formatter(&|v| format!("{:.1}" , v)) |
26 | .y_label_formatter(&|v| format!("{:.1}" , v)) |
27 | .draw()?; |
28 | |
29 | cc.draw_series(LineSeries::new(x_axis.values().map(|x| (x, x.sin())), &RED))? |
30 | .label("Sine" ) |
31 | .legend(|(x, y)| PathElement::new(vec![(x, y), (x + 20, y)], &RED)); |
32 | |
33 | cc.draw_series(LineSeries::new( |
34 | x_axis.values().map(|x| (x, x.cos())), |
35 | &BLUE, |
36 | ))? |
37 | .label("Cosine" ) |
38 | .legend(|(x, y)| PathElement::new(vec![(x, y), (x + 20, y)], &BLUE)); |
39 | |
40 | cc.configure_series_labels().border_style(&BLACK).draw()?; |
41 | |
42 | /* |
43 | // It's possible to use a existing pointing element |
44 | cc.draw_series(PointSeries::<_, _, Circle<_>>::new( |
45 | (-3.0f32..2.1f32).step(1.0).values().map(|x| (x, x.sin())), |
46 | 5, |
47 | Into::<ShapeStyle>::into(&RGBColor(255,0,0)).filled(), |
48 | ))?;*/ |
49 | |
50 | // Otherwise you can use a function to construct your pointing element yourself |
51 | cc.draw_series(PointSeries::of_element( |
52 | (-3.0f32..2.1f32).step(1.0).values().map(|x| (x, x.sin())), |
53 | 5, |
54 | ShapeStyle::from(&RED).filled(), |
55 | &|coord, size, style| { |
56 | EmptyElement::at(coord) |
57 | + Circle::new((0, 0), size, style) |
58 | + Text::new(format!("{:?}" , coord), (0, 15), ("sans-serif" , 15)) |
59 | }, |
60 | ))?; |
61 | |
62 | let drawing_areas = lower.split_evenly((1, 2)); |
63 | |
64 | for (drawing_area, idx) in drawing_areas.iter().zip(1..) { |
65 | let mut cc = ChartBuilder::on(&drawing_area) |
66 | .x_label_area_size(30) |
67 | .y_label_area_size(30) |
68 | .margin_right(20) |
69 | .caption(format!("y = x^{}" , 1 + 2 * idx), ("sans-serif" , 40)) |
70 | .build_cartesian_2d(-1f32..1f32, -1f32..1f32)?; |
71 | cc.configure_mesh() |
72 | .x_labels(5) |
73 | .y_labels(3) |
74 | .max_light_lines(4) |
75 | .draw()?; |
76 | |
77 | cc.draw_series(LineSeries::new( |
78 | (-1f32..1f32) |
79 | .step(0.01) |
80 | .values() |
81 | .map(|x| (x, x.powf(idx as f32 * 2.0 + 1.0))), |
82 | &BLUE, |
83 | ))?; |
84 | } |
85 | |
86 | // To avoid the IO failure being ignored silently, we manually call the present function |
87 | root_area.present().expect("Unable to write result to file, please make sure 'plotters-doc-data' dir exists under current dir" ); |
88 | println!("Result has been saved to {}" , OUT_FILE_NAME); |
89 | Ok(()) |
90 | } |
91 | #[test] |
92 | fn entry_point() { |
93 | main().unwrap() |
94 | } |
95 | |