Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 56bfe3c

Browse files
committed
add examples on building responsive plots and custom HTML pages
Signed-off-by: Andrei Gherghescu <[email protected]>
1 parent 86e12ae commit 56bfe3c

File tree

4 files changed

+173
-1
lines changed

4 files changed

+173
-1
lines changed

examples/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22

33
This folder contains a multitude of usage examples covering as many features of the library as possible. Instructions on how to run each example can be found in each example's subdirectory.
44

5-
Pull requests with more examples of different behaviour are always welcome.
5+
Pull requests with more examples of different behaviour are always welcome.

examples/customization/Cargo.toml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[package]
2+
name = "customization"
3+
version = "0.1.0"
4+
authors = ["Andrei Gherghescu [email protected]"]
5+
edition = "2021"
6+
7+
[dependencies]
8+
build_html = "2.5.0"
9+
rand = "0.8"
10+
ndarray = "0.16"
11+
plotly = { path = "../../plotly" }

examples/customization/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# HTML Customization
2+
3+
We often get issues/questions regarding customization of the HTML output. In most situations, these are not related to Plotly functionality but rather custom behavior related to HTML rendering.
4+
5+
The directory [./customization](./customization) contains examples of the most frequent raised questions by users of `plotly-rs`, such as
6+
- making the resulting HTML plot responsive on browser window size change
7+
- making the resulting HTML fill the entire browser page
8+
- placing multiple plots in the same HTML page using the [`build_html`](https://crates.io/crates/build_html) crate

examples/customization/src/main.rs

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
#![allow(dead_code)]
2+
3+
use ndarray::Array;
4+
use plotly::{
5+
color::NamedColor,
6+
common::{Marker, Mode, Title},
7+
layout::{Center, DragMode, Mapbox, MapboxStyle, Margin},
8+
Configuration, DensityMapbox, Layout, Plot, Scatter, Scatter3D,
9+
};
10+
11+
use build_html::*;
12+
const DEFAULT_HTML_APP_NOT_FOUND: &str = "Could not find default application for HTML files.";
13+
14+
fn density_mapbox_responsive_autofill() {
15+
let trace = DensityMapbox::new(vec![45.5017], vec![-73.5673], vec![0.75]).zauto(true);
16+
17+
let layout = Layout::new()
18+
.drag_mode(DragMode::Zoom)
19+
.margin(Margin::new().top(0).left(0).bottom(0).right(0))
20+
.mapbox(
21+
Mapbox::new()
22+
.style(MapboxStyle::OpenStreetMap)
23+
.center(Center::new(45.5017, -73.5673))
24+
.zoom(5),
25+
);
26+
27+
let mut plot = Plot::new();
28+
plot.add_trace(trace);
29+
plot.set_layout(layout);
30+
plot.set_configuration(Configuration::default().responsive(true).fill_frame(true));
31+
32+
plot.show();
33+
}
34+
35+
fn multiple_plots_on_same_html_page() {
36+
let html: String = HtmlPage::new()
37+
.with_title("Plotly-rs Multiple Plots")
38+
.with_script_link("https://cdn.plot.ly/plotly-latest.min.js")
39+
.with_header(1, "Multiple Plotly plots on the same HTML page")
40+
.with_raw(first_plot())
41+
.with_raw(second_plot())
42+
.with_raw(third_plot())
43+
.to_html_string();
44+
45+
let file = write_html(&html);
46+
show_with_default_app(&file);
47+
}
48+
49+
fn first_plot() -> String {
50+
let n: usize = 100;
51+
let t: Vec<f64> = Array::linspace(0., 10., n).into_raw_vec_and_offset().0;
52+
let y: Vec<f64> = t.iter().map(|x| x.sin()).collect();
53+
54+
let trace = Scatter::new(t, y).mode(Mode::Markers);
55+
let mut plot = Plot::new();
56+
plot.add_trace(trace);
57+
plot.to_inline_html(Some("scattter_1"))
58+
}
59+
60+
fn second_plot() -> String {
61+
let trace = Scatter::new(vec![1, 2, 3, 4], vec![10, 11, 12, 13])
62+
.mode(Mode::Markers)
63+
.marker(
64+
Marker::new()
65+
.size_array(vec![40, 60, 80, 100])
66+
.color_array(vec![
67+
NamedColor::Red,
68+
NamedColor::Blue,
69+
NamedColor::Cyan,
70+
NamedColor::OrangeRed,
71+
]),
72+
);
73+
let mut plot = Plot::new();
74+
plot.add_trace(trace);
75+
plot.to_inline_html(Some("scatter_2"))
76+
}
77+
78+
fn third_plot() -> String {
79+
let n: usize = 100;
80+
let t: Vec<f64> = Array::linspace(0., 10., n).into_raw_vec_and_offset().0;
81+
let y: Vec<f64> = t.iter().map(|x| x.sin()).collect();
82+
let z: Vec<f64> = t.iter().map(|x| x.cos()).collect();
83+
84+
let trace = Scatter3D::new(t, y, z).mode(Mode::Markers);
85+
let mut plot = Plot::new();
86+
plot.add_trace(trace);
87+
let l = Layout::new()
88+
.title(Title::with_text("Scatter3d"))
89+
.height(800);
90+
plot.set_layout(l);
91+
plot.to_inline_html(Some("scatter_3_3d"))
92+
}
93+
94+
#[cfg(all(unix, not(target_os = "android"), not(target_os = "macos")))]
95+
fn show_with_default_app(temp_path: &str) {
96+
use std::process::Command;
97+
Command::new("xdg-open")
98+
.args([temp_path])
99+
.output()
100+
.expect(DEFAULT_HTML_APP_NOT_FOUND);
101+
}
102+
103+
#[cfg(target_os = "macos")]
104+
fn show_with_default_app(temp_path: &str) {
105+
use std::process::Command;
106+
Command::new("open")
107+
.args([temp_path])
108+
.output()
109+
.expect(DEFAULT_HTML_APP_NOT_FOUND);
110+
}
111+
112+
#[cfg(target_os = "windows")]
113+
fn show_with_default_app(temp_path: &str) {
114+
use std::process::Command;
115+
Command::new("cmd")
116+
.args(&["/C", "start", &format!(r#"{}"#, temp_path)])
117+
.spawn()
118+
.expect(DEFAULT_HTML_APP_NOT_FOUND);
119+
}
120+
121+
fn write_html(html_data: &str) -> String {
122+
use rand::{
123+
distributions::{Alphanumeric, DistString},
124+
thread_rng,
125+
};
126+
use std::env;
127+
use std::{fs::File, io::Write};
128+
129+
// Set up the temp file with a unique filename.
130+
let mut temp = env::temp_dir();
131+
let mut plot_name = Alphanumeric.sample_string(&mut thread_rng(), 22);
132+
plot_name.push_str(".html");
133+
plot_name = format!("plotly_{}", plot_name);
134+
temp.push(plot_name);
135+
136+
// Save the rendered plot to the temp file.
137+
let temp_path = temp.to_str().unwrap();
138+
139+
{
140+
let mut file = File::create(temp_path).unwrap();
141+
file.write_all(html_data.as_bytes())
142+
.expect("failed to write html output");
143+
file.flush().unwrap();
144+
}
145+
temp_path.to_string()
146+
}
147+
148+
fn main() {
149+
// Uncomment any of these lines to display the example.
150+
151+
// density_mapbox_responsive_autofill();
152+
// multiple_plots_on_same_html_page();
153+
}

0 commit comments

Comments
 (0)