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
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
//! Integration tests for embedded-charts
//!
//! These tests verify that the API works correctly and charts can be created and rendered.

use embedded_charts::{
    chart::traits::{Chart, ChartBuilder},
    data::{
        point::Point2D,
        series::{DataSeries, StaticDataSeries},
    },
};

#[cfg(feature = "line")]
use embedded_charts::chart::line::LineChart;

#[cfg(feature = "bar")]
use embedded_charts::chart::bar::BarChart;

#[cfg(feature = "pie")]
use embedded_charts::chart::pie::PieChart;
use embedded_graphics::{
    mock_display::MockDisplay, pixelcolor::Rgb565, prelude::*, primitives::Rectangle,
};

#[cfg(feature = "gauge")]
use embedded_charts::chart::gauge::{GaugeChart, GaugeType, NeedleShape};

#[cfg(feature = "scatter")]
use embedded_charts::chart::scatter::{
    CollisionSettings, CollisionStrategy, ColorMapping, ColorMappingStrategy, ConnectionStyle,
    LinePattern, PointShape, ScatterChart, SizeMapping, SizeScaling,
};

#[cfg(any(feature = "scatter", feature = "gauge"))]
use heapless::Vec;

#[test]
#[cfg(feature = "line")]
fn test_line_chart_creation_and_basic_usage() {
    // Create sample data
    let mut series: StaticDataSeries<Point2D, 256> = StaticDataSeries::new();
    series.push(Point2D::new(0.0, 10.0)).unwrap();
    series.push(Point2D::new(1.0, 20.0)).unwrap();
    series.push(Point2D::new(2.0, 15.0)).unwrap();
    series.push(Point2D::new(3.0, 25.0)).unwrap();

    // Create line chart
    let chart: LineChart<Rgb565> = LineChart::builder()
        .line_color(Rgb565::BLUE)
        .line_width(2)
        .with_title("Test Chart")
        .background_color(Rgb565::WHITE)
        .build()
        .unwrap();

    // Verify chart properties
    assert_eq!(chart.style().line_color, Rgb565::BLUE);
    assert_eq!(chart.style().line_width, 2);
    assert_eq!(
        chart.config().title.as_ref().map(|s| s.as_str()),
        Some("Test Chart")
    );
    assert_eq!(chart.config().background_color, Some(Rgb565::WHITE));

    // Test rendering with appropriate size
    let mut display: MockDisplay<Rgb565> = MockDisplay::new();
    display.set_allow_overdraw(true); // Allow overlapping pixels for line charts
    let viewport = Rectangle::new(Point::new(0, 0), Size::new(64, 64));

    let result = chart.draw(&series, chart.config(), viewport, &mut display);
    assert!(result.is_ok());
}

#[test]
#[cfg(feature = "bar")]
fn test_bar_chart_creation_and_basic_usage() {
    // Create sample data
    let mut series: StaticDataSeries<Point2D, 256> = StaticDataSeries::new();
    series.push(Point2D::new(1.0, 100.0)).unwrap();
    series.push(Point2D::new(2.0, 150.0)).unwrap();
    series.push(Point2D::new(3.0, 80.0)).unwrap();

    // Create bar chart
    let chart: BarChart<Rgb565> = BarChart::builder()
        .colors(&[Rgb565::BLUE, Rgb565::RED, Rgb565::GREEN])
        .spacing(5)
        .with_title("Sales Data")
        .build()
        .unwrap();

    // Verify chart properties
    assert_eq!(chart.style().spacing, 5);
    assert_eq!(
        chart.config().title.as_ref().map(|s| s.as_str()),
        Some("Sales Data")
    );

    // Test rendering
    let mut display: MockDisplay<Rgb565> = MockDisplay::new();
    let viewport = Rectangle::new(Point::new(0, 0), Size::new(64, 64));

    let result = chart.draw(&series, chart.config(), viewport, &mut display);
    assert!(result.is_ok());
}

#[test]
#[cfg(feature = "pie")]
fn test_pie_chart_creation_and_basic_usage() {
    // Create sample data
    let mut series: StaticDataSeries<Point2D, 256> = StaticDataSeries::new();
    series.push(Point2D::new(1.0, 35.0)).unwrap();
    series.push(Point2D::new(2.0, 25.0)).unwrap();
    series.push(Point2D::new(3.0, 40.0)).unwrap();

    // Create pie chart with appropriate size for viewport
    let chart: PieChart<Rgb565> = PieChart::builder()
        .center(Point::new(32, 32))
        .radius(5)
        .colors(&[Rgb565::BLUE, Rgb565::RED, Rgb565::GREEN])
        .build()
        .unwrap();

    // Verify chart properties
    assert_eq!(chart.center(), Point::new(32, 32));
    assert_eq!(chart.radius(), 5);
    // Title was removed to prevent text rendering outside bounds
    assert!(chart.config().title.is_none());

    // Test rendering with appropriate size - use smaller pie chart that fits in default MockDisplay
    let mut display: MockDisplay<Rgb565> = MockDisplay::new();
    display.set_allow_overdraw(true); // Allow overlapping pixels for pie chart edges
    let viewport = Rectangle::new(Point::new(0, 0), Size::new(50, 50));

    let result = chart.draw(&series, chart.config(), viewport, &mut display);
    assert!(result.is_ok());
}

