embedded-charts 0.3.0

A rich graph framework for embedded systems using embedded-graphics with std/no_std support
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
//! Builder pattern for legend configuration.

use crate::error::{ChartError, ChartResult};
use crate::legend::{
    position::{LegendAlignment, LegendMargins, LegendPosition},
    style::LegendStyle,
    traits::Legend,
    types::{
        CompactLegend, CompactLegendEntry, CustomLegend, CustomLegendEntry, LegendEntryType,
        LegendOrientation, StandardLegend, StandardLegendEntry,
    },
};
use embedded_graphics::prelude::*;

/// Builder for creating legends with fluent configuration
pub trait LegendBuilder<C: PixelColor> {
    /// The legend type this builder creates
    type Legend: Legend<C>;
    /// Error type for building operations
    type Error;

    /// Build the legend with current configuration
    fn build(self) -> Result<Self::Legend, Self::Error>;
}

/// Builder for standard legends
#[derive(Debug)]
pub struct StandardLegendBuilder<C: PixelColor> {
    position: LegendPosition,
    orientation: LegendOrientation,
    style: LegendStyle<C>,
    alignment: LegendAlignment,
    margins: LegendMargins,
    entries: heapless::Vec<StandardLegendEntry<C>, 16>,
}

/// Builder for compact legends
#[derive(Debug)]
pub struct CompactLegendBuilder<C: PixelColor> {
    position: LegendPosition,
    orientation: LegendOrientation,
    style: LegendStyle<C>,
    #[allow(dead_code)]
    alignment: LegendAlignment,
    #[allow(dead_code)]
    margins: LegendMargins,
    entries: heapless::Vec<CompactLegendEntry<C>, 8>,
}

/// Builder for custom legends
#[derive(Debug)]
pub struct CustomLegendBuilder<C: PixelColor> {
    position: LegendPosition,
    orientation: LegendOrientation,
    style: LegendStyle<C>,
    #[allow(dead_code)]
    alignment: LegendAlignment,
    #[allow(dead_code)]
    margins: LegendMargins,
    entries: heapless::Vec<CustomLegendEntry<C>, 12>,
    layout_params: crate::legend::types::CustomLayoutParams,
}

impl<C: PixelColor> StandardLegendBuilder<C>
where
    C: From<embedded_graphics::pixelcolor::Rgb565>,
{
    /// Create a new standard legend builder
    pub fn new() -> Self {
        Self {
            position: LegendPosition::Right,
            orientation: LegendOrientation::Vertical,
            style: LegendStyle::new(),
            alignment: LegendAlignment::Start,
            margins: LegendMargins::default(),
            entries: heapless::Vec::new(),
        }
    }

    /// Set the legend position
    pub fn position(mut self, position: LegendPosition) -> Self {
        self.position = position;
        self
    }

    /// Set the legend orientation
    pub fn orientation(mut self, orientation: LegendOrientation) -> Self {
        self.orientation = orientation;
        self
    }

    /// Set the legend style
    pub fn style(mut self, style: LegendStyle<C>) -> Self {
        self.style = style;
        self
    }

    /// Use minimal styling for small displays
    pub fn minimal_style(mut self) -> Self {
        self.style = LegendStyle::minimal();
        self
    }

    /// Use professional styling
    pub fn professional_style(mut self) -> Self {
        self.style = LegendStyle::professional();
        self
    }

    /// Use compact styling
    pub fn compact_style(mut self) -> Self {
        self.style = LegendStyle::compact();
        self
    }

    /// Set the legend alignment
    pub fn alignment(mut self, alignment: LegendAlignment) -> Self {
        self.alignment = alignment;
        self
    }

    /// Set the legend margins
    pub fn margins(mut self, margins: LegendMargins) -> Self {
        self.margins = margins;
        self
    }

    /// Add a line entry to the legend
    pub fn add_line_entry(mut self, label: &str, color: C) -> ChartResult<Self> {
        let entry_type = LegendEntryType::Line {
            color,
            width: 2,
            pattern: crate::style::LinePattern::Solid,
            marker: None,
        };
        let entry = StandardLegendEntry::new(label, entry_type)?;
        self.entries
            .push(entry)
            .map_err(|_| ChartError::ConfigurationError)?;
        Ok(self)
    }

    /// Add a line entry with marker to the legend
    pub fn add_line_entry_with_marker(
        mut self,
        label: &str,
        color: C,
        marker: crate::legend::types::MarkerStyle<C>,
    ) -> ChartResult<Self> {
        let entry_type = LegendEntryType::Line {
            color,
            width: 2,
            pattern: crate::style::LinePattern::Solid,
            marker: Some(marker),
        };
        let entry = StandardLegendEntry::new(label, entry_type)?;
        self.entries
            .push(entry)
            .map_err(|_| ChartError::ConfigurationError)?;
        Ok(self)
    }

    /// Add a bar entry to the legend
    pub fn add_bar_entry(mut self, label: &str, color: C) -> ChartResult<Self> {
        let entry_type = LegendEntryType::Bar {
            color,
            border_color: None,
            border_width: 0,
        };
        let entry = StandardLegendEntry::new(label, entry_type)?;
        self.entries
            .push(entry)
            .map_err(|_| ChartError::ConfigurationError)?;
        Ok(self)
    }

    /// Add a bar entry with border to the legend
    pub fn add_bar_entry_with_border(
        mut self,
        label: &str,
        color: C,
        border_color: C,
        border_width: u32,
    ) -> ChartResult<Self> {
        let entry_type = LegendEntryType::Bar {
            color,
            border_color: Some(border_color),
            border_width,
        };
        let entry = StandardLegendEntry::new(label, entry_type)?;
        self.entries
            .push(entry)
            .map_err(|_| ChartError::ConfigurationError)?;
        Ok(self)
    }

    /// Add a pie entry to the legend
    pub fn add_pie_entry(mut self, label: &str, color: C) -> ChartResult<Self> {
        let entry_type = LegendEntryType::Pie {
            color,
            border_color: None,
            border_width: 0,
        };
        let entry = StandardLegendEntry::new(label, entry_type)?;
        self.entries
            .push(entry)
            .map_err(|_| ChartError::ConfigurationError)?;
        Ok(self)
    }

    /// Add a custom symbol entry to the legend
    pub fn add_custom_entry(
        mut self,
        label: &str,
        color: C,
        shape: crate::legend::types::SymbolShape,
        size: u32,
    ) -> ChartResult<Self> {
        let entry_type = LegendEntryType::Custom { color, shape, size };
        let entry = StandardLegendEntry::new(label, entry_type)?;
        self.entries
            .push(entry)
            .map_err(|_| ChartError::ConfigurationError)?;
        Ok(self)
    }

    /// Add a generic entry to the legend
    pub fn add_entry(mut self, label: &str, entry_type: LegendEntryType<C>) -> ChartResult<Self> {
        let entry = StandardLegendEntry::new(label, entry_type)?;
        self.entries
            .push(entry)
            .map_err(|_| ChartError::ConfigurationError)?;
        Ok(self)
    }

    /// Clear all entries
    pub fn clear_entries(mut self) -> Self {
        self.entries.clear();
        self
    }
}

