plotly_patched/
bar.rs

1//! Bar plot
2
3use crate::common::{
4    Calendar, ConstrainText, Dim, ErrorData, Font, HoverInfo, Label, Marker, Orientation, PlotType,
5    TextAnchor, TextPosition,
6};
7use crate::Trace;
8use serde::Serialize;
9
10use crate::private;
11
12#[derive(Serialize, Debug, Default)]
13pub struct Bar<X, Y>
14where
15    X: Serialize + Default,
16    Y: Serialize + Default,
17{
18    x: Vec<X>,
19    y: Vec<Y>,
20    r#type: PlotType,
21    #[serde(skip_serializing_if = "Option::is_none")]
22    name: Option<String>,
23    #[serde(skip_serializing_if = "Option::is_none")]
24    visible: Option<bool>,
25    #[serde(skip_serializing_if = "Option::is_none", rename = "showlegend")]
26    show_legend: Option<bool>,
27    #[serde(skip_serializing_if = "Option::is_none", rename = "legendgroup")]
28    legend_group: Option<String>,
29    #[serde(skip_serializing_if = "Option::is_none")]
30    opacity: Option<f64>,
31    #[serde(skip_serializing_if = "Option::is_none")]
32    ids: Option<Vec<String>>,
33    #[serde(skip_serializing_if = "Option::is_none")]
34    width: Option<usize>,
35    #[serde(skip_serializing_if = "Option::is_none")]
36    offset: Option<Dim<usize>>,
37    #[serde(skip_serializing_if = "Option::is_none")]
38    text: Option<Dim<String>>,
39    #[serde(skip_serializing_if = "Option::is_none", rename = "textposition")]
40    text_position: Option<Dim<TextPosition>>,
41    #[serde(skip_serializing_if = "Option::is_none", rename = "texttemplate")]
42    text_template: Option<Dim<String>>,
43    #[serde(skip_serializing_if = "Option::is_none", rename = "hovertext")]
44    hover_text: Option<Dim<String>>,
45    #[serde(skip_serializing_if = "Option::is_none", rename = "hoverinfo")]
46    hover_info: Option<HoverInfo>,
47    #[serde(skip_serializing_if = "Option::is_none", rename = "hovertemplate")]
48    hover_template: Option<Dim<String>>,
49    #[serde(skip_serializing_if = "Option::is_none", rename = "xaxis")]
50    x_axis: Option<String>,
51    #[serde(skip_serializing_if = "Option::is_none", rename = "yaxis")]
52    y_axis: Option<String>,
53    #[serde(skip_serializing_if = "Option::is_none")]
54    orientation: Option<Orientation>,
55    #[serde(skip_serializing_if = "Option::is_none", rename = "alignmentgroup")]
56    alignment_group: Option<String>,
57    #[serde(skip_serializing_if = "Option::is_none", rename = "offsetgroup")]
58    offset_group: Option<String>,
59    #[serde(skip_serializing_if = "Option::is_none")]
60    marker: Option<Marker>,
61    #[serde(skip_serializing_if = "Option::is_none", rename = "textangle")]
62    text_angle: Option<f64>,
63    #[serde(skip_serializing_if = "Option::is_none", rename = "textfont")]
64    text_font: Option<Font>,
65    #[serde(skip_serializing_if = "Option::is_none")]
66    error_x: Option<ErrorData>,
67    #[serde(skip_serializing_if = "Option::is_none")]
68    error_y: Option<ErrorData>,
69    #[serde(skip_serializing_if = "Option::is_none", rename = "cliponaxis")]
70    clip_on_axis: Option<bool>,
71    #[serde(skip_serializing_if = "Option::is_none", rename = "constraintext")]
72    constrain_text: Option<ConstrainText>,
73    #[serde(skip_serializing_if = "Option::is_none", rename = "hoverlabel")]
74    hover_label: Option<Label>,
75    #[serde(skip_serializing_if = "Option::is_none", rename = "insidetextanchor")]
76    inside_text_anchor: Option<TextAnchor>,
77    #[serde(skip_serializing_if = "Option::is_none", rename = "insidetextfont")]
78    inside_text_font: Option<Font>,
79    #[serde(skip_serializing_if = "Option::is_none", rename = "outsidetextfont")]
80    outside_text_font: Option<Font>,
81    #[serde(skip_serializing_if = "Option::is_none", rename = "xcalendar")]
82    x_calendar: Option<Calendar>,
83    #[serde(skip_serializing_if = "Option::is_none", rename = "ycalendar")]
84    y_calendar: Option<Calendar>,
85}
86
87impl<X, Y> Bar<X, Y>
88where
89    X: Serialize + Default,
90    Y: Serialize + Default,
91{
92    pub fn new(x: Vec<X>, y: Vec<Y>) -> Box<Bar<X, Y>> {
93        Box::new(Bar {
94            x,
95            y,
96            r#type: PlotType::Bar,
97            ..Default::default()
98        })
99    }
100
101    pub fn name(mut self, name: &str) -> Box<Bar<X, Y>> {
102        self.name = Some(name.to_owned());
103        Box::new(self)
104    }
105
106    pub fn visible(mut self, visible: bool) -> Box<Bar<X, Y>> {
107        self.visible = Some(visible);
108        Box::new(self)
109    }
110
111    pub fn show_legend(mut self, show_legend: bool) -> Box<Bar<X, Y>> {
112        self.show_legend = Some(show_legend);
113        Box::new(self)
114    }
115
116    pub fn legend_group(mut self, legend_group: &str) -> Box<Bar<X, Y>> {
117        self.legend_group = Some(legend_group.to_owned());
118        Box::new(self)
119    }
120
121    pub fn opacity(mut self, opacity: f64) -> Box<Bar<X, Y>> {
122        self.opacity = Some(opacity);
123        Box::new(self)
124    }
125
126    pub fn ids<S: AsRef<str>>(mut self, ids: Vec<S>) -> Box<Bar<X, Y>> {
127        let ids = private::owned_string_vector(ids);
128        self.ids = Some(ids);
129        Box::new(self)
130    }
131
132    pub fn width(mut self, width: usize) -> Box<Bar<X, Y>> {
133        self.width = Some(width);
134        Box::new(self)
135    }
136
137    pub fn offset(mut self, offset: usize) -> Box<Bar<X, Y>> {
138        self.offset = Some(Dim::Scalar(offset));
139        Box::new(self)
140    }
141
142    pub fn offset_array(mut self, offset: Vec<usize>) -> Box<Bar<X, Y>> {
143        self.offset = Some(Dim::Vector(offset));
144        Box::new(self)
145    }
146
147    pub fn text(mut self, text: &str) -> Box<Bar<X, Y>> {
148        self.text = Some(Dim::Scalar(text.to_owned()));
149        Box::new(self)
150    }
151
152    pub fn text_array<S: AsRef<str>>(mut self, text: Vec<S>) -> Box<Bar<X, Y>> {
153        let text = private::owned_string_vector(text);
154        self.text = Some(Dim::Vector(text));
155        Box::new(self)
156    }
157
158    pub fn text_position(mut self, text_position: TextPosition) -> Box<Bar<X, Y>> {
159        self.text_position = Some(Dim::Scalar(text_position));
160        Box::new(self)
161    }
162
163    pub fn text_position_array(mut self, text_position: Vec<TextPosition>) -> Box<Bar<X, Y>> {
164        self.text_position = Some(Dim::Vector(text_position));
165        Box::new(self)
166    }
167
168    pub fn text_template(mut self, text_template: &str) -> Box<Bar<X, Y>> {
169        self.text_template = Some(Dim::Scalar(text_template.to_owned()));
170        Box::new(self)
171    }
172
173    pub fn text_template_array<S: AsRef<str>>(mut self, text_template: Vec<S>) -> Box<Bar<X, Y>> {
174        let text_template = private::owned_string_vector(text_template);
175        self.text_template = Some(Dim::Vector(text_template));
176        Box::new(self)
177    }
178
179    pub fn hover_text(mut self, hover_text: &str) -> Box<Bar<X, Y>> {
180        self.hover_text = Some(Dim::Scalar(hover_text.to_owned()));
181        Box::new(self)
182    }
183
184    pub fn hover_text_array<S: AsRef<str>>(mut self, hover_text: Vec<S>) -> Box<Bar<X, Y>> {
185        let hover_text = private::owned_string_vector(hover_text);
186        self.hover_text = Some(Dim::Vector(hover_text));
187        Box::new(self)
188    }
189
190    pub fn hover_info(mut self, hover_info: HoverInfo) -> Box<Bar<X, Y>> {
191        self.hover_info = Some(hover_info);
192        Box::new(self)
193    }
194
195    pub fn hover_template(mut self, hover_template: &str) -> Box<Bar<X, Y>> {
196        self.hover_template = Some(Dim::Scalar(hover_template.to_owned()));
197        Box::new(self)
198    }
199
200    pub fn x_axis(mut self, axis: &str) -> Box<Bar<X, Y>> {
201        self.x_axis = Some(axis.to_owned());
202        Box::new(self)
203    }
204
205    pub fn y_axis(mut self, axis: &str) -> Box<Bar<X, Y>> {
206        self.y_axis = Some(axis.to_owned());
207        Box::new(self)
208    }
209
210    pub fn hover_template_array<S: AsRef<str>>(mut self, hover_template: Vec<S>) -> Box<Bar<X, Y>> {
211        let hover_template = private::owned_string_vector(hover_template);
212        self.hover_template = Some(Dim::Vector(hover_template));
213        Box::new(self)
214    }
215
216    pub fn orientation(mut self, orientation: Orientation) -> Box<Bar<X, Y>> {
217        self.orientation = Some(orientation);
218        Box::new(self)
219    }
220
221    pub fn alignment_group(mut self, alignment_group: &str) -> Box<Bar<X, Y>> {
222        self.alignment_group = Some(alignment_group.to_owned());
223        Box::new(self)
224    }
225
226    pub fn offset_group(mut self, offset_group: &str) -> Box<Bar<X, Y>> {
227        self.offset_group = Some(offset_group.to_owned());
228        Box::new(self)
229    }
230
231    pub fn marker(mut self, marker: Marker) -> Box<Bar<X, Y>> {
232        self.marker = Some(marker);
233        Box::new(self)
234    }
235
236    pub fn text_angle(mut self, text_angle: f64) -> Box<Bar<X, Y>> {
237        self.text_angle = Some(text_angle);
238        Box::new(self)
239    }
240
241    pub fn text_font(mut self, text_font: Font) -> Box<Bar<X, Y>> {
242        self.text_font = Some(text_font);
243        Box::new(self)
244    }
245
246    pub fn error_x(mut self, error_x: ErrorData) -> Box<Bar<X, Y>> {
247        self.error_x = Some(error_x);
248        Box::new(self)
249    }
250
251    pub fn error_y(mut self, error_y: ErrorData) -> Box<Bar<X, Y>> {
252        self.error_y = Some(error_y);
253        Box::new(self)
254    }
255
256    pub fn clip_on_axis(mut self, clip_on_axis: bool) -> Box<Bar<X, Y>> {
257        self.clip_on_axis = Some(clip_on_axis);
258        Box::new(self)
259    }
260
261    pub fn constrain_text(mut self, constrain_text: ConstrainText) -> Box<Bar<X, Y>> {
262        self.constrain_text = Some(constrain_text);
263        Box::new(self)
264    }
265
266    pub fn hover_label(mut self, hover_label: Label) -> Box<Bar<X, Y>> {
267        self.hover_label = Some(hover_label);
268        Box::new(self)
269    }
270
271    pub fn inside_text_anchor(mut self, inside_text_anchor: TextAnchor) -> Box<Bar<X, Y>> {
272        self.inside_text_anchor = Some(inside_text_anchor);
273        Box::new(self)
274    }
275
276    pub fn inside_text_font(mut self, inside_text_font: Font) -> Box<Bar<X, Y>> {
277        self.inside_text_font = Some(inside_text_font);
278        Box::new(self)
279    }
280
281    pub fn outside_text_font(mut self, outside_text_font: Font) -> Box<Bar<X, Y>> {
282        self.outside_text_font = Some(outside_text_font);
283        Box::new(self)
284    }
285
286    pub fn x_calendar(mut self, x_calendar: Calendar) -> Box<Bar<X, Y>> {
287        self.x_calendar = Some(x_calendar);
288        Box::new(self)
289    }
290
291    pub fn y_calendar(mut self, y_calendar: Calendar) -> Box<Bar<X, Y>> {
292        self.y_calendar = Some(y_calendar);
293        Box::new(self)
294    }
295}
296
297impl<X, Y> Trace for Bar<X, Y>
298where
299    X: Serialize + Default,
300    Y: Serialize + Default,
301{
302    fn serialize(&self) -> String {
303        serde_json::to_string(&self).unwrap()
304    }
305}