Plotters 0.3 - cannot plot to a buffer

I am trying to use Plotters + Iced. The goal is to use Iced for the UI to select different plots. I am trying to plot these to
backend = BitMapBackend::with_buffer()
then presenting it as an Iced Image The problem, I cannot get the .with_buffer() to accept either a [u8] or Vec buffer. I keep getting a "wrong size buffer" error. I apologize ahead of time, but I am not at work and typing on my phone and trying to format the code:
let buffer = Vec::::new();
let drawing_area = BitMapBackend::with_buffer(&buffer, (800, 600)).into_drawing_area();

Has anyone else run into difficulties building a DrawingArea buffer?

Your byte buffer needs to be pre-allocated at the correct size. height * width

let mut buffer = vec![0; height * width * pixel_size];
let drawing_area = BitMapBackend::with_buffer(&mut buffer, (width, height));

Correction, you need to also multiply that by the pixel size you choose PixelFormat in plotters_bitmap::bitmap_pixel - Rust

Awesome thanks!
I'll check it out. I knew it was something simple.

@erelde Thanks for the help! I got the buffer partially working.

But, the image is rendering the graph multiple times in the iced::Image. Also, it appears graph is built on gaps on top of my window background and it's not using theChartContext color settings. I set my window background to RED to help clarify. I'm assuming this is the way I built my buffer, because if I use a BitmapBackend in plotters, Image renders the created PNG correctly.

Ah-ha! Solved it! The DrawingArea from BitMapBackend::with_buffer is a RGB backend which wasn't being processed by Iced::Image correctly. Once I changed the DrawingArea to a BitMapBackend::with_buffer_and_format to a BGRXPixel, all worked out!

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.