Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Graph example not working when writing to a buffer #637

Open
manuelarte opened this issue Sep 9, 2024 Discussed in #635 · 0 comments
Open

Graph example not working when writing to a buffer #637

manuelarte opened this issue Sep 9, 2024 Discussed in #635 · 0 comments

Comments

@manuelarte
Copy link

Discussed in https://github.com/orgs/plotters-rs/discussions/635

Originally posted by manuelarte September 8, 2024
Hi, I would like to run the example of the graphs but instead of writing to a file, get the buffer so I can send the image. This is what I changed

use plotters::prelude::*;

const OUT_FILE_NAME: &str = "plotters-doc-data/sample.png";

fn main() -> Result<Vec<u8>, Box<dyn std::error::Error>> {
    let width = 1024;
    let height = 768;
    let mut buffer = vec![0; (width as usize) * (height as usize) * 3];
    {
      let root_area =
                  BitMapBackend::with_buffer(&mut buffer, (width , height )).into_drawing_area();
  
      root_area.fill(&WHITE)?;
  
      let root_area = root_area.titled("Image Title", ("sans-serif", 60))?;
  
      let (upper, lower) = root_area.split_vertically(512);
  
      let x_axis = (-3.4f32..3.4).step(0.1);
  
      let mut cc = ChartBuilder::on(&upper)
          .margin(5)
          .set_all_label_area_size(50)
          .caption("Sine and Cosine", ("sans-serif", 40))
          .build_cartesian_2d(-3.4f32..3.4, -1.2f32..1.2f32)?;
  
      cc.configure_mesh()
          .x_labels(20)
          .y_labels(10)
          .disable_mesh()
          .x_label_formatter(&|v| format!("{:.1}", v))
          .y_label_formatter(&|v| format!("{:.1}", v))
          .draw()?;
  
      cc.draw_series(LineSeries::new(x_axis.values().map(|x| (x, x.sin())), &RED))?
          .label("Sine")
          .legend(|(x, y)| PathElement::new(vec![(x, y), (x + 20, y)], RED));
  
      cc.draw_series(LineSeries::new(
          x_axis.values().map(|x| (x, x.cos())),
          &BLUE,
      ))?
      .label("Cosine")
      .legend(|(x, y)| PathElement::new(vec![(x, y), (x + 20, y)], BLUE));
  
      cc.configure_series_labels().border_style(BLACK).draw()?;
  
      /*
      // It's possible to use a existing pointing element
       cc.draw_series(PointSeries::<_, _, Circle<_>>::new(
          (-3.0f32..2.1f32).step(1.0).values().map(|x| (x, x.sin())),
          5,
          Into::<ShapeStyle>::into(&RGBColor(255,0,0)).filled(),
      ))?;*/
  
      // Otherwise you can use a function to construct your pointing element yourself
      cc.draw_series(PointSeries::of_element(
          (-3.0f32..2.1f32).step(1.0).values().map(|x| (x, x.sin())),
          5,
          ShapeStyle::from(&RED).filled(),
          &|coord, size, style| {
              EmptyElement::at(coord)
                  + Circle::new((0, 0), size, style)
                  + Text::new(format!("{:?}", coord), (0, 15), ("sans-serif", 15))
          },
      ))?;
  
      let drawing_areas = lower.split_evenly((1, 2));
  
      for (drawing_area, idx) in drawing_areas.iter().zip(1..) {
          let mut cc = ChartBuilder::on(drawing_area)
              .x_label_area_size(30)
              .y_label_area_size(30)
              .margin_right(20)
              .caption(format!("y = x^{}", 1 + 2 * idx), ("sans-serif", 40))
              .build_cartesian_2d(-1f32..1f32, -1f32..1f32)?;
          cc.configure_mesh()
              .x_labels(5)
              .y_labels(3)
              .max_light_lines(4)
              .draw()?;
  
          cc.draw_series(LineSeries::new(
              (-1f32..1f32)
                  .step(0.01)
                  .values()
                  .map(|x| (x, x.powf(idx as f32 * 2.0 + 1.0))),
              &BLUE,
          ))?;
      }
  
      // To avoid the IO failure being ignored silently, we manually call the present function
      root_area.present().expect("Unable to write result to file, please make sure 'plotters-doc-data' dir exists under current dir");
    }
    Ok(buffer)
}
#[test]
fn entry_point() {
    let buffer = main().unwrap()
    let image = image::RgbImage::from_raw(input.width, input.height, buffer.clone()).unwrap();
    let path = format!("{}/{}", OUT_DIRECTORY, "graphs.png".to_string());
    image.save(&Path::new(path.as_str()))?;
    println!("Result has been saved to {}", OUT_DIRECTORY);
}

But I get a weird image (with the graphs overlapping), not the graphs, any idea what I am doing wrong? I have very similar code with the histogram and it works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant