Skip to content

Commit b874b80

Browse files
committed
refactor examples and add themes example to mdbook
Signed-off-by: Andrei Gherghescu <[email protected]>
1 parent d381fd1 commit b874b80

File tree

30 files changed

+175
-177
lines changed

30 files changed

+175
-177
lines changed

docs/book/src/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,4 @@
2929
- [Subplots](./recipes/subplots.md)
3030
- [Subplots](./recipes/subplots/subplots.md)
3131
- [Multiple Axes](./recipes/subplots/multiple_axes.md)
32+
- [Themes](./recipes/themes.md)

docs/book/src/recipes/themes.md

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# Themes
2+
3+
The complete source code for the following examples can also be found [here](https://github.com/plotly/plotly.rs/tree/main/examples/themes).
4+
5+
# Use different theme templates
6+
7+
Similar to [Plotly Python templates](https://plotly.com/python/templates/), plotly.rs provides several built-in themes that can be applied to your plots.
8+
9+
```rust,no_run
10+
use plotly::{
11+
common::{Marker, Mode, Title},
12+
layout::{Layout, BuiltinTheme},
13+
Plot, Scatter,
14+
};
15+
```
16+
17+
The `to_inline_html` method is used to produce the html plot displayed in this page.
18+
19+
## Default Theme (Plotly)
20+
21+
{{#include ../../../../examples/themes/output/inline_gapminder_plotly.html}}
22+
23+
## Plotly White Theme
24+
25+
{{#include ../../../../examples/themes/output/inline_gapminder_plotly_white.html}}
26+
27+
## Plotly Dark Theme
28+
29+
{{#include ../../../../examples/themes/output/inline_gapminder_plotly_dark.html}}
30+
31+
## Seaborn Theme
32+
33+
{{#include ../../../../examples/themes/output/inline_gapminder_seaborn.html}}
34+
35+
## Matplotlib Theme
36+
37+
{{#include ../../../../examples/themes/output/inline_gapminder_matplotlib.html}}
38+
39+
## Plotnine Theme
40+
41+
{{#include ../../../../examples/themes/output/inline_gapminder_plotnine.html}}
42+
43+
## Available Themes
44+
45+
The following built-in themes are available in plotly.rs:
46+
47+
- `BuiltinTheme::Default` - Default Plotly theme
48+
- `BuiltinTheme::PlotlyWhite` - Clean white background theme
49+
- `BuiltinTheme::PlotlyDark` - Dark theme
50+
- `BuiltinTheme::Seaborn` - Seaborn-style theme
51+
- `BuiltinTheme::SeabornWhitegrid` - Seaborn with white grid
52+
- `BuiltinTheme::SeabornDark` - Dark Seaborn theme
53+
- `BuiltinTheme::Matplotlib` - Matplotlib-style theme
54+
- `BuiltinTheme::Plotnine` - Plotnine-style theme
55+
56+
## Using Themes
57+
58+
To apply a theme to your plot, use the `template()` method on the layout:
59+
60+
```rust
61+
let theme = BuiltinTheme::Seaborn;
62+
let layout = Layout::new().template(theme.build());
63+
plot.set_layout(layout);
64+
```
65+
66+
The example above uses real Gapminder 2007 data showing the relationship between GDP per capita, life expectancy, and population size across different continents, with marker sizes representing population and colors representing continents.

examples/3d_charts/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ edition = "2021"
88
ndarray = "0.16"
99
rand = "0.9"
1010
plotly = { path = "../../plotly" }
11+
plotly_utils = { path = "../plotly_utils" }

examples/3d_charts/src/main.rs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#![allow(dead_code)]
22

33
use ndarray::Array;
4+
use plotly_utils::write_example_to_html;
45
use plotly::{
56
color::Rgb,
67
common::{ColorBar, ColorScale, ColorScalePalette, Font, Marker, MarkerSymbol, Mode},
@@ -259,17 +260,6 @@ fn colorscale_plot(show: bool, file_name: &str) {
259260
}
260261
// ANCHOR_END: colorscale_plot
261262

262-
fn write_example_to_html(plot: &Plot, name: &str) -> String {
263-
std::fs::create_dir_all("./output").unwrap();
264-
// Write inline HTML
265-
let html = plot.to_inline_html(Some(name));
266-
let path = format!("./output/inline_{}.html", name);
267-
std::fs::write(path, html).unwrap();
268-
// Write standalone HTML
269-
let path = format!("./output/{}.html", name);
270-
plot.write_html(&path);
271-
path
272-
}
273263

274264
fn main() {
275265
// Change false to true on any of these lines to display the example.

examples/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
[workspace]
2-
members = ["*"]
2+
members = ["*", "plotly_utils"]
33
resolver = "2"
44
exclude = ["jupyter", "target", "wasm-yew"]

examples/basic_charts/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@ edition = "2021"
77
[dependencies]
88
ndarray = "0.16"
99
plotly = { path = "../../plotly" }
10+
plotly_utils = { path = "../plotly_utils" }
1011
rand = "0.9"
1112
rand_distr = "0.5"

examples/basic_charts/src/main.rs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use plotly::{
1515
traces::table::{Cells, Header},
1616
Bar, Pie, Plot, Sankey, Scatter, ScatterPolar, Table,
1717
};
18+
use plotly_utils::write_example_to_html;
1819
use rand_distr::{Distribution, Normal, Uniform};
1920

2021
// Scatter Plots
@@ -996,18 +997,6 @@ fn grouped_donout_pie_charts(show: bool, file_name: &str) {
996997
}
997998
// ANCHOR_END: grouped_donout_pie_charts
998999

999-
fn write_example_to_html(plot: &Plot, name: &str) -> String {
1000-
std::fs::create_dir_all("./output").unwrap();
1001-
// Write inline HTML
1002-
let html = plot.to_inline_html(Some(name));
1003-
let path = format!("./output/inline_{}.html", name);
1004-
std::fs::write(path, html).unwrap();
1005-
// Write standalone HTML
1006-
let path = format!("./output/{}.html", name);
1007-
plot.write_html(&path);
1008-
path
1009-
}
1010-
10111000
fn main() {
10121001
// Change false to true on any of these lines to display the example.
10131002

examples/custom_controls/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ edition = "2021"
77
[dependencies]
88
itertools = "0.10"
99
plotly = { path = "../../plotly" }
10+
plotly_utils = { path = "../plotly_utils" }

examples/custom_controls/src/main.rs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#![allow(dead_code)]
22

33
use itertools::Itertools;
4+
use plotly_utils::write_example_to_html;
45
use plotly::{
56
common::{Anchor, ColorScalePalette, Visible},
67
layout::{
@@ -117,17 +118,6 @@ fn bar_chart_with_modifiable_bar_mode(show: bool, file_name: &str) {
117118
}
118119
// ANCHOR_END: colorscale_plot
119120

120-
fn write_example_to_html(plot: &Plot, name: &str) -> String {
121-
std::fs::create_dir_all("./output").unwrap();
122-
// Write inline HTML
123-
let html = plot.to_inline_html(Some(name));
124-
let path = format!("./output/inline_{}.html", name);
125-
std::fs::write(path, html).unwrap();
126-
// Write standalone HTML
127-
let path = format!("./output/{}.html", name);
128-
plot.write_html(&path);
129-
path
130-
}
131121

132122
fn main() {
133123
// Change false to true on any of these lines to display the example.

examples/customization/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ build_html = "2.5.0"
99
rand = "0.9"
1010
ndarray = "0.16"
1111
plotly = { path = "../../plotly" }
12+
plotly_utils = { path = "../plotly_utils" }

0 commit comments

Comments
 (0)