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
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
//! Legend type implementations.

use crate::error::{ChartError, ChartResult};
use crate::legend::position::LegendPosition;
use crate::legend::style::{LegendStyle, SymbolStyle};
use crate::legend::traits::{Legend, LegendEntry};
use embedded_graphics::{prelude::*, primitives::Rectangle};

#[cfg(feature = "std")]
use std::vec::Vec;

#[cfg(all(feature = "no_std", not(feature = "std")))]
extern crate alloc;

#[cfg(all(feature = "no_std", not(feature = "std")))]
use alloc::vec::Vec;

/// Legend orientation options
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LegendOrientation {
    /// Vertical layout (entries stacked vertically)
    Vertical,
    /// Horizontal layout (entries arranged horizontally)
    Horizontal,
}

/// Types of legend entries
#[derive(Debug, Clone)]
pub enum LegendEntryType<C: PixelColor> {
    /// Line entry for line charts
    Line {
        /// Line color
        color: C,
        /// Line width
        width: u32,
        /// Line pattern (solid, dashed, etc.)
        pattern: crate::style::LinePattern,
        /// Optional marker
        marker: Option<MarkerStyle<C>>,
    },
    /// Bar entry for bar charts
    Bar {
        /// Fill color
        color: C,
        /// Optional border color
        border_color: Option<C>,
        /// Border width
        border_width: u32,
    },
    /// Pie entry for pie charts
    Pie {
        /// Fill color
        color: C,
        /// Optional border color
        border_color: Option<C>,
        /// Border width
        border_width: u32,
    },
    /// Custom symbol entry
    Custom {
        /// Symbol color
        color: C,
        /// Symbol shape
        shape: SymbolShape,
        /// Symbol size
        size: u32,
    },
}

/// Marker styles for line entries
#[derive(Debug, Clone)]
pub struct MarkerStyle<C: PixelColor> {
    /// Marker shape
    pub shape: MarkerShape,
    /// Marker color
    pub color: C,
    /// Marker size
    pub size: u32,
    /// Whether to fill the marker
    pub filled: bool,
}

/// Available marker shapes
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MarkerShape {
    /// Circle marker
    Circle,
    /// Square marker
    Square,
    /// Triangle marker
    Triangle,
    /// Diamond marker
    Diamond,
    /// Cross marker
    Cross,
    /// Plus marker
    Plus,
}

/// Available symbol shapes for custom entries
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SymbolShape {
    /// Circle symbol
    Circle,
    /// Square symbol
    Square,
    /// Triangle symbol
    Triangle,
    /// Diamond symbol
    Diamond,
    /// Star symbol
    Star,
    /// Cross symbol
    Cross,
}

/// Standard legend implementation
#[derive(Debug, Clone)]
pub struct StandardLegend<C: PixelColor> {
    /// Legend entries
    entries: heapless::Vec<StandardLegendEntry<C>, 16>,
    /// Legend position
    position: LegendPosition,
    /// Legend orientation
    orientation: LegendOrientation,
    /// Legend style
    style: LegendStyle<C>,
}

/// Standard legend entry
#[derive(Debug, Clone)]
pub struct StandardLegendEntry<C: PixelColor> {
    /// Entry label
    label: heapless::String<64>,
    /// Entry type
    entry_type: LegendEntryType<C>,
    /// Visibility flag
    visible: bool,
}

/// Compact legend for space-constrained environments
#[derive(Debug, Clone)]
pub struct CompactLegend<C: PixelColor> {
    /// Legend entries (limited capacity)
    entries: heapless::Vec<CompactLegendEntry<C>, 8>,
    /// Legend position
    position: LegendPosition,
    /// Legend orientation
    orientation: LegendOrientation,
    /// Legend style
    style: LegendStyle<C>,
}

