1 use plotters::prelude::*;
2 
3 const OUT_FILE_NAME: &'static str = "plotters-doc-data/matshow.png";
main() -> Result<(), Box<dyn std::error::Error>>4 fn main() -> Result<(), Box<dyn std::error::Error>> {
5     let root = BitMapBackend::new(OUT_FILE_NAME, (1024, 768)).into_drawing_area();
6 
7     root.fill(&WHITE)?;
8 
9     let mut chart = ChartBuilder::on(&root)
10         .caption("Matshow Example", ("sans-serif", 80))
11         .margin(5)
12         .top_x_label_area_size(40)
13         .y_label_area_size(40)
14         .build_cartesian_2d(0i32..15i32, 15i32..0i32)?;
15 
16     chart
17         .configure_mesh()
18         .x_labels(15)
19         .y_labels(15)
20         .max_light_lines(4)
21         .x_label_offset(35)
22         .y_label_offset(25)
23         .disable_x_mesh()
24         .disable_y_mesh()
25         .label_style(("sans-serif", 20))
26         .draw()?;
27 
28     let mut matrix = [[0; 15]; 15];
29 
30     for i in 0..15 {
31         matrix[i][i] = i + 4;
32     }
33 
34     chart.draw_series(
35         matrix
36             .iter()
37             .zip(0..)
38             .map(|(l, y)| l.iter().zip(0..).map(move |(v, x)| (x as i32, y as i32, v)))
39             .flatten()
40             .map(|(x, y, v)| {
41                 Rectangle::new(
42                     [(x, y), (x + 1, y + 1)],
43                     HSLColor(
44                         240.0 / 360.0 - 240.0 / 360.0 * (*v as f64 / 20.0),
45                         0.7,
46                         0.1 + 0.4 * *v as f64 / 20.0,
47                     )
48                     .filled(),
49                 )
50             }),
51     )?;
52 
53     // To avoid the IO failure being ignored silently, we manually call the present function
54     root.present().expect("Unable to write result to file, please make sure 'plotters-doc-data' dir exists under current dir");
55     println!("Result has been saved to {}", OUT_FILE_NAME);
56 
57     Ok(())
58 }
59 #[test]
entry_point()60 fn entry_point() {
61     main().unwrap()
62 }
63