@@ -82,6 +82,72 @@ macro_rules! plot_vector {
82
82
. draw( )
83
83
. unwrap( ) ;
84
84
} } ;
85
+ ( $( $v: expr) ,+ => $file: expr) => { {
86
+ use plotters:: prelude:: * ;
87
+
88
+ let series: Vec <_> = vec![ $( $v) ,+] ;
89
+
90
+ let mut global_min = std:: f64 :: MAX ;
91
+ let mut global_max = std:: f64 :: MIN ;
92
+ let mut global_x_max: usize = 0 ;
93
+ for s in series. iter( ) {
94
+ for & val in s. iter( ) {
95
+ if val < global_min {
96
+ global_min = val;
97
+ }
98
+ if val > global_max {
99
+ global_max = val;
100
+ }
101
+ }
102
+ if s. len( ) > global_x_max {
103
+ global_x_max = s. len( ) ;
104
+ }
105
+ }
106
+
107
+ let root = BitMapBackend :: new( $file, ( 640 , 480 ) ) . into_drawing_area( ) ;
108
+ root. fill( & WHITE ) . unwrap( ) ;
109
+
110
+ let mut chart = ChartBuilder :: on( & root)
111
+ . caption( $file, ( "sans-serif" , 30 ) . into_font( ) )
112
+ . margin( 5 )
113
+ . x_label_area_size( 30 )
114
+ . y_label_area_size( 30 )
115
+ . build_cartesian_2d(
116
+ 0f64 ..( global_x_max as f64 ) ,
117
+ ( global_min * 0.95 ) ..( global_max * 1.05 ) // 5% padding on y-axis
118
+ )
119
+ . unwrap( ) ;
120
+
121
+ chart. configure_mesh( ) . draw( ) . unwrap( ) ;
122
+
123
+ let colors = [ RED , BLUE , GREEN , CYAN , MAGENTA , YELLOW , BLACK ] ;
124
+
125
+ for ( i, s) in series. iter( ) . enumerate( ) {
126
+ let points: Vec <( f64 , f64 ) > = s
127
+ . iter( )
128
+ . enumerate( )
129
+ . map( |( idx, & val) | ( idx as f64 , val) )
130
+ . collect( ) ;
131
+
132
+ chart
133
+ . draw_series( LineSeries :: new( points, colors[ i % colors. len( ) ] ) )
134
+ . unwrap( )
135
+ . label( format!( "Series {}" , i) )
136
+ . legend( move |( x, y) | {
137
+ PathElement :: new(
138
+ vec![ ( x, y) , ( x + 20 , y) ] ,
139
+ colors[ i % colors. len( ) ] . clone( ) ,
140
+ )
141
+ } ) ;
142
+ }
143
+
144
+ chart
145
+ . configure_series_labels( )
146
+ . background_style( WHITE . mix( 0.8 ) )
147
+ . border_style( & BLACK )
148
+ . draw( )
149
+ . unwrap( ) ;
150
+ } } ;
85
151
}
86
152
87
153
#[ cfg( test) ]
@@ -128,4 +194,28 @@ mod tests_plotters {
128
194
println ! ( "File does not exist." ) ;
129
195
}
130
196
}
197
+
198
+ #[ test]
199
+ fn test_plot_vector_macro_multi ( ) {
200
+ let v1 = [ 1.0 , 2.0 , 3.0 , 4.0 , 5.0 , 4.0 , 6.0 , 3.0 , 7.0 , 2.0 , 8.0 , 1.0 ] ;
201
+ let v2 = [ 2.0 , 3.0 , 4.0 , 5.0 , 4.0 , 6.0 , 3.0 , 7.0 , 2.0 , 8.0 , 1.0 , 8.0 ] ;
202
+ let file = "./plot_macro2.png" ;
203
+
204
+ // THIS WORKS.
205
+ plot_vector ! ( v1, v2 => & file) ;
206
+
207
+ // Check if the file exists
208
+ if std:: path:: PathBuf :: from ( file) . exists ( ) {
209
+ println ! ( "File exists. Attempting to remove..." ) ;
210
+
211
+ // Remove the file
212
+ if let Err ( e) = std:: fs:: remove_file ( file) {
213
+ println ! ( "Failed to remove file" ) ;
214
+ } else {
215
+ println ! ( "Successfully removed file." ) ;
216
+ }
217
+ } else {
218
+ println ! ( "File does not exist" ) ;
219
+ }
220
+ }
131
221
}
0 commit comments