charming_fork_zephyr/series/
bar.rs

1use std::vec;
2
3use serde::Serialize;
4
5use crate::{
6    datatype::{DataFrame, DataPoint},
7    element::{BackgroundStyle, ColorBy, CoordinateSystem, Emphasis, ItemStyle, Label, MarkLine},
8};
9
10#[derive(Serialize)]
11#[serde(rename_all = "camelCase")]
12pub struct Bar {
13    #[serde(rename = "type")]
14    type_: String,
15
16    #[serde(skip_serializing_if = "Option::is_none")]
17    id: Option<String>,
18
19    #[serde(skip_serializing_if = "Option::is_none")]
20    color_by: Option<ColorBy>,
21
22    #[serde(skip_serializing_if = "Option::is_none")]
23    name: Option<String>,
24
25    #[serde(skip_serializing_if = "Option::is_none")]
26    legend_hover_link: Option<bool>,
27
28    #[serde(skip_serializing_if = "Option::is_none")]
29    coordinate_system: Option<CoordinateSystem>,
30
31    #[serde(skip_serializing_if = "Option::is_none")]
32    x_axis_index: Option<i64>,
33
34    #[serde(skip_serializing_if = "Option::is_none")]
35    y_axis_index: Option<i64>,
36
37    #[serde(skip_serializing_if = "Option::is_none")]
38    polar_index: Option<i64>,
39
40    #[serde(skip_serializing_if = "Option::is_none")]
41    round_cap: Option<bool>,
42
43    #[serde(skip_serializing_if = "Option::is_none")]
44    realtime_sort: Option<bool>,
45
46    #[serde(skip_serializing_if = "Option::is_none")]
47    show_background: Option<bool>,
48
49    #[serde(skip_serializing_if = "Option::is_none")]
50    background_style: Option<BackgroundStyle>,
51
52    #[serde(skip_serializing_if = "Option::is_none")]
53    label: Option<Label>,
54
55    #[serde(skip_serializing_if = "Option::is_none")]
56    item_style: Option<ItemStyle>,
57
58    #[serde(skip_serializing_if = "Option::is_none")]
59    emphais: Option<Emphasis>,
60
61    #[serde(skip_serializing_if = "Option::is_none")]
62    mark_line: Option<MarkLine>,
63
64    #[serde(skip_serializing_if = "Option::is_none")]
65    stack: Option<String>,
66
67    #[serde(skip_serializing_if = "Option::is_none")]
68    bar_width: Option<i64>,
69
70    #[serde(skip_serializing_if = "Vec::is_empty")]
71    data: DataFrame,
72}
73
74impl Bar {
75    pub fn new() -> Self {
76        Self {
77            type_: "bar".to_string(),
78            id: None,
79            name: None,
80            color_by: None,
81            legend_hover_link: None,
82            coordinate_system: None,
83            x_axis_index: None,
84            y_axis_index: None,
85            polar_index: None,
86            round_cap: None,
87            realtime_sort: None,
88            show_background: None,
89            background_style: None,
90            label: None,
91            item_style: None,
92            emphais: None,
93            mark_line: None,
94            stack: None,
95            bar_width: None,
96            data: vec![],
97        }
98    }
99
100    pub fn id<S: Into<String>>(mut self, id: S) -> Self {
101        self.id = Some(id.into());
102        self
103    }
104
105    pub fn name<S: Into<String>>(mut self, name: S) -> Self {
106        self.name = Some(name.into());
107        self
108    }
109
110    pub fn color_by<C: Into<ColorBy>>(mut self, color_by: C) -> Self {
111        self.color_by = Some(color_by.into());
112        self
113    }
114
115    pub fn legend_hover_link(mut self, legend_hover_link: bool) -> Self {
116        self.legend_hover_link = Some(legend_hover_link);
117        self
118    }
119
120    pub fn coordinate_system<C: Into<CoordinateSystem>>(mut self, coordiate_system: C) -> Self {
121        self.coordinate_system = Some(coordiate_system.into());
122        self
123    }
124
125    pub fn x_axis_index<F: Into<i64>>(mut self, x_axis_index: F) -> Self {
126        self.x_axis_index = Some(x_axis_index.into());
127        self
128    }
129
130    pub fn y_axis_index<F: Into<i64>>(mut self, y_axis_index: F) -> Self {
131        self.y_axis_index = Some(y_axis_index.into());
132        self
133    }
134
135    pub fn polar_index<F: Into<i64>>(mut self, polar_index: F) -> Self {
136        self.polar_index = Some(polar_index.into());
137        self
138    }
139
140    pub fn round_cap(mut self, round_cap: bool) -> Self {
141        self.round_cap = Some(round_cap);
142        self
143    }
144
145    pub fn realtime_sort(mut self, realtime_sort: bool) -> Self {
146        self.realtime_sort = Some(realtime_sort);
147        self
148    }
149
150    pub fn show_background(mut self, show_background: bool) -> Self {
151        self.show_background = Some(show_background);
152        self
153    }
154
155    pub fn background_style<S: Into<BackgroundStyle>>(mut self, background_style: S) -> Self {
156        self.background_style = Some(background_style.into());
157        self
158    }
159
160    pub fn label<L: Into<Label>>(mut self, label: L) -> Self {
161        self.label = Some(label.into());
162        self
163    }
164
165    pub fn item_style<S: Into<ItemStyle>>(mut self, item_style: S) -> Self {
166        self.item_style = Some(item_style.into());
167        self
168    }
169
170    pub fn emphasis<E: Into<Emphasis>>(mut self, emphasis: E) -> Self {
171        self.emphais = Some(emphasis.into());
172        self
173    }
174
175    pub fn mark_line<M: Into<MarkLine>>(mut self, mark_line: M) -> Self {
176        self.mark_line = Some(mark_line.into());
177        self
178    }
179
180    pub fn stack<S: Into<String>>(mut self, stack: S) -> Self {
181        self.stack = Some(stack.into());
182        self
183    }
184
185    pub fn bar_width<F: Into<i64>>(mut self, bar_width: F) -> Self {
186        self.bar_width = Some(bar_width.into());
187        self
188    }
189
190    pub fn data<D: Into<DataPoint>>(mut self, data: Vec<D>) -> Self {
191        self.data = data.into_iter().map(|d| d.into()).collect();
192        self
193    }
194}