#[test]
#[cfg(feature = "pie")]
fn test_donut_chart_comprehensive_functionality() {
    // Create sample data for donut chart
    let mut series: StaticDataSeries<Point2D, 256> = StaticDataSeries::new();
    series.push(Point2D::new(1.0, 45.0)).unwrap(); // Documents: 45%
    series.push(Point2D::new(2.0, 25.0)).unwrap(); // Photos: 25%
    series.push(Point2D::new(3.0, 20.0)).unwrap(); // Videos: 20%
    series.push(Point2D::new(4.0, 10.0)).unwrap(); // Other: 10%

    // Test different donut configurations
    let colors = [Rgb565::BLUE, Rgb565::RED, Rgb565::GREEN, Rgb565::YELLOW];

    // 1. Test manual donut creation
    let manual_donut: PieChart<Rgb565> = PieChart::builder()
        .center(Point::new(25, 25))
        .radius(20)
        .donut(10) // 50% inner radius
        .colors(&colors)
        .build()
        .unwrap();

    assert_eq!(manual_donut.style().donut_inner_radius, Some(10));

    // 2. Test percentage-based donut
    let percentage_donut: PieChart<Rgb565> = PieChart::builder()
        .center(Point::new(25, 25))
        .radius(20)
        .donut_percentage(40) // 40% inner radius = 8 pixels
        .colors(&colors)
        .build()
        .unwrap();

    assert_eq!(percentage_donut.style().donut_inner_radius, Some(8));

    // 3. Test convenience methods
    let balanced_donut: PieChart<Rgb565> = PieChart::builder()
        .center(Point::new(25, 25))
        .radius(20)
        .balanced_donut() // 50% inner radius = 10 pixels
        .colors(&colors)
        .build()
        .unwrap();

    assert_eq!(balanced_donut.style().donut_inner_radius, Some(10));

    let thin_donut: PieChart<Rgb565> = PieChart::builder()
        .center(Point::new(25, 25))
        .radius(20)
        .thin_donut() // 25% inner radius = 5 pixels
        .colors(&colors)
        .build()
        .unwrap();

    assert_eq!(thin_donut.style().donut_inner_radius, Some(5));

    let thick_donut: PieChart<Rgb565> = PieChart::builder()
        .center(Point::new(25, 25))
        .radius(20)
        .thick_donut() // 75% inner radius = 15 pixels
        .colors(&colors)
        .build()
        .unwrap();

    assert_eq!(thick_donut.style().donut_inner_radius, Some(15));

    // Test rendering all donut variations
    let mut display: MockDisplay<Rgb565> = MockDisplay::new();
    display.set_allow_overdraw(true);
    let viewport = Rectangle::new(Point::new(0, 0), Size::new(50, 50));

    // Test that all donut charts render successfully
    assert!(manual_donut
        .draw(&series, manual_donut.config(), viewport, &mut display)
        .is_ok());
    assert!(percentage_donut
        .draw(&series, percentage_donut.config(), viewport, &mut display)
        .is_ok());
    assert!(balanced_donut
        .draw(&series, balanced_donut.config(), viewport, &mut display)
        .is_ok());
    assert!(thin_donut
        .draw(&series, thin_donut.config(), viewport, &mut display)
        .is_ok());
    assert!(thick_donut
        .draw(&series, thick_donut.config(), viewport, &mut display)
        .is_ok());

    // Test comparison with regular pie chart
    let regular_pie: PieChart<Rgb565> = PieChart::builder()
        .center(Point::new(25, 25))
        .radius(20)
        // No donut configuration = regular pie
        .colors(&colors)
        .build()
        .unwrap();

    assert_eq!(regular_pie.style().donut_inner_radius, None);
    assert!(regular_pie
        .draw(&series, regular_pie.config(), viewport, &mut display)
        .is_ok());
}

#[test]
fn test_data_series_functionality() {
    let mut series: StaticDataSeries<Point2D, 10> = StaticDataSeries::new();

    // Test initial state
    assert_eq!(series.len(), 0);
    assert!(series.is_empty());
    assert_eq!(series.capacity(), 10);
    assert_eq!(series.remaining_capacity(), 10);

    // Test adding data
    series.push(Point2D::new(1.0, 2.0)).unwrap();
    assert_eq!(series.len(), 1);
    assert!(!series.is_empty());
    assert_eq!(series.remaining_capacity(), 9);

    // Test data retrieval
    assert_eq!(series.get(0), Some(Point2D::new(1.0, 2.0)));
    assert_eq!(series.get(1), None);

    // Test from_tuples
    let series2 =
        StaticDataSeries::<Point2D, 10>::from_tuples(&[(1.0, 2.0), (3.0, 4.0), (5.0, 6.0)])
            .unwrap();

    assert_eq!(series2.len(), 3);
    assert_eq!(series2.get(0), Some(Point2D::new(1.0, 2.0)));
    assert_eq!(series2.get(1), Some(Point2D::new(3.0, 4.0)));
    assert_eq!(series2.get(2), Some(Point2D::new(5.0, 6.0)));
}

