Skip to content

Commit 5fd4401

Browse files
authored
Align API for saving/showing plots more closely with plotly.py (#89)
* reconfigure API for saving/showing plots * update changelog * update book introduction with new api
1 parent 8c8f9f7 commit 5fd4401

File tree

11 files changed

+280
-313
lines changed

11 files changed

+280
-313
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ Cargo.lock
44
gh-pages/
55
Untitled*
66
.ipynb_checkpoints/
7-
.DS_Store
7+
.DS_Store
8+
.vscode

CHANGELOG.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ 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

77
## [0.8.0] - 2022-xx-xx
8+
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.
89
### Added
910
- impl `Clone`, `Serialize` and `PartialEq` for `Plot`
1011
- impl `Clone` +/- `Copy` for color types
@@ -18,6 +19,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1819
- Improve implementation of `private::NumOrString` to support more primitive types ([Issue
1920
#47](https://github.com/igiagkiozis/plotly/issues/47))
2021
- Remove `private::TruthyEnum` in favour of a more robust way of serializing to `String` or `bool`
22+
- Refactor `Color` module
23+
- Refactored HTML templates with cleaner Javascript code
24+
- `Plot::save()` is renamed to `Plot::write_image()`
25+
- `Plot::show_{png | jpeg}` is now made generic via the new `Plot::show_image()` across static image formats with the `ImageFormat` enum
26+
- `Plot::write_html()` now takes a filepath and saves the plot to that location
27+
- `Plot::to_html()` now has similar behaviour to `Plot::to_inline_html()` and just returns a `String`
2128
### Fixed
2229
- Typos in `CONTRIBUTING.md`
2330
- Serialization of `plotly_kaleido::PlotData` ([Issue #50](https://github.com/igiagkiozis/plotly/issues/50))
@@ -26,8 +33,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2633
- `serde` to `1.0.132`.
2734
- `serde_json` to `1.0.73`.
2835
- `askama` to `0.11.0`.
29-
- `rand` to `0.8.4`.
30-
- `rand_distr` to `0.4.2`.
36+
- `rand` to `0.8`.
37+
- `rand_distr` to `0.4`.
3138
- `plotly.js` to `2.12.1`
3239

3340
## [0.7.0] - 2022-01-01

docs/book/src/getting_started.md

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,38 +18,28 @@
1818

1919
# Getting Started
2020

21-
To start using [Plotly.rs](https://github.com/igiagkiozis/plotly) in your project add the following to your `Cargo.toml`:
21+
To start using [plotly.rs](https://github.com/igiagkiozis/plotly) in your project add the following to your `Cargo.toml`:
2222

2323
```toml
2424
[dependencies]
2525
plotly = "0.7.0"
2626
```
2727

28-
To add the ability to save plots in the following formats: png, jpeg, webp, svg, pdf and eps, you can use the `kaleido` feature. This feature depends on [plotly/Kaleido](https://github.com/plotly/Kaleido); a cross-platform library for generating static images. All the necessary binaries have been included with `plotly_kaleido` for `linux`, `windows` and `macos`. Previous versions of [Plotly.rs](https://github.com/igiagkiozis/plotly) used the `orca` feature; however this has been deprecated as it provided the same functionality but required additional installation steps. To enable the `kaleido` feature add the following to your `Cargo.toml` instead:
28+
[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.
2929

30-
```toml
31-
[dependencies]
32-
plotly = { version = "0.7.0", features = ["kaleido"] }
33-
```
30+
A `Plot` struct contains one or more `Trace` objects which describe the structure of data to be displayed. Optional `Layout` and `Configuration` structs can be used to specify the layout and config of the plot, respectively.
3431

35-
Plotly Kaleido is an open source project
32+
The builder pattern is used extensively throughout the library, which means you only need to specify the attributes and details you desire. Any attributes that are not set will fall back to the default value used by `plotly.js`.
3633

37-
38-
[Plotly.rs](https://github.com/igiagkiozis/plotly) has three main components:
39-
40-
- Traces; these are containers for the data for display,
41-
- Layout, fine tunes the display of traces on the canvas and more generally controls the way that the plot is displayed, and,
42-
- Plot; is the component that brings traces and the layout together to display the plot in either html format or rasterise the resulting view.
43-
44-
All available traces (e.g. `Scatter`, `Bar`, `Histogram` etc), the `Layout` and `Plot` have been hoisted in the `plotly` namespace so that they can be imported simply using the following:
34+
All available traces (e.g. `Scatter`, `Bar`, `Histogram`, etc), the `Layout`, `Configuration` and `Plot` have been hoisted in the `plotly` namespace so that they can be imported simply using the following:
4535

4636
```rust
4737
use plotly::{Plot, Layout, Scatter};
4838
```
4939

5040
The aforementioned components can be combined to produce as simple plot as follows:
41+
5142
```rust
52-
extern crate plotly;
5343
use plotly::common::Mode;
5444
use plotly::{Plot, Scatter};
5545

@@ -75,26 +65,37 @@ fn main() -> std::io::Result<()> {
7565
}
7666
```
7767

78-
which results in the following figure:
68+
which results in the following figure (displayed here as a static png file):
7969

8070
![line_and_scatter_plot](img/line_and_scatter_plot.png)
8171

82-
The above code will generate an html page of the `Plot` and display it in the default browser. The `html` for the plot is stored in the platform specific temporary directory. To save the `html` result the following can be used:
72+
The above code will generate an interactive `html` page of the `Plot` and display it in the default browser. The `html` for the plot is stored in the platform specific temporary directory. To save the `html` result, you can do so quite simply:
8373

8474
```rust
85-
plot.to_html("/home/user/line_and_scatter_plot.html");
75+
plot.write_html("/home/user/line_and_scatter_plot.html");
8676
```
8777

88-
It is often the case that plots are produced to be included in a document and a different format for the plot is desirable (e.g. png, jpeg etc). Given that the `html` version of the plot is composed of vector graphics, the display when converted to a non-vector format (e.g. png) is not guaranteed to be identical to the one displayed in `html`. This means that some fine tuning may be required to get to the desired output. To support that iterative workflow the `Plot` has `show_*` methods which display the rasterised output to the target format, for example this:
78+
It is often the case that plots are produced to be included in a document and a different format for the plot is desirable (e.g. png, jpeg, etc). Given that the `html` version of the plot is composed of vector graphics, the display when converted to a non-vector format (e.g. png) is not guaranteed to be identical to the one displayed in `html`. This means that some fine tuning may be required to get to the desired output. To support that iterative workflow, `Plot` has a `show_image()` method which will display the rasterised output to the target format, for example:
8979

9080
```rust
91-
plot.show_png(1280, 900);
81+
plot.show_image(ImageFormat::PNG, 1280, 900);
9282
```
9383

94-
will display in the browser the rasterised plot; 1280 pixels wide and 900 pixels tall, in png format. Once a satisfactory result is achieved, and assuming the `kaleido` feature is enabled, the plot can be saved using the following:
84+
will display in the browser the rasterised plot; 1280 pixels wide and 900 pixels tall, in png format.
85+
86+
Once a satisfactory result is achieved, and assuming the [`kaleido`](getting_started#saving-plots) feature is enabled, the plot can be saved using the following:
9587

9688
```rust
97-
plot.save("/home/user/plot_name.ext", ImageFormat::PNG, 1280, 900, 1.0);
89+
plot.write_image("/home/user/plot_name.ext", ImageFormat::PNG, 1280, 900, 1.0);
9890
```
9991

100-
The extension in the file-name path is optional as the appropriate extension (`ImageFormat::PNG`) will be included. Note that in all functions that save files to disk both relative and absolute paths are supported.
92+
The extension in the file-name path is optional as the appropriate extension (`ImageFormat::PNG`) will be included. Note that in all functions that save files to disk, both relative and absolute paths are supported.
93+
94+
## Saving Plots
95+
96+
To add the ability to save plots in the following formats: png, jpeg, webp, svg, pdf and eps, you can use the `kaleido` feature. This feature depends on [plotly/Kaleido](https://github.com/plotly/Kaleido): a cross-platform open source library for generating static images. All the necessary binaries have been included with `plotly_kaleido` for `Linux`, `Windows` and `MacOS`. Previous versions of [plotly.rs](https://github.com/igiagkiozis/plotly) used the `orca` feature, however, this has been deprecated as it provided the same functionality but required additional installation steps. To enable the `kaleido` feature add the following to your `Cargo.toml`:
97+
98+
```toml
99+
[dependencies]
100+
plotly = { version = "0.7.0", features = ["kaleido"] }
101+
```

docs/book/src/plotly_rs.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,19 @@
1818

1919
# Plotly.rs
2020

21-
Plotly.rs is a plotting library powered by [Plotly.js](https://plot.ly/javascript/). The aim is to bring over to Rust all the functionality that `Python` users have come to rely on; with the added benefit of type safety and speed.
21+
Plotly.rs is a plotting library powered by [Plotly.js](https://plot.ly/javascript/). The aim is to bring over to Rust all the functionality that `Python` users have come to rely on with the added benefit of type safety and speed.
2222

2323
Plotly.rs is free and open source. You can find the source on [GitHub](https://github.com/igiagkiozis/plotly). Issues and feature requests can be posted on the [issue tracker](https://github.com/igiagkiozis/plotly/issues).
2424

2525
## API Docs
2626

27-
This book is intended to be a *recipe* index and is complemented by the [API documentation](https://docs.rs/plotly).
27+
This book is intended to be a recipe index, which closely follows the [plotly.js examples](https://plotly.com/javascript/), and is complemented by the [API documentation](https://docs.rs/plotly).
28+
29+
## Contributing
30+
Contributions are always welcomed, no matter how large or small. Refer to the [contributing guidelines](https://github.com/igiagkiozis/plotly/blob/master/CONTRIBUTING.md) for further pointers, and, if in doubt, [open an issue](https://github.com/igiagkiozis/plotly/issues).
2831

2932
## License
3033

3134
Plotly.rs is distributed under the terms of the MIT license.
3235

33-
See [LICENSE-MIT](LICENSE-MIT), and [COPYRIGHT](COPYRIGHT) for details.
36+
See [LICENSE-MIT](https://github.com/igiagkiozis/plotly/blob/master/LICENSE-MIT), and [COPYRIGHT](https://github.com/igiagkiozis/plotly/blob/master/COPYRIGHT) for details.

plotly/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ once_cell = "1"
3232
serde = { version = "1.0.132", features = ["derive"] }
3333
serde_json = "1.0.73"
3434
serde_repr = "0.1"
35-
rand = "0.8.4"
36-
rand_distr = "0.4.2"
35+
rand = "0.8"
36+
rand_distr = "0.4"
3737
wasm-bindgen = { version = "0.2", optional = true }
3838
wasm-bindgen-futures = { version = "0.4", optional = true }
3939

0 commit comments

Comments
 (0)