Skip to content

Commit 13a8971

Browse files
authored
Improve ergonomics of Title and LegendGroupTitle structs (#154)
* Improve ergonomics of LegendGroupTitle method/struct - Add From<String> impl for Title * update changelog * disable spurious kaleido save to file failing tests on MacOS and Windows --------- Co-authored-by: Michael Freeborn <[email protected]> Co-authored-by: Andrei Gherghescu <[email protected]>
1 parent a6302ea commit 13a8971

File tree

31 files changed

+207
-116
lines changed

31 files changed

+207
-116
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a
1313
- [[#163](https://github.com/plotly/plotly.rs/pull/163)] Added `DensityMapbox`.
1414
- [[#161](https://github.com/plotly/plotly.rs/pull/161)] Added `Axis` `scaleanchor` settter.
1515
- [[#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`.
1618
- [[#153](https://github.com/plotly/plotly.rs/pull/153)] Added `LayoutScene`.
1719

1820
## [0.8.4] - 2023-07-09

examples/3d_charts/src/main.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use ndarray::Array;
44
use plotly::{
55
color::Rgb,
6-
common::{ColorBar, 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
};
@@ -69,27 +69,27 @@ fn customized_scatter3d_plot() {
6969
let front_color: Rgb = Rgb::new(255, 255, 255);
7070

7171
let layout = Layout::new()
72-
.title("Helix".into())
72+
.title("Helix")
7373
.legend(Legend::new().x(0.9).y(0.9))
7474
.font(Font::new().color(front_color))
7575
.paper_background_color(background_color)
7676
.scene(
7777
LayoutScene::new()
7878
.x_axis(
7979
Axis::new()
80-
.title("x (A meaningful axis name goes here)".into())
80+
.title("x (A meaningful axis name goes here)")
8181
.tick_angle(0f64)
8282
.grid_color(front_color)
8383
.color(front_color),
8484
)
8585
.y_axis(
8686
Axis::new()
87-
.title(Title::new("This is the label of the Y axis"))
87+
.title("This is the label of the Y axis")
8888
.tick_format(".1f")
8989
.grid_color(front_color)
9090
.color(front_color),
9191
)
92-
.z_axis(Axis::new().title("".into()).tick_values(vec![]))
92+
.z_axis(Axis::new().title("").tick_values(vec![]))
9393
.aspect_mode(plotly::layout::AspectMode::Manual)
9494
.aspect_ratio((3.0, 1.0, 1.0).into())
9595
.camera(
@@ -213,7 +213,7 @@ fn colorscale_plot() {
213213

214214
let layout = Layout::new()
215215
.font(Font::new().size(18).family("Palatino-Linotype"))
216-
.title(format!("Colorscale: {colorscale:?}").as_str().into())
216+
.title(format!("Colorscale: {colorscale:?}"))
217217
.width(1200)
218218
.height(1000)
219219
.scene(

examples/basic_charts/src/main.rs

+12-12
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ 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},
@@ -118,9 +118,9 @@ fn data_labels_hover() {
118118
plot.add_trace(trace2);
119119

120120
let layout = Layout::new()
121-
.title("Data Labels Hover".into())
122-
.x_axis(Axis::new().title("x".into()).range(vec![0.75, 5.25]))
123-
.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.]));
124124
plot.set_layout(layout);
125125

126126
plot.show();
@@ -143,7 +143,7 @@ fn data_labels_on_the_plot() {
143143
plot.add_trace(trace2);
144144

145145
let layout = Layout::new()
146-
.title("Data Labels on the Plot".into())
146+
.title("Data Labels on the Plot")
147147
.x_axis(Axis::new().range(vec![0.75, 5.25]))
148148
.y_axis(Axis::new().range(vec![0., 8.]));
149149
plot.set_layout(layout);
@@ -216,14 +216,14 @@ fn colored_and_styled_scatter_plot() {
216216
.marker(Marker::new().color(Rgb::new(142, 124, 195)).size(12));
217217

218218
let layout = Layout::new()
219-
.title(Title::new("Quarter 1 Growth"))
219+
.title("Quarter 1 Growth")
220220
.x_axis(
221221
Axis::new()
222-
.title(Title::new("GDP per Capita"))
222+
.title("GDP per Capita")
223223
.show_grid(false)
224224
.zero_line(false),
225225
)
226-
.y_axis(Axis::new().title(Title::new("Percent")).show_line(false));
226+
.y_axis(Axis::new().title("Percent").show_line(false));
227227
let mut plot = Plot::new();
228228
plot.add_trace(trace1);
229229
plot.add_trace(trace2);
@@ -280,7 +280,7 @@ fn adding_names_to_line_and_scatter_plot() {
280280
.mode(Mode::LinesMarkers)
281281
.name("Scatter + Lines");
282282

283-
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");
284284
let mut plot = Plot::new();
285285
plot.add_trace(trace1);
286286
plot.add_trace(trace2);
@@ -305,7 +305,7 @@ fn line_and_scatter_styling() {
305305
.marker(Marker::new().color(Rgb::new(128, 0, 128)).size(12))
306306
.line(Line::new().color(Rgb::new(128, 0, 128)).width(1.0));
307307

308-
let layout = Layout::new().title(Title::new("Line and Scatter Styling"));
308+
let layout = Layout::new().title("Line and Scatter Styling");
309309
let mut plot = Plot::new();
310310
plot.add_trace(trace1);
311311
plot.add_trace(trace2);
@@ -326,7 +326,7 @@ fn styling_line_plot() {
326326
.line(Line::new().color(Rgb::new(55, 128, 191)).width(1.0));
327327

328328
let layout = Layout::new()
329-
.title(Title::new("Styling Line Plot"))
329+
.title("Styling Line Plot")
330330
.width(500)
331331
.height(500);
332332
let mut plot = Plot::new();
@@ -595,7 +595,7 @@ fn basic_sankey_diagram() {
595595
);
596596

597597
let layout = Layout::new()
598-
.title("Basic Sankey".into())
598+
.title("Basic Sankey")
599599
.font(Font::new().size(10));
600600

601601
let mut plot = Plot::new();

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();

examples/scientific_charts/src/main.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
use std::f64::consts::PI;
44

5-
use plotly::common::{ColorScale, ColorScalePalette, Font, Title};
5+
use plotly::common::{ColorScale, ColorScalePalette, Font};
66
use plotly::contour::Contours;
77
use plotly::{Contour, HeatMap, Layout, Plot};
88

@@ -47,7 +47,7 @@ fn colorscale_for_contour_plot() {
4747
];
4848
let trace = Contour::new_z(z).color_scale(ColorScale::Palette(ColorScalePalette::Jet));
4949

50-
let layout = Layout::new().title(Title::new("Colorscale for Contour Plot"));
50+
let layout = Layout::new().title("Colorscale for Contour Plot");
5151
let mut plot = Plot::new();
5252
plot.set_layout(layout);
5353
plot.add_trace(trace);
@@ -68,7 +68,7 @@ fn customizing_size_and_range_of_a_contour_plots_contours() {
6868
.auto_contour(false)
6969
.contours(Contours::new().start(0.0).end(8.0).size(2));
7070

71-
let layout = Layout::new().title(Title::new("Customizing Size and Range of Contours"));
71+
let layout = Layout::new().title("Customizing Size and Range of Contours");
7272
let mut plot = Plot::new();
7373
plot.set_layout(layout);
7474
plot.add_trace(trace);
@@ -91,7 +91,7 @@ fn customizing_spacing_between_x_and_y_ticks() {
9191
.dy(10.0)
9292
.y0(10.0);
9393

94-
let layout = Layout::new().title(Title::new("Customizing Size and Range of Contours"));
94+
let layout = Layout::new().title("Customizing Size and Range of Contours");
9595
let mut plot = Plot::new();
9696
plot.set_layout(layout);
9797
plot.add_trace(trace);
@@ -136,7 +136,7 @@ fn customized_heat_map() {
136136
.color_scale(colorscale.into());
137137

138138
let layout = Layout::new()
139-
.title(Title::new("Customized Heatmap"))
139+
.title("Customized Heatmap")
140140
.font(Font::new().size(32));
141141

142142
let mut plot = Plot::new();

examples/shapes/src/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ fn creating_tangent_lines_with_shapes() {
138138
plot.add_trace(trace);
139139

140140
let mut layout =
141-
Layout::new().title("$f(x)=x\\sin(x^2)+1\\\\ f\'(x)=\\sin(x^2)+2x^2\\cos(x^2)$".into());
141+
Layout::new().title("$f(x)=x\\sin(x^2)+1\\\\ f\'(x)=\\sin(x^2)+2x^2\\cos(x^2)$");
142142

143143
layout.add_shape(
144144
Shape::new()

examples/statistical_charts/src/main.rs

+10-20
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use ndarray::Array;
44
use plotly::{
55
box_plot::{BoxMean, BoxPoints},
66
color::{NamedColor, Rgb, Rgba},
7-
common::{ErrorData, ErrorType, Line, Marker, Mode, Orientation, Title},
7+
common::{ErrorData, ErrorType, Line, Marker, Mode, Orientation},
88
histogram::{Bins, Cumulative, HistFunc, HistNorm},
99
layout::{Axis, BarMode, BoxMode, Layout, Margin},
1010
Bar, BoxPlot, Histogram, Plot, Scatter,
@@ -203,11 +203,7 @@ fn grouped_box_plot() {
203203
plot.add_trace(trace3);
204204

205205
let layout = Layout::new()
206-
.y_axis(
207-
Axis::new()
208-
.title(Title::new("normalized moisture"))
209-
.zero_line(false),
210-
)
206+
.y_axis(Axis::new().title("normalized moisture").zero_line(false))
211207
.box_mode(BoxMode::Group);
212208

213209
plot.set_layout(layout);
@@ -248,7 +244,7 @@ fn box_plot_styling_outliers() {
248244
.marker(Marker::new().color(Rgb::new(107, 174, 214)))
249245
.box_points(BoxPoints::Outliers);
250246

251-
let layout = Layout::new().title(Title::new("Box Plot Styling Outliers"));
247+
let layout = Layout::new().title("Box Plot Styling Outliers");
252248

253249
let mut plot = Plot::new();
254250
plot.set_layout(layout);
@@ -274,7 +270,7 @@ fn box_plot_styling_mean_and_standard_deviation() {
274270
.name("Mean and Standard Deviation")
275271
.marker(Marker::new().color(Rgb::new(8, 81, 156)))
276272
.box_mean(BoxMean::StandardDeviation);
277-
let layout = Layout::new().title(Title::new("Box Plot Styling Mean and Standard Deviation"));
273+
let layout = Layout::new().title("Box Plot Styling Mean and Standard Deviation");
278274

279275
let mut plot = Plot::new();
280276
plot.set_layout(layout);
@@ -321,12 +317,8 @@ fn grouped_horizontal_box_plot() {
321317
plot.add_trace(trace3);
322318

323319
let layout = Layout::new()
324-
.title(Title::new("Grouped Horizontal Box Plot"))
325-
.x_axis(
326-
Axis::new()
327-
.title(Title::new("normalized moisture"))
328-
.zero_line(false),
329-
)
320+
.title("Grouped Horizontal Box Plot")
321+
.x_axis(Axis::new().title("normalized moisture").zero_line(false))
330322
.box_mode(BoxMode::Group);
331323

332324
plot.set_layout(layout);
@@ -370,9 +362,7 @@ fn fully_styled_box_plot() {
370362

371363
let mut plot = Plot::new();
372364
let layout = Layout::new()
373-
.title(Title::new(
374-
"Points Scored by the Top 9 Scoring NBA Players in 2012",
375-
))
365+
.title("Points Scored by the Top 9 Scoring NBA Players in 2012")
376366
.y_axis(
377367
Axis::new()
378368
.auto_range(true)
@@ -522,9 +512,9 @@ fn colored_and_styled_histograms() {
522512
.auto_bin_x(false)
523513
.x_bins(Bins::new(-3.2, 4.0, 0.06));
524514
let layout = Layout::new()
525-
.title(Title::new("Colored and Styled Histograms"))
526-
.x_axis(Axis::new().title(Title::new("Value")))
527-
.y_axis(Axis::new().title(Title::new("Count")))
515+
.title("Colored and Styled Histograms")
516+
.x_axis(Axis::new().title("Value"))
517+
.y_axis(Axis::new().title("Count"))
528518
.bar_mode(BarMode::Overlay)
529519
.bar_gap(0.05)
530520
.bar_group_gap(0.2);

examples/subplots/src/main.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ fn multiple_custom_sized_subplots() {
194194
plot.add_trace(trace4);
195195

196196
let layout = Layout::new()
197-
.title(Title::new("Multiple Custom Sized Subplots"))
197+
.title("Multiple Custom Sized Subplots")
198198
.x_axis(Axis::new().domain(&[0., 0.45]).anchor("y1"))
199199
.y_axis(Axis::new().domain(&[0.5, 1.]).anchor("x1"))
200200
.x_axis2(Axis::new().domain(&[0.55, 1.]).anchor("y2"))
@@ -220,11 +220,11 @@ fn two_y_axes() {
220220
plot.add_trace(trace2);
221221

222222
let layout = Layout::new()
223-
.title(Title::new("Double Y Axis Example"))
224-
.y_axis(Axis::new().title(Title::new("yaxis title")))
223+
.title("Double Y Axis Example")
224+
.y_axis(Axis::new().title("yaxis title"))
225225
.y_axis2(
226226
Axis::new()
227-
.title(Title::new("yaxis2 title").font(Font::new().color(Rgb::new(148, 103, 189))))
227+
.title(Title::from("yaxis2 title").font(Font::new().color(Rgb::new(148, 103, 189))))
228228
.tick_font(Font::new().color(Rgb::new(148, 103, 189)))
229229
.overlaying("y")
230230
.side(AxisSide::Right),
@@ -249,17 +249,17 @@ fn multiple_axes() {
249249
plot.add_trace(trace4);
250250

251251
let layout = Layout::new()
252-
.title(Title::new("multiple y-axes example"))
252+
.title("multiple y-axes example")
253253
.width(800)
254254
.x_axis(Axis::new().domain(&[0.3, 0.7]))
255255
.y_axis(
256256
Axis::new()
257-
.title(Title::new("yaxis title").font(Font::new().color("#1f77b4")))
257+
.title(Title::from("yaxis title").font(Font::new().color("#1f77b4")))
258258
.tick_font(Font::new().color("#1f77b4")),
259259
)
260260
.y_axis2(
261261
Axis::new()
262-
.title(Title::new("yaxis2 title").font(Font::new().color("#ff7f0e")))
262+
.title(Title::from("yaxis2 title").font(Font::new().color("#ff7f0e")))
263263
.tick_font(Font::new().color("#ff7f0e"))
264264
.anchor("free")
265265
.overlaying("y")
@@ -268,15 +268,15 @@ fn multiple_axes() {
268268
)
269269
.y_axis3(
270270
Axis::new()
271-
.title(Title::new("yaxis3 title").font(Font::new().color("#d62728")))
271+
.title(Title::from("yaxis3 title").font(Font::new().color("#d62728")))
272272
.tick_font(Font::new().color("#d62728"))
273273
.anchor("x")
274274
.overlaying("y")
275275
.side(AxisSide::Right),
276276
)
277277
.y_axis4(
278278
Axis::new()
279-
.title(Title::new("yaxis4 title").font(Font::new().color("#9467bd")))
279+
.title(Title::from("yaxis4 title").font(Font::new().color("#9467bd")))
280280
.tick_font(Font::new().color("#9467bd"))
281281
.anchor("free")
282282
.overlaying("y")

examples/wasm-yew-minimal/src/main.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ pub fn plot_component() -> Html {
99
let trace = Scatter::new(vec![0, 1, 2], vec![2, 1, 0]);
1010
plot.add_trace(trace);
1111

12-
let layout =
13-
plotly::Layout::new().title(plotly::common::Title::new("Displaying a Chart in Yew"));
12+
let layout = plotly::Layout::new().title("Displaying a Chart in Yew");
1413
plot.set_layout(layout);
1514

1615
async move {

0 commit comments

Comments
 (0)