charming_fork_zephyr/component/
aria.rs

1use serde::Serialize;
2
3use crate::{
4    datatype::CompositeValue,
5    element::{Color, Label, Symbol},
6};
7
8/**
9A single decal pattern.
10 */
11#[derive(Serialize)]
12#[serde(rename_all = "camelCase")]
13pub struct DecalItem {
14    /// The symbol type of the decal.
15    #[serde(skip_serializing_if = "Option::is_none")]
16    symbol: Option<Symbol>,
17
18    /// The size of symbol relative to decal, ranging from 0 to 1.
19    #[serde(skip_serializing_if = "Option::is_none")]
20    symbol_size: Option<i64>,
21
22    /// Whether to keep the aspect ratio of the pattern.
23    #[serde(skip_serializing_if = "Option::is_none")]
24    symbol_keep_aspect: Option<bool>,
25
26    ///The color of the decal pattern. it is recommended to use a translucent
27    /// color, which can be superimposed on the color of the series itself.
28    #[serde(skip_serializing_if = "Option::is_none")]
29    color: Option<Color>,
30
31    /// The background color of the decal will be over the color of the series
32    /// itself, under the decal pattern.
33    #[serde(skip_serializing_if = "Option::is_none")]
34    background_color: Option<Color>,
35
36    /// Controls the horizontal pattern.
37    #[serde(skip_serializing_if = "Option::is_none")]
38    dash_array_x: Option<CompositeValue>,
39
40    /// Controls the vertical pattern.
41    #[serde(skip_serializing_if = "Option::is_none")]
42    dash_array_y: Option<CompositeValue>,
43
44    /// The overall rotation angle (in radians) of the pattern.
45    #[serde(skip_serializing_if = "Option::is_none")]
46    rotation: Option<i64>,
47
48    /// The upper limit of the width of the generated pattern before it is
49    /// duplicated.
50    #[serde(skip_serializing_if = "Option::is_none")]
51    max_tile_width: Option<i64>,
52
53    /// The upper limit of the height of the generated pattern before it is
54    /// duplicated.
55    #[serde(skip_serializing_if = "Option::is_none")]
56    max_tile_height: Option<i64>,
57}
58
59impl DecalItem {
60    pub fn new() -> DecalItem {
61        DecalItem {
62            symbol: None,
63            symbol_size: None,
64            symbol_keep_aspect: None,
65            color: None,
66            background_color: None,
67            dash_array_x: None,
68            dash_array_y: None,
69            rotation: None,
70            max_tile_width: None,
71            max_tile_height: None,
72        }
73    }
74
75    pub fn symbol<S: Into<Symbol>>(mut self, symbol: S) -> DecalItem {
76        self.symbol = Some(symbol.into());
77        self
78    }
79
80    pub fn symbol_size<F: Into<i64>>(mut self, symbol_size: F) -> DecalItem {
81        self.symbol_size = Some(symbol_size.into());
82        self
83    }
84
85    pub fn symbol_keep_aspect(mut self, symbol_keep_aspect: bool) -> DecalItem {
86        self.symbol_keep_aspect = Some(symbol_keep_aspect);
87        self
88    }
89
90    pub fn color<C: Into<Color>>(mut self, color: C) -> DecalItem {
91        self.color = Some(color.into());
92        self
93    }
94
95    pub fn background_color<C: Into<Color>>(mut self, background_color: C) -> DecalItem {
96        self.background_color = Some(background_color.into());
97        self
98    }
99
100    pub fn dash_array_x<F: Into<CompositeValue>>(mut self, dash_array_x: F) -> DecalItem {
101        self.dash_array_x = Some(dash_array_x.into());
102        self
103    }
104
105    pub fn dash_array_y<F: Into<CompositeValue>>(mut self, dash_array_y: F) -> DecalItem {
106        self.dash_array_y = Some(dash_array_y.into());
107        self
108    }
109
110    pub fn rotation<F: Into<i64>>(mut self, rotation: F) -> DecalItem {
111        self.rotation = Some(rotation.into());
112        self
113    }
114
115    pub fn max_tile_width<F: Into<i64>>(mut self, max_tile_width: F) -> DecalItem {
116        self.max_tile_width = Some(max_tile_width.into());
117        self
118    }
119
120    pub fn max_tile_height<F: Into<i64>>(mut self, max_tile_height: F) -> DecalItem {
121        self.max_tile_height = Some(max_tile_height.into());
122        self
123    }
124}
125
126/**
127Decal patterns to be applied to series data.
128 */
129#[derive(Serialize)]
130#[serde(rename_all = "camelCase")]
131pub struct Decal {
132    /// Whether to show decal patterns. If `decals` is not set, this option is
133    /// used to enable the default decal pattern.
134    #[serde(skip_serializing_if = "Option::is_none")]
135    show: Option<bool>,
136
137    /// The style of decal patterns. If multiple items are set, then each item
138    /// in the array will have one style and the data will be looped through
139    /// the array in order.
140    #[serde(skip_serializing_if = "Vec::is_empty")]
141    decals: Vec<DecalItem>,
142}
143
144impl Decal {
145    pub fn new() -> Decal {
146        Decal {
147            show: None,
148            decals: vec![],
149        }
150    }
151
152    pub fn show(mut self, show: bool) -> Decal {
153        self.show = Some(show);
154        self
155    }
156
157    pub fn decals<D: Into<DecalItem>>(mut self, decals: Vec<D>) -> Decal {
158        self.decals = decals.into_iter().map(|d| d.into()).collect();
159        self
160    }
161}
162
163/**
164The WAI-ARIA (Accessible Rich Internet Applications Suite) is a W3C standard
165that dedicates to make web content and web applications more accessible.
166
167It is turned off by default, and needs to be turned on by setting `enabled` to
168`true`.
169
170Here's a simple example that enables default decal pattern on a bar chart:
171
172```rust
173use charming::{
174    component::{Aria, Axis, Decal},
175    element::AxisType,
176    series::Bar,
177    Chart,
178};
179
180Chart::new()
181    .x_axis(
182        Axis::new()
183            .type_(AxisType::Category)
184            .data(vec!["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]),
185    )
186    .y_axis(Axis::new().type_(AxisType::Value))
187    .aria(Aria::new().enabled(true).decal(Decal::new().show(true)))
188    .series(Bar::new().data(vec![120, 200, 150, 80, 70, 110, 130]))
189    .series(Bar::new().data(vec![20, 40, 90, 40, 30, 70, 120]))
190    .series(Bar::new().data(vec![140, 230, 120, 50, 30, 150, 120]));
191```
192 */
193#[derive(Serialize)]
194#[serde(rename_all = "camelCase")]
195pub struct Aria {
196    /// Whether to enable WAI-ARIA.
197    #[serde(skip_serializing_if = "Option::is_none")]
198    enabled: Option<bool>,
199
200    /// If `enabled` is set to `true`, `label` is enabled by default. When
201    /// enabled, the description of the chart will be automatically and
202    /// intelligently generated based on the chart, data title, etc. Users can
203    /// also modify the description through `label`.
204    #[serde(skip_serializing_if = "Option::is_none")]
205    label: Option<Label>,
206
207    /// Decal patterns are added to series data as an additional hint other
208    /// than colors to help differentiate the data.
209    #[serde(skip_serializing_if = "Option::is_none")]
210    decal: Option<Decal>,
211}
212
213impl Aria {
214    pub fn new() -> Aria {
215        Aria {
216            enabled: None,
217            label: None,
218            decal: None,
219        }
220    }
221
222    pub fn enabled(mut self, enabled: bool) -> Aria {
223        self.enabled = Some(enabled);
224        self
225    }
226
227    pub fn label<L: Into<Label>>(mut self, label: L) -> Aria {
228        self.label = Some(label.into());
229        self
230    }
231
232    pub fn decal<D: Into<Decal>>(mut self, decal: D) -> Aria {
233        self.decal = Some(decal.into());
234        self
235    }
236}