@@ -36,119 +36,119 @@ macro_rules! assert_approx_equal {
3636 } ;
3737}
3838
39- /// Plot a vector of values.
40- #[ macro_export]
41- macro_rules! plot_vector {
42- ( $v: expr, $file: expr) => { {
43- // Macros are hygienic, so we need to import the libraries we want to use.
44- use plotters:: prelude:: * ;
45-
46- let mut min = $v[ 0 ] ;
47- let mut max = $v[ 0 ] ;
48- let mut vec2d = Vec :: new( ) ;
49-
50- for ( i, & val) in $v. iter( ) . enumerate( ) {
51- vec2d. push( ( i as f64 , val) ) ;
52- if val > max {
53- max = val;
54- } else if val < min {
55- min = val;
56- }
57- }
58-
59- let root = BitMapBackend :: new( $file, ( 640 , 480 ) ) . into_drawing_area( ) ;
60- root. fill( & full_palette:: WHITE ) . unwrap( ) ;
61-
62- let mut chart = plotters:: prelude:: ChartBuilder :: on( & root)
63- . caption( $file, ( "sans-serif" , 30 ) . into_font( ) )
64- . margin( 5 )
65- . x_label_area_size( 30 )
66- . y_label_area_size( 30 )
67- . build_cartesian_2d(
68- 0f64 ..vec2d. len( ) as f64 ,
69- ( min * 0.95 ) ..( max * 1.05 ) , // 5% padding on y-axis
70- )
71- . unwrap( ) ;
72-
73- chart. configure_mesh( ) . draw( ) . unwrap( ) ;
74- chart
75- . draw_series( LineSeries :: new( vec2d, RED ) )
76- . unwrap( )
77- . label( $file) ;
78- chart
79- . configure_series_labels( )
80- . background_style( WHITE . mix( 0.8 ) )
81- . border_style( BLACK )
82- . draw( )
83- . unwrap( ) ;
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- } } ;
151- }
39+ // // / Plot a vector of values.
40+ // #[macro_export]
41+ // macro_rules! plot_vector {
42+ // ($v:expr, $file:expr) => {{
43+ // // Macros are hygienic, so we need to import the libraries we want to use.
44+ // use plotters::prelude::*;
45+
46+ // let mut min = $v[0];
47+ // let mut max = $v[0];
48+ // let mut vec2d = Vec::new();
49+
50+ // for (i, &val) in $v.iter().enumerate() {
51+ // vec2d.push((i as f64, val));
52+ // if val > max {
53+ // max = val;
54+ // } else if val < min {
55+ // min = val;
56+ // }
57+ // }
58+
59+ // let root = BitMapBackend::new($file, (640, 480)).into_drawing_area();
60+ // root.fill(&full_palette::WHITE).unwrap();
61+
62+ // let mut chart = plotters::prelude::ChartBuilder::on(&root)
63+ // .caption($file, ("sans-serif", 30).into_font())
64+ // .margin(5)
65+ // .x_label_area_size(30)
66+ // .y_label_area_size(30)
67+ // .build_cartesian_2d(
68+ // 0f64..vec2d.len() as f64,
69+ // (min * 0.95)..(max * 1.05), // 5% padding on y-axis
70+ // )
71+ // .unwrap();
72+
73+ // chart.configure_mesh().draw().unwrap();
74+ // chart
75+ // .draw_series(LineSeries::new(vec2d, RED))
76+ // .unwrap()
77+ // .label($file);
78+ // chart
79+ // .configure_series_labels()
80+ // .background_style(WHITE.mix(0.8))
81+ // .border_style(BLACK)
82+ // .draw()
83+ // .unwrap();
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+ // }};
151+ // }
152152
153153#[ cfg( test) ]
154154mod tests_plotters {
@@ -172,50 +172,50 @@ mod tests_plotters {
172172 assert_approx_equal ! ( 1_f64 . acosh( ) , 0.0 , EPS ) ;
173173 }
174174
175- #[ test]
176- fn test_plot_vector_macro ( ) {
177- let v = [ 1.0 , 2.0 , 3.0 , 4.0 , 5.0 , 4.0 , 6.0 , 3.0 , 7.0 , 2.0 , 8.0 , 1.0 ] ;
178- let file = "plot_macro.png" ;
179-
180- // THIS WORKS.
181- plot_vector ! ( v, file) ;
182-
183- // Check if the file exists
184- if std:: fs:: metadata ( file) . is_ok ( ) {
185- println ! ( "File exists. Attempting to remove..." ) ;
186-
187- // Remove the file
188- if let Err ( e) = std:: fs:: remove_file ( file) {
189- println ! ( "Failed to remove file: {}" , e) ;
190- } else {
191- println ! ( "Successfully removed file." ) ;
192- }
193- } else {
194- println ! ( "File does not exist." ) ;
195- }
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 ( _) = 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- }
175+ // #[test]
176+ // fn test_plot_vector_macro() {
177+ // let v = [1.0, 2.0, 3.0, 4.0, 5.0, 4.0, 6.0, 3.0, 7.0, 2.0, 8.0, 1.0];
178+ // let file = "plot_macro.png";
179+
180+ // // THIS WORKS.
181+ // plot_vector!(v, file);
182+
183+ // // Check if the file exists
184+ // if std::fs::metadata(file).is_ok() {
185+ // println!("File exists. Attempting to remove...");
186+
187+ // // Remove the file
188+ // if let Err(e) = std::fs::remove_file(file) {
189+ // println!("Failed to remove file: {}", e);
190+ // } else {
191+ // println!("Successfully removed file.");
192+ // }
193+ // } else {
194+ // println!("File does not exist.");
195+ // }
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(_) = 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+ // }
221221}
0 commit comments