8000 fix rendering of scientific charts · plotly/plotly.rs@e228676 · GitHub
[go: up one dir, main page]

Skip to content
8000
8000

Commit e228676

Browse files
committed
fix rendering of scientific charts
1 parent d27c261 commit e228676

File tree

4 files changed

+87
-205
lines changed

4 files changed

+87
-205
lines changed

.github/workflows/book.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ jobs:
2020
run: |
2121
cd ${{ github.workspace }}/examples/basic_charts && cargo run
2222
cd ${{ github.workspace }}/examples/statistical_charts && cargo run
23+
cd ${{ github.workspace }}/examples/scientific_charts && cargo run
2324
- run: mdbook build docs/book
2425
- name: Checkout gh-pages branch
2526
run: |

docs/book/src/recipes/scientific_charts/contour_plots.md

Lines changed: 17 additions & 162 deletions
Large diffs are not rendered by default.

docs/book/src/recipes/scientific_charts/heatmaps.md

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
The following imports have been used to produce the plots below:
44

5-
```rust
5+
```rust,no_run
66
use plotly::common::{ColorScale, ColorScalePalette, Title};
77
use plotly::contour::Contours;
88
use plotly::{Contour, HeatMap, Layout, Plot};
@@ -12,27 +12,8 @@ use std::f64::consts::PI;
1212
The `to_inline_html` method is used to produce the html plot displayed in this page.
1313

