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
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
//! Legend styling configuration.

use embedded_graphics::prelude::*;

/// Complete legend styling configuration
#[derive(Debug, Clone)]
pub struct LegendStyle<C: PixelColor> {
    /// Text styling
    pub text: TextStyle<C>,
    /// Symbol styling
    pub symbol: SymbolStyle<C>,
    /// Background styling
    pub background: BackgroundStyle<C>,
    /// Spacing configuration
    pub spacing: SpacingStyle,
}

/// Text styling for legend labels
#[derive(Debug, Clone)]
pub struct TextStyle<C: PixelColor> {
    /// Text color
    pub color: C,
    /// Font size (in pixels)
    pub font_size: u32,
    /// Line height (in pixels)
    pub line_height: u32,
    /// Character width (for monospace estimation)
    pub char_width: u32,
    /// Maximum text width (for layout calculations)
    pub max_text_width: u32,
    /// Text alignment
    pub alignment: TextAlignment,
}

/// Symbol styling for legend entries
#[derive(Debug, Clone)]
pub struct SymbolStyle<C: PixelColor> {
    /// Default symbol size
    pub size: u32,
    /// Symbol border width
    pub border_width: u32,
    /// Default symbol color (used as fallback)
    pub default_color: C,
    /// Symbol rendering quality
    pub quality: SymbolQuality,
}

/// Background styling for the legend
#[derive(Debug, Clone)]
pub struct BackgroundStyle<C: PixelColor> {
    /// Background color (None for transparent)
    pub color: Option<C>,
    /// Border color (None for no border)
    pub border_color: Option<C>,
    /// Border width
    pub border_width: u32,
    /// Corner radius for rounded backgrounds
    pub corner_radius: u32,
    /// Background opacity (0-255, 255 = opaque)
    pub opacity: u8,
}

/// Spacing configuration for legend layout
#[derive(Debug, Clone)]
pub struct SpacingStyle {
    /// Width allocated for symbols
    pub symbol_width: u32,
    /// Gap between symbol and text
    pub symbol_text_gap: u32,
    /// Spacing between legend entries
    pub entry_spacing: u32,
    /// Padding around the entire legend
    pub padding: Padding,
    /// Margins outside the legend
    pub margins: Margins,
}

/// Text alignment options
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TextAlignment {
    /// Left-aligned text
    Left,
    /// Center-aligned text
    Center,
    /// Right-aligned text
    Right,
}

/// Symbol rendering quality options
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SymbolQuality {
    /// Fast rendering with basic shapes
    Fast,
    /// Standard quality with anti-aliasing where possible
    Standard,
    /// High quality with smooth curves (may be slower)
    High,
}

/// Padding configuration
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Padding {
    /// Top padding
    pub top: u32,
    /// Right padding
    pub right: u32,
    /// Bottom padding
    pub bottom: u32,
    /// Left padding
    pub left: u32,
}

/// Margins configuration
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Margins {
    /// Top margin
    pub top: u32,
    /// Right margin
    pub right: u32,
    /// Bottom margin
    pub bottom: u32,
    /// Left margin
    pub left: u32,
}

impl<C: PixelColor> LegendStyle<C> {
    /// Create a new legend style with default values
    pub fn new() -> Self
    where
        C: From<embedded_graphics::pixelcolor::Rgb565>,
    {
        Self {
            text: TextStyle::new(),
            symbol: SymbolStyle::new(),
            background: BackgroundStyle::new(),
            spacing: SpacingStyle::new(),
        }
    }

    /// Create a minimal style for small displays
    pub fn minimal() -> Self
    where
        C: From<embedded_graphics::pixelcolor::Rgb565>,
    {
        Self {
            text: TextStyle::minimal(),
            symbol: SymbolStyle::minimal(),
            background: BackgroundStyle::minimal(),
            spacing: SpacingStyle::minimal(),
        }
    }

    /// Create a professional style
    pub fn professional() -> Self
    where
        C: From<embedded_graphics::pixelcolor::Rgb565>,
    {
        Self {
            text: TextStyle::professional(),
            symbol: SymbolStyle::professional(),
            background: BackgroundStyle::professional(),
            spacing: SpacingStyle::professional(),
        }
    }