#[test]
fn test_data_series_bounds_calculation() {
    let mut series: StaticDataSeries<Point2D, 10> = StaticDataSeries::new();
    series.push(Point2D::new(1.0, 5.0)).unwrap();
    series.push(Point2D::new(3.0, 2.0)).unwrap();
    series.push(Point2D::new(2.0, 8.0)).unwrap();

    let bounds = series.bounds().unwrap();
    assert_eq!(bounds.min_x, 1.0);
    assert_eq!(bounds.max_x, 3.0);
    assert_eq!(bounds.min_y, 2.0);
    assert_eq!(bounds.max_y, 8.0);
}

#[test]
#[cfg(feature = "line")]
fn test_error_handling() {
    // Test data series capacity limit
    let mut series: StaticDataSeries<Point2D, 2> = StaticDataSeries::new();
    series.push(Point2D::new(1.0, 2.0)).unwrap();
    series.push(Point2D::new(3.0, 4.0)).unwrap();

    // This should fail due to capacity limit
    let result = series.push(Point2D::new(5.0, 6.0));
    assert!(result.is_err());

    // Test empty data series
    let empty_series: StaticDataSeries<Point2D, 256> = StaticDataSeries::new();
    let chart: LineChart<Rgb565> = LineChart::new();
    let mut display = MockDisplay::new();
    let viewport = Rectangle::new(Point::new(0, 0), Size::new(60, 60));

    let result = chart.draw(&empty_series, chart.config(), viewport, &mut display);
    assert!(result.is_err());
}

// ============================================================================
// SCATTER CHART INTEGRATION TESTS
// ============================================================================

#[test]
#[cfg(feature = "scatter")]
fn test_scatter_chart_point_shapes() {
    let shapes = [
        PointShape::Circle,
        PointShape::Square,
        PointShape::Diamond,
        PointShape::Triangle,
        PointShape::Cross,
        PointShape::X,
        PointShape::Star,
    ];

    for shape in shapes.iter() {
        // Create sample data
        let mut series: StaticDataSeries<Point2D, 256> = StaticDataSeries::new();
        series.push(Point2D::new(1.0, 2.0)).unwrap();
        series.push(Point2D::new(2.0, 4.0)).unwrap();
        series.push(Point2D::new(3.0, 3.0)).unwrap();

        // Create scatter chart with specific shape
        let chart: ScatterChart<Rgb565> = ScatterChart::builder()
            .point_shape(*shape)
            .point_size(8)
            .point_color(Rgb565::RED)
            .with_title(&format!("Scatter Chart - {shape:?}"))
            .build()
            .unwrap();

        // Verify chart properties
        assert_eq!(chart.style().point_style.shape, *shape);
        assert_eq!(chart.style().point_style.size, 8);
        assert_eq!(chart.style().point_style.color, Rgb565::RED);

        // Test rendering
        let mut display: MockDisplay<Rgb565> = MockDisplay::new();
        display.set_allow_overdraw(true);
        let viewport = Rectangle::new(Point::new(5, 5), Size::new(50, 50));

        let result = chart.draw(&series, chart.config(), viewport, &mut display);
        assert!(result.is_ok(), "Failed to render shape: {shape:?}");
    }
}

#[test]
#[cfg(feature = "scatter")]
fn test_scatter_chart_size_mapping() {
    let scaling_types = [
        SizeScaling::Linear,
        SizeScaling::SquareRoot,
        SizeScaling::Logarithmic,
    ];

    for scaling in scaling_types.iter() {
        // Create sample data with varying Y values for size mapping
        let mut series: StaticDataSeries<Point2D, 256> = StaticDataSeries::new();
        series.push(Point2D::new(1.0, 10.0)).unwrap();
        series.push(Point2D::new(2.0, 50.0)).unwrap();
        series.push(Point2D::new(3.0, 100.0)).unwrap();
        series.push(Point2D::new(4.0, 25.0)).unwrap();

        // Create size mapping
        let size_mapping = SizeMapping {
            min_size: 4,
            max_size: 20,
            scaling: *scaling,
        };

        // Create scatter chart with size mapping
        let chart: ScatterChart<Rgb565> = ScatterChart::builder()
            .point_shape(PointShape::Circle)
            .point_color(Rgb565::BLUE)
            .with_size_mapping(size_mapping)
            .with_title(&format!("Bubble Chart - {scaling:?}"))
            .build()
            .unwrap();

        // Verify size mapping is configured
        assert!(chart.style().size_mapping.is_some());
        let mapping = chart.style().size_mapping.unwrap();
        assert_eq!(mapping.min_size, 4);
        assert_eq!(mapping.max_size, 20);
        assert_eq!(mapping.scaling, *scaling);

        // Test rendering
        let mut display: MockDisplay<Rgb565> = MockDisplay::new();
        display.set_allow_overdraw(true);
        let viewport = Rectangle::new(Point::new(0, 0), Size::new(60, 60));

        let result = chart.draw(&series, chart.config(), viewport, &mut display);
        assert!(result.is_ok(), "Failed to render size mapping: {scaling:?}");
    }
}

