charming_fork_zephyr/component/
axis.rs

1use serde::Serialize;
2
3use crate::{
4    datatype::CompositeValue,
5    element::{
6        AxisLabel, AxisLine, AxisPointer, AxisTick, AxisType, BoundaryGap, NameLocation, SplitArea,
7        SplitLine, TextStyle,
8    },
9};
10
11/// Axis in cartesian coordinate.
12#[derive(Serialize)]
13#[serde(rename_all = "camelCase")]
14pub struct Axis {
15    /// Type of axis.
16    #[serde(skip_serializing_if = "Option::is_none")]
17    #[serde(rename = "type")]
18    type_: Option<AxisType>,
19
20    /// Id of axis.
21    #[serde(skip_serializing_if = "Option::is_none")]
22    id: Option<String>,
23
24    /// Whether to show the axis.
25    #[serde(skip_serializing_if = "Option::is_none")]
26    show: Option<bool>,
27
28    /// Index of grid which is used to place this axis.
29    #[serde(skip_serializing_if = "Option::is_none")]
30    grid_index: Option<i64>,
31
32    /// Offset of this axis relative to default position.
33    #[serde(skip_serializing_if = "Option::is_none")]
34    offset: Option<i64>,
35
36    /// Name of axis.
37    #[serde(skip_serializing_if = "Option::is_none")]
38    name: Option<String>,
39
40    /// Location of axis name.
41    #[serde(skip_serializing_if = "Option::is_none")]
42    name_location: Option<NameLocation>,
43
44    /// Text style of axis name.
45    #[serde(skip_serializing_if = "Option::is_none")]
46    name_text_style: Option<TextStyle>,
47
48    /// Gap between axis name and axis line.
49    #[serde(skip_serializing_if = "Option::is_none")]
50    name_gap: Option<i64>,
51
52    /// Rotation of axis name
53    #[serde(skip_serializing_if = "Option::is_none")]
54    name_rotation: Option<i64>,
55
56    /// Set this to `true` to invert the axis.
57    #[serde(skip_serializing_if = "Option::is_none")]
58    inverse: Option<bool>,
59
60    #[serde(skip_serializing_if = "Option::is_none")]
61    align_ticks: Option<bool>,
62
63    /// The boundary gap on both sides of a coordinate axis.
64    #[serde(skip_serializing_if = "Option::is_none")]
65    boundary_gap: Option<BoundaryGap>,
66
67    #[serde(skip_serializing_if = "Option::is_none")]
68    position: Option<CompositeValue>,
69
70    /// The mimimum value of axis.
71    #[serde(skip_serializing_if = "Option::is_none")]
72    min: Option<CompositeValue>,
73
74    /// The maximum value of axis.
75    #[serde(skip_serializing_if = "Option::is_none")]
76    max: Option<CompositeValue>,
77
78    #[serde(skip_serializing_if = "Option::is_none")]
79    scale: Option<bool>,
80
81    /// Number of segments that the axis is split into.
82    #[serde(skip_serializing_if = "Option::is_none")]
83    split_number: Option<i64>,
84
85    /// Minimum gap between split lines.
86    #[serde(skip_serializing_if = "Option::is_none")]
87    min_interval: Option<i64>,
88
89    /// Maximum gap between split lines.
90    #[serde(skip_serializing_if = "Option::is_none")]
91    max_interval: Option<i64>,
92
93    /// Compulsively set segmentation interval for axis.
94    #[serde(skip_serializing_if = "Option::is_none")]
95    interval: Option<i64>,
96
97    /// Base of logarithm, which is valid only for numeric axes with `log` type.
98    #[serde(skip_serializing_if = "Option::is_none")]
99    log_base: Option<i64>,
100
101    /// Settings related to axis label.
102    #[serde(skip_serializing_if = "Option::is_none")]
103    axis_label: Option<AxisLabel>,
104
105    /// Settings related to axis tick.
106    #[serde(skip_serializing_if = "Option::is_none")]
107    axis_tick: Option<AxisTick>,
108
109    /// Settings related to axis line.
110    #[serde(skip_serializing_if = "Option::is_none")]
111    axis_line: Option<AxisLine>,
112
113    /// Settings related to axis pointer.
114    #[serde(skip_serializing_if = "Option::is_none")]
115    axis_pointer: Option<AxisPointer>,
116
117    /// Settings related to split area.
118    #[serde(skip_serializing_if = "Option::is_none")]
119    split_area: Option<SplitArea>,
120
121    /// Settings related to split line.
122    #[serde(skip_serializing_if = "Option::is_none")]
123    split_line: Option<SplitLine>,
124
125    #[serde(skip_serializing_if = "Vec::is_empty")]
126    data: Vec<String>,
127}
128
129impl Axis {
130    pub fn new() -> Self {
131        Self {
132            type_: None,
133            id: None,
134            show: None,
135            grid_index: None,
136            offset: None,
137            name: None,
138            name_location: None,
139            name_text_style: None,
140            name_gap: None,
141            name_rotation: None,
142            inverse: None,
143            boundary_gap: None,
144            position: None,
145            min: None,
146            max: None,
147            scale: None,
148            split_number: None,
149            min_interval: None,
150            max_interval: None,
151            interval: None,
152            align_ticks: None,
153            log_base: None,
154            axis_label: None,
155            axis_tick: None,
156            axis_line: None,
157            axis_pointer: None,
158            split_area: None,
159            split_line: None,
160            data: vec![],
161        }
162    }
163
164    pub fn type_<T: Into<AxisType>>(mut self, type_: T) -> Self {
165        self.type_ = Some(type_.into());
166        self
167    }
168
169    pub fn id<S: Into<String>>(mut self, id: S) -> Self {
170        self.id = Some(id.into());
171        self
172    }
173
174    pub fn show(mut self, show: bool) -> Self {
175        self.show = Some(show);
176        self
177    }
178
179    pub fn grid_index<F: Into<i64>>(mut self, grid_index: F) -> Self {
180        self.grid_index = Some(grid_index.into());
181        self
182    }
183
184    pub fn offset<F: Into<i64>>(mut self, offset: F) -> Self {
185        self.offset = Some(offset.into());
186        self
187    }
188
189    pub fn name<S: Into<String>>(mut self, name: S) -> Self {
190        self.name = Some(name.into());
191        self
192    }
193
194    pub fn name_location<E: Into<NameLocation>>(mut self, name_location: E) -> Self {
195        self.name_location = Some(name_location.into());
196        self
197    }
198
199    pub fn name_text_style<T: Into<TextStyle>>(mut self, name_text_style: T) -> Self {
200        self.name_text_style = Some(name_text_style.into());
201        self
202    }
203
204    pub fn name_gap<F: Into<i64>>(mut self, name_gap: F) -> Self {
205        self.name_gap = Some(name_gap.into());
206        self
207    }
208
209    pub fn name_rotation<F: Into<i64>>(mut self, name_rotation: F) -> Self {
210        self.name_rotation = Some(name_rotation.into());
211        self
212    }
213
214    pub fn inverse(mut self, inverse: bool) -> Self {
215        self.inverse = Some(inverse);
216        self
217    }
218
219    pub fn align_ticks(mut self, align_ticks: bool) -> Self {
220        self.align_ticks = Some(align_ticks);
221        self
222    }
223
224    pub fn boundary_gap<B: Into<BoundaryGap>>(mut self, boundary_gap: B) -> Self {
225        self.boundary_gap = Some(boundary_gap.into());
226        self
227    }
228
229    pub fn position<C: Into<CompositeValue>>(mut self, position: C) -> Self {
230        self.position = Some(position.into());
231        self
232    }
233
234    pub fn min<C: Into<CompositeValue>>(mut self, min: C) -> Self {
235        self.min = Some(min.into());
236        self
237    }
238
239    pub fn max<C: Into<CompositeValue>>(mut self, max: C) -> Self {
240        self.max = Some(max.into());
241        self
242    }
243
244    pub fn scale(mut self, scale: bool) -> Self {
245        self.scale = Some(scale);
246        self
247    }
248
249    pub fn split_number<F: Into<i64>>(mut self, split_number: F) -> Self {
250        self.split_number = Some(split_number.into());
251        self
252    }
253
254    pub fn min_interval<F: Into<i64>>(mut self, min_interval: F) -> Self {
255        self.min_interval = Some(min_interval.into());
256        self
257    }
258
259    pub fn max_interval<F: Into<i64>>(mut self, max_interval: F) -> Self {
260        self.max_interval = Some(max_interval.into());
261        self
262    }
263
264    pub fn interval<F: Into<i64>>(mut self, interval: F) -> Self {
265        self.interval = Some(interval.into());
266        self
267    }
268
269    pub fn log_base<F: Into<i64>>(mut self, log_base: F) -> Self {
270        self.log_base = Some(log_base.into());
271        self
272    }
273
274    pub fn axis_label<L: Into<AxisLabel>>(mut self, axis_label: L) -> Self {
275        self.axis_label = Some(axis_label.into());
276        self
277    }
278
279    pub fn axis_tick<T: Into<AxisTick>>(mut self, axis_tick: T) -> Self {
280        self.axis_tick = Some(axis_tick.into());
281        self
282    }
283
284    pub fn axis_line<L: Into<AxisLine>>(mut self, axis_line: L) -> Self {
285        self.axis_line = Some(axis_line.into());
286        self
287    }
288
289    pub fn axis_pointer<P: Into<AxisPointer>>(mut self, axis_pointer: P) -> Self {
290        self.axis_pointer = Some(axis_pointer.into());
291        self
292    }
293
294    pub fn split_area<A: Into<SplitArea>>(mut self, split_area: A) -> Self {
295        self.split_area = Some(split_area.into());
296        self
297    }
298
299    pub fn split_line<A: Into<SplitLine>>(mut self, split_line: A) -> Self {
300        self.split_line = Some(split_line.into());
301        self
302    }
303
304    pub fn data<S: Into<String>>(mut self, data: Vec<S>) -> Self {
305        self.data = data.into_iter().map(|s| s.into()).collect();
306        self
307    }
308}