    /// Create a compact style for space-constrained environments
    pub fn compact() -> Self
    where
        C: From<embedded_graphics::pixelcolor::Rgb565>,
    {
        Self {
            text: TextStyle::compact(),
            symbol: SymbolStyle::compact(),
            background: BackgroundStyle::compact(),
            spacing: SpacingStyle::compact(),
        }
    }
}

impl<C: PixelColor> TextStyle<C> {
    /// Create a new text style with default values
    pub fn new() -> Self
    where
        C: From<embedded_graphics::pixelcolor::Rgb565>,
    {
        Self {
            color: C::from(embedded_graphics::pixelcolor::Rgb565::BLACK),
            font_size: 12,
            line_height: 16,
            char_width: 6,
            max_text_width: 120,
            alignment: TextAlignment::Left,
        }
    }

    /// Create a minimal text style
    pub fn minimal() -> Self
    where
        C: From<embedded_graphics::pixelcolor::Rgb565>,
    {
        Self {
            color: C::from(embedded_graphics::pixelcolor::Rgb565::BLACK),
            font_size: 8,
            line_height: 10,
            char_width: 4,
            max_text_width: 60,
            alignment: TextAlignment::Left,
        }
    }

    /// Create a professional text style
    pub fn professional() -> Self
    where
        C: From<embedded_graphics::pixelcolor::Rgb565>,
    {
        Self {
            color: C::from(embedded_graphics::pixelcolor::Rgb565::new(8, 16, 8)), // Dark gray
            font_size: 14,
            line_height: 18,
            char_width: 7,
            max_text_width: 150,
            alignment: TextAlignment::Left,
        }
    }

    /// Create a compact text style
    pub fn compact() -> Self
    where
        C: From<embedded_graphics::pixelcolor::Rgb565>,
    {
        Self {
            color: C::from(embedded_graphics::pixelcolor::Rgb565::BLACK),
            font_size: 10,
            line_height: 12,
            char_width: 5,
            max_text_width: 80,
            alignment: TextAlignment::Left,
        }
    }
}

impl<C: PixelColor> SymbolStyle<C> {
    /// Create a new symbol style with default values
    pub fn new() -> Self
    where
        C: From<embedded_graphics::pixelcolor::Rgb565>,
    {
        Self {
            size: 16,
            border_width: 1,
            default_color: C::from(embedded_graphics::pixelcolor::Rgb565::BLUE),
            quality: SymbolQuality::Standard,
        }
    }

    /// Create a minimal symbol style
    pub fn minimal() -> Self
    where
        C: From<embedded_graphics::pixelcolor::Rgb565>,
    {
        Self {
            size: 8,
            border_width: 0,
            default_color: C::from(embedded_graphics::pixelcolor::Rgb565::BLUE),
            quality: SymbolQuality::Fast,
        }
    }

    /// Create a professional symbol style
    pub fn professional() -> Self
    where
        C: From<embedded_graphics::pixelcolor::Rgb565>,
    {
        Self {
            size: 20,
            border_width: 2,
            default_color: C::from(embedded_graphics::pixelcolor::Rgb565::new(14, 28, 14)), // Steel blue
            quality: SymbolQuality::High,
        }
    }

    /// Create a compact symbol style
    pub fn compact() -> Self
    where
        C: From<embedded_graphics::pixelcolor::Rgb565>,
    {
        Self {
            size: 12,
            border_width: 1,
            default_color: C::from(embedded_graphics::pixelcolor::Rgb565::BLUE),
            quality: SymbolQuality::Standard,
        }
    }
}

impl<C: PixelColor> BackgroundStyle<C> {
    /// Create a new background style with default values
    pub fn new() -> Self {
        Self {
            color: None, // Transparent by default
            border_color: None,
            border_width: 0,
            corner_radius: 0,
            opacity: 255,
        }
    }

    /// Create a minimal background style
    pub fn minimal() -> Self {
        Self {
            color: None,
            border_color: None,
            border_width: 0,
            corner_radius: 0,
            opacity: 255,
        }
    }