#[test]
#[cfg(feature = "scatter")]
fn test_scatter_chart_color_mapping() {
    let strategies = [
        ColorMappingStrategy::ValueBased,
        ColorMappingStrategy::IndexBased,
        ColorMappingStrategy::DistanceBased,
    ];

    for strategy in strategies.iter() {
        // Create sample data
        let mut series: StaticDataSeries<Point2D, 256> = StaticDataSeries::new();
        series.push(Point2D::new(1.0, 10.0)).unwrap();
        series.push(Point2D::new(2.0, 30.0)).unwrap();
        series.push(Point2D::new(3.0, 20.0)).unwrap();
        series.push(Point2D::new(4.0, 40.0)).unwrap();

        // Create color mapping
        let mut colors: Vec<Rgb565, 16> = Vec::new();
        colors.push(Rgb565::RED).unwrap();
        colors.push(Rgb565::GREEN).unwrap();
        colors.push(Rgb565::BLUE).unwrap();
        colors.push(Rgb565::YELLOW).unwrap();

        let color_mapping = ColorMapping {
            colors,
            strategy: *strategy,
        };

        // Create scatter chart with color mapping
        let chart: ScatterChart<Rgb565> = ScatterChart::builder()
            .point_shape(PointShape::Circle)
            .point_size(10)
            .with_color_mapping(color_mapping)
            .with_title(&format!("Color Mapping - {strategy:?}"))
            .build()
            .unwrap();

        // Verify color mapping is configured
        assert!(chart.style().color_mapping.is_some());
        let mapping = chart.style().color_mapping.as_ref().unwrap();
        assert_eq!(mapping.strategy, *strategy);
        assert_eq!(mapping.colors.len(), 4);

        // Test rendering
        let mut display: MockDisplay<Rgb565> = MockDisplay::new();
        display.set_allow_overdraw(true);
        let viewport = Rectangle::new(Point::new(0, 0), Size::new(120, 120));

        let result = chart.draw(&series, chart.config(), viewport, &mut display);
        assert!(
            result.is_ok(),
            "Failed to render color mapping: {strategy:?}"
        );
    }
}

#[test]
#[cfg(feature = "scatter")]
fn test_scatter_chart_collision_detection() {
    let strategies = [
        CollisionStrategy::Hide,
        CollisionStrategy::Offset,
        CollisionStrategy::Merge,
        CollisionStrategy::None,
    ];

    for strategy in strategies.iter() {
        // Create overlapping data points
        let mut series: StaticDataSeries<Point2D, 256> = StaticDataSeries::new();
        series.push(Point2D::new(1.0, 1.0)).unwrap();
        series.push(Point2D::new(1.01, 1.01)).unwrap(); // Very close to first point
        series.push(Point2D::new(2.0, 2.0)).unwrap();
        series.push(Point2D::new(2.01, 2.01)).unwrap(); // Very close to third point

        // Create collision settings
        let collision_settings = CollisionSettings {
            enabled: *strategy != CollisionStrategy::None,
            min_distance: 5,
            strategy: *strategy,
        };

        // Create scatter chart with collision detection
        let chart: ScatterChart<Rgb565> = ScatterChart::builder()
            .point_shape(PointShape::Circle)
            .point_size(12)
            .point_color(Rgb565::MAGENTA)
            .with_collision_detection(collision_settings)
            .with_title(&format!("Collision Detection - {strategy:?}"))
            .build()
            .unwrap();

        // Verify collision settings
        assert_eq!(chart.style().collision_detection.strategy, *strategy);
        assert_eq!(chart.style().collision_detection.min_distance, 5);

        // Test rendering
        let mut display: MockDisplay<Rgb565> = MockDisplay::new();
        display.set_allow_overdraw(true);
        let viewport = Rectangle::new(Point::new(0, 0), Size::new(100, 100));

        let result = chart.draw(&series, chart.config(), viewport, &mut display);
        assert!(
            result.is_ok(),
            "Failed to render collision detection: {strategy:?}"
        );
    }
}

#[test]
#[cfg(feature = "scatter")]
fn test_scatter_chart_connection_lines() {
    let patterns = [LinePattern::Solid, LinePattern::Dashed, LinePattern::Dotted];

    for pattern in patterns.iter() {
        // Create sample data
        let mut series: StaticDataSeries<Point2D, 256> = StaticDataSeries::new();
        series.push(Point2D::new(1.0, 2.0)).unwrap();
        series.push(Point2D::new(2.0, 4.0)).unwrap();
        series.push(Point2D::new(3.0, 3.0)).unwrap();
        series.push(Point2D::new(4.0, 5.0)).unwrap();

        // Create connection style
        let connection_style = ConnectionStyle {
            color: Rgb565::CYAN,
            width: 2,
            pattern: *pattern,
        };

        // Create scatter chart with connections
        let chart: ScatterChart<Rgb565> = ScatterChart::builder()
            .point_shape(PointShape::Circle)
            .point_size(6)
            .point_color(Rgb565::RED)
            .with_connections(connection_style)
            .with_title(&format!("Connected Scatter - {pattern:?}"))
            .build()
            .unwrap();

        // Verify connection settings
        assert!(chart.style().show_connections);
        assert!(chart.style().connection_style.is_some());
        let style = chart.style().connection_style.unwrap();
        assert_eq!(style.pattern, *pattern);
        assert_eq!(style.color, Rgb565::CYAN);
        assert_eq!(style.width, 2);

        // Test rendering
        let mut display: MockDisplay<Rgb565> = MockDisplay::new();
        display.set_allow_overdraw(true);
        // Use smaller viewport that fits in default MockDisplay (64x64)
        let viewport = Rectangle::new(Point::new(0, 0), Size::new(60, 60));

        let result = chart.draw(&series, chart.config(), viewport, &mut display);
        assert!(result.is_ok(), "Failed to render connections: {pattern:?}");
    }
}

