1use crate::error::{length, BindingResult};
3use crate::frontend::{context, Context};
4use crate::{
5 AdapterError, ArraySource, BarPointLayer, CategoryInput, CategoryValues, Color, NumericInput,
6 NumericValues, PointLayer, Text, TextLayout,
7};
8
9#[derive(Clone)]
11pub struct PointLayerAdapter<'source> {
12 layer: PointLayer,
13 source: Context<'source>,
14 len: usize,
15}
16impl<'source> PointLayerAdapter<'source> {
17 pub fn new(
19 x: impl NumericInput,
20 y: impl NumericInput,
21 ) -> std::result::Result<Self, AdapterError> {
22 Self::bind(None, x, y)
23 }
24 pub fn from_data(
26 source: impl ArraySource + 'source,
27 x: impl NumericInput,
28 y: impl NumericInput,
29 ) -> std::result::Result<Self, AdapterError> {
30 Self::bind(context(source)?, x, y)
31 }
32 fn bind(
33 source: Context<'source>,
34 x: impl NumericInput,
35 y: impl NumericInput,
36 ) -> BindingResult<Self> {
37 let x = x.bind_numeric(source.as_deref(), "point layer x")?;
38 let y = y.bind_numeric(source.as_deref(), "point layer y")?;
39 let len = x.len();
40 length("point layer y", y.name(), len, y.len())?;
41 Ok(Self {
42 layer: PointLayer::new(x, y),
43 source,
44 len,
45 })
46 }
47 pub fn labels(mut self, labels: impl CategoryInput) -> std::result::Result<Self, AdapterError> {
49 let labels = labels.bind_category(self.source.as_deref(), "point layer labels")?;
50 length("point layer labels", labels.name(), self.len, labels.len())?;
51 self.layer = self.layer.labels(labels);
52 Ok(self)
53 }
54 #[must_use]
56 pub fn color(mut self, color: Color) -> Self {
57 self.layer = self.layer.color(color);
58 self
59 }
60 pub fn radius(mut self, radius: u32) -> crate::Result<Self> {
62 self.layer = self.layer.radius(radius)?;
63 Ok(self)
64 }
65 #[must_use]
67 pub fn alpha(mut self, alpha: u8) -> Self {
68 self.layer = self.layer.alpha(alpha);
69 self
70 }
71 #[must_use]
73 pub fn legend_label(mut self, label: impl Into<Text>) -> Self {
74 self.layer = self.layer.legend_label(label);
75 self
76 }
77 #[must_use]
79 pub fn clear_legend_label_style(mut self) -> Self {
80 self.layer = self.layer.clear_legend_label_style();
81 self
82 }
83 #[must_use]
85 pub fn clear_legend_label_layout(mut self) -> Self {
86 self.layer = self.layer.clear_legend_label_layout();
87 self
88 }
89 #[must_use]
91 pub fn label_text_layout(mut self, layout: TextLayout) -> Self {
92 self.layer = self.layer.label_text_layout(layout);
93 self
94 }
95 pub fn get_color(&self) -> Option<Color> {
97 self.layer.get_color()
98 }
99 pub fn get_radius(&self) -> Option<u32> {
101 self.layer.get_radius()
102 }
103 pub fn get_alpha(&self) -> Option<u8> {
105 self.layer.get_alpha()
106 }
107}
108impl From<PointLayerAdapter<'_>> for PointLayer {
109 fn from(adapter: PointLayerAdapter<'_>) -> Self {
110 adapter.layer
111 }
112}
113
114#[derive(Clone)]
116pub struct BarPointLayerAdapter<'source> {
117 layer: BarPointLayer,
118 source: Context<'source>,
119}
120impl<'source> BarPointLayerAdapter<'source> {
121 pub fn new(
123 category: impl CategoryInput,
124 value: impl NumericInput,
125 color: Color,
126 radius: u32,
127 ) -> std::result::Result<Self, AdapterError> {
128 Self::bind(None, category, value, color, radius)
129 }
130 pub fn from_data(
132 source: impl ArraySource + 'source,
133 category: impl CategoryInput,
134 value: impl NumericInput,
135 color: Color,
136 radius: u32,
137 ) -> std::result::Result<Self, AdapterError> {
138 Self::bind(context(source)?, category, value, color, radius)
139 }
140 fn bind(
141 source: Context<'source>,
142 category: impl CategoryInput,
143 value: impl NumericInput,
144 color: Color,
145 radius: u32,
146 ) -> BindingResult<Self> {
147 let category = category.bind_category(source.as_deref(), "bar point layer category")?;
148 let value = value.bind_numeric(source.as_deref(), "bar point layer value")?;
149 length(
150 "bar point layer value",
151 value.name(),
152 category.len(),
153 value.len(),
154 )?;
155 Ok(Self {
156 layer: BarPointLayer::new(category, None, value, color, radius)?,
157 source,
158 })
159 }
160 pub fn series(mut self, series: impl CategoryInput) -> std::result::Result<Self, AdapterError> {
162 let series = series.bind_category(self.source.as_deref(), "bar point layer series")?;
163 length(
164 "bar point layer series",
165 series.name(),
166 self.layer.values().len(),
167 series.len(),
168 )?;
169 self.layer = BarPointLayer::new(
170 self.layer.category_values().clone(),
171 Some(series),
172 self.layer.values().clone(),
173 self.layer.color(),
174 self.layer.radius(),
175 )?;
176 Ok(self)
177 }
178 pub fn category_values(&self) -> &CategoryValues {
180 self.layer.category_values()
181 }
182 pub fn series_values(&self) -> Option<&CategoryValues> {
184 self.layer.series_values()
185 }
186 pub fn values(&self) -> &NumericValues {
188 self.layer.values()
189 }
190 pub fn color(&self) -> Color {
192 self.layer.color()
193 }
194 pub fn radius(&self) -> u32 {
196 self.layer.radius()
197 }
198}
199impl From<BarPointLayerAdapter<'_>> for BarPointLayer {
200 fn from(adapter: BarPointLayerAdapter<'_>) -> Self {
201 adapter.layer
202 }
203}