@@ -2,6 +2,8 @@ use crate::coord::Shift;
2
2
use crate :: drawing:: { DrawingArea , IntoDrawingArea } ;
3
3
use plotters_backend:: DrawingBackend ;
4
4
use plotters_svg:: SVGBackend ;
5
+ use std:: fs:: File ;
6
+ use std:: io:: Write ;
5
7
6
8
#[ cfg( feature = "evcxr_bitmap" ) ]
7
9
#[ cfg_attr( doc_cfg, doc( cfg( feature = "evcxr_bitmap" ) ) ) ]
@@ -46,6 +48,25 @@ pub fn evcxr_figure<
46
48
SVGWrapper ( buffer, "" . to_string ( ) )
47
49
}
48
50
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
+ }
49
70
/// Start drawing an evcxr figure
50
71
#[ cfg( feature = "evcxr_bitmap" ) ]
51
72
#[ cfg_attr( doc_cfg, doc( cfg( feature = "evcxr_bitmap" ) ) ) ]
0 commit comments