#[test]
#[cfg(feature = "scatter")]
fn test_scatter_chart_comprehensive_features() {
    // Create comprehensive scatter chart with all features
    let mut series: StaticDataSeries<Point2D, 256> = StaticDataSeries::new();
    series.push(Point2D::new(1.0, 10.0)).unwrap();
    series.push(Point2D::new(2.0, 25.0)).unwrap();
    series.push(Point2D::new(3.0, 15.0)).unwrap();
    series.push(Point2D::new(4.0, 35.0)).unwrap();
    series.push(Point2D::new(5.0, 20.0)).unwrap();

    // Create color mapping
    let mut colors: Vec<Rgb565, 16> = Vec::new();
    colors.push(Rgb565::RED).unwrap();
    colors.push(Rgb565::GREEN).unwrap();
    colors.push(Rgb565::BLUE).unwrap();

    let color_mapping = ColorMapping {
        colors,
        strategy: ColorMappingStrategy::ValueBased,
    };

    // Create size mapping
    let size_mapping = SizeMapping {
        min_size: 6,
        max_size: 16,
        scaling: SizeScaling::SquareRoot,
    };

    // Create collision settings
    let collision_settings = CollisionSettings {
        enabled: true,
        min_distance: 3,
        strategy: CollisionStrategy::Offset,
    };

    // Create connection style
    let connection_style = ConnectionStyle {
        color: Rgb565::WHITE,
        width: 1,
        pattern: LinePattern::Solid,
    };

    // Create comprehensive scatter chart
    let chart: ScatterChart<Rgb565> = ScatterChart::builder()
        .point_shape(PointShape::Diamond)
        .with_size_mapping(size_mapping)
        .with_color_mapping(color_mapping)
        .with_collision_detection(collision_settings)
        .with_connections(connection_style)
        .with_title("Comprehensive Scatter Chart")
        .background_color(Rgb565::BLACK)
        .build()
        .unwrap();

    // Verify all features are configured
    assert_eq!(chart.style().point_style.shape, PointShape::Diamond);
    assert!(chart.style().size_mapping.is_some());
    assert!(chart.style().color_mapping.is_some());
    assert!(chart.style().collision_detection.enabled);
    assert!(chart.style().show_connections);
    assert_eq!(chart.config().background_color, Some(Rgb565::BLACK));

    // Test rendering
    let mut display: MockDisplay<Rgb565> = MockDisplay::new();
    display.set_allow_overdraw(true);
    let viewport = Rectangle::new(Point::new(0, 0), Size::new(60, 60));

    let result = chart.draw(&series, chart.config(), viewport, &mut display);
    assert!(result.is_ok());
}

// ============================================================================
// GAUGE CHART INTEGRATION TESTS
// ============================================================================

#[test]
#[cfg(feature = "gauge")]
fn test_gauge_chart_types() {
    let gauge_types = [
        GaugeType::Semicircle,
        GaugeType::ThreeQuarter,
        GaugeType::FullCircle,
        GaugeType::Custom {
            start_angle: -45.0,
            end_angle: 225.0,
        },
    ];

    for gauge_type in gauge_types.iter() {
        // Create single value data
        let mut series: StaticDataSeries<Point2D, 1> = StaticDataSeries::new();
        series.push(Point2D::new(0.0, 75.0)).unwrap(); // 75% value

        // Create gauge chart
        let chart: GaugeChart<Rgb565> = GaugeChart::builder()
            .gauge_type(*gauge_type)
            .value_range(0.0, 100.0)
            .radius(20)
            .with_title(&format!("Gauge - {gauge_type:?}"))
            .build()
            .unwrap();

        // Verify gauge type
        assert_eq!(chart.gauge_type(), *gauge_type);
        assert_eq!(chart.value_range().min, 0.0);
        assert_eq!(chart.value_range().max, 100.0);

        // Test rendering
        let mut display: MockDisplay<Rgb565> = MockDisplay::new();
        display.set_allow_overdraw(true);
        let viewport = Rectangle::new(Point::new(0, 0), Size::new(60, 60));

        let result = chart.draw(&series, chart.config(), viewport, &mut display);
        assert!(
            result.is_ok(),
            "Failed to render gauge type: {gauge_type:?}"
        );
    }
}

