I cannot find a way to draw an image on my plot. The examples on the github repo are outdated and the documentation isn't cooperating.
I am trying to draw an image on my plot, but the examples on the github are not compiling (I'm compiling my project with rustc 1.84.0 (9fc6b4312 2025-01-07)
).
The documentation is also non-existent (plotters-rs is literally a white page).
Here is my code so far :
use image::{DynamicImage, ImageBuffer, imageops::FilterType, ImageFormat, Rgba};
use std::io::BufReader;
. . .
let image_path = Path::new("res").join(image_name);
let size: (u32, u32) = (width as u32, height as u32);
let pos: (u32, u32) = (x as u32, y as u32);
let image: DynamicImage = image::load(
BufReader::new(
File::open(image_path).map_err(|e| {
eprintln!("Error loading image: {}", e);
e
})?),
ImageFormat::Png,
)?.resize_exact(size.0, size.1, FilterType::Nearest);
let imagebuf: ImageBuffer<Rgba<u8>, Vec<u8>> = image.to_rgba8();
let elem: BitMapElement<_> = BitMapElement::with_owned_buffer(pos, size, imagebuf.into_vec()).unwrap();
chart.draw_series(std::iter::once(elem))?;
The blocking point is the call to draw_series(). With this code, I am faced with this compiler error:
error[E0277]: the trait bound `for<'b> &'b plotters::element::BitMapElement<'_, (u32, u32)>: PointCollection<'b, (i32, i32)>` is not satisfied
--> src/display.rs:114:19
|
114 | chart.draw_series(std::iter::once(elem))?;
| ^^^^^^^^^^^ the trait `for<'b> PointCollection<'b, (i32, i32)>` is not implemented for `&'b plotters::element::BitMapElement<'_, (u32, u32)>`
|
= help: the trait `PointCollection<'b, (i32, i32), BackendCoordOnly>` is not implemented for `&'b plotters::element::BitMapElement<'_, _, _>`
but trait `PointCollection<'_, (u32, u32), BackendCoordOnly>` is implemented for `&plotters::element::BitMapElement<'_, _, _>`
= help: for that trait implementation, expected `u32`, found `i32`
I don't know where the compiler finds the i32 values, everything is cast to u32. Can someone enlighten me ?