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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
#[cfg(feature = "orca")]
extern crate plotly_orca;

use askama::Template;
use rand::Rng;
use std::env;
use std::fs::File;
use std::io::Write;
use std::path::{Path, PathBuf};
use std::process::Command;

use crate::Layout;

const PLOTLY_JS: &str = "plotly-1.52.2.min.js";

#[derive(Template)]
#[template(path = "plotly-1.52.2.min.js", escape = "none")]
struct PlotlyJs;

#[derive(Template)]
#[template(path = "plot.html", escape = "none")]
struct PlotTemplate<'a> {
    plot_data: &'a str,
    plotly_javascript: &'a str,
    export_image: bool,
    image_type: &'a str,
    image_width: usize,
    image_height: usize,
}

/// A struct that implements `Trace` can be serialized to json format that is understood by Plotly.js.
pub trait Trace {
    fn serialize(&self) -> String;
}

/// Plot is a container for structs that implement the `Trace` trait. Optionally a `Layout` can
/// also be specified. Its function is to serialize `Trace`s and the `Layout` in html format and
/// display and/or persist the resulting plot.
///
/// # Examples
///
/// ```
/// extern crate plotly;
/// use plotly::common::Mode;
/// use plotly::{Plot, Scatter};
///
/// fn line_and_scatter_plot() {
///     let trace1 = Scatter::new(vec![1, 2, 3, 4], vec![10, 15, 13, 17])
///         .name("trace1")
///         .mode(Mode::Markers);
///     let trace2 = Scatter::new(vec![2, 3, 4, 5], vec![16, 5, 11, 9])
///         .name("trace2")
///         .mode(Mode::Lines);
///     let trace3 = Scatter::new(vec![1, 2, 3, 4], vec![12, 9, 15, 12]).name("trace3");
///
///     let mut plot = Plot::new();
///     plot.add_trace(trace1);
///     plot.add_trace(trace2);
///     plot.add_trace(trace3);
///     plot.show();
/// }
///
/// fn main() -> std::io::Result<()> {
///     line_and_scatter_plot();
///     Ok(())
/// }
/// ```
pub struct Plot {
    traces: Vec<Box<dyn Trace>>,
    layout: Option<Layout>,
}

const DEFAULT_HTML_APP_NOT_FOUND: &str = r#"Could not find default application for HTML files.
Consider using the `to_html` method to save the plot instead. If using the `orca` feature the following
additional formats are available accessed by following methods:
- to_png
- to_jpeg
- to_webp
- to_svg
- to_pdf
- to_eps
"#;

impl Plot {
    /// Create a new `Plot`.
    pub fn new() -> Plot {
        Plot {
            traces: Vec::with_capacity(1),
            layout: None,
        }
    }

    /// Add a `Trace` to the `Plot`.
    pub fn add_trace(&mut self, trace: Box<dyn Trace>) {
        self.traces.push(trace);
    }

    /// Set the `Layout` to be used by `Plot`.
    pub fn set_layout(&mut self, layout: Layout) {
        self.layout = Some(layout);
    }

    /// Renders the contents of the `Plot` and displays them in the system default browser.
    ///
    /// This will serialize the `Trace`s and `Layout` in an html page which is saved in the temp
    /// directory. For example on Linux it will generate a file `plotly_<22 random characters>.html`
    /// in the /tmp directory.
    pub fn show(&self) {
        let rendered = self.render(false, "", 0, 0);
        let rendered = rendered.as_bytes();
        let mut temp = env::temp_dir();

        let mut plot_name = rand::thread_rng()
            .sample_iter(&rand::distributions::Alphanumeric)
            .take(22)
            .collect::<String>();
        plot_name.push_str(".html");
        plot_name = format!("plotly_{}", plot_name);

        temp.push(plot_name);
        let temp_path = temp.to_str().unwrap();
        {
            let mut file = File::create(temp_path).unwrap();
            file.write_all(rendered)
                .expect("failed to write html output");
            file.flush().unwrap();
        }

        Plot::show_with_default_app(temp_path);
    }

    /// Renders the contents of the `Plot`, creates a png raster and displays it in the system default browser.
    ///
    /// To save the resulting png right-click on the resulting image and select `Save As...`.
    pub fn show_png(&self, width: usize, height: usize) {
        let rendered = self.render(true, "png", width, height);
        let rendered = rendered.as_bytes();
        let mut temp = env::temp_dir();

        let mut plot_name = rand::thread_rng()
            .sample_iter(&rand::distributions::Alphanumeric)
            .take(22)
            .collect::<String>();
        plot_name.push_str(".html");

        temp.push(plot_name);
        let temp_path = temp.to_str().unwrap();
        {
            let mut file = File::create(temp_path).unwrap();
            file.write_all(rendered)
                .expect("failed to write html output");
            file.flush().unwrap();
        }

        Plot::show_with_default_app(temp_path);
    }