impl<C: PixelColor> CompactLegend<C>
where
    C: From<embedded_graphics::pixelcolor::Rgb565>,
{
    /// Create a new compact legend
    pub fn new(position: LegendPosition) -> Self {
        Self {
            entries: heapless::Vec::new(),
            position,
            orientation: LegendOrientation::Vertical,
            style: LegendStyle::compact(),
        }
    }

    /// Set the legend orientation
    pub fn set_orientation(&mut self, orientation: LegendOrientation) {
        self.orientation = orientation;
    }

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

    /// Add an entry to the legend
    pub fn add_entry(&mut self, entry: CompactLegendEntry<C>) -> ChartResult<()> {
        self.entries
            .push(entry)
            .map_err(|_| ChartError::ConfigurationError)?;
        Ok(())
    }
}

impl<C: PixelColor> crate::legend::traits::Legend<C> for CompactLegend<C>
where
    C: From<embedded_graphics::pixelcolor::Rgb565>,
{
    type Entry = CompactLegendEntry<C>;

    fn position(&self) -> LegendPosition {
        self.position
    }

    fn orientation(&self) -> LegendOrientation {
        self.orientation
    }

    fn entries(&self) -> &[Self::Entry] {
        &self.entries
    }

    fn entries_mut(&mut self) -> &mut [Self::Entry] {
        &mut self.entries
    }

    fn add_entry(&mut self, entry: Self::Entry) -> ChartResult<()> {
        self.entries
            .push(entry)
            .map_err(|_| ChartError::ConfigurationError)?;
        Ok(())
    }

    fn remove_entry(&mut self, index: usize) -> ChartResult<()> {
        if index < self.entries.len() {
            self.entries.swap_remove(index);
            Ok(())
        } else {
            Err(ChartError::InvalidConfiguration)
        }
    }

    fn clear_entries(&mut self) {
        self.entries.clear();
    }

    fn set_position(&mut self, position: LegendPosition) {
        self.position = position;
    }

    fn set_orientation(&mut self, orientation: LegendOrientation) {
        self.orientation = orientation;
    }

    fn calculate_size(&self) -> embedded_graphics::prelude::Size {
        // Simple size calculation for compact legend
        let entry_count = self.entries.len() as u32;
        match self.orientation {
            LegendOrientation::Vertical => {
                embedded_graphics::prelude::Size::new(80, entry_count * 16 + 8)
            }
            LegendOrientation::Horizontal => {
                embedded_graphics::prelude::Size::new(entry_count * 60 + 8, 20)
            }
        }
    }
}

/// Compact legend entry with shorter labels
#[derive(Debug, Clone)]
pub struct CompactLegendEntry<C: PixelColor> {
    /// Entry label (shorter for compact display)
    pub label: heapless::String<16>,
    /// Entry type
    pub entry_type: LegendEntryType<C>,
    /// Visibility flag
    pub visible: bool,
}

impl<C: PixelColor> CompactLegendEntry<C> {
    /// Create a new compact legend entry
    pub fn new(label: &str, entry_type: LegendEntryType<C>) -> ChartResult<Self> {
        let mut label_string = heapless::String::new();
        label_string
            .push_str(label)
            .map_err(|_| ChartError::ConfigurationError)?;
        Ok(Self {
            label: label_string,
            entry_type,
            visible: true,
        })
    }
}

/// Custom legend for specialized layouts
#[derive(Debug, Clone)]
pub struct CustomLegend<C: PixelColor> {
    /// Legend entries
    entries: heapless::Vec<CustomLegendEntry<C>, 12>,
    /// Legend position
    position: LegendPosition,
    /// Legend orientation
    orientation: LegendOrientation,
    /// Legend style
    style: LegendStyle<C>,
    /// Custom layout parameters
    layout_params: CustomLayoutParams,
}