#[test]
#[cfg(feature = "gauge")]
fn test_gauge_chart_needle_shapes() {
    let needle_shapes = [NeedleShape::Line, NeedleShape::Arrow, NeedleShape::Pointer];

    for needle_shape in needle_shapes.iter() {
        // Create single value data
        let mut series: StaticDataSeries<Point2D, 1> = StaticDataSeries::new();
        series.push(Point2D::new(0.0, 60.0)).unwrap(); // 60% value

        // Create gauge chart with specific needle shape
        let chart: GaugeChart<Rgb565> = GaugeChart::builder()
            .gauge_type(GaugeType::Semicircle)
            .value_range(0.0, 100.0)
            .radius(15)
            .needle_style(*needle_shape, Rgb565::RED, 0.6, 2)
            .with_title(&format!("Needle - {needle_shape:?}"))
            .build()
            .unwrap();

        // Verify needle configuration
        assert_eq!(chart.style().needle_style.shape, *needle_shape);
        assert_eq!(chart.style().needle_style.color, Rgb565::RED);
        assert_eq!(chart.style().needle_style.length, 0.6);
        assert_eq!(chart.style().needle_style.width, 2);

        // Test rendering with appropriate size
        let mut display: MockDisplay<Rgb565> = MockDisplay::new();
        display.set_allow_overdraw(true);
        let viewport = Rectangle::new(Point::new(0, 0), Size::new(55, 55));

        let result = chart.draw(&series, chart.config(), viewport, &mut display);
        assert!(
            result.is_ok(),
            "Failed to render needle shape: {needle_shape:?}"
        );
    }
}

#[test]
#[cfg(feature = "gauge")]
fn test_gauge_chart_threshold_zones() {
    // Create single value data
    let mut series: StaticDataSeries<Point2D, 1> = StaticDataSeries::new();
    series.push(Point2D::new(0.0, 85.0)).unwrap(); // 85% value (in warning zone)

    // Create gauge chart with threshold zones
    let chart: GaugeChart<Rgb565> = GaugeChart::builder()
        .gauge_type(GaugeType::Semicircle)
        .value_range(0.0, 100.0)
        .radius(15)
        .add_threshold_zone(0.0, 60.0, Rgb565::GREEN) // Safe zone
        .add_threshold_zone(60.0, 80.0, Rgb565::YELLOW) // Warning zone
        .add_threshold_zone(80.0, 100.0, Rgb565::RED) // Danger zone
        .needle_style(NeedleShape::Arrow, Rgb565::BLACK, 0.6, 1)
        .with_title("Temperature Gauge")
        .build()
        .unwrap();

    // Verify threshold zones
    assert_eq!(chart.style().threshold_zones.len(), 6);

    let zones = &chart.style().threshold_zones;
    assert_eq!(zones[0].start, 0.0);
    assert_eq!(zones[0].end, 30.0);
    assert_eq!(zones[0].color, Rgb565::GREEN);

    assert_eq!(zones[1].start, 30.0);
    assert_eq!(zones[1].end, 70.0);
    assert_eq!(zones[1].color, Rgb565::YELLOW);

    assert_eq!(zones[2].start, 70.0);
    assert_eq!(zones[2].end, 100.0);
    assert_eq!(zones[2].color, Rgb565::RED);

    // Test rendering
    let mut display: MockDisplay<Rgb565> = MockDisplay::new();
    display.set_allow_overdraw(true);
    let viewport = Rectangle::new(Point::new(0, 0), Size::new(60, 60));

    let result = chart.draw(&series, chart.config(), viewport, &mut display);
    assert!(result.is_ok());
}

// ============================================================================
// PERFORMANCE TESTS FOR LARGE DATASETS
// ============================================================================

#[test]
#[cfg(feature = "line")]
fn test_line_chart_large_dataset_performance() {
    // Create large dataset (250+ points - max capacity)
    let mut series: StaticDataSeries<Point2D, 256> = StaticDataSeries::new();

    // Generate sine wave with 250 points
    for i in 0..250 {
        let x = i as f32 * 0.01;
        let y = 50.0 + 30.0 * (x * 2.0).sin() + 10.0 * (x * 5.0).cos();
        series.push(Point2D::new(x, y)).unwrap();
    }

    // Create line chart
    let chart: LineChart<Rgb565> = LineChart::builder()
        .line_color(Rgb565::BLUE)
        .line_width(1)
        .with_title("Large Dataset Line Chart")
        .build()
        .unwrap();

    // Test rendering performance
    let mut display: MockDisplay<Rgb565> = MockDisplay::new();
    display.set_allow_overdraw(true);
    let viewport = Rectangle::new(Point::new(0, 0), Size::new(60, 60));

    let result = chart.draw(&series, chart.config(), viewport, &mut display);
    assert!(result.is_ok());

    // Verify data integrity
    assert_eq!(series.len(), 250);

    // Test bounds calculation performance
    let bounds = series.bounds().unwrap();
    assert!(bounds.min_x >= 0.0);
    assert!(bounds.max_x <= 2.5);
    assert!(bounds.min_y >= 10.0);
    assert!(bounds.max_y <= 90.0);
}

