Skip to content

Commit d15b0c0

Browse files
authored
Merge pull request #559 from JaminMartin/evcxr-jupyter-saving
added saving of figures as a new function in evxcr.rs issue #552
2 parents 8f14c1e + 8261bc9 commit d15b0c0

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

plotters/src/evcxr.rs

+21
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ use crate::coord::Shift;
22
use crate::drawing::{DrawingArea, IntoDrawingArea};
33
use plotters_backend::DrawingBackend;
44
use plotters_svg::SVGBackend;
5+
use std::fs::File;
6+
use std::io::Write;
57

68
#[cfg(feature = "evcxr_bitmap")]
79
#[cfg_attr(doc_cfg, doc(cfg(feature = "evcxr_bitmap")))]
@@ -46,6 +48,25 @@ pub fn evcxr_figure<
4648
SVGWrapper(buffer, "".to_string())
4749
}
4850

51+
// An evcxr figure that can save to the local file system and render in a notebook.
52+
53+
pub fn evcxr_figure_with_saving<
54+
Draw: FnOnce(DrawingArea<SVGBackend, Shift>) -> Result<(), Box<dyn std::error::Error>>,
55+
>(
56+
filename: &str,
57+
size: (u32, u32),
58+
draw: Draw,
59+
) -> SVGWrapper {
60+
let mut buffer = "".to_string();
61+
let root = SVGBackend::with_string(&mut buffer, size).into_drawing_area();
62+
draw(root).expect("Drawing failure");
63+
64+
let mut file = File::create(filename).expect("Unable to create file");
65+
file.write_all(buffer.as_bytes())
66+
.expect("Unable to write data");
67+
68+
SVGWrapper(buffer, "".to_string())
69+
}
4970
/// Start drawing an evcxr figure
5071
#[cfg(feature = "evcxr_bitmap")]
5172
#[cfg_attr(doc_cfg, doc(cfg(feature = "evcxr_bitmap")))]

0 commit comments

Comments
 (0)