impl<C: PixelColor> CustomLegend<C>
where
    C: From<embedded_graphics::pixelcolor::Rgb565>,
{
    /// Create a new custom legend
    pub fn new(position: LegendPosition) -> Self {
        Self {
            entries: heapless::Vec::new(),
            position,
            orientation: LegendOrientation::Vertical,
            style: LegendStyle::new(),
            layout_params: CustomLayoutParams::default(),
        }
    }

    /// Set the legend orientation
    pub fn set_orientation(&mut self, orientation: LegendOrientation) {
        self.orientation = orientation;
    }

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

    /// Set custom layout parameters
    pub fn set_layout_params(&mut self, params: CustomLayoutParams) {
        self.layout_params = params;
    }

    /// Add an entry to the legend
    pub fn add_entry(&mut self, entry: CustomLegendEntry<C>) -> ChartResult<()> {
        self.entries
            .push(entry)
            .map_err(|_| ChartError::ConfigurationError)?;
        Ok(())
    }
}

impl<C: PixelColor> crate::legend::traits::Legend<C> for CustomLegend<C>
where
    C: From<embedded_graphics::pixelcolor::Rgb565>,
{
    type Entry = CustomLegendEntry<C>;

    fn position(&self) -> LegendPosition {
        self.position
    }

    fn orientation(&self) -> LegendOrientation {
        self.orientation
    }

    fn entries(&self) -> &[Self::Entry] {
        &self.entries
    }

    fn entries_mut(&mut self) -> &mut [Self::Entry] {
        &mut self.entries
    }

    fn add_entry(&mut self, entry: Self::Entry) -> ChartResult<()> {
        self.entries
            .push(entry)
            .map_err(|_| ChartError::ConfigurationError)?;
        Ok(())
    }

    fn remove_entry(&mut self, index: usize) -> ChartResult<()> {
        if index < self.entries.len() {
            self.entries.swap_remove(index);
            Ok(())
        } else {
            Err(ChartError::InvalidConfiguration)
        }
    }

    fn clear_entries(&mut self) {
        self.entries.clear();
    }

    fn set_position(&mut self, position: LegendPosition) {
        self.position = position;
    }

    fn set_orientation(&mut self, orientation: LegendOrientation) {
        self.orientation = orientation;
    }

    fn calculate_size(&self) -> embedded_graphics::prelude::Size {
        // Size calculation based on layout parameters
        let entry_count = self.entries.len() as u32;
        let symbol_size = self.layout_params.symbol_size;
        let entry_spacing = self.layout_params.entry_spacing;

        match self.orientation {
            LegendOrientation::Vertical => {
                let width = symbol_size + 100; // Symbol + text space
                let height = entry_count * (symbol_size + entry_spacing) + 16;
                embedded_graphics::prelude::Size::new(width, height)
            }
            LegendOrientation::Horizontal => {
                let width = entry_count * (symbol_size + 80 + entry_spacing) + 16;
                let height = symbol_size + 16;
                embedded_graphics::prelude::Size::new(width, height)
            }
        }
    }
}

/// Custom legend entry with additional metadata
#[derive(Debug, Clone)]
pub struct CustomLegendEntry<C: PixelColor> {
    /// Entry label
    label: heapless::String<32>,
    /// Entry type
    entry_type: LegendEntryType<C>,
    /// Visibility flag
    visible: bool,
    /// Custom positioning offset
    #[allow(dead_code)]
    offset: Point,
    /// Custom size override
    size_override: Option<Size>,
}

impl<C: PixelColor> CustomLegendEntry<C> {
    /// Create a new custom legend entry
    pub fn new(label: &str, entry_type: LegendEntryType<C>) -> ChartResult<Self> {
        let mut label_string = heapless::String::new();
        label_string
            .push_str(label)
            .map_err(|_| ChartError::ConfigurationError)?;
        Ok(Self {
            label: label_string,
            entry_type,
            visible: true,
            offset: embedded_graphics::prelude::Point::zero(),
            size_override: None,
        })
    }
}

/// Custom layout parameters
#[derive(Debug, Clone)]
pub struct CustomLayoutParams {
    /// Custom entry spacing
    pub entry_spacing: u32,
    /// Custom symbol size
    pub symbol_size: u32,
    /// Custom text offset
    pub text_offset: Point,
    /// Whether to use automatic layout
    pub auto_layout: bool,
}

