charming_fork_zephyr/element/
axis_line.rs1use serde::{ser::SerializeSeq, Serialize};
2
3use super::color::Color;
4
5pub struct ColorSegment(i64, Color);
6
7impl Serialize for ColorSegment {
8 fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
9 let mut seq = serializer.serialize_seq(Some(2))?;
10 seq.serialize_element(&self.0)?;
11 seq.serialize_element(&self.1)?;
12 seq.end()
13 }
14}
15
16impl From<(i64, &str)> for ColorSegment {
17 fn from(tuple: (i64, &str)) -> Self {
18 Self(tuple.0, tuple.1.into())
19 }
20}
21
22impl From<(i64, Color)> for ColorSegment {
23 fn from(tuple: (i64, Color)) -> Self {
24 Self(tuple.0, tuple.1)
25 }
26}
27
28#[derive(Serialize)]
29#[serde(rename_all = "camelCase")]
30pub struct AxisLineStyle {
31 color: Vec<ColorSegment>,
32
33 #[serde(skip_serializing_if = "Option::is_none")]
34 width: Option<i64>,
35
36 #[serde(skip_serializing_if = "Option::is_none")]
37 shadow_blur: Option<i64>,
38
39 #[serde(skip_serializing_if = "Option::is_none")]
40 shadow_color: Option<Color>,
41
42 #[serde(skip_serializing_if = "Option::is_none")]
43 shadow_offset_x: Option<i64>,
44
45 #[serde(skip_serializing_if = "Option::is_none")]
46 shadow_offset_y: Option<i64>,
47
48 #[serde(skip_serializing_if = "Option::is_none")]
49 opacity: Option<i64>,
50}
51
52impl AxisLineStyle {
53 pub fn new() -> Self {
54 Self {
55 color: vec![],
56 width: None,
57 shadow_blur: None,
58 shadow_color: None,
59 shadow_offset_x: None,
60 shadow_offset_y: None,
61 opacity: None,
62 }
63 }
64
65 pub fn color<C: Into<ColorSegment>>(mut self, color: C) -> Self {
66 self.color.push(color.into());
67 self
68 }
69
70 pub fn width<F: Into<i64>>(mut self, width: F) -> Self {
71 self.width = Some(width.into());
72 self
73 }
74
75 pub fn shadow_blur<F: Into<i64>>(mut self, shadow_blur: F) -> Self {
76 self.shadow_blur = Some(shadow_blur.into());
77 self
78 }
79
80 pub fn shadow_color<C: Into<Color>>(mut self, shadow_color: C) -> Self {
81 self.shadow_color = Some(shadow_color.into());
82 self
83 }
84
85 pub fn shadow_offset_x<F: Into<i64>>(mut self, shadow_offset_x: F) -> Self {
86 self.shadow_offset_x = Some(shadow_offset_x.into());
87 self
88 }
89
90 pub fn shadow_offset_y<F: Into<i64>>(mut self, shadow_offset_y: F) -> Self {
91 self.shadow_offset_y = Some(shadow_offset_y.into());
92 self
93 }
94
95 pub fn opacity<F: Into<i64>>(mut self, opacity: F) -> Self {
96 self.opacity = Some(opacity.into());
97 self
98 }
99}
100
101impl From<(i64, &str)> for AxisLineStyle {
102 fn from(tuple: (i64, &str)) -> Self {
103 Self::new().color(tuple)
104 }
105}
106
107impl From<(i64, &str, i64)> for AxisLineStyle {
108 fn from(tuple: (i64, &str, i64)) -> Self {
109 Self::new().color((tuple.0, tuple.1)).width(tuple.2)
110 }
111}
112
113#[derive(Serialize)]
114#[serde(rename_all = "camelCase")]
115pub struct AxisLine {
116 #[serde(skip_serializing_if = "Option::is_none")]
117 show: Option<bool>,
118
119 #[serde(skip_serializing_if = "Option::is_none")]
120 on_zero: Option<bool>,
121
122 #[serde(skip_serializing_if = "Option::is_none")]
123 round_cap: Option<bool>,
124
125 #[serde(skip_serializing_if = "Option::is_none")]
126 line_style: Option<AxisLineStyle>,
127}
128
129impl AxisLine {
130 pub fn new() -> Self {
131 Self {
132 show: None,
133 on_zero: None,
134 round_cap: None,
135 line_style: None,
136 }
137 }
138
139 pub fn show(mut self, show: bool) -> Self {
140 self.show = Some(show);
141 self
142 }
143
144 pub fn on_zero(mut self, on_zero: bool) -> Self {
145 self.on_zero = Some(on_zero);
146 self
147 }
148
149 pub fn round_cap(mut self, round_cap: bool) -> Self {
150 self.round_cap = Some(round_cap);
151 self
152 }
153
154 pub fn line_style<S: Into<AxisLineStyle>>(mut self, line_style: S) -> Self {
155 self.line_style = Some(line_style.into());
156 self
157 }
158}