Skip to content

Commit 3feaa0c

Browse files
committed
Improve ergonomics of LegendGroupTitle method/struct
1 parent 3228ef1 commit 3feaa0c

17 files changed

+55
-22
lines changed

Diff for: plotly/src/common/mod.rs

+26-4
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,36 @@ pub enum HoverInfo {
5858
#[serde_with::skip_serializing_none]
5959
#[derive(Serialize, Clone, Debug, Default)]
6060
pub struct LegendGroupTitle {
61-
text: String,
61+
text: Option<String>,
6262
font: Option<Font>,
6363
}
6464

65+
impl From<&str> for LegendGroupTitle {
66+
fn from(title: &str) -> Self {
67+
LegendGroupTitle::with_text(title)
68+
}
69+
}
70+
71+
impl From<String> for LegendGroupTitle {
72+
fn from(value: String) -> Self {
73+
LegendGroupTitle::with_text(value)
74+
}
75+
}
76+
77+
impl From<&String> for LegendGroupTitle {
78+
fn from(value: &String) -> Self {
79+
LegendGroupTitle::with_text(value)
80+
}
81+
}
82+
6583
impl LegendGroupTitle {
66-
pub fn new(text: &str) -> Self {
67-
Self {
68-
text: text.to_string(),
84+
pub fn new() -> Self {
85+
Default::default()
86+
}
87+
88+
pub fn with_text<S: Into<String>>(text: S) -> Self {
89+
LegendGroupTitle {
90+
text: Some(text.into()),
6991
..Default::default()
7092
}
7193
}

Diff for: plotly/src/traces/bar.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ mod tests {
159159
.inside_text_anchor(TextAnchor::End)
160160
.inside_text_font(Font::new())
161161
.legend_group("legend-group")
162-
.legend_group_title(LegendGroupTitle::new("legend-group-title"))
162+
.legend_group_title("legend-group-title")
163163
.marker(Marker::new())
164164
.name("Bar")
165165
.offset(5)

Diff for: plotly/src/traces/box_plot.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ mod tests {
283283
.jitter(0.5)
284284
.line(Line::new())
285285
.legend_group("one")
286-
.legend_group_title(LegendGroupTitle::new("Legend Group Title"))
286+
.legend_group_title("Legend Group Title")
287287
.lower_fence(vec![0., 1.])
288288
.marker(Marker::new())
289289
.mean(vec![12., 13.])

Diff for: plotly/src/traces/candlestick.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ mod tests {
144144
.visible(Visible::True)
145145
.show_legend(false)
146146
.legend_group("group_1")
147-
.legend_group_title(LegendGroupTitle::new("Legend Group Title"))
147+
.legend_group_title("Legend Group Title")
148148
.opacity(0.3)
149149
.text_array(vec!["text", "here"])
150150
.text("text here")

Diff for: plotly/src/traces/contour.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -339,8 +339,11 @@ where
339339
Box::new(self)
340340
}
341341

342-
pub fn legend_group_title(mut self, legend_group_title: LegendGroupTitle) -> Box<Self> {
343-
self.legend_group_title = Some(legend_group_title);
342+
pub fn legend_group_title(
343+
mut self,
344+
legend_group_title: impl Into<LegendGroupTitle>,
345+
) -> Box<Self> {
346+
self.legend_group_title = Some(legend_group_title.into());
344347
Box::new(self)
345348
}
346349

@@ -583,7 +586,7 @@ mod tests {
583586
.hover_template_array(vec!["ok {1}", "ok {2}"])
584587
.hover_text(vec!["p3", "p4"])
585588
.legend_group("group_1")
586-
.legend_group_title(LegendGroupTitle::new("Legend Group Title"))
589+
.legend_group_title("Legend Group Title")
587590
.line(Line::new())
588591
.n_contours(5)
589592
.name("contour trace")

Diff for: plotly/src/traces/heat_map.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ mod tests {
202202
.hover_template_array(vec!["tmpl1", "tmpl2"])
203203
.hover_text(vec!["hov", "er"])
204204
.legend_group("1")
205-
.legend_group_title(LegendGroupTitle::new("Legend Group Title"))
205+
.legend_group_title("Legend Group Title")
206206
.name("name")
207207
.opacity(0.99)
208208
.reverse_scale(false)

Diff for: plotly/src/traces/histogram.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ mod tests {
410410
.hover_text("hover_text")
411411
.hover_text_array(vec!["hover_text_1", "hover_text_2"])
412412
.legend_group("legendgroup")
413-
.legend_group_title(LegendGroupTitle::new("Legend Group Title"))
413+
.legend_group_title("Legend Group Title")
414414
.marker(Marker::new())
415415
.n_bins_x(5)
416416
.n_bins_y(10)

Diff for: plotly/src/traces/image.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ mod tests {
408408
.name("image name")
409409
.visible(Visible::True)
410410
.legend_rank(1000)
411-
.legend_group_title(LegendGroupTitle::new("Legend Group Title"))
411+
.legend_group_title("Legend Group Title")
412412
.opacity(0.5)
413413
.ids(vec!["one"])
414414
.x0(0.0)

Diff for: plotly/src/traces/mesh3d.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,7 @@ mod tests {
493493
.show_legend(true)
494494
.legend_rank(1000)
495495
.legend_group("legend_group")
496-
.legend_group_title(LegendGroupTitle::new("Legend Group Title"))
496+
.legend_group_title("Legend Group Title")
497497
.opacity(0.5)
498498
.ids(vec!["one"])
499499
.face_color(vec!["#ff00ff"])

Diff for: plotly/src/traces/ohlc.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use crate::{
2828
/// let expected = serde_json::json!({
2929
/// "type": "ohlc",
3030
/// "x": ["2022-08-22", "2022-08-23"],
31-
/// "open": [5, 6],
31+
/// "open": [5, 6],
3232
/// "high": [8, 10],
3333
/// "low": [2, 4],
3434
/// "close": [6, 7]
@@ -133,7 +133,7 @@ mod test {
133133
.hover_text("1")
134134
.increasing(Direction::Increasing { line: Line::new() })
135135
.legend_group("legendgroup")
136-
.legend_group_title(LegendGroupTitle::new("Legend Group Title"))
136+
.legend_group_title("Legend Group Title")
137137
.line(Line::new())
138138
.name("ohlc_trace")
139139
.opacity(0.4)

Diff for: plotly/src/traces/sankey.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ mod tests {
436436
.name("sankey")
437437
.visible(true)
438438
.legend_rank(1000)
439-
.legend_group_title(LegendGroupTitle::new("Legend Group Title"))
439+
.legend_group_title("Legend Group Title")
440440
.ids(vec!["one"])
441441
.hover_info(HoverInfo::All)
442442
.hover_label(Label::new())

Diff for: plotly/src/traces/scatter.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ mod tests {
456456
.hover_template_array(vec!["hover_template"])
457457
.ids(vec!["1"])
458458
.legend_group("legend_group")
459-
.legend_group_title(LegendGroupTitle::new("Legend Group Title"))
459+
.legend_group_title("Legend Group Title")
460460
.line(Line::new())
461461
.marker(Marker::new())
462462
.meta("meta")

Diff for: plotly/src/traces/scatter3d.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ mod tests {
367367
.ids(vec!["1"])
368368
.legend_group("legend_group")
369369
.legend_rank(1000)
370-
.legend_group_title(LegendGroupTitle::new("Legend Group Title"))
370+
.legend_group_title("Legend Group Title")
371371
.line(Line::new())
372372
.marker(Marker::new())
373373
.meta("meta")

Diff for: plotly/src/traces/scatter_mapbox.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ mod tests {
311311
.show_legend(true)
312312
.legend_rank(1000)
313313
.legend_group("legend group")
314-
.legend_group_title(LegendGroupTitle::new("Legend Group Title"))
314+
.legend_group_title("Legend Group Title")
315315
.opacity(0.5)
316316
.mode(Mode::LinesText)
317317
.ids(vec!["one"])

Diff for: plotly/src/traces/scatter_polar.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ mod tests {
368368
.hover_text_array(vec!["hover_text"])
369369
.ids(vec!["1"])
370370
.legend_group("legend_group")
371-
.legend_group_title(LegendGroupTitle::new("Legend Group Title"))
371+
.legend_group_title("Legend Group Title")
372372
.line(Line::new())
373373
.marker(Marker::new())
374374
.meta("meta")

Diff for: plotly/src/traces/surface.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ mod tests {
328328
.hover_text("hover_text")
329329
.hover_text_array(vec!["hover_text_1"])
330330
.legend_group("legend_group")
331-
.legend_group_title(LegendGroupTitle::new("Legend Group Title"))
331+
.legend_group_title("Legend Group Title")
332332
.lighting(Lighting::new())
333333
.light_position(Position::new(0, 0, 0))
334334
.name("surface_trace")

Diff for: plotly_derive/src/field_setter.rs

+8
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ enum FieldType {
141141
OptionNumOrString,
142142
OptionNumOrStringCollection,
143143
OptionTitle,
144+
OptionLegendGroupTitle,
144145
OptionOther(syn::Type),
145146
}
146147

@@ -204,6 +205,7 @@ impl FieldType {
204205
FieldType::OptionOther(inner) => quote![#inner],
205206
FieldType::OptionBoxOther(inner) => quote![Box<#inner>],
206207
FieldType::OptionTitle => quote![Title],
208+
FieldType::OptionLegendGroupTitle => quote![LegendGroupTitle],
207209
}
208210
}
209211

@@ -228,6 +230,7 @@ impl FieldType {
228230
["Box", ..] => FieldType::OptionBoxOther(types.get(2).cloned().unwrap()),
229231
["Vec", "Box", "Color"] => FieldType::OptionVecBoxColor,
230232
["Title"] => FieldType::OptionTitle,
233+
["LegendGroupTitle"] => FieldType::OptionLegendGroupTitle,
231234
_ => FieldType::OptionOther(types.get(1).cloned().unwrap()),
232235
}
233236
}
@@ -348,6 +351,11 @@ impl FieldReceiver {
348351
quote![],
349352
),
350353
FieldType::OptionTitle => (quote![impl Into<Title>], quote![value.into()], quote![]),
354+
FieldType::OptionLegendGroupTitle => (
355+
quote![impl Into<LegendGroupTitle>],
356+
quote![value.into()],
357+
quote![],
358+
),
351359
};
352360

353361
struct ModifyEnum {

0 commit comments

Comments
 (0)