1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#[cfg(feature = "plotly_static")]
pub mod sync {
use std::path::Path;
use crate::{plot::Plot, ImageFormat};
/// Extension methods for exporting plots using a synchronous exporter.
pub trait ExporterSyncExt {
/// Convert the `Plot` to a static image of the given image format and
/// save at the given location using a provided StaticExporter.
///
/// This method allows you to reuse a StaticExporter instance across
/// multiple plots, which is more efficient than creating a new one for
/// each operation.
///
/// This method requires the usage of the `plotly_static` crate using
/// one of the available feature flags. For advanced usage (parallelism, exporter reuse, custom config), see the [plotly_static documentation](https://docs.rs/plotly_static/).
///
/// # Arguments
///
/// * `exporter` - A mutable reference to a StaticExporter instance
/// * `filename` - The destination path for the output file
/// * `format` - The desired output image format
/// * `width` - The width of the output image in pixels
/// * `height` - The height of the output image in pixels
/// * `scale` - The scale factor for the image (1.0 = normal size)
///
/// # Examples
///
/// ```no_run
/// use plotly::{Plot, Scatter};
/// use plotly::export::sync::ExporterSyncExt as _;
/// use plotly::plotly_static::{StaticExporterBuilder, ImageFormat};
///
/// let mut plot = Plot::new();
/// plot.add_trace(Scatter::new(vec![1, 2, 3], vec![4, 5, 6]));
///
/// let mut exporter = StaticExporterBuilder::default()
/// .build()
/// .expect("Failed to create StaticExporter");
///
/// // Export multiple plots using the same exporter
/// exporter.write_image(&plot, "plot1", ImageFormat::PNG, 800, 600, 1.0)
/// .expect("Failed to export plot");
///
/// exporter.close();
/// ```
fn write_image<P: AsRef<Path>>(
&mut self,
plot: &Plot,
filename: P,
format: ImageFormat,
width: usize,
height: usize,
scale: f64,
) -> Result<(), Box<dyn std::error::Error>>;
/// Convert the `Plot` to a static image and return the image as a
/// `base64` string. Supported formats are [ImageFormat::JPEG],
/// [ImageFormat::PNG] and [ImageFormat::WEBP].
///
/// This method allows you to reuse the same StaticExporter instance
/// across multiple plots, which is more efficient than creating
/// a new one for each operation.
///
/// For advanced usage (parallelism, exporter reuse, custom config), see the [plotly_static documentation](https://docs.rs/plotly_static/).
///
/// # Arguments
///
/// * `format` - The desired output image format
/// * `width` - The width of the output image in pixels
/// * `height` - The height of the output image in pixels
/// * `scale` - The scale factor for the image (1.0 = normal size)
///
/// # Examples
///
/// ```no_run
/// use plotly::{Plot, Scatter};
/// use plotly::export::sync::ExporterSyncExt as _;
/// use plotly::plotly_static::{StaticExporterBuilder, ImageFormat};
///
/// let mut plot = Plot::new();
/// plot.add_trace(Scatter::new(vec![1, 2, 3], vec![4, 5, 6]));
///
/// let mut exporter = StaticExporterBuilder::default()
/// .build()
/// .expect("Failed to create StaticExporter");
///
/// let base64_data = exporter.to_base64(&plot, ImageFormat::PNG, 800, 600, 1.0)
/// .expect("Failed to export plot");
///
/// exporter.close();
/// ```
fn to_base64(
&mut self,
plot: &Plot,
format: ImageFormat,
width: usize,
height: usize,
scale: f64,
) -> Result<String, Box<dyn std::error::Error>>;
/// Convert the `Plot` to SVG and return it as a String.
///
/// This method allows you to reuse the same StaticExporter instance
/// across multiple plots, which is more efficient than creating
/// a new one for each operation.
///
/// For advanced usage (parallelism, exporter reuse, custom config), see the [plotly_static documentation](https://docs.rs/plotly_static/).
///
/// # Arguments
///
/// * `width` - The width of the output image in pixels
/// * `height` - The height of the output image in pixels
/// * `scale` - The scale factor for the image (1.0 = normal size)
///
/// # Examples
///
/// ```no_run
/// use plotly::{Plot, Scatter};
/// use plotly::export::sync::ExporterSyncExt as _;
/// use plotly::plotly_static::{StaticExporterBuilder, ImageFormat};
///
/// let mut plot = Plot::new();
/// plot.add_trace(Scatter::new(vec![1, 2, 3], vec![4, 5, 6]));
///
/// let mut exporter = StaticExporterBuilder::default()
/// .build()
/// .expect("Failed to create StaticExporter");
///
/// let svg_data = exporter.to_svg(&plot, 800, 600, 1.0)
/// .expect("Failed to export plot");
///
/// exporter.close();
/// ```
fn to_svg(
&mut self,
plot: &Plot,
width: usize,
height: usize,
scale: f64,
) -> Result<String, Box<dyn std::error::Error>>;
}
impl ExporterSyncExt for plotly_static::StaticExporter {
fn write_image<P: AsRef<Path>>(
&mut self,
plot: &Plot,
filename: P,
format: ImageFormat,
width: usize,
height: usize,
scale: f64,
) -> Result<(), Box<dyn std::error::Error>> {
self.write_fig(
filename.as_ref(),
&serde_json::to_value(plot)?,
format,
width,
height,
scale,
)
}
fn to_base64(
&mut self,
plot: &Plot,
format: ImageFormat,
width: usize,
height: usize,
scale: f64,
) -> Result<String, Box<dyn std::error::Error>> {
match format {
ImageFormat::JPEG | ImageFormat::PNG | ImageFormat::WEBP => self.write_to_string(
&serde_json::to_value(plot)?,
format,
width,
height,
scale,
),
_ => Err(format!(
"Cannot generate base64 string for ImageFormat:{format}. Allowed formats are JPEG, PNG, WEBP"
)
.into()),
}
}
fn to_svg(
&mut self,
plot: &Plot,
width: usize,
height: usize,
scale: f64,
) -> Result<String, Box<dyn std::error::Error>> {
self.write_to_string(
&serde_json::to_value(plot)?,
ImageFormat::SVG,
width,
height,
scale,
)
}
}
}
#[cfg(feature = "plotly_static")]
pub mod r#async {
use std::path::Path;
use async_trait::async_trait;
use crate::{plot::Plot, ImageFormat};
/// Extension methods for exporting plots using an asynchronous exporter.
#[async_trait(?Send)]
pub trait ExporterAsyncExt {
/// Convert the `Plot` to a static image of the given format and save at
/// the given location using the asynchronous exporter.
///
/// The exporter must have been built with the `build_async` method of
/// the StaticExporterBuilder.
///
/// Functionally signature equivalent to the sync version in
/// [`crate::export::sync::ExporterSyncExt::write_image`], but meant for
/// async contexts.
///
/// For more details see the [plotly_static documentation](https://docs.rs/plotly_static/).
async fn write_image<P: AsRef<Path>>(
&mut self,
plot: &Plot,
filename: P,
format: ImageFormat,
width: usize,
height: usize,
scale: f64,
) -> Result<(), Box<dyn std::error::Error>>;
/// Convert the `Plot` to a static image and return the image as a
/// `base64` string using the asynchronous exporter.
///
/// The exporter must have been built with the `build_async` method of
/// the StaticExporterBuilder.
///
/// Functionally signature equivalent to the sync version in
/// [`crate::export::sync::ExporterSyncExt::to_base64`], but meant for
/// async contexts.
///
/// For more details see the [plotly_static documentation](https://docs.rs/plotly_static/).
async fn to_base64(
&mut self,
plot: &Plot,
format: ImageFormat,
width: usize,
height: usize,
scale: f64,
) -> Result<String, Box<dyn std::error::Error>>;
/// Convert the `Plot` to SVG and return it as a String using the
/// asynchronous exporter.
///
/// Functionally signature equivalent to the sync version in
/// [`crate::export::sync::ExporterSyncExt::to_svg`], but meant for
/// async contexts.
///
/// For more details see the [plotly_static documentation](https://docs.rs/plotly_static/).
async fn to_svg(
&mut self,
plot: &Plot,
width: usize,
height: usize,
scale: f64,
) -> Result<String, Box<dyn std::error::Error>>;
}
#[async_trait(?Send)]
impl ExporterAsyncExt for plotly_static::AsyncStaticExporter {
async fn write_image<P: AsRef<Path>>(
&mut self,
plot: &Plot,
filename: P,
format: ImageFormat,
width: usize,
height: usize,
scale: f64,
) -> Result<(), Box<dyn std::error::Error>> {
self.write_fig(
filename.as_ref(),
&serde_json::to_value(plot)?,
format,
width,
height,
scale,
)
.await
}
async fn to_base64(
&mut self,
plot: &Plot,
format: ImageFormat,
width: usize,
height: usize,
scale: f64,
) -> Result<String, Box<dyn std::error::Error>> {
match format {
ImageFormat::JPEG | ImageFormat::PNG | ImageFormat::WEBP => self
.write_to_string(
&serde_json::to_value(plot)?,
format,
width,
height,
scale,
)
.await,
_ => Err(format!(
"Cannot generate base64 string for ImageFormat:{format}. Allowed formats are JPEG, PNG, WEBP"
)
.into()),
}
}
async fn to_svg(
&mut self,
plot: &Plot,
width: usize,
height: usize,
scale: f64,
) -> Result<String, Box<dyn std::error::Error>> {
self.write_to_string(
&serde_json::to_value(plot)?,
ImageFormat::SVG,
width,
height,
scale,
)
.await
}
}
}