|
| 1 | +use env_logger; |
| 2 | +use log::info; |
| 3 | +use plotly::plotly_static::{ImageFormat, StaticExporterBuilder}; |
| 4 | +use plotly::{Plot, Scatter}; |
| 5 | + |
| 6 | +fn main() -> Result<(), Box<dyn std::error::Error>> { |
| 7 | + // Initialize logging |
| 8 | + env_logger::init(); |
| 9 | + |
| 10 | + // Create multiple plots |
| 11 | + let mut plot1 = Plot::new(); |
| 12 | + plot1.add_trace(Scatter::new(vec![1, 2, 3, 4], vec![10, 15, 13, 17]).name("trace1")); |
| 13 | + |
| 14 | + let mut plot2 = Plot::new(); |
| 15 | + plot2.add_trace(Scatter::new(vec![2, 3, 4, 5], vec![16, 5, 11, 9]).name("trace2")); |
| 16 | + |
| 17 | + let mut plot3 = Plot::new(); |
| 18 | + plot3.add_trace(Scatter::new(vec![1, 2, 3, 4], vec![12, 9, 15, 12]).name("trace3")); |
| 19 | + |
| 20 | + // Create a single StaticExporter to reuse across all plots |
| 21 | + let mut exporter = StaticExporterBuilder::default() |
| 22 | + .build() |
| 23 | + .expect("Failed to create StaticExporter"); |
| 24 | + |
| 25 | + info!("Exporting multiple plots using a single StaticExporter..."); |
| 26 | + |
| 27 | + // Export all plots using the same exporter (more efficient) |
| 28 | + plot1.write_image_with_exporter(&mut exporter, "plot1", ImageFormat::PNG, 800, 600, 1.0)?; |
| 29 | + plot2.write_image_with_exporter(&mut exporter, "plot2", ImageFormat::JPEG, 800, 600, 1.0)?; |
| 30 | + plot3.write_image_with_exporter(&mut exporter, "plot3", ImageFormat::SVG, 800, 600, 1.0)?; |
| 31 | + |
| 32 | + // Export to PDF |
| 33 | + plot1.write_image_with_exporter(&mut exporter, "plot1", ImageFormat::PDF, 800, 600, 1.0)?; |
| 34 | + |
| 35 | + // Get base64 data |
| 36 | + let base64_data = |
| 37 | + plot1.to_base64_with_exporter(&mut exporter, ImageFormat::PNG, 400, 300, 1.0)?; |
| 38 | + info!("Base64 data length: {}", base64_data.len()); |
| 39 | + |
| 40 | + // Get SVG data |
| 41 | + let svg_data = plot1.to_svg_with_exporter(&mut exporter, 400, 300, 1.0)?; |
| 42 | + info!("SVG data starts with: {}", &svg_data[..50]); |
| 43 | + |
| 44 | + info!("All exports completed successfully!"); |
| 45 | + info!("Generated files:"); |
| 46 | + info!(" - plot1.png"); |
| 47 | + info!(" - plot2.jpeg"); |
| 48 | + info!(" - plot3.svg"); |
| 49 | + info!(" - plot1.pdf"); |
| 50 | + |
| 51 | + Ok(()) |
| 52 | +} |
0 commit comments