#[test]
#[cfg(feature = "scatter")]
fn test_scatter_chart_large_dataset_performance() {
    // Create large scatter dataset (250+ points - max capacity)
    let mut series: StaticDataSeries<Point2D, 256> = StaticDataSeries::new();

    // Generate random-like scatter data
    for i in 0..250 {
        let x = (i as f32 * 0.1) % 50.0;
        let y = 25.0 + 20.0 * ((i as f32 * 0.17).sin() + (i as f32 * 0.23).cos());
        series.push(Point2D::new(x, y)).unwrap();
    }

    // Create scatter chart with size mapping for bubble effect
    let size_mapping = SizeMapping {
        min_size: 2,
        max_size: 8,
        scaling: SizeScaling::Linear,
    };

    let chart: ScatterChart<Rgb565> = ScatterChart::builder()
        .point_shape(PointShape::Circle)
        .point_color(Rgb565::RED)
        .with_size_mapping(size_mapping)
        .with_title("Large Dataset Scatter Chart")
        .build()
        .unwrap();

    // Test rendering performance
    let mut display: MockDisplay<Rgb565> = MockDisplay::new();
    display.set_allow_overdraw(true);
    let viewport = Rectangle::new(Point::new(0, 0), Size::new(60, 60));

    let result = chart.draw(&series, chart.config(), viewport, &mut display);
    assert!(result.is_ok());

    // Verify data integrity
    assert_eq!(series.len(), 250);

    // Test bounds calculation performance
    let bounds = series.bounds().unwrap();
    assert!(bounds.min_x >= 0.0);
    assert!(bounds.max_x <= 50.0);
}

#[test]
#[cfg(feature = "line")]
fn test_memory_usage_validation() {
    // Test memory usage with different dataset sizes
    let sizes = [50, 100, 200, 256];

    for &size in sizes.iter() {
        // Create dataset of specified size
        let mut series: StaticDataSeries<Point2D, 256> = StaticDataSeries::new();

        for i in 0..size {
            let x = i as f32;
            let y = (i as f32 * 0.1).sin() * 100.0;
            series.push(Point2D::new(x, y)).unwrap();
        }

        // Test with line chart
        let line_chart: LineChart<Rgb565> = LineChart::builder()
            .line_color(Rgb565::GREEN)
            .line_width(1)
            .build()
            .unwrap();

        let mut display: MockDisplay<Rgb565> = MockDisplay::new();
        display.set_allow_overdraw(true);
        let viewport = Rectangle::new(Point::new(0, 0), Size::new(60, 60));

        let result = line_chart.draw(&series, line_chart.config(), viewport, &mut display);
        assert!(result.is_ok(), "Failed with dataset size: {size}");

        #[cfg(feature = "scatter")]
        {
            // Test with scatter chart
            let scatter_chart: ScatterChart<Rgb565> = ScatterChart::builder()
                .point_shape(PointShape::Circle)
                .point_size(3)
                .point_color(Rgb565::BLUE)
                .build()
                .unwrap();

            let mut display2: MockDisplay<Rgb565> = MockDisplay::new();
            display2.set_allow_overdraw(true);

            let result2 =
                scatter_chart.draw(&series, scatter_chart.config(), viewport, &mut display2);
            assert!(result2.is_ok(), "Scatter failed with dataset size: {size}");
        }

        // Verify series properties
        assert_eq!(series.len(), size);
        assert!(!series.is_empty());

        if size > 0 {
            let bounds = series.bounds().unwrap();
            assert!(bounds.max_x >= bounds.min_x);
            assert!(bounds.max_y >= bounds.min_y);
        }
    }
}

#[test]
#[cfg(feature = "line")]
fn test_rendering_performance_benchmarks() {
    // Create benchmark dataset
    let mut series: StaticDataSeries<Point2D, 256> = StaticDataSeries::new();

    // Generate complex waveform (250 points)
    for i in 0..250 {
        let x = i as f32 * 0.02;
        let y = 50.0 + 20.0 * (x * 3.0).sin() + 10.0 * (x * 7.0).cos() + 5.0 * (x * 15.0).sin();
        series.push(Point2D::new(x, y)).unwrap();
    }

    // Test line chart rendering
    let line_chart: LineChart<Rgb565> = LineChart::builder()
        .line_color(Rgb565::CYAN)
        .line_width(2)
        .with_title("Performance Benchmark - Line")
        .build()
        .unwrap();

    let mut display: MockDisplay<Rgb565> = MockDisplay::new();
    display.set_allow_overdraw(true);
    let viewport = Rectangle::new(Point::new(0, 0), Size::new(60, 60));

    let result = line_chart.draw(&series, line_chart.config(), viewport, &mut display);
    assert!(result.is_ok());

    #[cfg(feature = "scatter")]
    {
        // Test scatter chart with all features
        let mut colors: Vec<Rgb565, 16> = Vec::new();
        colors.push(Rgb565::RED).unwrap();
        colors.push(Rgb565::GREEN).unwrap();
        colors.push(Rgb565::BLUE).unwrap();
        colors.push(Rgb565::YELLOW).unwrap();

        let color_mapping = ColorMapping {
            colors,
            strategy: ColorMappingStrategy::ValueBased,
        };

        let size_mapping = SizeMapping {
            min_size: 2,
            max_size: 6,
            scaling: SizeScaling::Linear,
        };

        let scatter_chart: ScatterChart<Rgb565> = ScatterChart::builder()
            .point_shape(PointShape::Circle)
            .with_size_mapping(size_mapping)
            .with_color_mapping(color_mapping)
            .with_title("Performance Benchmark - Scatter")
            .build()
            .unwrap();

        let mut display2: MockDisplay<Rgb565> = MockDisplay::new();
        display2.set_allow_overdraw(true);

        let result2 = scatter_chart.draw(&series, scatter_chart.config(), viewport, &mut display2);
        assert!(result2.is_ok());
    }

    // Verify data integrity after rendering
    assert_eq!(series.len(), 250);
    let bounds = series.bounds().unwrap();
    assert!(bounds.min_x >= 0.0);
    assert!(bounds.max_x <= 5.0);
}

