diff --git a/.github/workflows/book.yml b/.github/workflows/book.yml new file mode 100644 index 00000000..284e7dc6 --- /dev/null +++ b/.github/workflows/book.yml @@ -0,0 +1,37 @@ +name: Book + +on: + workflow_dispatch: + push: + tags: + - '[0-9]+.[0-9]+.[0-9]+' + +env: + RUST_BACKTRACE: full + +jobs: + build: + name: Build and deploy book + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - run: cargo install mdbook + - name: Build examples + run: cd ${{ github.workspace }}/examples/basic_charts && cargo run + - run: mdbook build docs/book + - name: Checkout gh-pages branch + run: | + git fetch origin gh-pages:gh-pages + git checkout gh-pages + - name: Overwrite book content + run: | + rm -rf content + cp -r gh-pages/content . + - name: Deploy to GitHub Pages + run: | + git config --global user.name 'github-actions[bot]' + git config --global user.email 'github-actions[bot]@users.noreply.github.com' + + git add content + git commit --allow-empty -m 'Deploy to GitHub Pages' + git push origin gh-pages \ No newline at end of file diff --git a/docs/book/book.toml b/docs/book/book.toml index 19aaa613..68b54c66 100644 --- a/docs/book/book.toml +++ b/docs/book/book.toml @@ -9,6 +9,7 @@ src = "src" [build] build-dir = "../../gh-pages/content" create-missing = false +extra-watch-dirs = ["../../examples"] [output.html] default-theme = "Ayu" diff --git a/docs/book/src/recipes/basic_charts/bar_charts.md b/docs/book/src/recipes/basic_charts/bar_charts.md index 8417367e..32c0d276 100644 --- a/docs/book/src/recipes/basic_charts/bar_charts.md +++ b/docs/book/src/recipes/basic_charts/bar_charts.md @@ -2,8 +2,8 @@ The following imports have been used to produce the plots below: -```rust -use itertools_num::linspace; +```rust,no_run +use ndarray::Array; use plotly::common::{ ColorScale, ColorScalePalette, DashType, Fill, Font, Line, LineShape, Marker, Mode, Title, }; @@ -16,99 +16,22 @@ The `to_inline_html` method is used to produce the html plot displayed in this p ## Basic Bar Chart -```rust -fn basic_bar_chart(show: bool) { - let animals = vec!["giraffes", "orangutans", "monkeys"]; - let t = Bar::new(animals, vec![20, 14, 23]); - let mut plot = Plot::new(); - plot.add_trace(t); - if show { - plot.show(); - } - println!("{}", plot.to_inline_html(Some("basic_bar_chart"))); -} - +```rust,no_run +{{#include ../../../../../examples/basic_charts/src/main.rs:basic_bar_chart}} ``` -
- - -## Grouped Bar Chart -```rust -fn grouped_bar_chart(show: bool) { - let animals1 = vec!["giraffes", "orangutans", "monkeys"]; - let trace1 = Bar::new(animals1, vec![20, 14, 23]).name("SF Zoo"); - let animals2 = vec!["giraffes", "orangutans", "monkeys"]; - let trace2 = Bar::new(animals2, vec![12, 18, 29]).name("LA Zoo"); +{{#include ../../../../../examples/basic_charts/out/basic_bar_chart.html}} - let layout = Layout::new().bar_mode(BarMode::Group); - - let mut plot = Plot::new(); - plot.add_trace(trace1); - plot.add_trace(trace2); - plot.set_layout(layout); - if show { - plot.show(); - } - println!("{}", plot.to_inline_html(Some("grouped_bar_chart"))); -} +## Grouped Bar Chart +```rust,no_run +{{#include ../../../../../examples/basic_charts/src/main.rs:grouped_bar_chart}} ``` -
- +{{#include ../../../../../examples/basic_charts/out/grouped_bar_chart.html}} ## Stacked Bar Chart -```rust -fn stacked_bar_chart(show: bool) { - let animals1 = vec!["giraffes", "orangutans", "monkeys"]; - let trace1 = Bar::new(animals1, vec![20, 14, 23]).name("SF Zoo"); - - let animals2 = vec!["giraffes", "orangutans", "monkeys"]; - let trace2 = Bar::new(animals2, vec![12, 18, 29]).name("LA Zoo"); - - let layout = Layout::new().bar_mode(BarMode::Stack); - - let mut plot = Plot::new(); - plot.add_trace(trace1); - plot.add_trace(trace2); - plot.set_layout(layout); - if show { - plot.show(); - } - println!("{}", plot.to_inline_html(Some("stacked_bar_chart"))); -} +```rust,no_run +{{#include ../../../../../examples/basic_charts/src/main.rs:stacked_bar_chart}} ``` -
- \ No newline at end of file + +{{#include ../../../../../examples/basic_charts/out/stacked_bar_chart.html}} diff --git a/docs/book/src/recipes/basic_charts/line_charts.md b/docs/book/src/recipes/basic_charts/line_charts.md index 2bab65ba..1035d9f0 100644 --- a/docs/book/src/recipes/basic_charts/line_charts.md +++ b/docs/book/src/recipes/basic_charts/line_charts.md @@ -2,8 +2,8 @@ The following imports have been used to produce the plots below: -```rust -use itertools_num::linspace; +```rust,no_run +use ndarray::Array; use plotly::common::{ ColorScale, ColorScalePalette, DashType, Fill, Font, Line, LineShape, Marker, Mode, Title, }; @@ -16,384 +16,44 @@ The `to_inline_html` method is used to produce the html plot displayed in this p ## Adding Names to Line and Scatter Plot -```rust -fn adding_names_to_line_and_scatter_plot(show: bool) { - let trace1 = Scatter::new(vec![1, 2, 3, 4], vec![10, 15, 13, 17]) - .mode(Mode::Markers) - .name("Scatter"); - let trace2 = Scatter::new(vec![2, 3, 4, 5], vec![16, 5, 11, 9]) - .mode(Mode::Lines) - .name("Lines"); - let trace3 = Scatter::new(vec![1, 2, 3, 4], vec![12, 9, 15, 12]) - .mode(Mode::LinesMarkers) - .name("Scatter + Lines"); - - let layout = Layout::new().title(Title::with_text("Adding Names to Line and Scatter Plot")); - let mut plot = Plot::new(); - plot.add_trace(trace1); - plot.add_trace(trace2); - plot.add_trace(trace3); - plot.set_layout(layout); - if show { - plot.show(); - } - println!("{}", plot.to_inline_html(Some("adding_names_to_line_and_scatter_plot"))); -} - +```rust,no_run +{{#include ../../../../../examples/basic_charts/src/main.rs:adding_names_to_line_and_scatter_plot}} ``` -
- +{{#include ../../../../../examples/basic_charts/out/adding_names_to_line_and_scatter_plot.html}} -## Line and Scatter Styling -```rust -fn line_and_scatter_styling(show: bool) { - let trace1 = Scatter::new(vec![1, 2, 3, 4], vec![10, 15, 13, 17]) - .mode(Mode::Markers) - .name("trace1") - .marker(Marker::new().color(Rgb::new(219, 64, 82)).size(12)); - let trace2 = Scatter::new(vec![2, 3, 4, 5], vec![16, 5, 11, 9]) - .mode(Mode::Lines) - .name("trace2") - .line(Line::new().color(Rgb::new(55, 128, 191)).width(3.0)); - let trace3 = Scatter::new(vec![1, 2, 3, 4], vec![12, 9, 15, 12]) - .mode(Mode::LinesMarkers) - .name("trace3") - .marker(Marker::new().color(Rgb::new(128, 0, 128)).size(12)) - .line(Line::new().color(Rgb::new(128, 0, 128)).width(1.0)); - let layout = Layout::new().title(Title::with_text("Line and Scatter Styling")); - let mut plot = Plot::new(); - plot.add_trace(trace1); - plot.add_trace(trace2); - plot.add_trace(trace3); - plot.set_layout(layout); - if show { - plot.show(); - } - println!("{}", plot.to_inline_html(Some("line_and_scatter_styling"))); -} +## Line and Scatter Styling +```rust,no_run +{{#include ../../../../../examples/basic_charts/src/main.rs:line_and_scatter_styling}} ``` -
- -## Styling Line Plot -```rust -fn styling_line_plot(show: bool) { - let trace1 = Scatter::new(vec![1, 2, 3, 4], vec![10, 15, 13, 17]) - .mode(Mode::Markers) - .name("Red") - .line(Line::new().color(Rgb::new(219, 64, 82)).width(3.0)); - let trace2 = Scatter::new(vec![1, 2, 3, 4], vec![12, 9, 15, 12]) - .mode(Mode::LinesMarkers) - .name("Blue") - .line(Line::new().color(Rgb::new(55, 128, 191)).width(1.0)); +{{#include ../../../../../examples/basic_charts/out/line_and_scatter_styling.html}} - let layout = Layout::new() - .title(Title::with_text("Styling Line Plot")) - .width(500) - .height(500); - let mut plot = Plot::new(); - plot.add_trace(trace1); - plot.add_trace(trace2); - plot.set_layout(layout); - if show { - plot.show(); - } - println!("{}", plot.to_inline_html(Some("styling_line_plot"))); -} +## Styling Line Plot +```rust,no_run +{{#include ../../../../../examples/basic_charts/src/main.rs:styling_line_plot}} ``` -
- -## Line Shape Options for Interpolation -```rust -fn line_shape_options_for_interpolation(show: bool) { - let trace1 = Scatter::new(vec![1, 2, 3, 4, 5], vec![1, 3, 2, 3, 1]) - .mode(Mode::LinesMarkers) - .name("linear") - .line(Line::new().shape(LineShape::Linear)); - let trace2 = Scatter::new(vec![1, 2, 3, 4, 5], vec![6, 8, 7, 8, 6]) - .mode(Mode::LinesMarkers) - .name("spline") - .line(Line::new().shape(LineShape::Spline)); - let trace3 = Scatter::new(vec![1, 2, 3, 4, 5], vec![11, 13, 12, 13, 11]) - .mode(Mode::LinesMarkers) - .name("vhv") - .line(Line::new().shape(LineShape::Vhv)); - let trace4 = Scatter::new(vec![1, 2, 3, 4, 5], vec![16, 18, 17, 18, 16]) - .mode(Mode::LinesMarkers) - .name("hvh") - .line(Line::new().shape(LineShape::Hvh)); - let trace5 = Scatter::new(vec![1, 2, 3, 4, 5], vec![21, 23, 22, 23, 21]) - .mode(Mode::LinesMarkers) - .name("vh") - .line(Line::new().shape(LineShape::Vh)); - let trace6 = Scatter::new(vec![1, 2, 3, 4, 5], vec![26, 28, 27, 28, 26]) - .mode(Mode::LinesMarkers) - .name("hv") - .line(Line::new().shape(LineShape::Hv)); +{{#include ../../../../../examples/basic_charts/out/styling_line_plot.html}} - let mut plot = Plot::new(); - let layout = Layout::new().legend( - Legend::new() - .y(0.5) - .trace_order("reversed") - .font(Font::new().size(16)), - ); - plot.set_layout(layout); - plot.add_trace(trace1); - plot.add_trace(trace2); - plot.add_trace(trace3); - plot.add_trace(trace4); - plot.add_trace(trace5); - plot.add_trace(trace6); - plot.show_png(1024, 680); - if show { - plot.show(); - } - println!("{}", plot.to_inline_html(Some("line_shape_options_for_interpolation"))); -} +## Line Shape Options for Interpolation +```rust,no_run +{{#include ../../../../../examples/basic_charts/src/main.rs:line_shape_options_for_interpolation}} ``` -
- -## Line Dash -```rust -fn line_dash(show: bool) { - let trace1 = Scatter::new(vec![1, 2, 3, 4, 5], vec![1, 3, 2, 3, 1]) - .mode(Mode::LinesMarkers) - .name("solid") - .line(Line::new().dash(DashType::Solid)); - let trace2 = Scatter::new(vec![1, 2, 3, 4, 5], vec![6, 8, 7, 8, 6]) - .mode(Mode::LinesMarkers) - .name("dashdot") - .line(Line::new().dash(DashType::DashDot)); - let trace3 = Scatter::new(vec![1, 2, 3, 4, 5], vec![11, 13, 12, 13, 11]) - .mode(Mode::LinesMarkers) - .name("dash") - .line(Line::new().dash(DashType::Dash)); - let trace4 = Scatter::new(vec![1, 2, 3, 4, 5], vec![16, 18, 17, 18, 16]) - .mode(Mode::LinesMarkers) - .name("dot") - .line(Line::new().dash(DashType::Dot)); - let trace5 = Scatter::new(vec![1, 2, 3, 4, 5], vec![21, 23, 22, 23, 21]) - .mode(Mode::LinesMarkers) - .name("longdash") - .line(Line::new().dash(DashType::LongDash)); - let trace6 = Scatter::new(vec![1, 2, 3, 4, 5], vec![26, 28, 27, 28, 26]) - .mode(Mode::LinesMarkers) - .name("longdashdot") - .line(Line::new().dash(DashType::LongDashDot)); +{{#include ../../../../../examples/basic_charts/out/line_shape_options_for_interpolation.html}} - let mut plot = Plot::new(); - let layout = Layout::new() - .legend( - Legend::new() - .y(0.5) - .trace_order("reversed") - .font(Font::new().size(16)), - ) - .x_axis(Axis::new().range(vec![0.95, 5.05]).auto_range(false)) - .y_axis(Axis::new().range(vec![0.0, 28.5]).auto_range(false)); - plot.set_layout(layout); - plot.add_trace(trace1); - plot.add_trace(trace2); - plot.add_trace(trace3); - plot.add_trace(trace4); - plot.add_trace(trace5); - plot.add_trace(trace6); - if show { - plot.show(); - } - println!("{}", plot.to_inline_html(Some("line_dash"))); -} +## Line Dash +```rust,no_run +{{#include ../../../../../examples/basic_charts/src/main.rs:line_dash}} ``` -
- +{{#include ../../../../../examples/basic_charts/out/line_dash.html}} ## Filled Lines -```rust -fn filled_lines(show: bool) { - let x1 = vec![ - 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 10.0, 9.0, 8.0, 7.0, 6.0, 5.0, 4.0, 3.0, - 2.0, 1.0, - ]; - let x2 = (1..=10).map(|iv| iv as f64).collect::>(); - let trace1 = Scatter::new( - x1.clone(), - vec![ - 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 9.0, 8.0, 7.0, 6.0, 5.0, 4.0, 3.0, - 2.0, 1.0, 0.0, - ], - ) - .fill(Fill::ToZeroX) - .fill_color(Rgba::new(0, 100, 80, 0.2)) - .line(Line::new().color(NamedColor::Transparent)) - .name("Fair") - .show_legend(false); - let trace2 = Scatter::new( - x1.clone(), - vec![ - 5.5, 3.0, 5.5, 8.0, 6.0, 3.0, 8.0, 5.0, 6.0, 5.5, 4.75, 5.0, 4.0, 7.0, 2.0, 4.0, 7.0, - 4.4, 2.0, 4.5, - ], - ) - .fill(Fill::ToZeroX) - .fill_color(Rgba::new(0, 176, 246, 0.2)) - .line(Line::new().color(NamedColor::Transparent)) - .name("Premium") - .show_legend(false); - let trace3 = Scatter::new( - x1.clone(), - vec![ - 11.0, 9.0, 7.0, 5.0, 3.0, 1.0, 3.0, 5.0, 3.0, 1.0, -1.0, 1.0, 3.0, 1.0, -0.5, 1.0, 3.0, - 5.0, 7.0, 9.0, - ], - ) - .fill(Fill::ToZeroX) - .fill_color(Rgba::new(231, 107, 243, 0.2)) - .line(Line::new().color(NamedColor::Transparent)) - .name("Fair") - .show_legend(false); - let trace4 = Scatter::new( - x2.clone(), - vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0], - ) - .line(Line::new().color(Rgb::new(0, 100, 80))) - .name("Fair"); - let trace5 = Scatter::new( - x2.clone(), - vec![5.0, 2.5, 5.0, 7.5, 5.0, 2.5, 7.5, 4.5, 5.5, 5.0], - ) - .line(Line::new().color(Rgb::new(0, 176, 246))) - .name("Premium"); - let trace6 = Scatter::new( - x2.clone(), - vec![10.0, 8.0, 6.0, 4.0, 2.0, 0.0, 2.0, 4.0, 2.0, 0.0], - ) - .line(Line::new().color(Rgb::new(231, 107, 243))) - .name("Ideal"); - - let layout = Layout::new() - .paper_background_color(Rgb::new(255, 255, 255)) - .plot_background_color(Rgb::new(229, 229, 229)) - .x_axis( - Axis::new() - .grid_color(Rgb::new(255, 255, 255)) - .range(vec![1.0, 10.0]) - .show_grid(true) - .show_line(false) - .show_tick_labels(true) - .tick_color(Rgb::new(127, 127, 127)) - .ticks(TicksDirection::Outside) - .zero_line(false), - ) - .y_axis( - Axis::new() - .grid_color(Rgb::new(255, 255, 255)) - .show_grid(true) - .show_line(false) - .show_tick_labels(true) - .tick_color(Rgb::new(127, 127, 127)) - .ticks(TicksDirection::Outside) - .zero_line(false), - ); - - let mut plot = Plot::new(); - plot.set_layout(layout); - plot.add_trace(trace1); - plot.add_trace(trace2); - plot.add_trace(trace3); - plot.add_trace(trace4); - plot.add_trace(trace5); - plot.add_trace(trace6); - if show { - plot.show(); - } - println!("{}", plot.to_inline_html(Some("filled_lines"))); -} +```rust,no_run +{{#include ../../../../../examples/basic_charts/src/main.rs:filled_lines}} ``` -
- \ No newline at end of file + +{{#include ../../../../../examples/basic_charts/out/filled_lines.html}} \ No newline at end of file diff --git a/docs/book/src/recipes/basic_charts/sankey_diagrams.md b/docs/book/src/recipes/basic_charts/sankey_diagrams.md index 538ca8eb..60955d33 100644 --- a/docs/book/src/recipes/basic_charts/sankey_diagrams.md +++ b/docs/book/src/recipes/basic_charts/sankey_diagrams.md @@ -2,8 +2,8 @@ The following imports have been used to produce the plots below: -```rust -use itertools_num::linspace; +```rust,no_run +use ndarray::Array; use plotly::common::{ ColorScale, ColorScalePalette, DashType, Fill, Font, Line, LineShape, Marker, Mode, Title, }; diff --git a/docs/book/src/recipes/basic_charts/scatter_plots.md b/docs/book/src/recipes/basic_charts/scatter_plots.md index 143561d3..14838d1d 100644 --- a/docs/book/src/recipes/basic_charts/scatter_plots.md +++ b/docs/book/src/recipes/basic_charts/scatter_plots.md @@ -2,8 +2,8 @@ The following imports have been used to produce the plots below: -```rust -use itertools_num::linspace; +```rust,no_run +use ndarray::Array; use plotly::common::{ ColorScale, ColorScalePalette, DashType, Fill, Font, Line, LineShape, Marker, Mode, Title, }; @@ -15,382 +15,55 @@ use rand_distr::{Distribution, Normal, Uniform}; The `to_inline_html` method is used to produce the html plot displayed in this page. ## Simple Scatter Plot -```rust -fn simple_scatter_plot(show: bool) { - let n: usize = 100; - let t: Vec = linspace(0., 10., n).collect(); - let y = t.iter().map(|x| x.sin()).collect(); - - let trace = Scatter::new(t, y).mode(Mode::Markers); - let mut plot = Plot::new(); - plot.add_trace(trace); - if show { - plot.show(); - } - println!("{}", plot.to_inline_html(Some("simple_scatter_plot"))); -} +```rust,no_run +{{#include ../../../../../examples/basic_charts/src/main.rs:simple_scatter_plot}} ``` -
- - -## Line and Scatter Plots -```rust -fn line_and_scatter_plots(show: bool) { - let n: usize = 100; - let mut rng = rand::thread_rng(); - let random_x: Vec = linspace(0., 1., n).collect(); - let random_y0: Vec = Normal::new(5., 1.) - .unwrap() - .sample_iter(rng) - .take(n) - .collect(); - let random_y1: Vec = Normal::new(0., 1.) - .unwrap() - .sample_iter(rng) - .take(n) - .collect(); - let random_y2: Vec = Normal::new(-5., 1.) - .unwrap() - .sample_iter(rng) - .take(n) - .collect(); +{{#include ../../../../../examples/basic_charts/out/simple_scatter_plot.html}} - let trace1 = Scatter::new(random_x.clone(), random_y0) - .mode(Mode::Markers) - .name("markers"); - let trace2 = Scatter::new(random_x.clone(), random_y1) - .mode(Mode::LinesMarkers) - .name("linex+markers"); - let trace3 = Scatter::new(random_x, random_y2) - .mode(Mode::Lines) - .name("lines"); - let mut plot = Plot::new(); - plot.add_trace(trace1); - plot.add_trace(trace2); - plot.add_trace(trace3); - if show { - plot.show(); - } - println!("{}", plot.to_inline_html(Some("line_and_scatter_plots"))); -} +## Line and Scatter Plots +```rust,no_run +{{#include ../../../../../examples/basic_charts/src/main.rs:line_and_scatter_plots}} ``` -
- + +{{#include ../../../../../examples/basic_charts/out/line_and_scatter_plots.html}} ## Bubble Scatter Plots -```rust -fn bubble_scatter_plots(show: bool) { - let trace1 = Scatter::new(vec![1, 2, 3, 4], vec![10, 11, 12, 13]) - .mode(Mode::Markers) - .marker( - Marker::new() - .size_array(vec![40, 60, 80, 100]) - .color_array(vec![ - NamedColor::Red, - NamedColor::Blue, - NamedColor::Cyan, - NamedColor::OrangeRed, - ]), - ); - let mut plot = Plot::new(); - plot.add_trace(trace1); - if show { - plot.show(); - } - println!("{}", plot.to_inline_html(Some("bubble_scatter_plots"))); -} +```rust,no_run +{{#include ../../../../../examples/basic_charts/src/main.rs:bubble_scatter_plots}} ``` -
- +{{#include ../../../../../examples/basic_charts/out/bubble_scatter_plots.html}} ## Data Labels Hover -```rust -fn data_labels_hover(show: bool) { - let trace1 = Scatter::new(vec![1, 2, 3, 4, 5], vec![1, 6, 3, 6, 1]) - .mode(Mode::Markers) - .name("Team A") - .marker(Marker::new().size(12)); - let trace2 = Scatter::new(vec![1.5, 2.5, 3.5, 4.5, 5.5], vec![4, 1, 7, 1, 4]) - .mode(Mode::Markers) - .name("Team B") - .marker(Marker::new().size(12)); - - let mut plot = Plot::new(); - plot.add_trace(trace1); - plot.add_trace(trace2); - - let layout = Layout::new() - .title(Title::with_text("Data Labels Hover")) - .x_axis(Axis::new().title(Title::with_text("x")).range(vec![0.75, 5.25])) - .y_axis(Axis::new().title(Title::with_text("y")).range(vec![0., 8.])); - plot.set_layout(layout); - if show { - plot.show(); - } - println!("{}", plot.to_inline_html(Some("data_labels_hover"))); -} +```rust,no_run +{{#include ../../../../../examples/basic_charts/src/main.rs:data_labels_hover}} ``` -
- +{{#include ../../../../../examples/basic_charts/out/data_labels_hover.html}} ## Data Labels on the Plot -```rust -fn data_labels_on_the_plot(show: bool) { - let trace1 = Scatter::new(vec![1, 2, 3, 4, 5], vec![1, 6, 3, 6, 1]) - .mode(Mode::Markers) - .name("Team A") - .marker(Marker::new().size(12)) - .text_array(vec!["A-1", "A-2", "A-3", "A-4", "A-5"]); - let trace2 = Scatter::new(vec![1.5, 2.5, 3.5, 4.5, 5.5], vec![4, 1, 7, 1, 4]) - .mode(Mode::Markers) - .name("Team B") - .text_array(vec!["B-a", "B-b", "B-c", "B-d", "B-e"]) - .marker(Marker::new().size(12)); - - let mut plot = Plot::new(); - plot.add_trace(trace1); - plot.add_trace(trace2); - - let layout = Layout::new() - .title(Title::with_text("Data Labels on the Plot")) - .x_axis(Axis::new().range(vec![0.75, 5.25])) - .y_axis(Axis::new().range(vec![0., 8.])); - plot.set_layout(layout); - if show { - plot.show(); - } - println!("{}", plot.to_inline_html(Some("data_labels_on_the_plot"))); -} - +```rust,no_run +{{#include ../../../../../examples/basic_charts/src/main.rs:data_labels_on_the_plot}} ``` -
- +{{#include ../../../../../examples/basic_charts/out/data_labels_on_the_plot.html}} ## Colored and Styled Scatter Plot -```rust -fn colored_and_styled_scatter_plot(show: bool) { - let trace1 = Scatter::new(vec![52698, 43117], vec![53, 31]) - .mode(Mode::Markers) - .name("North America") - .text_array(vec!["United States", "Canada"]) - .marker( - Marker::new() - .color(Rgb::new(164, 194, 244)) - .size(12) - .line(Line::new().color(NamedColor::White).width(0.5)), - ); - let trace2 = Scatter::new( - vec![ - 39317, 37236, 35650, 30066, 29570, 27159, 23557, 21046, 18007, - ], - vec![33, 20, 13, 19, 27, 19, 49, 44, 38], - ) - .mode(Mode::Markers) - .name("Europe") - .text_array(vec![ - "Germany", - "Britain", - "France", - "Spain", - "Italy", - "Czech Rep.", - "Greece", - "Poland", - ]) - .marker(Marker::new().color(Rgb::new(255, 217, 102)).size(12)); - let trace3 = Scatter::new( - vec![42952, 37037, 33106, 17478, 9813, 5253, 4692, 3899], - vec![23, 42, 54, 89, 14, 99, 93, 70], - ) - .mode(Mode::Markers) - .name("Asia/Pacific") - .text_array(vec![ - "Australia", - "Japan", - "South Korea", - "Malaysia", - "China", - "Indonesia", - "Philippines", - "India", - ]) - .marker(Marker::new().color(Rgb::new(234, 153, 153)).size(12)); - let trace4 = Scatter::new( - vec![19097, 18601, 15595, 13546, 12026, 7434, 5419], - vec![43, 47, 56, 80, 86, 93, 80], - ) - .mode(Mode::Markers) - .name("Latin America") - .text_array(vec![ - "Chile", - "Argentina", - "Mexico", - "Venezuela", - "Venezuela", - "El Salvador", - "Bolivia", - ]) - .marker(Marker::new().color(Rgb::new(142, 124, 195)).size(12)); - - let layout = Layout::new() - .title(Title::with_text("Quarter 1 Growth")) - .x_axis( - Axis::new() - .title(Title::with_text("GDP per Capita")) - .show_grid(false) - .zero_line(false), - ) - .y_axis(Axis::new().title(Title::with_text("Percent")).show_line(false)); - let mut plot = Plot::new(); - plot.add_trace(trace1); - plot.add_trace(trace2); - plot.add_trace(trace3); - plot.add_trace(trace4); - plot.set_layout(layout); - if show { - plot.show(); - } - println!( - "{}", - plot.to_inline_html(Some("colored_and_styled_scatter_plot")) - ); -} +```rust,no_run +{{#include ../../../../../examples/basic_charts/src/main.rs:colored_and_styled_scatter_plot}} ``` -
- +{{#include ../../../../../examples/basic_charts/out/colored_and_styled_scatter_plot.html}} ## Large Data Sets -```rust -fn large_data_sets(show: bool) { - let n: usize = 100_000; - let mut rng = rand::thread_rng(); - let r: Vec = Uniform::new(0., 1.).sample_iter(rng).take(n).collect(); - let theta: Vec = Normal::new(0., 2. * std::f64::consts::PI) - .unwrap() - .sample_iter(rng) - .take(n) - .collect(); - let colors: Vec = Normal::new(0., 1.) - .unwrap() - .sample_iter(rng) - .take(n) - .collect(); - - let x: Vec = r - .iter() - .zip(theta.iter()) - .map(|args| args.0 * args.1.cos()) - .collect(); - let y: Vec = r - .iter() - .zip(theta.iter()) - .map(|args| args.0 * args.1.sin()) - .collect(); - let trace = Scatter::new(x, y) - .open_gl_mode(true) - .mode(Mode::Markers) - .marker( - Marker::new() - .color_scale(ColorScale::Palette(ColorScalePalette::Viridis)) - .color_array(colors) - .line(Line::new().width(1.)), - ); - let mut plot = Plot::new(); - plot.add_trace(trace); - - if show { - plot.show(); - } - // Note the following will not show the full output of the `to_inline_html` method. - println!("{}", plot.to_inline_html(Some("large_data_sets"))); -} +```rust,no_run +{{#include ../../../../../examples/basic_charts/src/main.rs:large_data_sets}} ``` -
- \ No newline at end of file + +{{#include ../../../../../examples/basic_charts/out/large_data_sets.html}} \ No newline at end of file diff --git a/examples/.gitignore b/examples/.gitignore new file mode 100644 index 00000000..466e2480 --- /dev/null +++ b/examples/.gitignore @@ -0,0 +1 @@ +out/ \ No newline at end of file diff --git a/examples/basic_charts/src/main.rs b/examples/basic_charts/src/main.rs index aa092068..f66f5285 100644 --- a/examples/basic_charts/src/main.rs +++ b/examples/basic_charts/src/main.rs @@ -15,7 +15,8 @@ use plotly::{ use rand_distr::{Distribution, Normal, Uniform}; // Scatter Plots -fn simple_scatter_plot() { +// ANCHOR: simple_scatter_plot +fn simple_scatter_plot(show: bool) -> Plot { let n: usize = 100; let t: Vec = Array::linspace(0., 10., n).into_raw_vec_and_offset().0; let y: Vec = t.iter().map(|x| x.sin()).collect(); @@ -24,10 +25,15 @@ fn simple_scatter_plot() { let mut plot = Plot::new(); plot.add_trace(trace); - plot.show(); + if show { + plot.show(); + } + plot } +// ANCHOR_END: simple_scatter_plot -fn line_and_scatter_plots() { +// ANCHOR: line_and_scatter_plots +fn line_and_scatter_plots(show: bool) -> Plot { let n: usize = 100; let mut rng = rand::thread_rng(); let random_x: Vec = Array::linspace(0., 1., n).into_raw_vec_and_offset().0; @@ -62,10 +68,15 @@ fn line_and_scatter_plots() { plot.add_trace(trace2); plot.add_trace(trace3); - plot.show(); + if show { + plot.show(); + } + plot } +// ANCHOR_END: line_and_scatter_plots -fn bubble_scatter_plots() { +// ANCHOR: bubble_scatter_plots +fn bubble_scatter_plots(show: bool) -> Plot { let trace1 = Scatter::new(vec![1, 2, 3, 4], vec![10, 11, 12, 13]) .mode(Mode::Markers) .marker( @@ -81,10 +92,14 @@ fn bubble_scatter_plots() { let mut plot = Plot::new(); plot.add_trace(trace1); - plot.show(); + if show { + plot.show(); + } + plot } +// ANCHOR_END: bubble_scatter_plots -fn polar_scatter_plot() { +fn polar_scatter_plot(show: bool) -> Plot { let n: usize = 400; let theta: Vec = Array::linspace(0., 360., n).into_raw_vec_and_offset().0; let r: Vec = theta @@ -100,10 +115,14 @@ fn polar_scatter_plot() { let mut plot = Plot::new(); plot.add_trace(trace); - plot.show(); + if show { + plot.show(); + } + plot } -fn data_labels_hover() { +// ANCHOR: data_labels_hover +fn data_labels_hover(show: bool) -> Plot { let trace1 = Scatter::new(vec![1, 2, 3, 4, 5], vec![1, 6, 3, 6, 1]) .mode(Mode::Markers) .name("Team A") @@ -123,10 +142,15 @@ fn data_labels_hover() { .y_axis(Axis::new().title("y").range(vec![0., 8.])); plot.set_layout(layout); - plot.show(); + if show { + plot.show(); + } + plot } +// ANCHOR_END: data_labels_hover -fn data_labels_on_the_plot() { +// ANCHOR: data_labels_on_the_plot +fn data_labels_on_the_plot(show: bool) -> Plot { let trace1 = Scatter::new(vec![1, 2, 3, 4, 5], vec![1, 6, 3, 6, 1]) .mode(Mode::Markers) .name("Team A") @@ -148,10 +172,15 @@ fn data_labels_on_the_plot() { .y_axis(Axis::new().range(vec![0., 8.])); plot.set_layout(layout); - plot.show(); + if show { + plot.show(); + } + plot } +// ANCHOR_END: data_labels_on_the_plot -fn colored_and_styled_scatter_plot() { +// ANCHOR: colored_and_styled_scatter_plot +fn colored_and_styled_scatter_plot(show: bool) -> Plot { let trace1 = Scatter::new(vec![52698, 43117], vec![53, 31]) .mode(Mode::Markers) .name("North America") @@ -231,10 +260,15 @@ fn colored_and_styled_scatter_plot() { plot.add_trace(trace4); plot.set_layout(layout); - plot.show(); + if show { + plot.show(); + } + plot } +// ANCHOR_END: colored_and_styled_scatter_plot -fn large_data_sets() { +// ANCHOR: large_data_sets +fn large_data_sets(show: bool) -> Plot { let n: usize = 100_000; let mut rng = rand::thread_rng(); let r: Vec = Uniform::new(0., 1.).sample_iter(&mut rng).take(n).collect(); @@ -265,11 +299,16 @@ fn large_data_sets() { let mut plot = Plot::new(); plot.add_trace(trace); - plot.show(); + if show { + plot.show(); + } + plot } +// ANCHOR_END: large_data_sets // Line Charts -fn adding_names_to_line_and_scatter_plot() { +// ANCHOR: adding_names_to_line_and_scatter_plot +fn adding_names_to_line_and_scatter_plot(show: bool) -> Plot { let trace1 = Scatter::new(vec![1, 2, 3, 4], vec![10, 15, 13, 17]) .mode(Mode::Markers) .name("Scatter"); @@ -287,10 +326,15 @@ fn adding_names_to_line_and_scatter_plot() { plot.add_trace(trace3); plot.set_layout(layout); - plot.show(); + if show { + plot.show(); + } + plot } +// ANCHOR_END: adding_names_to_line_and_scatter_plot -fn line_and_scatter_styling() { +// ANCHOR: line_and_scatter_styling +fn line_and_scatter_styling(show: bool) -> Plot { let trace1 = Scatter::new(vec![1, 2, 3, 4], vec![10, 15, 13, 17]) .mode(Mode::Markers) .name("trace1") @@ -312,10 +356,15 @@ fn line_and_scatter_styling() { plot.add_trace(trace3); plot.set_layout(layout); - plot.show(); + if show { + plot.show(); + } + plot } +// ANCHOR_END: line_and_scatter_styling -fn styling_line_plot() { +// ANCHOR: styling_line_plot +fn styling_line_plot(show: bool) -> Plot { let trace1 = Scatter::new(vec![1, 2, 3, 4], vec![10, 15, 13, 17]) .mode(Mode::Markers) .name("Red") @@ -334,10 +383,15 @@ fn styling_line_plot() { plot.add_trace(trace2); plot.set_layout(layout); - plot.show(); + if show { + plot.show(); + } + plot } +// ANCHOR_END: styling_line_plot -fn line_shape_options_for_interpolation() { +// ANCHOR: line_shape_options_for_interpolation +fn line_shape_options_for_interpolation(show: bool) -> Plot { let trace1 = Scatter::new(vec![1, 2, 3, 4, 5], vec![1, 3, 2, 3, 1]) .mode(Mode::LinesMarkers) .name("linear") @@ -378,10 +432,15 @@ fn line_shape_options_for_interpolation() { plot.add_trace(trace5); plot.add_trace(trace6); - plot.show(); + if show { + plot.show(); + } + plot } +// ANCHOR_END: line_shape_options_for_interpolation -fn line_dash() { +// ANCHOR: line_dash +fn line_dash(show: bool) -> Plot { let trace1 = Scatter::new(vec![1, 2, 3, 4, 5], vec![1, 3, 2, 3, 1]) .mode(Mode::LinesMarkers) .name("solid") @@ -425,10 +484,15 @@ fn line_dash() { plot.add_trace(trace5); plot.add_trace(trace6); - plot.show(); + if show { + plot.show(); + } + plot } +// ANCHOR_END: line_dash -fn filled_lines() { +// ANCHOR: filled_lines +fn filled_lines(show: bool) -> Plot { let x1 = vec![ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 10.0, 9.0, 8.0, 7.0, 6.0, 5.0, 4.0, 3.0, 2.0, 1.0, @@ -520,11 +584,16 @@ fn filled_lines() { plot.add_trace(trace5); plot.add_trace(trace6); - plot.show(); + if show { + plot.show(); + } + plot } +// ANCHOR_END: filled_lines /// Scatter plot showing y axis categories and category ordering. -fn categories_scatter_chart() { +// ANCHOR: categories_scatter_chart +fn categories_scatter_chart(show: bool) -> Plot { // Categories are ordered on the y axis from bottom to top. let categories = vec!["Unknown", "Off", "On"]; @@ -563,20 +632,30 @@ fn categories_scatter_chart() { plot.set_layout(layout); - plot.show(); + if show { + plot.show(); + } + plot } +// ANCHOR_END: categories_scatter_chart // Bar Charts -fn basic_bar_chart() { +// ANCHOR: basic_bar_chart +fn basic_bar_chart(show: bool) -> Plot { let animals = vec!["giraffes", "orangutans", "monkeys"]; let t = Bar::new(animals, vec![20, 14, 23]); let mut plot = Plot::new(); plot.add_trace(t); - plot.show(); + if show { + plot.show(); + } + plot } +// ANCHOR_END: basic_bar_chart -fn grouped_bar_chart() { +// ANCHOR: grouped_bar_chart +fn grouped_bar_chart(show: bool) -> Plot { let animals1 = vec!["giraffes", "orangutans", "monkeys"]; let trace1 = Bar::new(animals1, vec![20, 14, 23]).name("SF Zoo"); @@ -590,10 +669,15 @@ fn grouped_bar_chart() { plot.add_trace(trace2); plot.set_layout(layout); - plot.show(); + if show { + plot.show(); + } + plot } +// ANCHOR_END: grouped_bar_chart -fn stacked_bar_chart() { +// ANCHOR: stacked_bar_chart +fn stacked_bar_chart(show: bool) -> Plot { let animals1 = vec!["giraffes", "orangutans", "monkeys"]; let trace1 = Bar::new(animals1, vec![20, 14, 23]).name("SF Zoo"); @@ -607,12 +691,17 @@ fn stacked_bar_chart() { plot.add_trace(trace2); plot.set_layout(layout); - plot.show(); + if show { + plot.show(); + } + plot } +// ANCHOR_END: stacked_bar_chart /// Graph a bar chart that orders the x axis categories by the total number /// of animals in each category. -fn category_order_bar_chart() { +// ANCHOR: category_order_bar_chart +fn category_order_bar_chart(show: bool) -> Plot { let animals1 = vec!["giraffes", "orangutans", "monkeys"]; let trace1 = Bar::new(animals1, vec![10, 14, 23]).name("SF Zoo"); @@ -630,10 +719,15 @@ fn category_order_bar_chart() { plot.add_trace(trace2); plot.set_layout(layout); - plot.show(); + if show { + plot.show(); + } + plot } +// ANCHOR_END: category_order_bar_chart -fn bar_chart_with_pattern_fills() { +// ANCHOR: bar_chart_with_pattern_fills +fn bar_chart_with_pattern_fills(show: bool) -> Plot { let animals1 = vec!["giraffes", "orangutans", "monkeys"]; let trace1 = Bar::new(animals1, vec![20, 14, 23]).name("SF Zoo").marker( Marker::new().line(Line::new().width(1.0)).pattern( @@ -659,11 +753,16 @@ fn bar_chart_with_pattern_fills() { plot.add_trace(trace2); plot.set_layout(layout); - plot.show(); + if show { + plot.show(); + } + plot } +// ANCHOR_END: bar_chart_with_pattern_fills // Sankey Diagrams -fn basic_sankey_diagram() { +// ANCHOR: basic_sankey_diagram +fn basic_sankey_diagram(show: bool) -> Plot { // https://plotly.com/javascript/sankey-diagram/#basic-sankey-diagram let trace = Sankey::new() .orientation(Orientation::Horizontal) @@ -697,49 +796,77 @@ fn basic_sankey_diagram() { plot.add_trace(trace); plot.set_layout(layout); - plot.show(); + if show { + plot.show(); + } + plot } +// ANCHOR_END: basic_sankey_diagram -fn table_chart() { +// ANCHOR: table_chart +fn table_chart(show: bool) -> Plot { let trace = Table::new( Header::new(vec![String::from("col1"), String::from("col2")]), Cells::new(vec![vec![1, 2], vec![2, 3]]), ); let mut plot = Plot::new(); plot.add_trace(trace); - plot.show(); + + if show { + plot.show(); + } + plot +} +// ANCHOR_END: table_chart + +fn write_example_to_html(plot: Plot, name: &str) { + std::fs::create_dir_all("./out").unwrap(); + let html = plot.to_inline_html(Some(name)); + std::fs::write(format!("./out/{}.html", name), html).unwrap(); } fn main() { - // Uncomment any of these lines to display the example. + // Change false to true on any of these lines to display the example. // Scatter Plots - // simple_scatter_plot(); - // line_and_scatter_plots(); - // bubble_scatter_plots(); - // polar_scatter_plot(); - // data_labels_hover(); - // data_labels_on_the_plot(); - // colored_and_styled_scatter_plot(); - // large_data_sets(); - // categories_scatter_chart(); + write_example_to_html(simple_scatter_plot(false), "simple_scatter_plot"); + write_example_to_html(line_and_scatter_plots(false), "line_and_scatter_plots"); + write_example_to_html(bubble_scatter_plots(false), "bubble_scatter_plots"); + write_example_to_html(polar_scatter_plot(false), "polar_scatter_plot"); + write_example_to_html(data_labels_hover(false), "data_labels_hover"); + write_example_to_html(data_labels_on_the_plot(false), "data_labels_on_the_plot"); + write_example_to_html( + colored_and_styled_scatter_plot(false), + "colored_and_styled_scatter_plot", + ); + write_example_to_html(large_data_sets(false), "large_data_sets"); + write_example_to_html(categories_scatter_chart(false), "categories_scatter_chart"); // Line Charts - // adding_names_to_line_and_scatter_plot(); - // line_and_scatter_styling(); - // styling_line_plot(); - // line_shape_options_for_interpolation(); - // line_dash(); - // filled_lines(); + write_example_to_html( + adding_names_to_line_and_scatter_plot(false), + "adding_names_to_line_and_scatter_plot", + ); + write_example_to_html(line_and_scatter_styling(false), "line_and_scatter_styling"); + write_example_to_html(styling_line_plot(false), "styling_line_plot"); + write_example_to_html( + line_shape_options_for_interpolation(false), + "line_shape_options_for_interpolation", + ); + write_example_to_html(line_dash(false), "line_dash"); + write_example_to_html(filled_lines(false), "filled_lines"); // Bar Charts - // basic_bar_chart(); - // grouped_bar_chart(); - // stacked_bar_chart(); - // table_chart(); - // category_order_bar_chart(); - // bar_chart_with_pattern_fills(); + write_example_to_html(basic_bar_chart(false), "basic_bar_chart"); + write_example_to_html(grouped_bar_chart(false), "grouped_bar_chart"); + write_example_to_html(stacked_bar_chart(false), "stacked_bar_chart"); + write_example_to_html(table_chart(false), "table_chart"); + write_example_to_html(category_order_bar_chart(false), "category_order_bar_chart"); + write_example_to_html( + bar_chart_with_pattern_fills(false), + "bar_chart_with_pattern_fills", + ); // Sankey Diagrams - // basic_sankey_diagram(); + write_example_to_html(basic_sankey_diagram(false), "basic_sankey_diagram"); }