// Implementation for StandardLegend
impl<C: PixelColor + From<embedded_graphics::pixelcolor::Rgb565>> StandardLegend<C> {
    /// Create a new standard legend
    pub fn new(position: LegendPosition) -> Self {
        Self {
            entries: heapless::Vec::new(),
            position,
            orientation: LegendOrientation::Vertical,
            style: LegendStyle::default(),
        }
    }

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

    /// Get the legend style
    pub fn style(&self) -> &LegendStyle<C> {
        &self.style
    }
}

impl<C: PixelColor> Legend<C> for StandardLegend<C> {
    type Entry = StandardLegendEntry<C>;

    fn entries(&self) -> &[Self::Entry] {
        &self.entries
    }

    fn entries_mut(&mut self) -> &mut [Self::Entry] {
        &mut self.entries
    }

    fn add_entry(&mut self, entry: Self::Entry) -> ChartResult<()> {
        self.entries
            .push(entry)
            .map_err(|_| crate::error::ChartError::ConfigurationError)
    }

    fn remove_entry(&mut self, index: usize) -> ChartResult<()> {
        if index < self.entries.len() {
            self.entries.remove(index);
            Ok(())
        } else {
            Err(crate::error::ChartError::ConfigurationError)
        }
    }

    fn clear_entries(&mut self) {
        self.entries.clear();
    }

    fn position(&self) -> LegendPosition {
        self.position
    }

    fn set_position(&mut self, position: LegendPosition) {
        self.position = position;
    }

    fn orientation(&self) -> LegendOrientation {
        self.orientation
    }

    fn set_orientation(&mut self, orientation: LegendOrientation) {
        self.orientation = orientation;
    }

    fn calculate_size(&self) -> Size {
        if self.entries.is_empty() {
            return Size::zero();
        }

        let visible_entries: Vec<_> = self.entries.iter().filter(|e| e.visible).collect();
        if visible_entries.is_empty() {
            return Size::zero();
        }

        match self.orientation {
            LegendOrientation::Vertical => {
                let max_width = visible_entries
                    .iter()
                    .map(|e| e.calculate_size(&self.style).width)
                    .max()
                    .unwrap_or(0);
                let total_height = visible_entries
                    .iter()
                    .map(|e| e.calculate_size(&self.style).height)
                    .sum::<u32>()
                    + (visible_entries.len().saturating_sub(1) as u32
                        * self.style.spacing.entry_spacing);
                Size::new(max_width, total_height)
            }
            LegendOrientation::Horizontal => {
                let total_width = visible_entries
                    .iter()
                    .map(|e| e.calculate_size(&self.style).width)
                    .sum::<u32>()
                    + (visible_entries.len().saturating_sub(1) as u32
                        * self.style.spacing.entry_spacing);
                let max_height = visible_entries
                    .iter()
                    .map(|e| e.calculate_size(&self.style).height)
                    .max()
                    .unwrap_or(0);
                Size::new(total_width, max_height)
            }
        }
    }
}

// Implementation for StandardLegendEntry
impl<C: PixelColor> StandardLegendEntry<C> {
    /// Create a new standard legend entry
    pub fn new(label: &str, entry_type: LegendEntryType<C>) -> ChartResult<Self> {
        let label_string = heapless::String::try_from(label)
            .map_err(|_| crate::error::ChartError::ConfigurationError)?;

        Ok(Self {
            label: label_string,
            entry_type,
            visible: true,
        })
    }
}

impl<C: PixelColor> LegendEntry<C> for StandardLegendEntry<C> {
    fn label(&self) -> &str {
        &self.label
    }

    fn set_label(&mut self, label: &str) -> ChartResult<()> {
        self.label = heapless::String::try_from(label)
            .map_err(|_| crate::error::ChartError::ConfigurationError)?;
        Ok(())
    }

    fn entry_type(&self) -> &LegendEntryType<C> {
        &self.entry_type
    }

    fn set_entry_type(&mut self, entry_type: LegendEntryType<C>) {
        self.entry_type = entry_type;
    }

    fn is_visible(&self) -> bool {
        self.visible
    }