// ============================================================================
// VISUAL REGRESSION TESTS
// ============================================================================

#[test]
#[cfg(feature = "line")]
fn test_visual_output_consistency() {
    // Create consistent test data
    let mut series: StaticDataSeries<Point2D, 256> = StaticDataSeries::new();
    series.push(Point2D::new(0.0, 0.0)).unwrap();
    series.push(Point2D::new(1.0, 1.0)).unwrap();
    series.push(Point2D::new(2.0, 0.5)).unwrap();
    series.push(Point2D::new(3.0, 1.5)).unwrap();

    // Test line chart visual consistency
    let line_chart: LineChart<Rgb565> = LineChart::builder()
        .line_color(Rgb565::BLUE)
        .line_width(2)
        .with_title("Visual Test - Line")
        .background_color(Rgb565::WHITE)
        .build()
        .unwrap();

    let mut display1: MockDisplay<Rgb565> = MockDisplay::new();
    display1.set_allow_overdraw(true);
    let viewport = Rectangle::new(Point::new(0, 0), Size::new(60, 60));

    let result1 = line_chart.draw(&series, line_chart.config(), viewport, &mut display1);
    assert!(result1.is_ok());

    #[cfg(feature = "scatter")]
    {
        // Test scatter chart visual consistency
        let scatter_chart: ScatterChart<Rgb565> = ScatterChart::builder()
            .point_shape(PointShape::Circle)
            .point_size(6)
            .point_color(Rgb565::RED)
            .with_title("Visual Test - Scatter")
            .background_color(Rgb565::WHITE)
            .build()
            .unwrap();

        let mut display2: MockDisplay<Rgb565> = MockDisplay::new();
        display2.set_allow_overdraw(true);

        let result2 = scatter_chart.draw(&series, scatter_chart.config(), viewport, &mut display2);
        assert!(result2.is_ok());
    }

    #[cfg(feature = "gauge")]
    {
        // Test gauge chart visual consistency
        let mut gauge_series: StaticDataSeries<Point2D, 1> = StaticDataSeries::new();
        gauge_series.push(Point2D::new(0.0, 75.0)).unwrap();

        let gauge_chart: GaugeChart<Rgb565> = GaugeChart::builder()
            .gauge_type(GaugeType::Semicircle)
            .value_range(0.0, 100.0)
            .radius(15)
            .add_threshold_zone(0.0, 50.0, Rgb565::GREEN)
            .add_threshold_zone(50.0, 80.0, Rgb565::YELLOW)
            .add_threshold_zone(80.0, 100.0, Rgb565::RED)
            .needle_style(NeedleShape::Arrow, Rgb565::BLACK, 0.6, 1)
            .build()
            .unwrap();

        let mut display3: MockDisplay<Rgb565> = MockDisplay::new();
        display3.set_allow_overdraw(true);

        let result3 =
            gauge_chart.draw(&gauge_series, gauge_chart.config(), viewport, &mut display3);
        assert!(result3.is_ok());
    }

    // Verify that line chart rendered without errors
    // In a real visual regression test, we would compare pixel data
    // For now, we just ensure no rendering errors occurred
    assert!(result1.is_ok());
}

#[test]
#[cfg(feature = "line")]
fn test_different_color_types() {
    use embedded_graphics::pixelcolor::BinaryColor;

    // Test with binary color (monochrome)
    let chart: LineChart<BinaryColor> = LineChart::new();
    let mut series: StaticDataSeries<Point2D, 256> = StaticDataSeries::new();
    series.push(Point2D::new(0.0, 1.0)).unwrap();

    let mut display = MockDisplay::new();
    let viewport = Rectangle::new(Point::new(0, 0), Size::new(100, 100));

    let result = chart.draw(&series, chart.config(), viewport, &mut display);
    assert!(result.is_ok());
}

#[test]
fn test_data_points_macro() {
    // Test if the data_points! macro works correctly
    use embedded_charts::prelude::*;

    // Test the macro
    let data = data_points![(0.0, 10.0), (1.0, 20.0), (2.0, 15.0)];

    // Verify the data
    assert_eq!(data.len(), 3);

    let points: heapless::Vec<_, 256> = data.iter().collect();
    assert_eq!(points[0].x(), 0.0);
    assert_eq!(points[0].y(), 10.0);
    assert_eq!(points[1].x(), 1.0);
    assert_eq!(points[1].y(), 20.0);
    assert_eq!(points[2].x(), 2.0);
    assert_eq!(points[2].y(), 15.0);

    // Test with trailing comma
    let data2 = data_points![(0.0, 5.0), (1.0, 10.0),];
    assert_eq!(data2.len(), 2);

    // Test empty macro (should create empty series)
    let empty_data = data_points![];
    assert_eq!(empty_data.len(), 0);
    assert!(empty_data.is_empty());
}