| 1 |
use plotters::prelude::*; |
| 2 |
|
| 3 |
use image::{imageops::FilterType, ImageFormat}; |
| 4 |
|
| 5 |
use std::fs::File; |
| 6 |
use std::io::BufReader; |
| 7 |
|
| 8 |
const OUT_FILE_NAME: &str = "plotters-doc-data/blit-bitmap.png"; |
| 9 |
|
| 10 |
fn main() -> Result<(), Box<dyn std::error::Error>> { |
| 11 |
let root = BitMapBackend::new(OUT_FILE_NAME, (1024, 768)).into_drawing_area(); |
| 12 |
root.fill(&WHITE)?; |
| 13 |
|
| 14 |
let mut chart = ChartBuilder::on(&root) |
| 15 |
.caption("Bitmap Example", ("sans-serif", 30)) |
| 16 |
.margin(5) |
| 17 |
.set_label_area_size(LabelAreaPosition::Left, 40) |
| 18 |
.set_label_area_size(LabelAreaPosition::Bottom, 40) |
| 19 |
.build_cartesian_2d(0.0..1.0, 0.0..1.0)?; |
| 20 |
|
| 21 |
chart.configure_mesh().disable_mesh().draw()?; |
| 22 |
|
| 23 |
let (w, h) = chart.plotting_area().dim_in_pixel(); |
| 24 |
let image = image::load( |
| 25 |
BufReader::new( |
| 26 |
File::open("plotters-doc-data/cat.png").map_err(|e| { |
| 27 |
eprintln!("Unable to open file plotters-doc-data.png, please make sure you have clone this repo with --recursive"); |
| 28 |
e |
| 29 |
})?), |
| 30 |
ImageFormat::Png, |
| 31 |
)? |
| 32 |
.resize_exact(w - w / 10, h - h / 10, FilterType::Nearest); |
| 33 |
|
| 34 |
let elem: BitMapElement<_> = ((0.05, 0.95), image).into(); |
| 35 |
|
| 36 |
chart.draw_series(std::iter::once(elem))?; |
| 37 |
// To avoid the IO failure being ignored silently, we manually call the present function |
| 38 |
root.present().expect("Unable to write result to file, please make sure 'plotters-doc-data' dir exists under current dir"); |
| 39 |
println!("Result has been saved to {}", OUT_FILE_NAME); |
| 40 |
Ok(()) |
| 41 |
} |
| 42 |
#[test] |
| 43 |
fn entry_point() { |
| 44 |
main().unwrap() |
| 45 |
} |