    fn set_visible(&mut self, visible: bool) {
        self.visible = visible;
    }

    fn calculate_size(&self, style: &LegendStyle<C>) -> Size {
        let text_width = self.label.len() as u32 * style.text.char_width;
        let total_width = style.spacing.symbol_width + style.spacing.symbol_text_gap + text_width;
        Size::new(total_width, style.text.line_height)
    }

    fn render_symbol<D>(
        &self,
        bounds: Rectangle,
        _style: &SymbolStyle<C>,
        target: &mut D,
    ) -> ChartResult<()>
    where
        D: DrawTarget<Color = C>,
    {
        use embedded_graphics::primitives::{Circle, PrimitiveStyle, Rectangle as EgRectangle};

        match &self.entry_type {
            LegendEntryType::Line { color, .. } => {
                // Draw a small line segment
                let line_y = bounds.top_left.y + bounds.size.height as i32 / 2;
                let line_start = Point::new(bounds.top_left.x + 2, line_y);
                let line_end = Point::new(bounds.top_left.x + bounds.size.width as i32 - 2, line_y);

                use embedded_graphics::primitives::Line;
                Line::new(line_start, line_end)
                    .into_styled(PrimitiveStyle::with_stroke(*color, 1))
                    .draw(target)
                    .map_err(|_| crate::error::ChartError::RenderingError)?;
            }
            LegendEntryType::Bar { color, .. } | LegendEntryType::Pie { color, .. } => {
                // Draw a small rectangle
                let rect_size = Size::new(bounds.size.width.min(16), bounds.size.height.min(12));
                let rect_pos = Point::new(
                    bounds.top_left.x + (bounds.size.width as i32 - rect_size.width as i32) / 2,
                    bounds.top_left.y + (bounds.size.height as i32 - rect_size.height as i32) / 2,
                );

                EgRectangle::new(rect_pos, rect_size)
                    .into_styled(PrimitiveStyle::with_fill(*color))
                    .draw(target)
                    .map_err(|_| crate::error::ChartError::RenderingError)?;
            }
            LegendEntryType::Custom { color, shape, size } => {
                let symbol_size = (*size).min(bounds.size.width).min(bounds.size.height);
                let center = Point::new(
                    bounds.top_left.x + bounds.size.width as i32 / 2,
                    bounds.top_left.y + bounds.size.height as i32 / 2,
                );

                match shape {
                    SymbolShape::Circle => {
                        Circle::with_center(center, symbol_size)
                            .into_styled(PrimitiveStyle::with_fill(*color))
                            .draw(target)
                            .map_err(|_| crate::error::ChartError::RenderingError)?;
                    }
                    SymbolShape::Square => {
                        let rect_size = Size::new(symbol_size, symbol_size);
                        let rect_pos = Point::new(
                            center.x - symbol_size as i32 / 2,
                            center.y - symbol_size as i32 / 2,
                        );
                        EgRectangle::new(rect_pos, rect_size)
                            .into_styled(PrimitiveStyle::with_fill(*color))
                            .draw(target)
                            .map_err(|_| crate::error::ChartError::RenderingError)?;
                    }
                    _ => {
                        // For other shapes, draw a circle as fallback
                        Circle::with_center(center, symbol_size)
                            .into_styled(PrimitiveStyle::with_fill(*color))
                            .draw(target)
                            .map_err(|_| crate::error::ChartError::RenderingError)?;
                    }
                }
            }
        }

        Ok(())
    }
}

impl<C: PixelColor> LegendEntry<C> for CompactLegendEntry<C> {
    fn label(&self) -> &str {
        &self.label
    }

    fn set_label(&mut self, label: &str) -> ChartResult<()> {
        self.label = heapless::String::try_from(label)
            .map_err(|_| crate::error::ChartError::ConfigurationError)?;
        Ok(())
    }

    fn entry_type(&self) -> &LegendEntryType<C> {
        &self.entry_type
    }

    fn set_entry_type(&mut self, entry_type: LegendEntryType<C>) {
        self.entry_type = entry_type;
    }

