Skip to content

Commit 7bd5202

Browse files
committed
fix choropleth plot for inline HTLM rendering
Signed-off-by: Andrei Gherghescu <8067229+andrei-ng@users.noreply.github.com>
1 parent aaf1add commit 7bd5202

3 files changed

Lines changed: 52 additions & 15 deletions

File tree

examples/financial_charts/src/main.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -381,10 +381,8 @@ fn hiding_weekends_and_holidays_with_rangebreaks(show: bool, file_name: &str) {
381381
.range(vec!["2015-12-01", "2016-01-15"])
382382
.title("Date")
383383
.range_breaks(vec![
384-
plotly::layout::RangeBreak::new()
385-
.bounds("sat", "mon"), // hide weekends
386-
plotly::layout::RangeBreak::new()
387-
.values(vec!["2015-12-25", "2016-01-01"]), // hide Christmas and New Year's
384+
plotly::layout::RangeBreak::new().bounds("sat", "mon"), // hide weekends
385+
plotly::layout::RangeBreak::new().values(vec!["2015-12-25", "2016-01-01"]), // hide Christmas and New Year's
388386
]),
389387
)
390388
.y_axis(Axis::new().title("Price"));

examples/maps/src/main.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use plotly::{
1111
Choropleth, ChoroplethMap, Configuration, DensityMapbox, Layout, Plot, ScatterGeo,
1212
ScatterMapbox,
1313
};
14-
use plotly_utils::write_example_to_html;
14+
use plotly_utils::{write_example_to_html, write_example_to_html_with_inline_config};
1515

1616
fn scatter_mapbox(show: bool, file_name: &str) {
1717
let trace = ScatterMapbox::new(vec![45.5017], vec![-73.5673])
@@ -210,7 +210,11 @@ fn choropleth(show: bool, file_name: &str) {
210210
plot.set_layout(layout);
211211
plot.set_configuration(Configuration::default().responsive(true).fill_frame(true));
212212

213-
let path = write_example_to_html(&plot, file_name);
213+
let path = write_example_to_html_with_inline_config(
214+
&plot,
215+
file_name,
216+
Some(Configuration::default().scroll_zoom(false)), // book friendly intline HTML
217+
);
214218
if show {
215219
plot.show_html(path);
216220
}
@@ -246,7 +250,11 @@ fn choropleth_map(show: bool, file_name: &str) {
246250
plot.set_layout(layout);
247251
plot.set_configuration(Configuration::default().responsive(true).fill_frame(true));
248252

249-
let path = write_example_to_html(&plot, file_name);
253+
let path = write_example_to_html_with_inline_config(
254+
&plot,
255+
file_name,
256+
Some(Configuration::default().scroll_zoom(false)), // book friendly intline HTML
257+
);
250258
if show {
251259
plot.show_html(path);
252260
}

examples/plotly_utils/src/lib.rs

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use plotly::Plot;
1+
use plotly::{Configuration, Plot};
22

33
/// Write a plot to HTML files for documentation and display
44
///
@@ -11,17 +11,48 @@ use plotly::Plot;
1111
/// * `plot` - The plot to write to HTML
1212
/// * `name` - The base name for the HTML files (without extension)
1313
///
14-
/// # Returns
15-
///
16-
/// The path to the standalone HTML file
14+
/// # Returns the path to the standalone HTML file
1715
pub fn write_example_to_html(plot: &Plot, name: &str) -> String {
16+
write_example_to_html_with_inline_config(plot, name, None)
17+
}
18+
19+
/// Write a plot to HTML files for documentation and display
20+
///
21+
/// This function creates both an inline HTML file (for mdbook inclusion) and
22+
/// a standalone HTML file (for direct viewing). The inline file is prefixed
23+
/// with "inline_" and both files are placed in the "./output" directory.
24+
/// The plot for inline HTML display can be configured with the provided
25+
/// `inline_config` when provided, otherwise the standalone inherited plot
26+
/// configuration is used.
27+
///
28+
/// # Arguments
29+
///
30+
/// * `plot` - The plot to write to HTML
31+
/// * `name` - The base name for the HTML files (without extension)
32+
/// * `inline_config` - The configuration to use for the inline HTML file
33+
///
34+
/// # Returns the path to the standalone HTML file
35+
pub fn write_example_to_html_with_inline_config(
36+
plot: &Plot,
37+
name: &str,
38+
inline_config: Option<Configuration>,
39+
) -> String {
1840
std::fs::create_dir_all("./output").unwrap();
41+
42+
let standalone_config = plot.configuration().clone();
43+
let inline_config = inline_config.unwrap_or(standalone_config.clone());
44+
1945
// Write inline HTML
20-
let html = plot.to_inline_html(Some(name));
21-
let path = format!("./output/inline_{name}.html");
22-
std::fs::write(path, html).unwrap();
46+
let mut inline_plot = plot.clone();
47+
inline_plot.set_configuration(inline_config);
48+
let html = inline_plot.to_inline_html(Some(name));
49+
let inline_path = format!("./output/inline_{name}.html");
50+
std::fs::write(inline_path, html).unwrap();
51+
2352
// Write standalone HTML
53+
let mut standalone_plot = plot.clone();
54+
standalone_plot.set_configuration(standalone_config);
2455
let path = format!("./output/{name}.html");
25-
plot.write_html(&path);
56+
standalone_plot.write_html(&path);
2657
path
2758
}

0 commit comments

Comments
 (0)