charming_fork_zephyr/series/
scatter.rs

1use serde::Serialize;
2
3use crate::{
4    datatype::{DataFrame, DataPoint},
5    element::{
6        ColorBy, CoordinateSystem, DimensionEncode, Emphasis, ItemStyle, MarkArea, MarkLine,
7        Symbol, SymbolSize,
8    },
9};
10
11#[derive(Serialize)]
12#[serde(rename_all = "camelCase")]
13pub struct Scatter {
14    #[serde(rename = "type")]
15    type_: String,
16
17    #[serde(skip_serializing_if = "Option::is_none")]
18    id: Option<String>,
19
20    #[serde(skip_serializing_if = "Option::is_none")]
21    name: Option<String>,
22
23    #[serde(skip_serializing_if = "Option::is_none")]
24    color_by: Option<ColorBy>,
25
26    #[serde(skip_serializing_if = "Option::is_none")]
27    dataset_index: Option<i64>,
28
29    #[serde(skip_serializing_if = "Option::is_none")]
30    coordinate_system: Option<CoordinateSystem>,
31
32    #[serde(skip_serializing_if = "Option::is_none")]
33    x_axis_index: Option<i64>,
34
35    #[serde(skip_serializing_if = "Option::is_none")]
36    y_axis_index: Option<i64>,
37
38    #[serde(skip_serializing_if = "Option::is_none")]
39    symbol: Option<Symbol>,
40
41    #[serde(skip_serializing_if = "Option::is_none")]
42    symbol_size: Option<SymbolSize>,
43
44    #[serde(skip_serializing_if = "Option::is_none")]
45    encode: Option<DimensionEncode>,
46
47    #[serde(skip_serializing_if = "Option::is_none")]
48    mark_line: Option<MarkLine>,
49
50    #[serde(skip_serializing_if = "Option::is_none")]
51    mark_area: Option<MarkArea>,
52
53    #[serde(skip_serializing_if = "Option::is_none")]
54    item_style: Option<ItemStyle>,
55
56    #[serde(skip_serializing_if = "Option::is_none")]
57    emphasis: Option<Emphasis>,
58
59    #[serde(skip_serializing_if = "Vec::is_empty")]
60    data: DataFrame,
61}
62
63impl Scatter {
64    pub fn new() -> Self {
65        Self {
66            type_: String::from("scatter"),
67            id: None,
68            name: None,
69            color_by: None,
70            dataset_index: None,
71            coordinate_system: None,
72            x_axis_index: None,
73            y_axis_index: None,
74            symbol: None,
75            symbol_size: None,
76            encode: None,
77            mark_line: None,
78            mark_area: None,
79            item_style: None,
80            emphasis: None,
81            data: vec![],
82        }
83    }
84
85    pub fn id<S: Into<String>>(mut self, id: S) -> Self {
86        self.id = Some(id.into());
87        self
88    }
89
90    pub fn name<S: Into<String>>(mut self, name: S) -> Self {
91        self.name = Some(name.into());
92        self
93    }
94
95    pub fn color_by(mut self, color_by: ColorBy) -> Self {
96        self.color_by = Some(color_by);
97        self
98    }
99
100    pub fn dataset_index<F: Into<i64>>(mut self, dataset_index: F) -> Self {
101        self.dataset_index = Some(dataset_index.into());
102        self
103    }
104
105    pub fn coordinate_system<C: Into<CoordinateSystem>>(mut self, coordinate_system: C) -> Self {
106        self.coordinate_system = Some(coordinate_system.into());
107        self
108    }
109
110    pub fn x_axis_index<F: Into<i64>>(mut self, x_axis_index: F) -> Self {
111        self.x_axis_index = Some(x_axis_index.into());
112        self
113    }
114
115    pub fn y_axis_index<F: Into<i64>>(mut self, y_axis_index: F) -> Self {
116        self.y_axis_index = Some(y_axis_index.into());
117        self
118    }
119
120    pub fn symbol(mut self, symbol: Symbol) -> Self {
121        self.symbol = Some(symbol);
122        self
123    }
124
125    pub fn symbol_size<S: Into<SymbolSize>>(mut self, symbol_size: S) -> Self {
126        self.symbol_size = Some(symbol_size.into());
127        self
128    }
129
130    pub fn encode<D: Into<DimensionEncode>>(mut self, encode: D) -> Self {
131        self.encode = Some(encode.into());
132        self
133    }
134
135    pub fn mark_line<M: Into<MarkLine>>(mut self, mark_line: M) -> Self {
136        self.mark_line = Some(mark_line.into());
137        self
138    }
139
140    pub fn mark_area<M: Into<MarkArea>>(mut self, mark_area: M) -> Self {
141        self.mark_area = Some(mark_area.into());
142        self
143    }
144
145    pub fn item_style<I: Into<ItemStyle>>(mut self, item_style: I) -> Self {
146        self.item_style = Some(item_style.into());
147        self
148    }
149
150    pub fn emphasis<E: Into<Emphasis>>(mut self, emphasis: E) -> Self {
151        self.emphasis = Some(emphasis.into());
152        self
153    }
154
155    pub fn data<D: Into<DataPoint>>(mut self, data: Vec<D>) -> Self {
156        self.data = data.into_iter().map(|d| d.into()).collect();
157        self
158    }
159}