Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve ergonomics of Title and LegendGroupTitle structs #154

Merged
merged 13 commits into from
Jun 25, 2024
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a
- [[#163](https://github.com/plotly/plotly.rs/pull/163)] Added `DensityMapbox`.
- [[#161](https://github.com/plotly/plotly.rs/pull/161)] Added `Axis` `scaleanchor` settter.
- [[#159](https://github.com/plotly/plotly.rs/pull/159)] Make `heat_map` module public to expose `Smoothing enum`.
- [[#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.
- [[#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`.
- [[#153](https://github.com/plotly/plotly.rs/pull/153)] Added `LayoutScene`.

## [0.8.4] - 2023-07-09
Expand Down
12 changes: 6 additions & 6 deletions examples/3d_charts/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use ndarray::Array;
use plotly::{
color::Rgb,
common::{ColorBar, ColorScale, ColorScalePalette, Font, Marker, MarkerSymbol, Mode, Title},
common::{ColorBar, ColorScale, ColorScalePalette, Font, Marker, MarkerSymbol, Mode},
layout::{Axis, Camera, Layout, LayoutScene, Legend, Margin, ProjectionType},
Mesh3D, Plot, Scatter3D, Surface,
};
Expand Down Expand Up @@ -69,27 +69,27 @@ fn customized_scatter3d_plot() {
let front_color: Rgb = Rgb::new(255, 255, 255);

let layout = Layout::new()
.title("Helix".into())
.title("Helix")
.legend(Legend::new().x(0.9).y(0.9))
.font(Font::new().color(front_color))
.paper_background_color(background_color)
.scene(
LayoutScene::new()
.x_axis(
Axis::new()
.title("x (A meaningful axis name goes here)".into())
.title("x (A meaningful axis name goes here)")
.tick_angle(0f64)
.grid_color(front_color)
.color(front_color),
)
.y_axis(
Axis::new()
.title(Title::new("This is the label of the Y axis"))
.title("This is the label of the Y axis")
.tick_format(".1f")
.grid_color(front_color)
.color(front_color),
)
.z_axis(Axis::new().title("".into()).tick_values(vec![]))
.z_axis(Axis::new().title("").tick_values(vec![]))
.aspect_mode(plotly::layout::AspectMode::Manual)
.aspect_ratio((3.0, 1.0, 1.0).into())
.camera(
Expand Down Expand Up @@ -213,7 +213,7 @@ fn colorscale_plot() {

let layout = Layout::new()
.font(Font::new().size(18).family("Palatino-Linotype"))
.title(format!("Colorscale: {colorscale:?}").as_str().into())
.title(format!("Colorscale: {colorscale:?}"))
.width(1200)
.height(1000)
.scene(
Expand Down
24 changes: 12 additions & 12 deletions examples/basic_charts/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use plotly::{
color::{NamedColor, Rgb, Rgba},
common::{
ColorScale, ColorScalePalette, DashType, Fill, Font, Line, LineShape, Marker, Mode,
Orientation, Title,
Orientation,
},
layout::{Axis, BarMode, Layout, Legend, TicksDirection, TraceOrder},
sankey::{Line as SankeyLine, Link, Node},
Expand Down Expand Up @@ -118,9 +118,9 @@ fn data_labels_hover() {
plot.add_trace(trace2);

let layout = Layout::new()
.title("Data Labels Hover".into())
.x_axis(Axis::new().title("x".into()).range(vec![0.75, 5.25]))
.y_axis(Axis::new().title("y".into()).range(vec![0., 8.]));
.title("Data Labels Hover")
.x_axis(Axis::new().title("x").range(vec![0.75, 5.25]))
.y_axis(Axis::new().title("y").range(vec![0., 8.]));
plot.set_layout(layout);

plot.show();
Expand All @@ -143,7 +143,7 @@ fn data_labels_on_the_plot() {
plot.add_trace(trace2);

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

let layout = Layout::new()
.title(Title::new("Quarter 1 Growth"))
.title("Quarter 1 Growth")
.x_axis(
Axis::new()
.title(Title::new("GDP per Capita"))
.title("GDP per Capita")
.show_grid(false)
.zero_line(false),
)
.y_axis(Axis::new().title(Title::new("Percent")).show_line(false));
.y_axis(Axis::new().title("Percent").show_line(false));
let mut plot = Plot::new();
plot.add_trace(trace1);
plot.add_trace(trace2);
Expand Down Expand Up @@ -280,7 +280,7 @@ fn adding_names_to_line_and_scatter_plot() {
.mode(Mode::LinesMarkers)
.name("Scatter + Lines");

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

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

let layout = Layout::new()
.title(Title::new("Styling Line Plot"))
.title("Styling Line Plot")
.width(500)
.height(500);
let mut plot = Plot::new();
Expand Down Expand Up @@ -595,7 +595,7 @@ fn basic_sankey_diagram() {
);

let layout = Layout::new()
.title("Basic Sankey".into())
.title("Basic Sankey")
.font(Font::new().size(10));

let mut plot = Plot::new();
Expand Down
6 changes: 3 additions & 3 deletions examples/financial_charts/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use std::env;
use std::path::PathBuf;

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

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

plot.show();
Expand All @@ -68,7 +68,7 @@ fn time_series_with_range_slider() {

let layout = Layout::new()
.x_axis(Axis::new().range_slider(RangeSlider::new().visible(true)))
.title(Title::new("Manually Set Date Range"));
.title("Manually Set Date Range");
plot.set_layout(layout);

plot.show();
Expand Down
10 changes: 5 additions & 5 deletions examples/scientific_charts/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use std::f64::consts::PI;

use plotly::common::{ColorScale, ColorScalePalette, Font, Title};
use plotly::common::{ColorScale, ColorScalePalette, Font};
use plotly::contour::Contours;
use plotly::{Contour, HeatMap, Layout, Plot};

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

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

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

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

let layout = Layout::new()
.title(Title::new("Customized Heatmap"))
.title("Customized Heatmap")
.font(Font::new().size(32));

let mut plot = Plot::new();
Expand Down
2 changes: 1 addition & 1 deletion examples/shapes/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ fn creating_tangent_lines_with_shapes() {
plot.add_trace(trace);

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

layout.add_shape(
Shape::new()
Expand Down
30 changes: 10 additions & 20 deletions examples/statistical_charts/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use ndarray::Array;
use plotly::{
box_plot::{BoxMean, BoxPoints},
color::{NamedColor, Rgb, Rgba},
common::{ErrorData, ErrorType, Line, Marker, Mode, Orientation, Title},
common::{ErrorData, ErrorType, Line, Marker, Mode, Orientation},
histogram::{Bins, Cumulative, HistFunc, HistNorm},
layout::{Axis, BarMode, BoxMode, Layout, Margin},
Bar, BoxPlot, Histogram, Plot, Scatter,
Expand Down Expand Up @@ -203,11 +203,7 @@ fn grouped_box_plot() {
plot.add_trace(trace3);

let layout = Layout::new()
.y_axis(
Axis::new()
.title(Title::new("normalized moisture"))
.zero_line(false),
)
.y_axis(Axis::new().title("normalized moisture").zero_line(false))
.box_mode(BoxMode::Group);

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

let layout = Layout::new().title(Title::new("Box Plot Styling Outliers"));
let layout = Layout::new().title("Box Plot Styling Outliers");

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

let mut plot = Plot::new();
plot.set_layout(layout);
Expand Down Expand Up @@ -321,12 +317,8 @@ fn grouped_horizontal_box_plot() {
plot.add_trace(trace3);

let layout = Layout::new()
.title(Title::new("Grouped Horizontal Box Plot"))
.x_axis(
Axis::new()
.title(Title::new("normalized moisture"))
.zero_line(false),
)
.title("Grouped Horizontal Box Plot")
.x_axis(Axis::new().title("normalized moisture").zero_line(false))
.box_mode(BoxMode::Group);

plot.set_layout(layout);
Expand Down Expand Up @@ -370,9 +362,7 @@ fn fully_styled_box_plot() {

let mut plot = Plot::new();
let layout = Layout::new()
.title(Title::new(
"Points Scored by the Top 9 Scoring NBA Players in 2012",
))
.title("Points Scored by the Top 9 Scoring NBA Players in 2012")
.y_axis(
Axis::new()
.auto_range(true)
Expand Down Expand Up @@ -522,9 +512,9 @@ fn colored_and_styled_histograms() {
.auto_bin_x(false)
.x_bins(Bins::new(-3.2, 4.0, 0.06));
let layout = Layout::new()
.title(Title::new("Colored and Styled Histograms"))
.x_axis(Axis::new().title(Title::new("Value")))
.y_axis(Axis::new().title(Title::new("Count")))
.title("Colored and Styled Histograms")
.x_axis(Axis::new().title("Value"))
.y_axis(Axis::new().title("Count"))
.bar_mode(BarMode::Overlay)
.bar_gap(0.05)
.bar_group_gap(0.2);
Expand Down
18 changes: 9 additions & 9 deletions examples/subplots/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ fn multiple_custom_sized_subplots() {
plot.add_trace(trace4);

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

let layout = Layout::new()
.title(Title::new("Double Y Axis Example"))
.y_axis(Axis::new().title(Title::new("yaxis title")))
.title("Double Y Axis Example")
.y_axis(Axis::new().title("yaxis title"))
.y_axis2(
Axis::new()
.title(Title::new("yaxis2 title").font(Font::new().color(Rgb::new(148, 103, 189))))
.title(Title::from("yaxis2 title").font(Font::new().color(Rgb::new(148, 103, 189))))
.tick_font(Font::new().color(Rgb::new(148, 103, 189)))
.overlaying("y")
.side(AxisSide::Right),
Expand All @@ -249,17 +249,17 @@ fn multiple_axes() {
plot.add_trace(trace4);

let layout = Layout::new()
.title(Title::new("multiple y-axes example"))
.title("multiple y-axes example")
.width(800)
.x_axis(Axis::new().domain(&[0.3, 0.7]))
.y_axis(
Axis::new()
.title(Title::new("yaxis title").font(Font::new().color("#1f77b4")))
.title(Title::from("yaxis title").font(Font::new().color("#1f77b4")))
.tick_font(Font::new().color("#1f77b4")),
)
.y_axis2(
Axis::new()
.title(Title::new("yaxis2 title").font(Font::new().color("#ff7f0e")))
.title(Title::from("yaxis2 title").font(Font::new().color("#ff7f0e")))
.tick_font(Font::new().color("#ff7f0e"))
.anchor("free")
.overlaying("y")
Expand All @@ -268,15 +268,15 @@ fn multiple_axes() {
)
.y_axis3(
Axis::new()
.title(Title::new("yaxis3 title").font(Font::new().color("#d62728")))
.title(Title::from("yaxis3 title").font(Font::new().color("#d62728")))
.tick_font(Font::new().color("#d62728"))
.anchor("x")
.overlaying("y")
.side(AxisSide::Right),
)
.y_axis4(
Axis::new()
.title(Title::new("yaxis4 title").font(Font::new().color("#9467bd")))
.title(Title::from("yaxis4 title").font(Font::new().color("#9467bd")))
.tick_font(Font::new().color("#9467bd"))
.anchor("free")
.overlaying("y")
Expand Down
3 changes: 1 addition & 2 deletions examples/wasm-yew-minimal/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ pub fn plot_component() -> Html {
let trace = Scatter::new(vec![0, 1, 2], vec![2, 1, 0]);
plot.add_trace(trace);

let layout =
plotly::Layout::new().title(plotly::common::Title::new("Displaying a Chart in Yew"));
let layout = plotly::Layout::new().title("Displaying a Chart in Yew");
plot.set_layout(layout);

async move {
Expand Down
Loading
Loading