    /// Create a professional background style
    pub fn professional() -> Self
    where
        C: From<embedded_graphics::pixelcolor::Rgb565>,
    {
        Self {
            color: Some(C::from(embedded_graphics::pixelcolor::Rgb565::new(
                31, 63, 31,
            ))), // Light gray
            border_color: Some(C::from(embedded_graphics::pixelcolor::Rgb565::new(
                16, 32, 16,
            ))), // Gray
            border_width: 1,
            corner_radius: 4,
            opacity: 240,
        }
    }

    /// Create a compact background style
    pub fn compact() -> Self {
        Self {
            color: None,
            border_color: None,
            border_width: 0,
            corner_radius: 0,
            opacity: 255,
        }
    }
}

impl SpacingStyle {
    /// Create a new spacing style with default values
    pub fn new() -> Self {
        Self {
            symbol_width: 20,
            symbol_text_gap: 8,
            entry_spacing: 4,
            padding: Padding::all(8),
            margins: Margins::all(4),
        }
    }

    /// Create a minimal spacing style
    pub fn minimal() -> Self {
        Self {
            symbol_width: 12,
            symbol_text_gap: 4,
            entry_spacing: 2,
            padding: Padding::all(2),
            margins: Margins::all(1),
        }
    }

    /// Create a professional spacing style
    pub fn professional() -> Self {
        Self {
            symbol_width: 24,
            symbol_text_gap: 12,
            entry_spacing: 8,
            padding: Padding::all(12),
            margins: Margins::all(8),
        }
    }

    /// Create a compact spacing style
    pub fn compact() -> Self {
        Self {
            symbol_width: 16,
            symbol_text_gap: 6,
            entry_spacing: 3,
            padding: Padding::all(4),
            margins: Margins::all(2),
        }
    }
}

impl Padding {
    /// Create uniform padding
    pub const fn all(value: u32) -> Self {
        Self {
            top: value,
            right: value,
            bottom: value,
            left: value,
        }
    }

    /// Create symmetric padding
    pub const fn symmetric(horizontal: u32, vertical: u32) -> Self {
        Self {
            top: vertical,
            right: horizontal,
            bottom: vertical,
            left: horizontal,
        }
    }

    /// Create custom padding
    pub const fn new(top: u32, right: u32, bottom: u32, left: u32) -> Self {
        Self {
            top,
            right,
            bottom,
            left,
        }
    }

    /// Get total horizontal padding
    pub const fn horizontal(&self) -> u32 {
        self.left + self.right
    }

    /// Get total vertical padding
    pub const fn vertical(&self) -> u32 {
        self.top + self.bottom
    }
}

impl Margins {
    /// Create uniform margins
    pub const fn all(value: u32) -> Self {
        Self {
            top: value,
            right: value,
            bottom: value,
            left: value,
        }
    }

    /// Create symmetric margins
    pub const fn symmetric(horizontal: u32, vertical: u32) -> Self {
        Self {
            top: vertical,
            right: horizontal,
            bottom: vertical,
            left: horizontal,
        }
    }

    /// Create custom margins
    pub const fn new(top: u32, right: u32, bottom: u32, left: u32) -> Self {
        Self {
            top,
            right,
            bottom,
            left,
        }
    }

    /// Get total horizontal margins
    pub const fn horizontal(&self) -> u32 {
        self.left + self.right
    }

    /// Get total vertical margins
    pub const fn vertical(&self) -> u32 {
        self.top + self.bottom
    }
}

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

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

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

impl<C: PixelColor> Default for BackgroundStyle<C> {
    fn default() -> Self {
        Self::new()
    }
}

impl Default for SpacingStyle {
    fn default() -> Self {
        Self::new()
    }
}

impl Default for TextAlignment {
    fn default() -> Self {
        Self::Left
    }
}

impl Default for SymbolQuality {
    fn default() -> Self {
        Self::Standard
    }
}

impl Default for Padding {
    fn default() -> Self {
        Self::all(4)
    }
}

impl Default for Margins {
    fn default() -> Self {
        Self::all(2)
    }
}