    fn is_visible(&self) -> bool {
        self.visible
    }

    fn set_visible(&mut self, visible: bool) {
        self.visible = visible;
    }

    fn calculate_size(&self, style: &LegendStyle<C>) -> Size {
        let text_width = self.label.len() as u32 * style.text.char_width;
        let total_width = style.spacing.symbol_width + style.spacing.symbol_text_gap + text_width;
        Size::new(total_width, style.text.line_height)
    }

    fn render_symbol<D>(
        &self,
        bounds: Rectangle,
        _style: &SymbolStyle<C>,
        target: &mut D,
    ) -> ChartResult<()>
    where
        D: DrawTarget<Color = C>,
    {
        use embedded_graphics::primitives::{Circle, PrimitiveStyle, Rectangle as EgRectangle};

        match &self.entry_type {
            LegendEntryType::Line { color, .. } => {
                // Draw a small line segment
                let line_y = bounds.top_left.y + bounds.size.height as i32 / 2;
                let line_start = Point::new(bounds.top_left.x + 2, line_y);
                let line_end = Point::new(bounds.top_left.x + bounds.size.width as i32 - 2, line_y);

                use embedded_graphics::primitives::Line;
                Line::new(line_start, line_end)
                    .into_styled(PrimitiveStyle::with_stroke(*color, 1))
                    .draw(target)
                    .map_err(|_| crate::error::ChartError::RenderingError)?;
            }
            LegendEntryType::Bar { color, .. } | LegendEntryType::Pie { color, .. } => {
                // Draw a small rectangle
                let rect_size = Size::new(bounds.size.width.min(16), bounds.size.height.min(12));
                let rect_pos = Point::new(
                    bounds.top_left.x + (bounds.size.width as i32 - rect_size.width as i32) / 2,
                    bounds.top_left.y + (bounds.size.height as i32 - rect_size.height as i32) / 2,
                );

                EgRectangle::new(rect_pos, rect_size)
                    .into_styled(PrimitiveStyle::with_fill(*color))
                    .draw(target)
                    .map_err(|_| crate::error::ChartError::RenderingError)?;
            }
            LegendEntryType::Custom { color, shape, size } => {
                let symbol_size = (*size).min(bounds.size.width).min(bounds.size.height);
                let center = Point::new(
                    bounds.top_left.x + bounds.size.width as i32 / 2,
                    bounds.top_left.y + bounds.size.height as i32 / 2,
                );

                match shape {
                    SymbolShape::Circle => {
                        Circle::with_center(center, symbol_size)
                            .into_styled(PrimitiveStyle::with_fill(*color))
                            .draw(target)
                            .map_err(|_| crate::error::ChartError::RenderingError)?;
                    }
                    SymbolShape::Square => {
                        let rect_size = Size::new(symbol_size, symbol_size);
                        let rect_pos = Point::new(
                            center.x - symbol_size as i32 / 2,
                            center.y - symbol_size as i32 / 2,
                        );
                        EgRectangle::new(rect_pos, rect_size)
                            .into_styled(PrimitiveStyle::with_fill(*color))
                            .draw(target)
                            .map_err(|_| crate::error::ChartError::RenderingError)?;
                    }
                    _ => {
                        // For other shapes, draw a circle as fallback
                        Circle::with_center(center, symbol_size)
                            .into_styled(PrimitiveStyle::with_fill(*color))
                            .draw(target)
                            .map_err(|_| crate::error::ChartError::RenderingError)?;
                    }
                }
            }
        }

        Ok(())
    }
}

// Similar implementations for CompactLegend and CustomLegend would follow the same pattern
// but with their respective constraints and features

impl Default for LegendOrientation {
    fn default() -> Self {
        Self::Vertical
    }
}

impl Default for CustomLayoutParams {
    fn default() -> Self {
        Self {
            entry_spacing: 8,
            symbol_size: 16,
            text_offset: Point::new(20, 0),
            auto_layout: true,
        }
    }
}