1414
## Basic Heatmap
15-
```rust
16-
fn basic_heat_map(show: bool) {
17-
let z = vec![vec![1, 20, 30], vec![20, 1, 60], vec![30, 60, 1]];
18-
let trace = HeatMap::new_z(z);
19-
let mut plot = Plot::new();
20-
plot.add_trace(trace);
21-
if show {
22-
plot.show();
23-
}
24-
println!("{}", plot.to_inline_html(Some("basic_heat_map")));
25-
}
15+
```rust,no_run
16+
{{#include ../../../../../examples/scientific_charts/src/main.rs:basic_heat_map}}
2617
```
27-
<div id="basic_heat_map" class="plotly-graph-div" style="height:100%; width:100%;"></div>
28-
<script type="text/javascript">
29-
window.PLOTLYENV=window.PLOTLYENV || {};
30-
if (document.getElementById("basic_heat_map")) {
31-
var d3 = Plotly.d3;
32-
var image_element= d3.select('#image-export');
33-
var trace_0 = {"type":"heatmap","x":null,"y":null,"z":[[1,20,30],[20,1,60],[30,60,1]]};
34-
var data = [trace_0];
35-
var layout = {};
36-
Plotly.newPlot('basic_heat_map', data, layout, {"responsive": true});
37-
};
38-
</script>
18+
19+
{{#include ../../../../../examples/scientific_charts/out/basic_heat_map.html}}

examples/scientific_charts/src/main.rs

Lines changed: 64 additions & 19 deletions
F438
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ use plotly::contour::Contours;
77
use plotly::{Contour, HeatMap, Layout, Plot};
88

99
// Contour Plots
10-
fn simple_contour_plot() {
10+
// ANCHOR: simple_contour_plot
11+
fn simple_contour_plot(show: bool) -> Plot {
1112
let n = 200;
1213
let mut x = Vec::<f64>::new();
1314
let mut y = Vec::<f64>::new();
@@ -34,10 +35,15 @@ fn simple_contour_plot() {
3435

3536
plot.add_trace(trace);
3637

37-
plot.show();
38+
if show {
< 6D40 /code>
39+
plot.show();
40+
}
41+
plot
3842
}
43+
// ANCHOR_END: simple_contour_plot
3944

40-
fn colorscale_for_contour_plot() {
45+
// ANCHOR: colorscale_for_contour_plot
46+
fn colorscale_for_contour_plot(show: bool) -> Plot {
4147
let z = vec![
4248
vec![10.0, 10.625, 12.5, 15.625, 20.0],
4349
vec![5.625, 6.25, 8.125, 11.25, 15.625],
@@ -52,10 +58,15 @@ fn colorscale_for_contour_plot() {
5258
plot.set_layout(layout);
5359
plot.add_trace(trace);
5460

55-
plot.show();
61+
if show {
62+
plot.show();
63+
}
64+
plot
5665
}
66+
// ANCHOR_END: colorscale_for_contour_plot
5767

58-
fn customizing_size_and_range_of_a_contour_plots_contours() {
68+
// ANCHOR: customizing_size_and_range_of_a_contour_plots_contours
69+
fn customizing_size_and_range_of_a_contour_plots_contours(show: bool) -> Plot {
5970
let z = vec![
6071
vec![10.0, 10.625, 12.5, 15.625, 20.0],
6172
vec![5.625, 6.25, 8.125, 11.25, 15.625],
@@ -73,10 +84,15 @@ fn customizing_size_and_range_of_a_contour_plots_contours() {
7384
plot.set_layout(layout);
7485
plot.add_trace(trace);
7586

76-
plot.show();
87+
if show {
88+
plot.show();
89+
}
90+
plot
7791
}
92+
// ANCHOR_END: customizing_size_and_range_of_a_contour_plots_contours
7893

79-
fn customizing_spacing_between_x_and_y_ticks() {
94+
// ANCHOR: customizing_spacing_between_x_and_y_ticks
95+
fn customizing_spacing_between_x_and_y_ticks(show: bool) -> Plot {
8096
let z = vec![
8197
vec![10.0, 10.625, 12.5, 15.625, 20.0],
8298
vec![5.625, 6.25, 8.125, 11.25, 15.625],
@@ -96,20 +112,30 @@ fn customizing_spacing_between_x_and_y_ticks() {
96112
plot.set_layout(layout);
97113
plot.add_trace(trace);
98114

99-
plot.show();
115+
if show {
116+
plot.show();
117+
}
118+
plot
100119
}
120+
// ANCHOR_END: customizing_spacing_between_x_and_y_ticks
101121

102122
// Heatmaps
103-
fn basic_heat_map() {
123+
// ANCHOR: basic_heat_map
124+
fn basic_heat_map(show: bool) -> Plot {
104125
let z = vec![vec![1, 20, 30], vec![20, 1, 60], vec![30, 60, 1]];
105126
let trace = HeatMap::new_z(z).zmin(1.0).zmax(60.0);
106127
let mut plot = Plot::new();
107128
plot.add_trace(trace);
108129

109-
plot.show();
130+
if show {
131+
plot.show();
132+
}
133+
plot
110134
}
135+
// ANCHOR_END: basic_heat_map
111136

112-
fn customized_heat_map() {
137+
// ANCHOR: customized_heat_map
138+
fn customized_heat_map(show: bool) -> Plot {
113139
let x = (0..100).map(|x| x as f64).collect::<Vec<f64>>();
114140
let y = (0..100).map(|y| y as f64).collect::<Vec<f64>>();
115141
let z: Vec<Vec<f64>> = y
@@ -143,19 +169,38 @@ fn customized_heat_map() {
143169
plot.set_layout(layout);
144170
plot.add_trace(trace);
145171

146-
plot.show();
172+
if show {
173+
plot.show();
174+
}
175+
plot
176+
}
177+
// ANCHOR_END: customized_heat_map
178+
179+
fn write_example_to_html(plot: Plot, name: &str) {
180+
std::fs::create_dir_all("./out").unwrap();
181+
let html = plot.to_inline_html(Some(name));
182+
std::fs::write(format!("./out/{}.html", name), html).unwrap();
147183
}
148184

149185
fn main() {
150-
// Uncomment any of these lines to display the example.
186+
// Change false to true on any of these lines to display the example.
151187

152188
// Contour Plots
153-
// simple_contour_plot();
154-
// colorscale_for_contour_plot();
155-
// customizing_size_and_range_of_a_contour_plots_contours();
156-
// customizing_spacing_between_x_and_y_ticks();
189+
write_example_to_html(simple_contour_plot(false), "simple_contour_plot");
190+
write_example_to_html(
191+
colorscale_for_contour_plot(false),
192+
"colorscale_for_contour_plot",
193+
);
194+
write_example_to_html(
195+
customizing_size_and_range_of_a_contour_plots_contours(false),
196+
"customizing_size_and_range_of_a_contour_plots_contours",
197+
);
198+
write_example_to_html(
199+
customizing_spacing_between_x_and_y_ticks(false),
200+
"customizing_spacing_between_x_and_y_ticks",
201+
);
157202

158203
// Heatmaps
159-
// basic_heat_map();
160-
// customized_heat_map();
204+
write_example_to_html(basic_heat_map(false), "basic_heat_map");
205+
write_example_to_html(customized_heat_map(false), "customized_heat_map");
161206
}

0 commit comments

Comments
 (0)
0