@@ -7,7 +7,8 @@ use plotly::contour::Contours;
7
7
use plotly:: { Contour , HeatMap , Layout , Plot } ;
8
8
9
9
// Contour Plots
10
- fn simple_contour_plot ( ) {
10
+ // ANCHOR: simple_contour_plot
11
+ fn simple_contour_plot ( show : bool ) -> Plot {
11
12
let n = 200 ;
12
13
let mut x = Vec :: < f64 > :: new ( ) ;
13
14
let mut y = Vec :: < f64 > :: new ( ) ;
@@ -34,10 +35,15 @@ fn simple_contour_plot() {
34
35
35
36
plot. add_trace ( trace) ;
36
37
37
- plot. show ( ) ;
38
+ if show {
39
+ plot. show ( ) ;
40
+ }
41
+ plot
38
42
}
43
+ // ANCHOR_END: simple_contour_plot
39
44
40
- fn colorscale_for_contour_plot ( ) {
45
+ // ANCHOR: colorscale_for_contour_plot
46
+ fn colorscale_for_contour_plot ( show : bool ) -> Plot {
41
47
let z = vec ! [
42
48
vec![ 10.0 , 10.625 , 12.5 , 15.625 , 20.0 ] ,
43
49
vec![ 5.625 , 6.25 , 8.125 , 11.25 , 15.625 ] ,
@@ -52,10 +58,15 @@ fn colorscale_for_contour_plot() {
52
58
plot. set_layout ( layout) ;
53
59
plot. add_trace ( trace) ;
54
60
55
- plot. show ( ) ;
61
+ if show {
62
+ plot. show ( ) ;
63
+ }
64
+ plot
56
65
}
66
+ // ANCHOR_END: colorscale_for_contour_plot
57
67
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 {
59
70
let z = vec ! [
60
71
vec![ 10.0 , 10.625 , 12.5 , 15.625 , 20.0 ] ,
61
72
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() {
73
84
plot. set_layout ( layout) ;
74
85
plot. add_trace ( trace) ;
75
86
76
- plot. show ( ) ;
87
+ if show {
88
+ plot. show ( ) ;
89
+ }
90
+ plot
77
91
}
92
+ // ANCHOR_END: customizing_size_and_range_of_a_contour_plots_contours
78
93
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 {
80
96
let z = vec ! [
81
97
vec![ 10.0 , 10.625 , 12.5 , 15.625 , 20.0 ] ,
82
98
vec![ 5.625 , 6.25 , 8.125 , 11.25 , 15.625 ] ,
@@ -96,20 +112,30 @@ fn customizing_spacing_between_x_and_y_ticks() {
96
112
plot. set_layout ( layout) ;
97
113
plot. add_trace ( trace) ;
98
114
99
- plot. show ( ) ;
115
+ if show {
116
+ plot. show ( ) ;
117
+ }
118
+ plot
100
119
}
120
+ // ANCHOR_END: customizing_spacing_between_x_and_y_ticks
101
121
102
122
// Heatmaps
103
- fn basic_heat_map ( ) {
123
+ // ANCHOR: basic_heat_map
124
+ fn basic_heat_map ( show : bool ) -> Plot {
104
125
let z = vec ! [ vec![ 1 , 20 , 30 ] , vec![ 20 , 1 , 60 ] , vec![ 30 , 60 , 1 ] ] ;
105
126
let trace = HeatMap :: new_z ( z) . zmin ( 1.0 ) . zmax ( 60.0 ) ;
106
127
let mut plot = Plot :: new ( ) ;
107
128
plot. add_trace ( trace) ;
108
129
109
- plot. show ( ) ;
130
+ if show {
131
+ plot. show ( ) ;
132
+ }
133
+ plot
110
134
}
135
+ // ANCHOR_END: basic_heat_map
111
136
112
- fn customized_heat_map ( ) {
137
+ // ANCHOR: customized_heat_map
138
+ fn customized_heat_map ( show : bool ) -> Plot {
113
139
let x = ( 0 ..100 ) . map ( |x| x as f64 ) . collect :: < Vec < f64 > > ( ) ;
114
140
let y = ( 0 ..100 ) . map ( |y| y as f64 ) . collect :: < Vec < f64 > > ( ) ;
115
141
let z: Vec < Vec < f64 > > = y
@@ -143,19 +169,38 @@ fn customized_heat_map() {
143
169
plot. set_layout ( layout) ;
144
170
plot. add_trace ( trace) ;
145
171
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 ( ) ;
147
183
}
148
184
149
185
fn 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.
151
187
152
188
// 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
+ ) ;
157
202
158
203
// 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" ) ;
161
206
}
0 commit comments