// Implementation of LegendEntry trait for CustomLegendEntry
impl<C: PixelColor> LegendEntry<C> for CustomLegendEntry<C> {
    fn label(&self) -> &str {
        &self.label
    }

    fn set_label(&mut self, label: &str) -> ChartResult<()> {
        self.label =
            heapless::String::try_from(label).map_err(|_| ChartError::ConfigurationError)?;
        Ok(())
    }

    fn entry_type(&self) -> &LegendEntryType<C> {
        &self.entry_type
    }

    fn set_entry_type(&mut self, entry_type: LegendEntryType<C>) {
        self.entry_type = entry_type;
    }

    fn is_visible(&self) -> bool {
        self.visible
    }

    fn set_visible(&mut self, visible: bool) {
        self.visible = visible;
    }

    fn calculate_size(&self, style: &LegendStyle<C>) -> Size {
        let text_width = self.label.len() as u32 * style.text.char_width;
        let total_width = style.spacing.symbol_width + style.spacing.symbol_text_gap + text_width;

        // Use size override if available
        if let Some(size_override) = self.size_override {
            size_override
        } else {
            Size::new(total_width, style.text.line_height)
        }
    }

    fn render_symbol<D>(
        &self,
        bounds: Rectangle,
        _style: &SymbolStyle<C>,
        target: &mut D,
    ) -> ChartResult<()>
    where
        D: DrawTarget<Color = C>,
    {
        use embedded_graphics::primitives::{
            Circle, Line, PrimitiveStyle, Rectangle as EgRectangle,
        };

        match &self.entry_type {
            LegendEntryType::Line { color, .. } => {
                let line_y = bounds.top_left.y + bounds.size.height as i32 / 2;
                let line_start = Point::new(bounds.top_left.x + 2, line_y);
                let line_end = Point::new(bounds.top_left.x + bounds.size.width as i32 - 2, line_y);

                Line::new(line_start, line_end)
                    .into_styled(PrimitiveStyle::with_stroke(*color, 1))
                    .draw(target)
                    .map_err(|_| ChartError::RenderingError)?;
            }
            LegendEntryType::Bar { color, .. } | LegendEntryType::Pie { color, .. } => {
                let rect_size = Size::new(bounds.size.width.min(16), bounds.size.height.min(12));
                let rect_pos = Point::new(
                    bounds.top_left.x + (bounds.size.width as i32 - rect_size.width as i32) / 2,
                    bounds.top_left.y + (bounds.size.height as i32 - rect_size.height as i32) / 2,
                );

                EgRectangle::new(rect_pos, rect_size)
                    .into_styled(PrimitiveStyle::with_fill(*color))
                    .draw(target)
                    .map_err(|_| ChartError::RenderingError)?;
            }
            LegendEntryType::Custom { color, shape, size } => {
                let symbol_size = (*size).min(bounds.size.width).min(bounds.size.height);
                let center = Point::new(
                    bounds.top_left.x + bounds.size.width as i32 / 2,
                    bounds.top_left.y + bounds.size.height as i32 / 2,
                );

                match shape {
                    SymbolShape::Circle => {
                        Circle::with_center(center, symbol_size)
                            .into_styled(PrimitiveStyle::with_fill(*color))
                            .draw(target)
                            .map_err(|_| ChartError::RenderingError)?;
                    }
                    SymbolShape::Square => {
                        let half_size = symbol_size / 2;
                        let rect_pos =
                            Point::new(center.x - half_size as i32, center.y - half_size as i32);
                        EgRectangle::new(rect_pos, Size::new(symbol_size, symbol_size))
                            .into_styled(PrimitiveStyle::with_fill(*color))
                            .draw(target)
                            .map_err(|_| ChartError::RenderingError)?;
                    }
                    _ => {
                        // For other shapes, default to circle
                        Circle::with_center(center, symbol_size)
                            .into_styled(PrimitiveStyle::with_fill(*color))
                            .draw(target)
                            .map_err(|_| ChartError::RenderingError)?;
                    }
                }
            }
        }

        Ok(())
    }
}