@@ -7,7 +7,8 @@ use plotly::contour::Contours;
77use plotly:: { Contour , HeatMap , Layout , Plot } ;
88
99// Contour Plots
10- fn simple_contour_plot ( ) {
10+ // ANCHOR: simple_contour_plot
11+ fn simple_contour_plot ( show : bool ) -> Plot {
1112 let n = 200 ;
1213 let mut x = Vec :: < f64 > :: new ( ) ;
1314 let mut y = Vec :: < f64 > :: new ( ) ;
@@ -34,10 +35,15 @@ fn simple_contour_plot() {
3435
3536 plot. add_trace ( trace) ;
3637
37- plot. show ( ) ;
38+ if show {
39+ plot. show ( ) ;
40+ }
41+ plot
3842}
43+ // ANCHOR_END: simple_contour_plot
3944
40- fn colorscale_for_contour_plot ( ) {
45+ // ANCHOR: colorscale_for_contour_plot
46+ fn colorscale_for_contour_plot ( show : bool ) -> Plot {
4147 let z = vec ! [
4248 vec![ 10.0 , 10.625 , 12.5 , 15.625 , 20.0 ] ,
4349 vec![ 5.625 , 6.25 , 8.125 , 11.25 , 15.625 ] ,
@@ -52,10 +58,15 @@ fn colorscale_for_contour_plot() {
5258 plot. set_layout ( layout) ;
5359 plot. add_trace ( trace) ;
5460
55- plot. show ( ) ;
61+ if show {
62+ plot. show ( ) ;
63+ }
64+ plot
5665}
66+ // ANCHOR_END: colorscale_for_contour_plot
5767
58- fn customizing_size_and_range_of_a_contour_plots_contours ( ) {
68+ // ANCHOR: customizing_size_and_range_of_a_contour_plots_contours
69+ fn customizing_size_and_range_of_a_contour_plots_contours ( show : bool ) -> Plot {
5970 let z = vec ! [
6071 vec![ 10.0 , 10.625 , 12.5 , 15.625 , 20.0 ] ,
6172 vec![ 5.625 , 6.25 , 8.125 , 11.25 , 15.625 ] ,
@@ -73,10 +84,15 @@ fn customizing_size_and_range_of_a_contour_plots_contours() {
7384 plot. set_layout ( layout) ;
7485 plot. add_trace ( trace) ;
7586
76- plot. show ( ) ;
87+ if show {
88+ plot. show ( ) ;
89+ }
90+ plot
7791}
92+ // ANCHOR_END: customizing_size_and_range_of_a_contour_plots_contours
7893
79- fn customizing_spacing_between_x_and_y_ticks ( ) {
94+ // ANCHOR: customizing_spacing_between_x_and_y_ticks
95+ fn customizing_spacing_between_x_and_y_ticks ( show : bool ) -> Plot {
8096 let z = vec ! [
8197 vec![ 10.0 , 10.625 , 12.5 , 15.625 , 20.0 ] ,
8298 vec![ 5.625 , 6.25 , 8.125 , 11.25 , 15.625 ] ,
@@ -96,20 +112,30 @@ fn customizing_spacing_between_x_and_y_ticks() {
96112 plot. set_layout ( layout) ;
97113 plot. add_trace ( trace) ;
98114
99- plot. show ( ) ;
115+ if show {
116+ plot. show ( ) ;
117+ }
118+ plot
100119}
120+ // ANCHOR_END: customizing_spacing_between_x_and_y_ticks
101121
102122// Heatmaps
103- fn basic_heat_map ( ) {
123+ // ANCHOR: basic_heat_map
124+ fn basic_heat_map ( show : bool ) -> Plot {
104125 let z = vec ! [ vec![ 1 , 20 , 30 ] , vec![ 20 , 1 , 60 ] , vec![ 30 , 60 , 1 ] ] ;
105126 let trace = HeatMap :: new_z ( z) . zmin ( 1.0 ) . zmax ( 60.0 ) ;
106127 let mut plot = Plot :: new ( ) ;
107128 plot. add_trace ( trace) ;
108129
109- plot. show ( ) ;
130+ if show {
131+ plot. show ( ) ;
132+ }
133+ plot
110134}
135+ // ANCHOR_END: basic_heat_map
111136
112- fn customized_heat_map ( ) {
137+ // ANCHOR: customized_heat_map
138+ fn customized_heat_map ( show : bool ) -> Plot {
113139 let x = ( 0 ..100 ) . map ( |x| x as f64 ) . collect :: < Vec < f64 > > ( ) ;
114140 let y = ( 0 ..100 ) . map ( |y| y as f64 ) . collect :: < Vec < f64 > > ( ) ;
115141 let z: Vec < Vec < f64 > > = y
@@ -143,19 +169,38 @@ fn customized_heat_map() {
143169 plot. set_layout ( layout) ;
144170 plot. add_trace ( trace) ;
145171
146- plot. show ( ) ;
172+ if show {
173+ plot. show ( ) ;
174+ }
175+ plot
176+ }
177+ // ANCHOR_END: customized_heat_map
178+
179+ fn write_example_to_html ( plot : Plot , name : & str ) {
180+ std:: fs:: create_dir_all ( "./out" ) . unwrap ( ) ;
181+ let html = plot. to_inline_html ( Some ( name) ) ;
182+ std:: fs:: write ( format ! ( "./out/{}.html" , name) , html) . unwrap ( ) ;
147183}
148184
149185fn main ( ) {
150- // Uncomment any of these lines to display the example.
186+ // Change false to true on any of these lines to display the example.
151187
152188 // Contour Plots
153- // simple_contour_plot();
154- // colorscale_for_contour_plot();
155- // customizing_size_and_range_of_a_contour_plots_contours();
156- // customizing_spacing_between_x_and_y_ticks();
189+ write_example_to_html ( simple_contour_plot ( false ) , "simple_contour_plot" ) ;
190+ write_example_to_html (
191+ colorscale_for_contour_plot ( false ) ,
192+ "colorscale_for_contour_plot" ,
193+ ) ;
194+ write_example_to_html (
195+ customizing_size_and_range_of_a_contour_plots_contours ( false ) ,
196+ "customizing_size_and_range_of_a_contour_plots_contours" ,
197+ ) ;
198+ write_example_to_html (
199+ customizing_spacing_between_x_and_y_ticks ( false ) ,
200+ "customizing_spacing_between_x_and_y_ticks" ,
201+ ) ;
157202
158203 // Heatmaps
159- // basic_heat_map();
160- // customized_heat_map();
204+ write_example_to_html ( basic_heat_map ( false ) , "basic_heat_map" ) ;
205+ write_example_to_html ( customized_heat_map ( false ) , "customized_heat_map" ) ;
161206}
0 commit comments