|
| 1 | +use itertools::Itertools; |
| 2 | +use plotly::{ |
| 3 | + common::{Anchor, ColorScalePalette, Visible}, |
| 4 | + layout::{ |
| 5 | + update_menu::{ButtonBuilder, UpdateMenu, UpdateMenuDirection, UpdateMenuType}, |
| 6 | + BarMode, |
| 7 | + }, |
| 8 | + Bar, HeatMap, Layout, Plot, |
| 9 | +}; |
| 10 | + |
| 11 | +// Dropdown to alternate between two bar plots |
| 12 | +fn bar_plot_visible_dropdown(show: bool) { |
| 13 | + type BarType = Bar<&'static str, i32>; |
| 14 | + let mut plot = Plot::new(); |
| 15 | + plot.add_trace( |
| 16 | + BarType::new(vec!["giraffes", "orangutans", "monkeys"], vec![20, 14, 23]).name("Animals"), |
| 17 | + ); |
| 18 | + plot.add_trace( |
| 19 | + BarType::new( |
| 20 | + vec!["parrot", "chicken", "bluebird", "own"], |
| 21 | + vec![8, 23, 17, 2], |
| 22 | + ) |
| 23 | + .name("Birds") |
| 24 | + .visible(Visible::False), |
| 25 | + ); |
| 26 | + let buttons = vec![ |
| 27 | + ButtonBuilder::new() |
| 28 | + .label("Animals") |
| 29 | + .push_restyle(BarType::modify_visible(vec![Visible::True, Visible::False])) |
| 30 | + .build(), |
| 31 | + ButtonBuilder::new() |
| 32 | + .label("Birds") |
| 33 | + .push_restyle(BarType::modify_visible(vec![Visible::False, Visible::True])) |
| 34 | + .build(), |
| 35 | + ]; |
| 36 | + plot.set_layout(Layout::new().update_menus(vec![UpdateMenu::new().y(0.8).buttons(buttons)])); |
| 37 | + if show { |
| 38 | + plot.show(); |
| 39 | + } |
| 40 | + println!("{}", plot.to_inline_html(Some("bar_plot_visible_dropdown"))); |
| 41 | +} |
| 42 | + |
| 43 | +// Heatmap with buttons to choose colorscale |
| 44 | +fn basic_heat_map(show: bool) { |
| 45 | + type HeatMapType = HeatMap<f64, f64, Vec<f64>>; |
| 46 | + let gauss = |v: i32| (-v as f64 * v as f64 / 200.0).exp(); |
| 47 | + let z = (-30..30) |
| 48 | + .map(|x| (-30..30).map(|y| gauss(x) * gauss(y)).collect_vec()) |
| 49 | + .collect_vec(); |
| 50 | + let trace = HeatMapType::new_z(z).color_scale(ColorScalePalette::Viridis.into()); |
| 51 | + let mut plot = Plot::new(); |
| 52 | + plot.add_trace(trace); |
| 53 | + |
| 54 | + let buttons = IntoIterator::into_iter([ |
| 55 | + ("Viridis", ColorScalePalette::Viridis), |
| 56 | + ("Portland", ColorScalePalette::Portland), |
| 57 | + ("Blackbody", ColorScalePalette::Blackbody), |
| 58 | + ]) |
| 59 | + .map(|(label, palette)| { |
| 60 | + ButtonBuilder::new() |
| 61 | + .label(label) |
| 62 | + .push_restyle(HeatMapType::modify_all_color_scale(palette.into())) |
| 63 | + .build() |
| 64 | + }) |
| 65 | + .collect_vec(); |
| 66 | + |
| 67 | + plot.set_layout(Layout::new().update_menus(vec![UpdateMenu::new() |
| 68 | + .ty(UpdateMenuType::Buttons) |
| 69 | + .y(0.8) |
| 70 | + .buttons(buttons)])); |
| 71 | + |
| 72 | + if show { |
| 73 | + plot.show(); |
| 74 | + } |
| 75 | + println!("{}", plot.to_inline_html(Some("basic_heat_map"))); |
| 76 | +} |
| 77 | + |
| 78 | +// Button to change barmode |
| 79 | +fn bar_plot_relayout(show: bool) { |
| 80 | + type BarType = Bar<&'static str, i32>; |
| 81 | + let mut plot = Plot::new(); |
| 82 | + plot.add_trace( |
| 83 | + BarType::new(vec!["giraffes", "orangutans", "monkeys"], vec![20, 14, 23]).name("Africa"), |
| 84 | + ); |
| 85 | + plot.add_trace( |
| 86 | + BarType::new(vec!["giraffes", "orangutans", "monkeys"], vec![30, 8, 15]).name("Australia"), |
| 87 | + ); |
| 88 | + let buttons = vec![("Group", BarMode::Group), ("Stack", BarMode::Stack)] |
| 89 | + .into_iter() |
| 90 | + .map(|(label, bar_mode)| { |
| 91 | + ButtonBuilder::new() |
| 92 | + .label(label) |
| 93 | + .push_relayout(Layout::modify_bar_mode(bar_mode)) |
| 94 | + .build() |
| 95 | + }) |
| 96 | + .collect_vec(); |
| 97 | + |
| 98 | + plot.set_layout(Layout::new().update_menus(vec![UpdateMenu::new() |
| 99 | + .x(0.1) |
| 100 | + .x_anchor(Anchor::Left) |
| 101 | + .y(1.2) |
| 102 | + .y_anchor(Anchor::Top) |
| 103 | + .ty(UpdateMenuType::Buttons) |
| 104 | + .direction(UpdateMenuDirection::Right) |
| 105 | + .buttons(buttons)])); |
| 106 | + if show { |
| 107 | + plot.show(); |
| 108 | + } |
| 109 | + println!("{}", plot.to_inline_html(Some("bar_plot_relayout"))); |
| 110 | +} |
| 111 | + |
| 112 | +fn main() -> std::io::Result<()> { |
| 113 | + bar_plot_visible_dropdown(true); |
| 114 | + basic_heat_map(true); |
| 115 | + bar_plot_relayout(true); |
| 116 | + Ok(()) |
| 117 | +} |
0 commit comments