    /// Renders the contents of the `Plot`, creates a jpeg raster and displays it in the system default browser.
    ///
    /// To save the resulting png right-click on the resulting image and select `Save As...`.
    pub fn show_jpeg(&self, width: usize, height: usize) {
        let rendered = self.render(true, "jpg", width, height);
        let rendered = rendered.as_bytes();
        let mut temp = env::temp_dir();

        let mut plot_name = rand::thread_rng()
            .sample_iter(&rand::distributions::Alphanumeric)
            .take(22)
            .collect::<String>();
        plot_name.push_str(".html");

        temp.push(plot_name);
        let temp_path = temp.to_str().unwrap();
        {
            let mut file = File::create(temp_path).unwrap();
            file.write_all(rendered)
                .expect("failed to write html output");
            file.flush().unwrap();
        }

        Plot::show_with_default_app(temp_path);
    }

    /// Renders the contents of the `Plot` and displays it in the system default browser.
    ///
    /// In contrast to `Plot::show()` this will save the resulting html in a user specified location
    /// instead of the system temp directory.
    pub fn to_html<P: AsRef<Path>>(&self, filename: P) {
        let rendered = self.render(false, "", 0, 0);
        let rendered = rendered.as_bytes();
        let mut file = File::create(filename.as_ref()).unwrap();
        file.write_all(rendered)
            .expect("failed to write html output");
    }

    /// Saves the `Plot` to png format.
    #[cfg(feature = "orca")]
    pub fn to_png<P: AsRef<Path>>(&self, filename: P, width: usize, height: usize) {
        let orca = plotly_orca::Orca::from(Plot::plotly_js_path());
        let rendered = self.render_orca_format();
        orca.save_png(filename.as_ref(), &rendered, width, height);
    }

    /// Saves the `Plot` to jpeg format.
    #[cfg(feature = "orca")]
    pub fn to_jpeg<P: AsRef<Path>>(&self, filename: P, width: usize, height: usize) {
        let orca = plotly_orca::Orca::from(Plot::plotly_js_path());
        let rendered = self.render_orca_format();
        orca.save_jpeg(filename.as_ref(), &rendered, width, height);
    }

    /// Saves the `Plot` to webp format.
    #[cfg(feature = "orca")]
    pub fn to_webp<P: AsRef<Path>>(&self, filename: P, width: usize, height: usize) {
        let orca = plotly_orca::Orca::from(Plot::plotly_js_path());
        let rendered = self.render_orca_format();
        orca.save_webp(filename.as_ref(), &rendered, width, height);
    }

    /// Saves the `Plot` to svg format.
    #[cfg(feature = "orca")]
    pub fn to_svg<P: AsRef<Path>>(&self, filename: P, width: usize, height: usize) {
        let orca = plotly_orca::Orca::from(Plot::plotly_js_path());
        let rendered = self.render_orca_format();
        orca.save_svg(filename.as_ref(), &rendered, width, height);
    }

    /// Saves the `Plot` to pdf format.
    #[cfg(feature = "orca")]
    pub fn to_pdf<P: AsRef<Path>>(&self, filename: P, width: usize, height: usize) {
        let orca = plotly_orca::Orca::from(Plot::plotly_js_path());
        let rendered = self.render_orca_format();
        orca.save_pdf(filename.as_ref(), &rendered, width, height);
    }

    /// Saves the `Plot` to eps format.
    #[cfg(feature = "orca")]
    pub fn to_eps<P: AsRef<Path>>(&self, filename: P, width: usize, height: usize) {
        let orca = plotly_orca::Orca::from(Plot::plotly_js_path());
        let rendered = self.render_orca_format();
        orca.save_eps(filename.as_ref(), &rendered, width, height);
    }

    fn plotly_js_path() -> PathBuf {
        let root = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
        let templates = root.join("templates");
        let plotly_path = templates.join(PLOTLY_JS);
        plotly_path
    }

