@@ -2,6 +2,8 @@ use crate::coord::Shift;
22use crate :: drawing:: { DrawingArea , IntoDrawingArea } ;
33use plotters_backend:: DrawingBackend ;
44use 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