Skip to content

Commit 6053699

Browse files
Button support and FieldSetter derive macro (#99)
* FieldSetter proc macro * Unit Tests * Remove unused trait * Fix compilation with all features turned on
1 parent 156a787 commit 6053699

File tree

22 files changed

+1666
-4879
lines changed

22 files changed

+1666
-4879
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
[workspace]
22
members = [
33
"plotly",
4+
"plotly_derive",
45
"plotly_kaleido"
56
]

plotly/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ erased-serde = "0.3"
2727
getrandom = { version = "0.2", features = ["js"], optional = true }
2828
js-sys = { version = "0.3", optional = true }
2929
plotly_kaleido = { version = "0.3.0", path = "../plotly_kaleido", optional = true }
30+
plotly_derive = { path = "../plotly_derive" }
3031
ndarray = { version = "0.15.4", optional = true }
3132
once_cell = "1"
3233
serde = { version = "1.0.132", features = ["derive"] }

plotly/examples/buttons.rs

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
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+
}

plotly/src/common/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,12 @@ pub enum ColorScale {
472472
Vector(Vec<ColorScaleElement>),
473473
}
474474

475+
impl From<ColorScalePalette> for ColorScale {
476+
fn from(src: ColorScalePalette) -> Self {
477+
ColorScale::Palette(src)
478+
}
479+
}
480+
475481
#[derive(Serialize, Clone, Debug)]
476482
#[serde(rename_all = "lowercase")]
477483
pub enum LineShape {

0 commit comments

Comments
 (0)