    fn render(
        &self,
        export_image: bool,
        image_type: &str,
        image_width: usize,
        image_height: usize,
    ) -> String {
        let mut plot_data = String::new();
        for (idx, trace) in self.traces.iter().enumerate() {
            let s = trace.serialize();
            plot_data.push_str(format!("var trace_{} = {};\n", idx, s).as_str());
        }
        plot_data.push_str("\n");
        plot_data.push_str("var data = [");
        for idx in 0..self.traces.len() {
            if idx != self.traces.len() - 1 {
                plot_data.push_str(format!("trace_{},", idx).as_str());
            } else {
                plot_data.push_str(format!("trace_{}", idx).as_str());
            }
        }
        plot_data.push_str("];\n");
        let layout_data = match &self.layout {
            Some(layout) => format!("var layout = {};\n", Trace::serialize(layout)),
            None => {
                let mut s = String::from("var layout = {");
                s.push_str("};\n");
                s
            }
        };
        plot_data.push_str(layout_data.as_str());

        let plotly_js = PlotlyJs {}.render().unwrap();
        let tmpl = PlotTemplate {
            plot_data: plot_data.as_str(),
            plotly_javascript: plotly_js.as_str(),
            export_image,
            image_type,
            image_width,
            image_height,
        };
        tmpl.render().unwrap()
    }

    fn render_orca_format(&self) -> String {
        let mut plot_data: Vec<String> = Vec::new();
        for trace in self.traces.iter() {
            let s = trace.serialize();
            plot_data.push(s);
        }
        let layout_data = match &self.layout {
            Some(layout) => Trace::serialize(layout),
            None => "{}".to_owned(),
        };

        let mut orca_data = String::new();
        orca_data.push_str(r#"{"data": ["#);

        for (index, data) in plot_data.iter().enumerate() {
            if index < plot_data.len() - 1 {
                orca_data.push_str(data);
                orca_data.push_str(r#","#);
            } else {
                orca_data.push_str(data);
                orca_data.push_str("]");
            }
        }
        orca_data.push_str(format!(r#", "layout": {}"#, layout_data).as_str());
        orca_data.push_str("}");
        orca_data
    }

    #[cfg(target_os = "linux")]
    fn show_with_default_app(temp_path: &str) {
        Command::new("xdg-open")
            .args(&[temp_path])
            .output()
            .expect(DEFAULT_HTML_APP_NOT_FOUND);
    }

    #[cfg(target_os = "macos")]
    fn show_with_default_app(temp_path: &str) {
        Command::new("open").args(&[temp_path]).output().expect(DEFAULT_HTML_APP_NOT_FOUND);
    }

    #[cfg(target_os = "windows")]
    fn show_with_default_app(temp_path: &str) {
        Command::new("cmd")
            .arg("/C")
            .arg(format!(r#"start {}"#, temp_path))
            .output()
            .expect(DEFAULT_HTML_APP_NOT_FOUND);
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::Scatter;

    fn create_test_plot() -> Plot {
        let trace1 = Scatter::new(vec![0, 1, 2], vec![6, 10, 2]).name("trace1");
        let mut plot = Plot::new();
        plot.add_trace(trace1);
        plot
    }

    #[test]
    #[cfg(feature = "orca")]
    fn test_to_png() {
        let plot = create_test_plot();
        let dst = PathBuf::from("example.png");
        plot.to_png(&dst, 1024, 680);
        assert!(dst.exists());
        std::fs::remove_file(&dst);
        assert!(!dst.exists());
    }

    #[test]
    #[cfg(feature = "orca")]
    fn test_to_jpeg() {
        let plot = create_test_plot();
        let dst = PathBuf::from("example.jpeg");
        plot.to_jpeg(&dst, 1024, 680);
        assert!(dst.exists());
        std::fs::remove_file(&dst);
        assert!(!dst.exists());
    }

    #[test]
    #[cfg(feature = "orca")]
    fn test_to_webp() {
        let plot = create_test_plot();
        let dst = PathBuf::from("example.webp");
        plot.to_webp(&dst, 1024, 680);
        assert!(dst.exists());
        std::fs::remove_file(&dst);
        assert!(!dst.exists());
    }

    #[test]
    #[cfg(feature = "orca")]
    fn test_to_svg() {
        let plot = create_test_plot();
        let dst = PathBuf::from("example.svg");
        plot.to_svg(&dst, 1024, 680);
        assert!(dst.exists());
        std::fs::remove_file(&dst);
        assert!(!dst.exists());
    }

    #[test]
    #[cfg(feature = "orca")]
    fn test_to_pdf() {
        let plot = create_test_plot();
        let dst = PathBuf::from("example.pdf");
        plot.to_pdf(&dst, 1024, 680);
        assert!(dst.exists());
        std::fs::remove_file(&dst);
        assert!(!dst.exists());
    }

    #[test]
    #[cfg(feature = "orca")]
    fn test_to_eps() {
        let plot = create_test_plot();
        let dst = PathBuf::from("example.eps");
        plot.to_eps(&dst, 1024, 680);
        assert!(dst.exists());
        std::fs::remove_file(&dst);
        assert!(!dst.exists());
    }
}