From f68b34a0d5f6efa41c14fb2b36b373f713ad5421 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20Toma=C5=9Bko?= Date: Thu, 27 Jul 2023 23:02:22 +0000 Subject: [PATCH 01/12] Add From impl for Title --- CHANGELOG.md | 1 + plotly/src/common/mod.rs | 16 ++++++++++++++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e0a39420..1201784b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a ## [0.8.5] - 2023-xx-xx ### Added - [[#153](https://github.com/igiagkiozis/plotly/pull/153)] Added `LayoutScene` +- [[#154](https://github.com/igiagkiozis/plotly/pull/154)] From implementation for `Title`. ## [0.8.4] - 2023-07-09 ### Added diff --git a/plotly/src/common/mod.rs b/plotly/src/common/mod.rs index 44fece3a..381ae781 100644 --- a/plotly/src/common/mod.rs +++ b/plotly/src/common/mod.rs @@ -1249,6 +1249,18 @@ pub struct Title { pad: Option, } +impl From for Title { + fn from(title: String) -> Self { + Title::new(title) + } +} + +impl From<&String> for Title { + fn from(title: &String) -> Self { + Title::new(title) + } +} + impl From<&str> for Title { fn from(title: &str) -> Self { Title::new(title) @@ -1256,9 +1268,9 @@ impl From<&str> for Title { } impl Title { - pub fn new(text: &str) -> Self { + pub fn new>(text: S) -> Self { Title { - text: text.to_owned(), + text: text.into(), ..Default::default() } } From 43645307b782699b8e3c0392cc3fb6984bb54d53 Mon Sep 17 00:00:00 2001 From: Michael Freeborn Date: Mon, 7 Aug 2023 21:15:26 +0100 Subject: [PATCH 02/12] improve ergonomics of title method/struct --- plotly/src/common/mod.rs | 46 +++++++++++++++++++------------ plotly/src/layout/mod.rs | 10 ++++--- plotly/src/layout/themes.rs | 7 ++--- plotly/src/layout/update_menu.rs | 2 +- plotly/src/plot.rs | 8 ++++-- plotly_derive/src/field_setter.rs | 4 +++ 6 files changed, 48 insertions(+), 29 deletions(-) diff --git a/plotly/src/common/mod.rs b/plotly/src/common/mod.rs index 381ae781..4bd3dfd9 100644 --- a/plotly/src/common/mod.rs +++ b/plotly/src/common/mod.rs @@ -974,8 +974,8 @@ impl ColorBar { self } - pub fn title(mut self, title: Title) -> Self { - self.title = Some(title); + pub fn title>(mut self, title: T) -> Self { + self.title = Some(title.into()); self } @@ -1233,7 +1233,7 @@ impl Pad { #[serde_with::skip_serializing_none] #[derive(Serialize, Clone, Debug, Default)] pub struct Title { - text: String, + text: Option, font: Option, side: Option, #[serde(rename = "xref")] @@ -1249,28 +1249,32 @@ pub struct Title { pad: Option, } -impl From for Title { - fn from(title: String) -> Self { - Title::new(title) +impl From<&str> for Title { + fn from(title: &str) -> Self { + Title::with_text(title) } } -impl From<&String> for Title { - fn from(title: &String) -> Self { - Title::new(title) +impl From for Title { + fn from(value: String) -> Self { + Title::with_text(value) } } -impl From<&str> for Title { - fn from(title: &str) -> Self { - Title::new(title) +impl From<&String> for Title { + fn from(value: &String) -> Self { + Title::with_text(value) } } impl Title { - pub fn new>(text: S) -> Self { + pub fn new() -> Self { + Default::default() + } + + pub fn with_text>(text: S) -> Self { Title { - text: text.into(), + text: Some(text.into()), ..Default::default() } } @@ -1704,7 +1708,7 @@ mod tests { .tick_width(55) .tick0(0.0) .ticks(Ticks::Outside) - .title(Title::new("title")) + .title(Title::new()) .x(5.0) .x_anchor(Anchor::Bottom) .x_pad(2.2) @@ -1745,7 +1749,7 @@ mod tests { "tickwidth": 55, "tick0": 0.0, "ticks": "outside", - "title": {"text": "title"}, + "title": {}, "x": 5.0, "xanchor": "bottom", "xpad": 2.2, @@ -2196,7 +2200,7 @@ mod tests { #[test] fn test_serialize_title() { - let title = Title::new("title") + let title = Title::with_text("title") .font(Font::new()) .side(Side::Top) .x_ref(Reference::Paper) @@ -2317,4 +2321,12 @@ mod tests { assert_eq!(to_value(HoverOn::PointsAndFills).unwrap(), json!("points+fills")); } + + #[test] + fn test_title_method_can_take_string() { + ColorBar::new().title("Title"); + ColorBar::new().title(format!("{}", "title")); + ColorBar::new().title(&format!("{}", "title")); + ColorBar::new().title(Title::with_text("Title")); + } } diff --git a/plotly/src/layout/mod.rs b/plotly/src/layout/mod.rs index 6026e6ed..e7c8a781 100644 --- a/plotly/src/layout/mod.rs +++ b/plotly/src/layout/mod.rs @@ -2176,7 +2176,7 @@ mod tests { .y(2.0) .y_anchor(Anchor::Left) .valign(VAlign::Middle) - .title(Title::new("title")) + .title(Title::with_text(&"title".to_owned())) .group_click(GroupClick::ToggleItem) .item_width(50); @@ -2426,7 +2426,7 @@ mod tests { let axis = Axis::new() .visible(false) .color("#678123") - .title(Title::new("title")) + .title(Title::with_text("title")) .type_(AxisType::Date) .auto_range(false) .range_mode(RangeMode::NonNegative) @@ -2958,7 +2958,7 @@ mod tests { #[test] fn test_serialize_layout_template() { let layout_template = LayoutTemplate::new() - .title("Title".into()) + .title("Title") .show_legend(false) .legend(Legend::new()) .margin(Margin::new()) @@ -3100,7 +3100,9 @@ mod tests { #[test] fn test_serialize_layout() { let layout = Layout::new() - .title("Title".into()) + .title("Title") + .title(String::from("Title")) + .title(Title::with_text("Title")) .show_legend(false) .legend(Legend::new()) .margin(Margin::new()) diff --git a/plotly/src/layout/themes.rs b/plotly/src/layout/themes.rs index a4fb2774..9232a2a1 100644 --- a/plotly/src/layout/themes.rs +++ b/plotly/src/layout/themes.rs @@ -62,7 +62,7 @@ pub static PLOTLY_WHITE: Lazy