impl<C: PixelColor> LegendBuilder<C> for StandardLegendBuilder<C>
where
    C: From<embedded_graphics::pixelcolor::Rgb565>,
{
    type Legend = StandardLegend<C>;
    type Error = ChartError;

    fn build(self) -> Result<Self::Legend, Self::Error> {
        let mut legend = StandardLegend::new(self.position);
        legend.set_orientation(self.orientation);
        legend.set_style(self.style);

        // Add all entries
        for entry in self.entries {
            legend.add_entry(entry)?;
        }

        Ok(legend)
    }
}

impl<C: PixelColor> Default for StandardLegendBuilder<C>
where
    C: From<embedded_graphics::pixelcolor::Rgb565>,
{
    fn default() -> Self {
        Self::new()
    }
}

impl<C: PixelColor> CompactLegendBuilder<C>
where
    C: From<embedded_graphics::pixelcolor::Rgb565>,
{
    /// Create a new compact legend builder
    pub fn new() -> Self {
        Self {
            position: LegendPosition::Right,
            orientation: LegendOrientation::Vertical,
            style: LegendStyle::compact(),
            alignment: LegendAlignment::Start,
            margins: LegendMargins::all(4),
            entries: heapless::Vec::new(),
        }
    }

    /// Set the legend position
    pub fn position(mut self, position: LegendPosition) -> Self {
        self.position = position;
        self
    }

    /// Set the legend orientation
    pub fn orientation(mut self, orientation: LegendOrientation) -> Self {
        self.orientation = orientation;
        self
    }

    /// Add a simple entry with just color
    pub fn add_simple_entry(mut self, label: &str, color: C) -> ChartResult<Self> {
        let entry_type = LegendEntryType::Custom {
            color,
            shape: crate::legend::types::SymbolShape::Circle,
            size: 8,
        };
        let entry = CompactLegendEntry::new(label, entry_type)?;
        self.entries
            .push(entry)
            .map_err(|_| ChartError::ConfigurationError)?;
        Ok(self)
    }
}

impl<C: PixelColor> LegendBuilder<C> for CompactLegendBuilder<C>
where
    C: From<embedded_graphics::pixelcolor::Rgb565>,
{
    type Legend = CompactLegend<C>;
    type Error = ChartError;

    fn build(self) -> Result<Self::Legend, Self::Error> {
        let mut legend = CompactLegend::new(self.position);
        legend.set_orientation(self.orientation);
        legend.set_style(self.style);

        // Add all entries
        for entry in self.entries {
            legend.add_entry(entry)?;
        }

        Ok(legend)
    }
}

impl<C: PixelColor> Default for CompactLegendBuilder<C>
where
    C: From<embedded_graphics::pixelcolor::Rgb565>,
{
    fn default() -> Self {
        Self::new()
    }
}

