forked from plotly/plotly.rs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot.rs
More file actions
734 lines (641 loc) · 21.1 KB
/
Copy pathplot.rs
File metadata and controls
734 lines (641 loc) · 21.1 KB
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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
#[cfg(feature = "kaleido")]
extern crate plotly_kaleido;
use askama::Template;
use dyn_clone::DynClone;
use erased_serde::Serialize as ErasedSerialize;
use rand::{thread_rng, Rng};
use serde::Serialize;
use std::fs::File;
use std::io::Write;
use std::path::Path;
use crate::{Configuration, Layout};
use rand_distr::Alphanumeric;
#[derive(Template)]
#[template(path = "plotly.min.js", escape = "none")]
struct PlotlyJs;
#[derive(Template)]
#[template(path = "plot.html", escape = "none")]
struct PlotTemplate<'a> {
plot: &'a Plot,
plotly_javascript: &'a str,
remote_plotly_js: bool,
export_image: bool,
image_type: &'a str,
image_width: usize,
image_height: usize,
}
#[derive(Template)]
#[template(path = "inline_plot.html", escape = "none")]
struct InlinePlotTemplate<'a> {
plot: &'a Plot,
plot_div_id: &'a str,
}
#[derive(Template)]
#[template(path = "jupyter_notebook_plot.html", escape = "none")]
struct JupyterNotebookPlotTemplate<'a> {
plot: &'a Plot,
plot_div_id: &'a str,
}
/// Image format for static image export.
pub enum ImageFormat {
PNG,
JPEG,
WEBP,
SVG,
PDF,
EPS,
}
/// A struct that implements `Trace` can be serialized to json format that is understood by Plotly.js.
pub trait Trace: DynClone + ErasedSerialize {
fn to_json(&self) -> String;
}
dyn_clone::clone_trait_object!(Trace);
erased_serde::serialize_trait_object!(Trace);
#[derive(Default, Serialize, Clone)]
#[serde(transparent)]
pub struct Traces {
traces: Vec<Box<dyn Trace>>,
}
impl Traces {
pub fn new() -> Self {
Self {
traces: Vec::with_capacity(1),
}
}
pub fn push(&mut self, trace: Box<dyn Trace>) {
self.traces.push(trace)
}
pub fn len(&self) -> usize {
self.traces.len()
}
pub fn iter(&self) -> std::slice::Iter<'_, Box<dyn Trace>> {
self.traces.iter()
}
pub fn to_json(&self) -> String {
serde_json::to_string(self).unwrap()
}
}
/// 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::{Layout, 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);
///
/// let layout = Layout::new().title("<b>Line and Scatter Plot</b>".into());
/// plot.set_layout(layout);
///
/// plot.show();
/// }
///
/// fn main() -> std::io::Result<()> {
/// line_and_scatter_plot();
/// Ok(())
/// }
/// ```
#[derive(Default, Serialize, Clone)]
pub struct Plot {
#[serde(rename = "data")]
traces: Traces,
layout: Layout,
#[serde(rename = "config")]
configuration: Configuration,
#[serde(skip)]
remote_plotly_js: bool,
}
#[cfg(not(feature = "wasm"))]
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 `kaleido` feature the
`save` method can be used to produce a static image in one of the following formats:
- ImageFormat::PNG
- ImageFormat::JPEG
- ImageFormat::WEBP
- ImageFormat::SVG
- ImageFormat::PDF
- ImageFormat::EPS
used as follows:
let plot = Plot::new();
...
let width = 1024;
let height = 680;
let scale = 1.0;
plot.save("filename", ImageFormat::PNG, width, height, scale);
See https://igiagkiozis.github.io/plotly/content/getting_started.html for further details.
"#;
impl Plot {
/// Create a new `Plot`.
pub fn new() -> Plot {
Plot {
traces: Traces::new(),
remote_plotly_js: true,
..Default::default()
}
}
/// This option results in the plotly.js library being written directly in the html output. The benefit is that the
/// plot will load faster in the browser and the downside is that the resulting html will be much larger.
pub fn use_local_plotly(&mut self) {
self.remote_plotly_js = false;
}
/// Add a `Trace` to the `Plot`.
pub fn add_trace(&mut self, trace: Box<dyn Trace>) {
self.traces.push(trace);
}
/// Add multiple `Trace`s to the `Plot`.
pub fn add_traces(&mut self, traces: Vec<Box<dyn Trace>>) {
for trace in traces {
self.add_trace(trace);
}
}
/// Set the `Layout` to be used by `Plot`.
pub fn set_layout(&mut self, layout: Layout) {
self.layout = layout;
}
/// Set the `Configuration` to be used by `Plot`.
pub fn set_configuration(&mut self, configuration: Configuration) {
self.configuration = configuration;
}
/// Get the contained data elements.
pub fn data(&self) -> &Traces {
&self.traces
}
/// Get the layout specification of the plot.
pub fn layout(&self) -> &Layout {
&self.layout
}
/// Get the configuration specification of the plot.
pub fn configuration(&self) -> &Configuration {
&self.configuration
}
/// 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.
#[cfg(not(feature = "wasm"))]
pub fn show(&self) {
use std::env;
let rendered = self.render(false, "", 0, 0);
let rendered = rendered.as_bytes();
let mut temp = env::temp_dir();
let mut plot_name = String::from_utf8(
thread_rng()
.sample_iter(&Alphanumeric)
.take(22)
.collect::<Vec<u8>>(),
)
.unwrap();
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...`.
#[cfg(not(feature = "wasm"))]
pub fn show_png(&self, width: usize, height: usize) {
use std::env;
let rendered = self.render(true, "png", width, height);
let rendered = rendered.as_bytes();
let mut temp = env::temp_dir();
let mut plot_name = String::from_utf8(
thread_rng()
.sample_iter(&Alphanumeric)
.take(22)
.collect::<Vec<u8>>(),
)
.unwrap();
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...`.
#[cfg(not(feature = "wasm"))]
pub fn show_jpeg(&self, width: usize, height: usize) {
use std::env;
let rendered = self.render(true, "jpg", width, height);
let rendered = rendered.as_bytes();
let mut temp = env::temp_dir();
let mut plot_name: String = String::from_utf8(
thread_rng()
.sample_iter(&Alphanumeric)
.take(22)
.collect::<Vec<u8>>(),
)
.unwrap();
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.
///
/// In contrast to `Plot::write_html`, this will save the resulting html to a file located at a
/// user specified location, instead of a writing it to anything that implements `std::io::Write`.
pub fn to_html<P: AsRef<Path>>(&self, filename: P) {
let mut file = File::create(filename.as_ref()).unwrap();
self.write_html(&mut file);
}
/// Renders the contents of the `Plot` to HTML and outputs them to a writable buffer.
///
/// In contrast to `Plot::to_html`, this will save the resulting html to a byte buffer using the
/// `std::io::Write` trait, instead of to a user specified file.
pub fn write_html<W: Write>(&self, buffer: &mut W) {
let rendered = self.render(false, "", 0, 0);
let rendered = rendered.as_bytes();
buffer
.write_all(rendered)
.expect("failed to write html output");
}
/// Renders the contents of the `Plot` and returns it as a String, for embedding in
/// web-pages or Jupyter notebooks. A `div` is generated with the supplied id followed by the
/// script that generates the plot. The assumption is that plotly.js is available within the
/// html page that this element is embedded. If that assumption is violated then the plot will
/// not be displayed.
///
/// If `plot_div_id` is `None` the plot div id will be randomly generated, otherwise the user
/// supplied div id is used.
pub fn to_inline_html<T: Into<Option<&'static str>>>(&self, plot_div_id: T) -> String {
let plot_div_id = plot_div_id.into();
match plot_div_id {
Some(id) => self.render_inline(id.as_ref()),
None => {
let rand_id = String::from_utf8(
thread_rng()
.sample_iter(&Alphanumeric)
.take(20)
.collect::<Vec<u8>>(),
)
.unwrap();
self.render_inline(rand_id.as_str())
}
}
}
fn to_jupyter_notebook_html(&self) -> String {
let plot_div_id = String::from_utf8(
thread_rng()
.sample_iter(&Alphanumeric)
.take(20)
.collect::<Vec<u8>>(),
)
.unwrap();
let tmpl = JupyterNotebookPlotTemplate {
plot: self,
plot_div_id: plot_div_id.as_str(),
};
tmpl.render().unwrap()
}
/// Display plot in Jupyter Notebook.
pub fn notebook_display(&self) {
let plot_data = self.to_jupyter_notebook_html();
println!(
"EVCXR_BEGIN_CONTENT text/html\n{}\nEVCXR_END_CONTENT",
plot_data
);
}
/// Display plot in Jupyter Lab.
pub fn lab_display(&self) {
let plot_data = self.to_json();
println!(
"EVCXR_BEGIN_CONTENT application/vnd.plotly.v1+json\n{}\nEVCXR_END_CONTENT",
plot_data
);
}
/// Displays the plot in Jupyter Lab; if running a Jupyter Notebook then use the
/// `notebook_display()` method instead.
pub fn evcxr_display(&self) {
self.lab_display();
}
/// Saves the `Plot` to the selected image format.
#[cfg(feature = "kaleido")]
pub fn save<P: AsRef<Path>>(
&self,
filename: P,
format: ImageFormat,
width: usize,
height: usize,
scale: f64,
) {
let kaleido = plotly_kaleido::Kaleido::new();
let image_format = match format {
ImageFormat::PNG => "png",
ImageFormat::JPEG => "jpeg",
ImageFormat::SVG => "svg",
ImageFormat::PDF => "pdf",
ImageFormat::EPS => "eps",
ImageFormat::WEBP => "webp",
};
kaleido
.save(
filename.as_ref(),
&serde_json::to_value(self).unwrap(),
image_format,
width,
height,
scale,
)
.unwrap_or_else(|_| panic!("failed to export plot to {:?}", filename.as_ref()));
}
fn render(
&self,
export_image: bool,
image_type: &str,
image_width: usize,
image_height: usize,
) -> String {
let plotly_js = PlotlyJs {}.render().unwrap();
let tmpl = PlotTemplate {
plot: self,
plotly_javascript: plotly_js.as_str(),
remote_plotly_js: self.remote_plotly_js,
export_image,
image_type,
image_width,
image_height,
};
tmpl.render().unwrap()
}
fn render_inline(&self, plot_div_id: &str) -> String {
let tmpl = InlinePlotTemplate {
plot: self,
plot_div_id,
};
tmpl.render().unwrap()
}
pub fn to_json(&self) -> String {
serde_json::to_string(self).unwrap()
}
#[cfg(feature = "wasm")]
/// Convert a `Plot` to a native Javasript `js_sys::Object`.
pub fn to_js_object(&self) -> js_sys::Object {
use wasm_bindgen::JsCast;
// The only reason this could fail is if to_json() produces structurally incorrect JSON. That
// would be a bug, and would require fixing in the to_json()/serialization methods, rather than here
js_sys::JSON::parse(&self.to_json())
.expect("Invalid JSON")
.dyn_into::<js_sys::Object>()
.expect("Invalid JSON structure - expected a top-level Object")
}
#[cfg(all(target_os = "linux", not(feature = "wasm")))]
fn show_with_default_app(temp_path: &str) {
use std::process::Command;
Command::new("xdg-open")
.args(&[temp_path])
.output()
.expect(DEFAULT_HTML_APP_NOT_FOUND);
}
#[cfg(all(target_os = "macos", not(feature = "wasm")))]
fn show_with_default_app(temp_path: &str) {
use std::process::Command;
Command::new("open")
.args(&[temp_path])
.output()
.expect(DEFAULT_HTML_APP_NOT_FOUND);
}
#[cfg(all(target_os = "windows", not(feature = "wasm")))]
fn show_with_default_app(temp_path: &str) {
use std::process::Command;
Command::new("cmd")
.arg("/C")
.arg(format!(r#"start {}"#, temp_path))
.output()
.expect(DEFAULT_HTML_APP_NOT_FOUND);
}
}
impl PartialEq for Plot {
fn eq(&self, other: &Self) -> bool {
self.to_json() == other.to_json()
}
}
#[cfg(test)]
mod tests {
use serde_json::{json, to_value};
#[cfg(feature = "kaleido")]
use std::path::PathBuf;
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]
fn test_inline_plot() {
let plot = create_test_plot();
let inline_plot_data = plot.to_inline_html("replace_this_with_the_div_id");
assert!(inline_plot_data.contains("replace_this_with_the_div_id"));
println!("{}", inline_plot_data);
let random_div_id = plot.to_inline_html(None);
println!("{}", random_div_id);
}
#[test]
fn test_jupyter_notebook_plot() {
let plot = create_test_plot();
let inline_plot_data = plot.to_jupyter_notebook_html();
println!("{}", inline_plot_data);
}
#[test]
fn test_notebook_display() {
let plot = create_test_plot();
plot.notebook_display();
}
#[test]
fn test_lab_display() {
let plot = create_test_plot();
plot.lab_display();
}
#[test]
fn test_plot_serialize_simple() {
let plot = create_test_plot();
let expected = json!({
"data": [
{
"type": "scatter",
"name": "trace1",
"x": [0, 1, 2],
"y": [6, 10, 2]
}
],
"layout": {},
"config": {},
});
assert_eq!(to_value(plot).unwrap(), expected);
}
#[test]
fn test_plot_serialize_with_layout() {
let mut plot = create_test_plot();
let layout = Layout::new().title("Title".into());
plot.set_layout(layout);
let expected = json!({
"data": [
{
"type": "scatter",
"name": "trace1",
"x": [0, 1, 2],
"y": [6, 10, 2]
}
],
"layout": {
"title": {
"text": "Title"
}
},
"config": {},
});
assert_eq!(to_value(plot).unwrap(), expected);
}
#[test]
fn test_data_to_json() {
let plot = create_test_plot();
let expected = json!([
{
"type": "scatter",
"name": "trace1",
"x": [0, 1, 2],
"y": [6, 10, 2]
}
]);
assert_eq!(to_value(plot.data()).unwrap(), expected);
}
#[test]
fn test_empty_layout_to_json() {
let plot = create_test_plot();
let expected = json!({});
assert_eq!(to_value(plot.layout()).unwrap(), expected);
}
#[test]
fn test_layout_to_json() {
let mut plot = create_test_plot();
let layout = Layout::new().title("TestTitle".into());
plot.set_layout(layout);
let expected = json!({
"title": {"text": "TestTitle"}
});
assert_eq!(to_value(plot.layout()).unwrap(), expected);
}
#[test]
fn test_plot_eq() {
let plot1 = create_test_plot();
let plot2 = create_test_plot();
assert!(plot1 == plot2);
}
#[test]
fn test_plot_neq() {
let plot1 = create_test_plot();
let trace2 = Scatter::new(vec![10, 1, 2], vec![6, 10, 2]).name("trace2");
let mut plot2 = Plot::new();
plot2.add_trace(trace2);
assert!(plot1 != plot2);
}
#[test]
fn test_plot_clone() {
let plot1 = create_test_plot();
let plot2 = plot1.clone();
assert!(plot1 == plot2);
}
#[test]
#[cfg(feature = "kaleido")]
fn test_save_to_png() {
let plot = create_test_plot();
let dst = PathBuf::from("example.png");
plot.save(&dst, ImageFormat::PNG, 1024, 680, 1.0);
assert!(dst.exists());
assert!(std::fs::remove_file(&dst).is_ok());
assert!(!dst.exists());
}
#[test]
#[cfg(feature = "kaleido")]
fn test_save_to_jpeg() {
let plot = create_test_plot();
let dst = PathBuf::from("example.jpeg");
plot.save(&dst, ImageFormat::JPEG, 1024, 680, 1.0);
assert!(dst.exists());
assert!(std::fs::remove_file(&dst).is_ok());
assert!(!dst.exists());
}
#[test]
#[cfg(feature = "kaleido")]
fn test_save_to_svg() {
let plot = create_test_plot();
let dst = PathBuf::from("example.svg");
plot.save(&dst, ImageFormat::SVG, 1024, 680, 1.0);
assert!(dst.exists());
assert!(std::fs::remove_file(&dst).is_ok());
assert!(!dst.exists());
}
#[test]
#[ignore]
#[cfg(feature = "kaleido")]
fn test_save_to_eps() {
let plot = create_test_plot();
let dst = PathBuf::from("example.eps");
plot.save(&dst, ImageFormat::EPS, 1024, 680, 1.0);
assert!(dst.exists());
assert!(std::fs::remove_file(&dst).is_ok());
assert!(!dst.exists());
}
#[test]
#[cfg(feature = "kaleido")]
fn test_save_to_pdf() {
let plot = create_test_plot();
let dst = PathBuf::from("example.pdf");
plot.save(&dst, ImageFormat::PDF, 1024, 680, 1.0);
assert!(dst.exists());
assert!(std::fs::remove_file(&dst).is_ok());
assert!(!dst.exists());
}
#[test]
#[cfg(feature = "kaleido")]
fn test_save_to_webp() {
let plot = create_test_plot();
let dst = PathBuf::from("example.webp");
plot.save(&dst, ImageFormat::WEBP, 1024, 680, 1.0);
assert!(dst.exists());
assert!(std::fs::remove_file(&dst).is_ok());
assert!(!dst.exists());
}
}