Skip to content

added saving of figures as a new function in evxcr.rs issue #552 #559

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

Merged
merged 1 commit into from
Apr 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions plotters/src/evcxr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use crate::coord::Shift;
use crate::drawing::{DrawingArea, IntoDrawingArea};
use plotters_backend::DrawingBackend;
use plotters_svg::SVGBackend;
use std::fs::File;
use std::io::Write;

#[cfg(feature = "evcxr_bitmap")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "evcxr_bitmap")))]
Expand Down Expand Up @@ -46,6 +48,25 @@ pub fn evcxr_figure<
SVGWrapper(buffer, "".to_string())
}

// An evcxr figure that can save to the local file system and render in a notebook.

pub fn evcxr_figure_with_saving<
Draw: FnOnce(DrawingArea<SVGBackend, Shift>) -> Result<(), Box<dyn std::error::Error>>,
>(
filename: &str,
size: (u32, u32),
draw: Draw,
) -> SVGWrapper {
let mut buffer = "".to_string();
let root = SVGBackend::with_string(&mut buffer, size).into_drawing_area();
draw(root).expect("Drawing failure");

let mut file = File::create(filename).expect("Unable to create file");
file.write_all(buffer.as_bytes())
.expect("Unable to write data");

SVGWrapper(buffer, "".to_string())
}
/// Start drawing an evcxr figure
#[cfg(feature = "evcxr_bitmap")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "evcxr_bitmap")))]
Expand Down