charming_fork_zephyr/element/
axis_pointer.rs

1use serde::Serialize;
2
3use crate::{
4    datatype::CompositeValue,
5    element::{Label, LineStyle},
6};
7
8#[derive(Serialize)]
9#[serde(rename_all = "snake_case")]
10pub enum AxisPointerType {
11    Line,
12    Shadow,
13    Cross,
14    None,
15}
16
17#[derive(Serialize)]
18#[serde(rename_all = "snake_case")]
19pub enum AxisPointerAxis {
20    X,
21    Y,
22    Radius,
23    Angle,
24}
25
26#[derive(Serialize)]
27#[serde(rename_all = "camelCase")]
28pub struct AxisPointerLink {
29    #[serde(skip_serializing_if = "Option::is_none")]
30    x_axis_index: Option<CompositeValue>,
31
32    #[serde(skip_serializing_if = "Option::is_none")]
33    x_axis_name: Option<String>,
34
35    #[serde(skip_serializing_if = "Option::is_none")]
36    y_axis_index: Option<CompositeValue>,
37
38    #[serde(skip_serializing_if = "Option::is_none")]
39    y_axis_name: Option<String>,
40}
41
42impl AxisPointerLink {
43    pub fn new() -> Self {
44        Self {
45            x_axis_index: None,
46            x_axis_name: None,
47            y_axis_index: None,
48            y_axis_name: None,
49        }
50    }
51
52    pub fn x_axis_index<C: Into<CompositeValue>>(mut self, x_axis_index: C) -> Self {
53        self.x_axis_index = Some(x_axis_index.into());
54        self
55    }
56
57    pub fn x_axis_name<S: Into<String>>(mut self, x_axis_name: S) -> Self {
58        self.x_axis_name = Some(x_axis_name.into());
59        self
60    }
61
62    pub fn y_axis_index<C: Into<CompositeValue>>(mut self, y_axis_index: C) -> Self {
63        self.y_axis_index = Some(y_axis_index.into());
64        self
65    }
66
67    pub fn y_axis_name<S: Into<String>>(mut self, y_axis_name: S) -> Self {
68        self.y_axis_name = Some(y_axis_name.into());
69        self
70    }
71}
72
73/// Axis Pointer is a tool for displaying reference line and axis value under
74/// mouse pointer.
75#[derive(Serialize)]
76#[serde(rename_all = "camelCase")]
77pub struct AxisPointer {
78    /// Component ID.
79    #[serde(skip_serializing_if = "Option::is_none")]
80    id: Option<String>,
81
82    /// Whether to show the axis pointer.
83    #[serde(skip_serializing_if = "Option::is_none")]
84    show: Option<bool>,
85
86    /// Indicator type.
87    #[serde(rename = "type")]
88    #[serde(skip_serializing_if = "Option::is_none")]
89    type_: Option<AxisPointerType>,
90
91    #[serde(skip_serializing_if = "Option::is_none")]
92    snap: Option<bool>,
93
94    #[serde(skip_serializing_if = "Option::is_none")]
95    animation: Option<bool>,
96
97    #[serde(skip_serializing_if = "Option::is_none")]
98    z: Option<i64>,
99
100    #[serde(skip_serializing_if = "Option::is_none")]
101    axis: Option<AxisPointerAxis>,
102
103    /// Label of axis pointer.
104    #[serde(skip_serializing_if = "Option::is_none")]
105    label: Option<Label>,
106
107    /// Line style of axis pointer.
108    #[serde(skip_serializing_if = "Option::is_none")]
109    line_style: Option<LineStyle>,
110
111    /// Axis pointer can be linked to each other.
112    #[serde(skip_serializing_if = "Vec::is_empty")]
113    link: Vec<AxisPointerLink>,
114}
115
116impl AxisPointer {
117    pub fn new() -> Self {
118        Self {
119            id: None,
120            show: None,
121            type_: None,
122            snap: None,
123            animation: None,
124            z: None,
125            axis: None,
126            label: None,
127            line_style: None,
128            link: vec![],
129        }
130    }
131
132    pub fn id<S: Into<String>>(mut self, id: S) -> Self {
133        self.id = Some(id.into());
134        self
135    }
136
137    pub fn show(mut self, show: bool) -> Self {
138        self.show = Some(show);
139        self
140    }
141
142    pub fn type_<A: Into<AxisPointerType>>(mut self, type_: A) -> Self {
143        self.type_ = Some(type_.into());
144        self
145    }
146
147    pub fn snap(mut self, snap: bool) -> Self {
148        self.snap = Some(snap);
149        self
150    }
151
152    pub fn animation(mut self, animation: bool) -> Self {
153        self.animation = Some(animation);
154        self
155    }
156
157    pub fn z<F: Into<i64>>(mut self, z: F) -> Self {
158        self.z = Some(z.into());
159        self
160    }
161
162    pub fn axis<A: Into<AxisPointerAxis>>(mut self, axis: A) -> Self {
163        self.axis = Some(axis.into());
164        self
165    }
166
167    pub fn label<A: Into<Label>>(mut self, label: A) -> Self {
168        self.label = Some(label.into());
169        self
170    }
171
172    pub fn line_style<A: Into<LineStyle>>(mut self, line_style: A) -> Self {
173        self.line_style = Some(line_style.into());
174        self
175    }
176
177    pub fn link<A: Into<AxisPointerLink>>(mut self, link: Vec<A>) -> Self {
178        self.link = link.into_iter().map(|a| a.into()).collect();
179        self
180    }
181}