Skip to content

Commit c0ca691

Browse files
authored
Merge pull request plotly#102 from igiagkiozis/dev
v0.8.1
2 parents 156a787 + c55a7db commit c0ca691

27 files changed

+1684
-4887
lines changed

CHANGELOG.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7-
## [0.8.1] - 2022-XX-XX
7+
## [0.8.1] - 2022-09-25
8+
### Added
9+
- Button support (i.e. [updatemenus](https://plotly.com/javascript/reference/layout/updatemenus/)) contibuted by [@sreenathkrishnan](https://github.com/sreenathkrishnan). Details and examples in this well written PR [#99](https://github.com/igiagkiozis/plotly/pull/99).
10+
- Internally, there is now a `plotly-derive` crate which defines a `FieldSetter` procedural macro. This massively cuts down the amount of code duplication by generating the setter methods based on the struct fields. Again thanks to @sreenathkrishnan for this effort.
811

912
## [0.8.0] - 2022-08-26
1013
Version 0.8.0 represents a significant release which refactors a lot of the codebase and tries to provide a cleaner API: there are several breaking changes listed below. On the whole, if migrating from v0.7.0, start by following any new compiler errors and, if you're still stuck, open an issue on the issue tracker and we can help out.

Cargo.toml

+1
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
]

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ Add this to your `Cargo.toml`:
3939

4040
```toml
4141
[dependencies]
42-
plotly = "0.8.0"
42+
plotly = "0.8.1"
4343
```
4444

4545
Documentation is available in the [Plotly.rs Book](https://igiagkiozis.github.io/plotly/content/getting_started.html) and [Recipes](https://igiagkiozis.github.io/plotly/content/recipes.html).
@@ -63,7 +63,7 @@ Saving to png, jpeg, webp, svg, pdf and eps formats can be made available by ena
6363

6464
```toml
6565
[dependencies]
66-
plotly = { version = "0.8.0", features = ["kaleido"] }
66+
plotly = { version = "0.8.1", features = ["kaleido"] }
6767
```
6868
For further details please see [plotly_kaleido](https://github.com/igiagkiozis/plotly/tree/master/plotly_kaleido).
6969

docs/book/src/getting_started.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ To start using [plotly.rs](https://github.com/igiagkiozis/plotly) in your projec
2222

2323
```toml
2424
[dependencies]
25-
plotly = "0.8.0"
25+
plotly = "0.8.1"
2626
```
2727

2828
[Plotly.rs](https://github.com/igiagkiozis/plotly) is ultimately a thin wrapper around the `plotly.js` library. The main job of this library is to provide `structs` and `enums` which get serialized to `json` and passed to the `plotly.js` library to actually do the heavy lifting. As such, if you are familiar with `plotly.js` or its derivatives (e.g. the equivalent Python library), then you should find [`plotly.rs`](https://github.com/igiagkiozis/plotly) intuitive to use.
@@ -97,7 +97,7 @@ To add the ability to save plots in the following formats: png, jpeg, webp, svg,
9797

9898
```toml
9999
[dependencies]
100-
plotly = { version = "0.8.0", features = ["kaleido"] }
100+
plotly = { version = "0.8.1", features = ["kaleido"] }
101101
```
102102

103103
## WebAssembly Support

plotly/Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "plotly"
3-
version = "0.8.0"
3+
version = "0.8.1"
44
description = "A plotting library powered by Plotly.js"
55
authors = ["Ioannis Giagkiozis <[email protected]>"]
66
license = "MIT"
@@ -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 = { version = "0.8.1", path = "../plotly_derive" }
3031
ndarray = { version = "0.15.4", optional = true }
3132
once_cell = "1"
3233
serde = { version = "1.0.132", features = ["derive"] }

plotly/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ Add this to your `Cargo.toml`:
3939

4040
```toml
4141
[dependencies]
42-
plotly = "0.8.0"
42+
plotly = "0.8.1"
4343
```
4444

4545
For changes since the last version please consult the [change log](https://github.com/igiagkiozis/plotly/blob/master/CHANGELOG.md).
@@ -61,7 +61,7 @@ Saving to png, jpeg, webp, svg, pdf and eps formats can be made available by ena
6161

6262
```toml
6363
[dependencies]
64-
plotly = { version = "0.8.0", features = ["kaleido"] }
64+
plotly = { version = "0.8.1", features = ["kaleido"] }
6565
```
6666
For further details please see [plotly_kaleido](https://github.com/igiagkiozis/plotly/tree/master/plotly_kaleido).
6767

plotly/examples/buttons.rs

+117
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

+6
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)