impl<C: PixelColor> CustomLegendBuilder<C>
where
    C: From<embedded_graphics::pixelcolor::Rgb565>,
{
    /// Create a new custom legend builder
    pub fn new() -> Self {
        Self {
            position: LegendPosition::Right,
            orientation: LegendOrientation::Vertical,
            style: LegendStyle::new(),
            alignment: LegendAlignment::Start,
            margins: LegendMargins::default(),
            entries: heapless::Vec::new(),
            layout_params: crate::legend::types::CustomLayoutParams::default(),
        }
    }

    /// Set the legend position
    pub fn position(mut self, position: LegendPosition) -> Self {
        self.position = position;
        self
    }

    /// Set the legend orientation
    pub fn orientation(mut self, orientation: LegendOrientation) -> Self {
        self.orientation = orientation;
        self
    }

    /// Set custom layout parameters
    pub fn layout_params(mut self, params: crate::legend::types::CustomLayoutParams) -> Self {
        self.layout_params = params;
        self
    }

    /// Set custom entry spacing
    pub fn entry_spacing(mut self, spacing: u32) -> Self {
        self.layout_params.entry_spacing = spacing;
        self
    }

    /// Set custom symbol size
    pub fn symbol_size(mut self, size: u32) -> Self {
        self.layout_params.symbol_size = size;
        self
    }

    /// Enable or disable automatic layout
    pub fn auto_layout(mut self, enabled: bool) -> Self {
        self.layout_params.auto_layout = enabled;
        self
    }
}

impl<C: PixelColor> LegendBuilder<C> for CustomLegendBuilder<C>
where
    C: From<embedded_graphics::pixelcolor::Rgb565>,
{
    type Legend = CustomLegend<C>;
    type Error = ChartError;

    fn build(self) -> Result<Self::Legend, Self::Error> {
        let mut legend = CustomLegend::new(self.position);
        legend.set_orientation(self.orientation);
        legend.set_style(self.style);
        legend.set_layout_params(self.layout_params);

        // Add all entries
        for entry in self.entries {
            legend.add_entry(entry)?;
        }

        Ok(legend)
    }
}

impl<C: PixelColor> Default for CustomLegendBuilder<C>
where
    C: From<embedded_graphics::pixelcolor::Rgb565>,
{
    fn default() -> Self {
        Self::new()
    }
}

// Convenience functions for creating common legend configurations
/// Preset legend configurations for common use cases
pub mod presets {
    use super::*;
    use embedded_graphics::pixelcolor::Rgb565;

    /// Create a standard legend on the right side
    pub fn right_legend<C: PixelColor + From<Rgb565>>() -> StandardLegendBuilder<C> {
        StandardLegendBuilder::new()
            .position(LegendPosition::Right)
            .orientation(LegendOrientation::Vertical)
    }

    /// Create a standard legend at the bottom
    pub fn bottom_legend<C: PixelColor + From<Rgb565>>() -> StandardLegendBuilder<C> {
        StandardLegendBuilder::new()
            .position(LegendPosition::Bottom)
            .orientation(LegendOrientation::Horizontal)
    }

    /// Create a minimal legend for small displays
    pub fn minimal_legend<C: PixelColor + From<Rgb565>>() -> CompactLegendBuilder<C> {
        CompactLegendBuilder::new()
            .position(LegendPosition::TopRight)
            .orientation(LegendOrientation::Vertical)
    }

    /// Create a professional legend with styling
    pub fn professional_legend<C: PixelColor + From<Rgb565>>() -> StandardLegendBuilder<C> {
        StandardLegendBuilder::new()
            .position(LegendPosition::Right)
            .professional_style()
            .margins(LegendMargins::all(12))
    }

    /// Create a floating legend that overlays the chart
    pub fn floating_legend<C: PixelColor + From<Rgb565>>(
        position: Point,
    ) -> StandardLegendBuilder<C> {
        StandardLegendBuilder::new()
            .position(LegendPosition::Floating(position))
            .orientation(LegendOrientation::Vertical)
    }
}

// CompactLegendEntry implementation is in types.rs

#[cfg(test)]
mod tests {
    use super::*;
    use embedded_graphics::pixelcolor::Rgb565;

    #[test]
    fn test_standard_legend_builder() {
        let legend = StandardLegendBuilder::new()
            .position(LegendPosition::Bottom)
            .orientation(LegendOrientation::Horizontal)
            .add_line_entry("Series 1", Rgb565::RED)
            .unwrap()
            .add_bar_entry("Series 2", Rgb565::BLUE)
            .unwrap()
            .build()
            .unwrap();

        assert_eq!(legend.position(), LegendPosition::Bottom);
        assert_eq!(legend.orientation(), LegendOrientation::Horizontal);
        assert_eq!(legend.entries().len(), 2);
    }

    #[test]
    fn test_preset_legends() {
        let legend = presets::right_legend::<Rgb565>()
            .add_line_entry("Test", Rgb565::GREEN)
            .unwrap()
            .build()
            .unwrap();

        assert_eq!(legend.position(), LegendPosition::Right);
        assert_eq!(legend.orientation(), LegendOrientation::Vertical);
    }
}