1 | use plotters::coord::Shift; |
2 | use plotters::prelude::*; |
3 | |
4 | fn draw_chart<B: DrawingBackend>(root: &DrawingArea<B, Shift>) -> DrawResult<(), B> { |
5 | let mut chart = ChartBuilder::on(root) |
6 | .caption( |
7 | "Relative Size Example" , |
8 | ("sans-serif" , (5).percent_height()), |
9 | ) |
10 | .x_label_area_size((10).percent_height()) |
11 | .y_label_area_size((10).percent_width()) |
12 | .margin(5) |
13 | .build_cartesian_2d(-5.0..5.0, -1.0..1.0)?; |
14 | |
15 | chart |
16 | .configure_mesh() |
17 | .disable_x_mesh() |
18 | .disable_y_mesh() |
19 | .label_style(("sans-serif" , (3).percent_height())) |
20 | .draw()?; |
21 | |
22 | chart.draw_series(LineSeries::new( |
23 | (0..1000) |
24 | .map(|x| x as f64 / 100.0 - 5.0) |
25 | .map(|x| (x, x.sin())), |
26 | &RED, |
27 | ))?; |
28 | Ok(()) |
29 | } |
30 | |
31 | const OUT_FILE_NAME: &'static str = "plotters-doc-data/relative_size.png" ; |
32 | fn main() -> Result<(), Box<dyn std::error::Error>> { |
33 | let root = BitMapBackend::new(OUT_FILE_NAME, (1024, 768)).into_drawing_area(); |
34 | |
35 | root.fill(&WHITE)?; |
36 | |
37 | let (left, right) = root.split_horizontally((70).percent_width()); |
38 | |
39 | draw_chart(&left)?; |
40 | |
41 | let (upper, lower) = right.split_vertically(300); |
42 | |
43 | draw_chart(&upper)?; |
44 | draw_chart(&lower)?; |
45 | let root = root.shrink((200, 200), (150, 100)); |
46 | draw_chart(&root)?; |
47 | |
48 | // To avoid the IO failure being ignored silently, we manually call the present function |
49 | root.present().expect("Unable to write result to file, please make sure 'plotters-doc-data' dir exists under current dir" ); |
50 | println!("Result has been saved to {}" , OUT_FILE_NAME); |
51 | |
52 | Ok(()) |
53 | } |
54 | #[test] |
55 | fn entry_point() { |
56 | main().unwrap() |
57 | } |
58 | |