Skip to content

Commit 5ca0375

Browse files
authored
Merge branch 'main' into update-license
2 parents eff2ed0 + 13a8971 commit 5ca0375

File tree

36 files changed

+632
-166
lines changed

36 files changed

+632
-166
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,16 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a
55

66
## [0.9.0] - 2024-xx-xx
77
### Added
8+
- [[#207](https://github.com/plotly/plotly,rs/pull/207)] Add `Table` trace.
89
- [[#181](https://github.com/plotly/plotly,rs/pull/181)] Fix compilation error when mixing the crate with `askama/with-axum` by adding `with-axum` feature.
910
- [[#180](https://github.com/plotly/plotly.rs/pull/180)] Add setter for `Mapbox::domain`.
11+
- [[#178](https://github.com/plotly/plotly.rs/pull/178)] Fix setter for `Axis::matches` to take string arg.
1012
- [[#166](https://github.com/plotly/plotly.rs/pull/166)] Added subplot example with multiple titles.
1113
- [[#163](https://github.com/plotly/plotly.rs/pull/163)] Added `DensityMapbox`.
1214
- [[#161](https://github.com/plotly/plotly.rs/pull/161)] Added `Axis` `scaleanchor` settter.
1315
- [[#159](https://github.com/plotly/plotly.rs/pull/159)] Make `heat_map` module public to expose `Smoothing enum`.
16+
- [[#157](https://github.com/plotly/plotly.rs/pull/157)] Fix `HeatMap`'s setters for correctly setting `zmin`, `zmax` and `zmin` independent of `Z` input type.
17+
- [[#154](https://github.com/plotly/plotly.rs/pull/154)] Improve ergonomics of `Title` and `LegendGroupTitle` structs: `new` method now takes no arguments as per other structs, whilst a new `with_text()` constructor is added for convenience. Where other structs contain a `Title` (and `LegendGroupTitle`), users can now call the `title()` (and `legend_group_title()`) method with anything that `impl`s `Into<Title>`, viz. `String`, `&String`, `&str` and `Title`.
1418
- [[#153](https://github.com/plotly/plotly.rs/pull/153)] Added `LayoutScene`.
1519
- [[#212](https://github.com/plotly/plotly.rs/pull/212)] Update LICENSE
1620

examples/3d_charts/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ edition = "2021"
66

77
[dependencies]
88
ndarray = "0.15.6"
9+
rand = "0.8.5"
910
plotly = { path = "../../plotly" }

examples/3d_charts/src/main.rs

+77-8
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
use ndarray::Array;
44
use plotly::{
55
color::Rgb,
6-
common::{ColorScale, ColorScalePalette, Font, Marker, MarkerSymbol, Mode, Title},
6+
common::{ColorBar, ColorScale, ColorScalePalette, Font, Marker, MarkerSymbol, Mode},
77
layout::{Axis, Camera, Layout, LayoutScene, Legend, Margin, ProjectionType},
88
Mesh3D, Plot, Scatter3D, Surface,
99
};
10+
use rand::Rng;
1011

1112
// 3D Scatter Plots
1213
fn simple_scatter3d_plot() {
@@ -41,10 +42,11 @@ fn customized_scatter3d_plot() {
4142
.map(|i| (i.abs() * 25f64) as usize)
4243
.collect(),
4344
)
44-
.color_scale(ColorScale::Palette(ColorScalePalette::Viridis)),
45+
.color_scale(ColorScale::Palette(ColorScalePalette::Viridis))
46+
.color_array(z.clone()),
4547
);
4648

47-
let trace2 = Scatter3D::new(t, z, y)
49+
let trace2 = Scatter3D::new(t, z.clone(), y)
4850
.name("Helix 2")
4951
.mode(Mode::Markers)
5052
.marker(
@@ -55,7 +57,8 @@ fn customized_scatter3d_plot() {
5557
.map(|i| (i.abs() * 25f64) as usize)
5658
.collect(),
5759
)
58-
.color_scale(ColorScale::Palette(ColorScalePalette::Viridis)),
60+
.color_scale(ColorScale::Palette(ColorScalePalette::Viridis))
61+
.color_array(z),
5962
);
6063

6164
let mut plot = Plot::new();
@@ -66,27 +69,27 @@ fn customized_scatter3d_plot() {
6669
let front_color: Rgb = Rgb::new(255, 255, 255);
6770

6871
let layout = Layout::new()
69-
.title("Helix".into())
72+
.title("Helix")
7073
.legend(Legend::new().x(0.9).y(0.9))
7174
.font(Font::new().color(front_color))
7275
.paper_background_color(background_color)
7376
.scene(
7477
LayoutScene::new()
7578
.x_axis(
7679
Axis::new()
77-
.title("x (A meaningful axis name goes here)".into())
80+
.title("x (A meaningful axis name goes here)")
7881
.tick_angle(0f64)
7982
.grid_color(front_color)
8083
.color(front_color),
8184
)
8285
.y_axis(
8386
Axis::new()
84-
.title(Title::new("This is the label of the Y axis"))
87+
.title("This is the label of the Y axis")
8588
.tick_format(".1f")
8689
.grid_color(front_color)
8790
.color(front_color),
8891
)
89-
.z_axis(Axis::new().title("".into()).tick_values(vec![]))
92+
.z_axis(Axis::new().title("").tick_values(vec![]))
9093
.aspect_mode(plotly::layout::AspectMode::Manual)
9194
.aspect_ratio((3.0, 1.0, 1.0).into())
9295
.camera(
@@ -161,13 +164,79 @@ fn mesh_3d_plot() {
161164
plot.show();
162165
}
163166

167+
fn colorscale_plot() {
168+
let mut plot = Plot::new();
169+
170+
let x = (0..100)
171+
.map(|x| ((x - 50) as f64) / 100f64)
172+
.collect::<Vec<f64>>();
173+
174+
let y = x.clone();
175+
176+
let iproduct = |x: &[f64], y: &[f64]| -> Vec<(f64, f64)> {
177+
let mut result = Vec::new();
178+
for x in x {
179+
for y in y {
180+
result.push((*x, *y));
181+
}
182+
}
183+
result
184+
};
185+
186+
let ((x, y), z): ((Vec<f64>, Vec<f64>), Vec<f64>) = iproduct(&x, &y)
187+
.into_iter()
188+
.map(|(x, y)| ((x, y), -(x.powi(2) + y.powi(2)) + 0.5))
189+
.unzip();
190+
191+
let color: Vec<f32> = z.clone().into_iter().rev().map(|x| x as f32).collect();
192+
let _color: Vec<usize> = (0..z.len()).collect();
193+
let _color: Vec<u8> = (0..z.len()).map(|x| x as u8).collect();
194+
let _color: Vec<i16> = {
195+
let mut rng = rand::thread_rng();
196+
(0..z.len()).map(|_| rng.gen_range(0..100)).collect()
197+
};
198+
199+
let color_max = color.iter().fold(f64::MIN, |acc, x| acc.max(*x as f64));
200+
201+
let colorscale = ColorScalePalette::YlGnBu;
202+
203+
let marker = Marker::new()
204+
.color_array(color)
205+
.color_scale(plotly::common::ColorScale::Palette(colorscale.clone()))
206+
.cauto(false)
207+
.cmax(color_max * 1.5)
208+
.color_bar(ColorBar::new());
209+
210+
let scatter = Scatter3D::new(x, y, z).mode(Mode::Markers).marker(marker);
211+
212+
plot.add_trace(scatter);
213+
214+
let layout = Layout::new()
215+
.font(Font::new().size(18).family("Palatino-Linotype"))
216+
.title(format!("Colorscale: {colorscale:?}"))
217+
.width(1200)
218+
.height(1000)
219+
.scene(
220+
LayoutScene::new()
221+
.aspect_mode(plotly::layout::AspectMode::Data)
222+
.x_axis(Axis::new().tick_format(".1f"))
223+
.y_axis(Axis::new().tick_format(".1f"))
224+
.z_axis(Axis::new().tick_format(".1f")),
225+
);
226+
227+
plot.set_layout(layout);
228+
229+
plot.show();
230+
}
231+
164232
fn main() {
165233
// Uncomment any of these lines to display the example.
166234

167235
// Scatter3D Plots
168236
// simple_scatter3d_plot();
169237
// simple_line3d_plot();
170238
// customized_scatter3d_plot();
239+
// colorscale_plot();
171240

172241
// Surface Plots
173242
// surface_plot();

examples/basic_charts/src/main.rs

+25-13
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@ use plotly::{
55
color::{NamedColor, Rgb, Rgba},
66
common::{
77
ColorScale, ColorScalePalette, DashType, Fill, Font, Line, LineShape, Marker, Mode,
8-
Orientation, Title,
8+
Orientation,
99
},
1010
layout::{Axis, BarMode, Layout, Legend, TicksDirection, TraceOrder},
1111
sankey::{Line as SankeyLine, Link, Node},
12-
Bar, Plot, Sankey, Scatter, ScatterPolar,
12+
traces::table::{Cells, Header},
13+
Bar, Plot, Sankey, Scatter, ScatterPolar, Table,
1314
};
1415
use rand_distr::{Distribution, Normal, Uniform};
1516

@@ -117,9 +118,9 @@ fn data_labels_hover() {
117118
plot.add_trace(trace2);
118119

119120
let layout = Layout::new()
120-
.title("Data Labels Hover".into())
121-
.x_axis(Axis::new().title("x".into()).range(vec![0.75, 5.25]))
122-
.y_axis(Axis::new().title("y".into()).range(vec![0., 8.]));
121+
.title("Data Labels Hover")
122+
.x_axis(Axis::new().title("x").range(vec![0.75, 5.25]))
123+
.y_axis(Axis::new().title("y").range(vec![0., 8.]));
123124
plot.set_layout(layout);
124125

125126
plot.show();
@@ -142,7 +143,7 @@ fn data_labels_on_the_plot() {
142143
plot.add_trace(trace2);
143144

144145
let layout = Layout::new()
145-
.title("Data Labels on the Plot".into())
146+
.title("Data Labels on the Plot")
146147
.x_axis(Axis::new().range(vec![0.75, 5.25]))
147148
.y_axis(Axis::new().range(vec![0., 8.]));
148149
plot.set_layout(layout);
@@ -215,14 +216,14 @@ fn colored_and_styled_scatter_plot() {
215216
.marker(Marker::new().color(Rgb::new(142, 124, 195)).size(12));
216217

217218
let layout = Layout::new()
218-
.title(Title::new("Quarter 1 Growth"))
219+
.title("Quarter 1 Growth")
219220
.x_axis(
220221
Axis::new()
221-
.title(Title::new("GDP per Capita"))
222+
.title("GDP per Capita")
222223
.show_grid(false)
223224
.zero_line(false),
224225
)
225-
.y_axis(Axis::new().title(Title::new("Percent")).show_line(false));
226+
.y_axis(Axis::new().title("Percent").show_line(false));
226227
let mut plot = Plot::new();
227228
plot.add_trace(trace1);
228229
plot.add_trace(trace2);
@@ -279,7 +280,7 @@ fn adding_names_to_line_and_scatter_plot() {
279280
.mode(Mode::LinesMarkers)
280281
.name("Scatter + Lines");
281282

282-
let layout = Layout::new().title(Title::new("Adding Names to Line and Scatter Plot"));
283+
let layout = Layout::new().title("Adding Names to Line and Scatter Plot");
283284
let mut plot = Plot::new();
284285
plot.add_trace(trace1);
285286
plot.add_trace(trace2);
@@ -304,7 +305,7 @@ fn line_and_scatter_styling() {
304305
.marker(Marker::new().color(Rgb::new(128, 0, 128)).size(12))
305306
.line(Line::new().color(Rgb::new(128, 0, 128)).width(1.0));
306307

307-
let layout = Layout::new().title(Title::new("Line and Scatter Styling"));
308+
let layout = Layout::new().title("Line and Scatter Styling");
308309
let mut plot = Plot::new();
309310
plot.add_trace(trace1);
310311
plot.add_trace(trace2);
@@ -325,7 +326,7 @@ fn styling_line_plot() {
325326
.line(Line::new().color(Rgb::new(55, 128, 191)).width(1.0));
326327

327328
let layout = Layout::new()
328-
.title(Title::new("Styling Line Plot"))
329+
.title("Styling Line Plot")
329330
.width(500)
330331
.height(500);
331332
let mut plot = Plot::new();
@@ -594,7 +595,7 @@ fn basic_sankey_diagram() {
594595
);
595596

596597
let layout = Layout::new()
597-
.title("Basic Sankey".into())
598+
.title("Basic Sankey")
598599
.font(Font::new().size(10));
599600

600601
let mut plot = Plot::new();
@@ -604,6 +605,16 @@ fn basic_sankey_diagram() {
604605
plot.show();
605606
}
606607

608+
fn table_chart() {
609+
let trace = Table::new(
610+
Header::new(vec![String::from("col1"), String::from("col2")]),
611+
Cells::new(vec![vec![1, 2], vec![2, 3]]),
612+
);
613+
let mut plot = Plot::new();
614+
plot.add_trace(trace);
615+
plot.show();
616+
}
617+
607618
fn main() {
608619
// Uncomment any of these lines to display the example.
609620

@@ -629,6 +640,7 @@ fn main() {
629640
// basic_bar_chart();
630641
// grouped_bar_chart();
631642
// stacked_bar_chart();
643+
// table_chart();
632644

633645
// Sankey Diagrams
634646
// basic_sankey_diagram();

examples/financial_charts/src/main.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use std::env;
44
use std::path::PathBuf;
55

6-
use plotly::common::{TickFormatStop, Title};
6+
use plotly::common::TickFormatStop;
77
use plotly::layout::{Axis, RangeSelector, RangeSlider, SelectorButton, SelectorStep, StepMode};
88
use plotly::{Candlestick, Layout, Ohlc, Plot, Scatter};
99
use serde::Deserialize;
@@ -50,7 +50,7 @@ fn time_series_plot_with_custom_date_range() {
5050

5151
let layout = Layout::new()
5252
.x_axis(Axis::new().range(vec!["2016-07-01", "2016-12-31"]))
53-
.title(Title::new("Manually Set Date Range"));
53+
.title("Manually Set Date Range");
5454
plot.set_layout(layout);
5555

5656
plot.show();
@@ -68,7 +68,7 @@ fn time_series_with_range_slider() {
6868

6969
let layout = Layout::new()
7070
.x_axis(Axis::new().range_slider(RangeSlider::new().visible(true)))
71-
.title(Title::new("Manually Set Date Range"));
71+
.title("Manually Set Date Range");
7272
plot.set_layout(layout);
7373

7474
plot.show();

0 commit comments

Comments
 (0)