|
| 1 | +use log::info; |
1 | 2 | use plotly::color::{NamedColor, Rgb}; |
2 | 3 | use plotly::common::{Anchor, Font, Line, Marker, MarkerSymbol, Mode, Title}; |
3 | 4 | use plotly::layout::{Axis, ItemSizing, Legend, Margin, Shape, ShapeLine, ShapeType}; |
4 | | -use plotly::{ImageFormat, Layout, Plot, Scatter}; |
| 5 | +use plotly::plotly_static::{ImageFormat, StaticExporterBuilder}; |
| 6 | +use plotly::{Layout, Plot, Scatter}; |
5 | 7 |
|
6 | 8 | fn line_and_scatter_plot( |
7 | 9 | x1: Vec<f64>, |
8 | 10 | y1: Vec<f64>, |
9 | 11 | x2: Vec<f64>, |
10 | 12 | y2: Vec<f64>, |
11 | | - flnm: &str, |
| 13 | + file_name: &str, |
12 | 14 | title: &str, |
13 | 15 | ) { |
14 | 16 | let bgcol = Rgb::new(255, 255, 255); |
@@ -140,18 +142,26 @@ fn line_and_scatter_plot( |
140 | 142 | plot.add_trace(trace2); |
141 | 143 | plot.set_layout(layout); |
142 | 144 |
|
143 | | - // Export to multiple formats to demonstrate the SVG export issue |
144 | | - println!("Exporting plot to multiple formats..."); |
145 | | - println!("Note: SVG export may have font sizing issues compared to PNG/PDF"); |
146 | | - |
147 | | - plot.write_image(flnm, ImageFormat::PDF, 1280, 960, 1.0); |
148 | | - plot.write_image(flnm, ImageFormat::SVG, 1280, 960, 1.0); |
149 | | - plot.write_image(flnm, ImageFormat::PNG, 1280, 960, 1.0); |
150 | | - |
151 | | - println!("Export complete. Check the output files:"); |
152 | | - println!(" - {flnm}.pdf"); |
153 | | - println!(" - {flnm}.svg"); |
154 | | - println!(" - {flnm}.png"); |
| 145 | + let mut exporter = StaticExporterBuilder::default() |
| 146 | + .spawn_webdriver(true) |
| 147 | + .webdriver_port(4444) |
| 148 | + .build() |
| 149 | + .unwrap(); |
| 150 | + |
| 151 | + info!("Exporting to PNG format..."); |
| 152 | + plot.write_image_with_exporter(&mut exporter, file_name, ImageFormat::PNG, 1280, 960, 1.0) |
| 153 | + .unwrap(); |
| 154 | + info!("Exporting to SVG format..."); |
| 155 | + plot.write_image_with_exporter(&mut exporter, file_name, ImageFormat::SVG, 1280, 960, 1.0) |
| 156 | + .unwrap(); |
| 157 | + info!("Exporting to PDF format..."); |
| 158 | + plot.write_image_with_exporter(&mut exporter, file_name, ImageFormat::PDF, 1280, 960, 1.0) |
| 159 | + .unwrap(); |
| 160 | + |
| 161 | + info!("Export complete. Check the output files:"); |
| 162 | + info!(" - {file_name}.pdf"); |
| 163 | + info!(" - {file_name}.svg"); |
| 164 | + info!(" - {file_name}.png"); |
155 | 165 | } |
156 | 166 |
|
157 | 167 | fn read_from_file(file_path: &str) -> Vec<Vec<f64>> { |
@@ -204,6 +214,8 @@ fn read_from_file(file_path: &str) -> Vec<Vec<f64>> { |
204 | 214 | } |
205 | 215 |
|
206 | 216 | fn main() { |
| 217 | + env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("info")).init(); |
| 218 | + |
207 | 219 | let data = read_from_file("assets/data_file.dat"); |
208 | 220 | let x1 = data[0].clone(); |
209 | 221 | let y1 = data[1].clone(); |
|
0 commit comments