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
//! ## Chart
//!
//! A component to plot one or more dataset in a cartesian coordinate system

/**
 * MIT License
 *
 * tui-realm - Copyright (C) 2021 Christian Visintin
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
use std::collections::LinkedList;
use tuirealm::event::Event;
use tuirealm::event::KeyCode;
use tuirealm::props::{
    Alignment, BlockTitle, BordersProps, Dataset, PropPayload, PropValue, Props, PropsBuilder,
};
use tuirealm::tui::{
    layout::Rect,
    style::{Color, Style},
    text::Span,
    widgets::{Axis, Block, BorderType, Borders, Chart as TuiChart, Dataset as TuiDataset},
};
use tuirealm::{Component, Frame, Msg, Payload};

// -- Props
const PROP_X_BOUNDS: &str = "x-bounds";
const PROP_Y_BOUNDS: &str = "y-bounds";
const PROP_X_LABELS: &str = "x-labels";
const PROP_X_STYLE: &str = "x-style";
const PROP_Y_LABELS: &str = "y-labels";
const PROP_X_TITLE: &str = "x-title";
const PROP_Y_STYLE: &str = "y-style";
const PROP_Y_TITLE: &str = "y-title";
const PROP_DATA: &str = "data";
const PROP_DISABLED: &str = "disabled";

pub struct ChartPropsBuilder {
    props: Option<Props>,
}

impl Default for ChartPropsBuilder {
    fn default() -> Self {
        Self {
            props: Some(Props::default()),
        }
    }
}

impl PropsBuilder for ChartPropsBuilder {
    fn build(&mut self) -> Props {
        self.props.take().unwrap()
    }

    fn hidden(&mut self) -> &mut Self {
        if let Some(props) = self.props.as_mut() {
            props.visible = false;
        }
        self
    }

    fn visible(&mut self) -> &mut Self {
        if let Some(props) = self.props.as_mut() {
            props.visible = true;
        }
        self
    }
}

impl From<Props> for ChartPropsBuilder {
    fn from(props: Props) -> Self {
        Self { props: Some(props) }
    }
}

impl ChartPropsBuilder {
    /// ### with_foreground
    ///
    /// Set foreground color for component
    pub fn with_foreground(&mut self, color: Color) -> &mut Self {
        if let Some(props) = self.props.as_mut() {
            props.foreground = color;
        }
        self
    }

    /// ### with_background
    ///
    /// Set background color for component
    pub fn with_background(&mut self, color: Color) -> &mut Self {
        if let Some(props) = self.props.as_mut() {
            props.background = color;
        }
        self
    }

    /// ### with_borders
    ///
    /// Set component borders style
    pub fn with_borders(
        &mut self,
        borders: Borders,
        variant: BorderType,
        color: Color,
    ) -> &mut Self {
        if let Some(props) = self.props.as_mut() {
            props.borders = BordersProps {
                borders,
                variant,
                color,
            }
        }
        self
    }

    /// ### with_title
    ///
    /// Set title
    pub fn with_title<S: AsRef<str>>(&mut self, title: S, alignment: Alignment) -> &mut Self {
        if let Some(props) = self.props.as_mut() {
            props.title = Some(BlockTitle::new(title, alignment));
        }
        self
    }

    /// ### with_x_bounds
    ///
    /// Define x axis bounds
    pub fn with_x_bounds(&mut self, (floor, ceil): (f64, f64)) -> &mut Self {
        if let Some(props) = self.props.as_mut() {
            props.own.insert(
                PROP_X_BOUNDS,
                PropPayload::Tup2((PropValue::F64(floor), PropValue::F64(ceil))),
            );
        }
        self
    }

    /// ### with_y_bounds
    ///
    /// Define y axis bounds
    pub fn with_y_bounds(&mut self, (floor, ceil): (f64, f64)) -> &mut Self {
        if let Some(props) = self.props.as_mut() {
            props.own.insert(
                PROP_Y_BOUNDS,
                PropPayload::Tup2((PropValue::F64(floor), PropValue::F64(ceil))),
            );
        }
        self
    }

    /// ### with_x_labels
    ///
    /// Define x axis labels
    pub fn with_x_labels(&mut self, labels: &[&str]) -> &mut Self {
        if let Some(props) = self.props.as_mut() {
            props.own.insert(
                PROP_X_LABELS,
                PropPayload::Vec(
                    labels
                        .iter()
                        .map(|x| PropValue::Str(x.to_string()))
                        .collect(),
                ),
            );
        }
        self
    }

    /// ### with_y_labels
    ///
    /// Define y axis labels
    pub fn with_y_labels(&mut self, labels: &[&str]) -> &mut Self {
        if let Some(props) = self.props.as_mut() {
            props.own.insert(
                PROP_Y_LABELS,
                PropPayload::Vec(
                    labels
                        .iter()
                        .map(|x| PropValue::Str(x.to_string()))
                        .collect(),
                ),
            );
        }
        self
    }

    /// ### with_x_style
    ///
    /// Define x axis style
    pub fn with_x_style(&mut self, s: Style) -> &mut Self {
        if let Some(props) = self.props.as_mut() {
            props
                .own
                .insert(PROP_X_STYLE, PropPayload::One(PropValue::Style(s)));
        }
        self
    }

    /// ### with_y_style
    ///
    /// Define y axis style
    pub fn with_y_style(&mut self, s: Style) -> &mut Self {
        if let Some(props) = self.props.as_mut() {
            props
                .own
                .insert(PROP_Y_STYLE, PropPayload::One(PropValue::Style(s)));
        }
        self
    }

    /// ### with_x_title
    ///
    /// Define x axis title
    pub fn with_x_title(&mut self, title: &str) -> &mut Self {
        if let Some(props) = self.props.as_mut() {
            props.own.insert(
                PROP_X_TITLE,
                PropPayload::One(PropValue::Str(title.to_string())),
            );
        }
        self
    }

    /// ### with_y_title
    ///
    /// Define y axis title
    pub fn with_y_title(&mut self, title: &str) -> &mut Self {
        if let Some(props) = self.props.as_mut() {
            props.own.insert(
                PROP_Y_TITLE,
                PropPayload::One(PropValue::Str(title.to_string())),
            );
        }
        self
    }

    /// ### with_data
    ///
    /// Define chart data as a list of dataset
    pub fn with_data(&mut self, data: &[Dataset]) -> &mut Self {
        if let Some(props) = self.props.as_mut() {
            // Create linked list
            let mut list: LinkedList<PropPayload> = LinkedList::new();
            data.iter()
                .for_each(|x| list.push_back(PropPayload::One(PropValue::Dataset(x.clone()))));
            props.own.insert(PROP_DATA, PropPayload::Linked(list));
        }
        self
    }

    /// ### push_record
    ///
    /// Just pushes a record to the front of the data
    /// Set is the index of the chart where you want to push the point
    /// Panics if data is not initialized
    pub fn push_record(&mut self, set: usize, point: (f64, f64)) -> &mut Self {
        if let Some(props) = self.props.as_mut() {
            match props.own.get_mut(PROP_DATA) {
                Some(PropPayload::Linked(list)) => {
                    if let Some(PropPayload::One(PropValue::Dataset(dataset))) =
                        list.iter_mut().nth(set)
                    {
                        dataset.push(point);
                    }
                }
                _ => panic!("Data must be initialized first"),
            }
        }
        self
    }

    /// ### pop_record_back
    ///
    /// Pop last record on the back of the data list
    /// Set is the index of the chart where you want to pop from
    pub fn pop_record_back(&mut self, set: usize) -> &mut Self {
        if let Some(props) = self.props.as_mut() {
            if let Some(PropPayload::Linked(list)) = props.own.get_mut(PROP_DATA) {
                if let Some(PropPayload::One(PropValue::Dataset(dataset))) =
                    list.iter_mut().nth(set)
                {
                    dataset.pop();
                }
            }
        }
        self
    }

    /// ### pop_record_front
    ///
    /// Pop first record on the back of the data list
    /// Set is the index of the chart where you want to pop from
    pub fn pop_record_front(&mut self, set: usize) -> &mut Self {
        if let Some(props) = self.props.as_mut() {
            if let Some(PropPayload::Linked(list)) = props.own.get_mut(PROP_DATA) {
                if let Some(PropPayload::One(PropValue::Dataset(dataset))) =
                    list.iter_mut().nth(set)
                {
                    dataset.pop_front();
                }
            }
        }
        self
    }

    /// ### disabled
    ///
    /// If component is set to `disabled`, then input commands won't work, and colors will be rendered
    /// as if the component would have focus
    pub fn disabled(&mut self, disabled: bool) -> &mut Self {
        if let Some(props) = self.props.as_mut() {
            props
                .own
                .insert(PROP_DISABLED, PropPayload::One(PropValue::Bool(disabled)));
        }
        self
    }
}

// -- states

// -- states

/// ### OwnStates
///
/// chart states
struct OwnStates {
    cursor: usize,
    focus: bool,
}

impl Default for OwnStates {
    fn default() -> Self {
        Self {
            cursor: 0,
            focus: false,
        }
    }
}

impl OwnStates {
    /// ### move_cursor_left
    ///
    /// Move cursor to the left
    pub fn move_cursor_left(&mut self) {
        if self.cursor > 0 {
            self.cursor -= 1;
        }
    }

    /// ### move_cursor_right
    ///
    /// Move cursor to the right
    pub fn move_cursor_right(&mut self, data_len: usize) {
        if data_len > 0 && self.cursor + 1 < data_len {
            self.cursor += 1;
        }
    }

    /// ### reset_cursor
    ///
    /// Reset cursor to 0
    pub fn reset_cursor(&mut self) {
        self.cursor = 0;
    }

    /// ### cursor_at_end
    ///
    /// Move cursor to the end of the chart
    pub fn cursor_at_end(&mut self, data_len: usize) {
        if data_len > 0 {
            self.cursor = data_len - 1;
        } else {
            self.cursor = 0;
        }
    }
}

// -- component

/// ### Chart
///
/// A component to display a chart on a cartesian coordinate system.
/// The chart can work both in "active" and "disabled" mode.
///
/// #### Disabled mode
///
/// When in disabled mode, the chart won't be interactive, so you won't be able to move through data using keys.
/// If you have more data than the maximum amount of bars that can be displayed, you'll have to update data to display the remaining entries
///
/// #### Active mode
///
/// While in active mode (default) you can put as many entries as you wish. You can move with arrows and END/HOME keys
pub struct Chart {
    props: Props,
    states: OwnStates,
}

impl Chart {
    /// ### new
    ///
    /// Instantiates a new `Chart` component
    pub fn new(props: Props) -> Self {
        Self {
            props,
            states: OwnStates::default(),
        }
    }

    /// ### disabled
    ///
    /// Returns whether the component is in `disabled` mode
    fn disabled(&self) -> bool {
        self.props
            .own
            .get(PROP_DISABLED)
            .map(|x| match x {
                PropPayload::One(PropValue::Bool(disabled)) => *disabled,
                _ => false,
            })
            .unwrap_or(false)
    }

    /// ### data_len
    ///
    /// Retrieve current data len from properties
    fn data_len(&self) -> usize {
        self.props
            .own
            .get(PROP_DATA)
            .map(|x| match x {
                PropPayload::Linked(l) => l.len(),
                _ => 0,
            })
            .unwrap_or(0)
    }

    /// ### max_dataset_len
    ///
    /// Get the maximum len among the datasets
    fn max_dataset_len(&self) -> usize {
        self.props
            .own
            .get(PROP_DATA)
            .map(|x| {
                (match x {
                    PropPayload::Linked(l) => l
                        .iter()
                        .map(|x| match x {
                            PropPayload::One(PropValue::Dataset(dataset)) => {
                                dataset.get_data().len()
                            }
                            _ => 0,
                        })
                        .max(),
                    _ => Some(0),
                })
                .unwrap_or(0)
            })
            .unwrap_or(0)
    }

    /// ### data
    ///
    /// Get data to be displayed, starting from provided index at `start` with a max length of `len`
    fn data(&self, start: usize, len: usize) -> Vec<TuiDataset> {
        if let Some(PropPayload::Linked(list)) = self.props.own.get(PROP_DATA) {
            let mut data: Vec<TuiDataset> = Vec::with_capacity(self.data_len());
            for item in list.iter() {
                if let PropPayload::One(PropValue::Dataset(dataset)) = item {
                    data.push(Self::get_tui_dataset(dataset, start, len));
                }
            }
            data
        } else {
            Vec::new()
        }
    }
}

impl<'a> Chart {
    /// ### get_tui_dataset
    ///
    /// Create tui_dataset from dataset
    /// Only elements from `start` to `len` are preserved from dataset
    fn get_tui_dataset(dataset: &'a Dataset, start: usize, len: usize) -> TuiDataset<'a> {
        // Recalc len
        let points = dataset.get_data();
        let len: usize = match points.len() > start {
            true => std::cmp::min(len, points.len() - start),
            false => 0,
        };
        // Prepare data storage
        let end: usize = points.len() - len;
        TuiDataset::default()
            .name(dataset.name.clone())
            .marker(dataset.marker)
            .graph_type(dataset.graph_type)
            .style(dataset.style)
            .data(&points[start..end])
    }
}

impl Component for Chart {
    /// ### render
    ///
    /// Based on the current properties and states, renders a widget using the provided render engine in the provided Area
    /// If focused, cursor is also set (if supported by widget)
    fn render(&self, render: &mut Frame, area: Rect) {
        if self.props.visible {
            // If component is disabled, will be displayed as `active`; as focus state otherwise
            let active: bool = match self.disabled() {
                true => true,
                false => self.states.focus,
            };
            let block: Block =
                crate::utils::get_block(&self.props.borders, self.props.title.as_ref(), active);
            // Get data
            let data: Vec<TuiDataset> = self.data(self.states.cursor, area.width as usize);
            // Create widget
            // -- x axis
            let mut x_axis: Axis = Axis::default();
            if let Some(PropPayload::Tup2((PropValue::F64(floor), PropValue::F64(ceil)))) =
                self.props.own.get(PROP_X_BOUNDS)
            {
                let why_using_vecs_when_you_can_use_useless_arrays: [f64; 2] = [*floor, *ceil];
                x_axis = x_axis.bounds(why_using_vecs_when_you_can_use_useless_arrays);
            }
            if let Some(PropPayload::Vec(labels)) = self.props.own.get(PROP_X_LABELS) {
                x_axis = x_axis.labels(
                    labels
                        .iter()
                        .map(|x| Span::from(x.unwrap_str().to_string()))
                        .collect(),
                );
            }
            if let Some(PropPayload::One(PropValue::Style(s))) = self.props.own.get(PROP_X_STYLE) {
                x_axis = x_axis.style(*s);
            }
            if let Some(PropPayload::One(PropValue::Str(title))) = self.props.own.get(PROP_X_TITLE)
            {
                x_axis = x_axis.title(Span::styled(
                    title,
                    Style::default()
                        .fg(self.props.foreground)
                        .bg(self.props.background),
                ));
            }
            // -- y axis
            let mut y_axis: Axis = Axis::default();
            if let Some(PropPayload::Tup2((PropValue::F64(floor), PropValue::F64(ceil)))) =
                self.props.own.get(PROP_Y_BOUNDS)
            {
                let why_using_vecs_when_you_can_use_useless_arrays: [f64; 2] = [*floor, *ceil];
                y_axis = y_axis.bounds(why_using_vecs_when_you_can_use_useless_arrays);
            }
            if let Some(PropPayload::Vec(labels)) = self.props.own.get(PROP_Y_LABELS) {
                y_axis = y_axis.labels(
                    labels
                        .iter()
                        .map(|x| Span::from(x.unwrap_str().to_string()))
                        .collect(),
                );
            }
            if let Some(PropPayload::One(PropValue::Style(s))) = self.props.own.get(PROP_Y_STYLE) {
                y_axis = y_axis.style(*s);
            }
            if let Some(PropPayload::One(PropValue::Str(title))) = self.props.own.get(PROP_Y_TITLE)
            {
                y_axis = y_axis.title(Span::styled(
                    title,
                    Style::default()
                        .fg(self.props.foreground)
                        .bg(self.props.background),
                ));
            }
            // Build widget
            let widget: TuiChart = TuiChart::new(data)
                .block(block)
                .x_axis(x_axis)
                .y_axis(y_axis);
            // Render
            render.render_widget(widget, area);
        }
    }

    /// ### update
    ///
    /// Update component properties
    /// Properties should first be retrieved through `get_props` which creates a builder from
    /// existing properties and then edited before calling update.
    /// Returns a Msg to the view
    fn update(&mut self, props: Props) -> Msg {
        self.props = props;
        // Reset cursor
        self.states.reset_cursor();
        Msg::None
    }

    /// ### get_props
    ///
    /// Returns a props builder starting from component properties.
    /// This returns a prop builder in order to make easier to create
    /// new properties for the element.
    fn get_props(&self) -> Props {
        self.props.clone()
    }

    /// ### on
    ///
    /// Handle input event and update internal states.
    /// Returns a Msg to the view
    fn on(&mut self, ev: Event) -> Msg {
        if let Event::Key(key) = ev {
            if self.disabled() {
                Msg::OnKey(key)
            } else {
                match key.code {
                    KeyCode::Left => {
                        // Move cursor left; msg None
                        self.states.move_cursor_left();
                        Msg::None
                    }
                    KeyCode::Right => {
                        // Move cursor right; Msg None
                        self.states.move_cursor_right(self.max_dataset_len());
                        Msg::None
                    }
                    KeyCode::End => {
                        // Cursor at last position
                        self.states.cursor_at_end(self.max_dataset_len());
                        Msg::None
                    }
                    KeyCode::Home => {
                        // Cursor at first positon
                        self.states.reset_cursor();
                        Msg::None
                    }
                    _ => Msg::OnKey(key),
                }
            }
        } else {
            Msg::None
        }
    }

    /// ### get_state
    ///
    /// Get current state from component
    /// This component always returns `None`
    fn get_state(&self) -> Payload {
        Payload::None
    }

    // -- events

    /// ### blur
    ///
    /// Blur component; basically remove focus
    fn blur(&mut self) {
        self.states.focus = false;
    }

    /// ### active
    ///
    /// Active component; basically give focus
    /// Works only if not disabled
    fn active(&mut self) {
        if !self.disabled() {
            self.states.focus = true;
        }
    }
}

#[cfg(test)]
mod test {

    use super::*;

    use crossterm::event::KeyEvent;
    use pretty_assertions::assert_eq;
    use tuirealm::tui::{symbols::Marker, widgets::GraphType};

    #[test]
    fn test_components_chart_states() {
        let mut states: OwnStates = OwnStates::default();
        assert_eq!(states.cursor, 0);
        assert_eq!(states.focus, false);
        // Incr
        states.move_cursor_right(2);
        assert_eq!(states.cursor, 1);
        // At end
        states.move_cursor_right(2);
        assert_eq!(states.cursor, 1);
        // Decr
        states.move_cursor_left();
        assert_eq!(states.cursor, 0);
        // At begin
        states.move_cursor_left();
        assert_eq!(states.cursor, 0);
        // Move at end
        states.cursor_at_end(3);
        assert_eq!(states.cursor, 2);
        states.reset_cursor();
        assert_eq!(states.cursor, 0);
    }

    #[test]
    fn test_components_chart() {
        let mut component: Chart = Chart::new(
            ChartPropsBuilder::default()
                .hidden()
                .visible()
                .disabled(false)
                .with_background(Color::Reset)
                .with_foreground(Color::Reset)
                .with_borders(Borders::ALL, BorderType::Double, Color::Yellow)
                .with_title("average temperatures in Udine", Alignment::Center)
                .with_x_bounds((0.0, 11.0))
                .with_x_labels(&[
                    "january",
                    "february",
                    "march",
                    "april",
                    "may",
                    "june",
                    "july",
                    "august",
                    "september",
                    "october",
                    "november",
                    "december",
                ])
                .with_x_style(Style::default().fg(Color::LightBlue))
                .with_x_title("Temperature (°C)")
                .with_y_bounds((-5.0, 35.0))
                .with_y_labels(&["-5", "0", "5", "10", "15", "20", "25", "30", "35"])
                .with_y_style(Style::default().fg(Color::LightYellow))
                .with_y_title("Month")
                .with_data(&[
                    Dataset::default()
                        .name("Minimum")
                        .graph_type(GraphType::Scatter)
                        .marker(Marker::Braille)
                        .style(Style::default().fg(Color::Cyan))
                        .data(vec![
                            (0.0, -1.0),
                            (1.0, 1.0),
                            (2.0, 3.0),
                            (3.0, 7.0),
                            (4.0, 11.0),
                            (5.0, 15.0),
                            (6.0, 17.0),
                            (7.0, 17.0),
                            (8.0, 13.0),
                            (9.0, 9.0),
                            (10.0, 4.0),
                            (11.0, 0.0),
                        ]),
                    Dataset::default()
                        .name("Maximum")
                        .graph_type(GraphType::Line)
                        .marker(Marker::Dot)
                        .style(Style::default().fg(Color::LightRed))
                        .data(vec![
                            (0.0, 7.0),
                            (1.0, 9.0),
                            (2.0, 13.0),
                            (3.0, 17.0),
                            (4.0, 22.0),
                            (5.0, 25.0),
                            (6.0, 28.0),
                            (7.0, 28.0),
                            (8.0, 24.0),
                            (9.0, 19.0),
                            (10.0, 13.0),
                            (11.0, 8.0),
                        ]),
                ])
                .build(),
        );
        assert_eq!(component.props.foreground, Color::Reset);
        assert_eq!(component.props.background, Color::Reset);
        assert_eq!(component.props.visible, true);
        assert_eq!(component.props.borders.borders, Borders::ALL);
        assert_eq!(component.props.borders.variant, BorderType::Double);
        assert_eq!(component.props.borders.color, Color::Yellow);
        assert_eq!(
            component.props.title.as_ref().unwrap().text(),
            "average temperatures in Udine"
        );
        assert_eq!(
            component.props.title.as_ref().unwrap().alignment(),
            Alignment::Center
        );
        assert_eq!(
            *component.props.own.get(PROP_X_BOUNDS).unwrap(),
            PropPayload::Tup2((PropValue::F64(0.0), PropValue::F64(11.0)))
        );
        assert!(component.props.own.get(PROP_X_LABELS).is_some());
        assert!(component.props.own.get(PROP_X_STYLE).is_some());
        assert!(component.props.own.get(PROP_X_TITLE).is_some());
        assert_eq!(
            *component.props.own.get(PROP_Y_BOUNDS).unwrap(),
            PropPayload::Tup2((PropValue::F64(-5.0), PropValue::F64(35.0)))
        );
        assert!(component.props.own.get(PROP_Y_LABELS).is_some());
        assert!(component.props.own.get(PROP_Y_STYLE).is_some());
        assert!(component.props.own.get(PROP_Y_TITLE).is_some());
        assert!(component.props.own.get(PROP_DATA).is_some());
        assert_eq!(
            *component.props.own.get(PROP_DISABLED).unwrap(),
            PropPayload::One(PropValue::Bool(false))
        );
        // focus
        component.active();
        assert_eq!(component.states.focus, true);
        component.blur();
        assert_eq!(component.states.focus, false);
        // Commands
        assert_eq!(component.get_state(), Payload::None);
        // -> Right
        assert_eq!(
            component.on(Event::Key(KeyEvent::from(KeyCode::Right))),
            Msg::None
        );
        assert_eq!(component.states.cursor, 1);
        // <- Left
        assert_eq!(
            component.on(Event::Key(KeyEvent::from(KeyCode::Left))),
            Msg::None
        );
        assert_eq!(component.states.cursor, 0);
        // End
        assert_eq!(
            component.on(Event::Key(KeyEvent::from(KeyCode::End))),
            Msg::None
        );
        assert_eq!(component.states.cursor, 11);
        // Home
        assert_eq!(
            component.on(Event::Key(KeyEvent::from(KeyCode::Home))),
            Msg::None
        );
        assert_eq!(component.states.cursor, 0);
        // other keys
        assert_eq!(
            component.on(Event::Key(KeyEvent::from(KeyCode::Char('a')))),
            Msg::OnKey(KeyEvent::from(KeyCode::Char('a'))),
        );
        // component funcs
        assert_eq!(component.data_len(), 2);
        assert_eq!(component.max_dataset_len(), 12);
        assert_eq!(component.disabled(), false);
        assert_eq!(component.data(2, 4).len(), 2);
        // Update and test empty data
        component.states.cursor_at_end(12);
        assert_eq!(
            component.update(
                ChartPropsBuilder::from(component.get_props())
                    .with_data(&[])
                    .build()
            ),
            Msg::None
        );
        assert_eq!(component.max_dataset_len(), 0);
        // Cursor is reset
        assert_eq!(component.states.cursor, 0);
    }

    #[test]
    fn test_components_chart_disabled() {
        let mut component: Chart = Chart::new(
            ChartPropsBuilder::default()
                .hidden()
                .visible()
                .disabled(true)
                .with_background(Color::Reset)
                .with_foreground(Color::Reset)
                .with_title("average temperatures in Udine", Alignment::Center)
                .with_borders(Borders::ALL, BorderType::Double, Color::Yellow)
                .with_x_bounds((0.0, 11.0))
                .with_x_labels(&[
                    "january",
                    "february",
                    "march",
                    "april",
                    "may",
                    "june",
                    "july",
                    "august",
                    "september",
                    "october",
                    "november",
                    "december",
                ])
                .with_x_style(Style::default().fg(Color::LightBlue))
                .with_x_title("Temperature (°C)")
                .with_y_bounds((-5.0, 35.0))
                .with_y_labels(&["-5", "0", "5", "10", "15", "20", "25", "30", "35"])
                .with_y_style(Style::default().fg(Color::LightYellow))
                .with_y_title("Month")
                .with_data(&[
                    Dataset::default()
                        .name("Minimum")
                        .graph_type(GraphType::Scatter)
                        .marker(Marker::Braille)
                        .style(Style::default().fg(Color::Cyan))
                        .data(vec![
                            (0.0, -1.0),
                            (1.0, 1.0),
                            (2.0, 3.0),
                            (3.0, 7.0),
                            (4.0, 11.0),
                            (5.0, 15.0),
                            (6.0, 17.0),
                            (7.0, 17.0),
                            (8.0, 13.0),
                            (9.0, 9.0),
                            (10.0, 4.0),
                            (11.0, 0.0),
                        ]),
                    Dataset::default()
                        .name("Maximum")
                        .graph_type(GraphType::Line)
                        .marker(Marker::Dot)
                        .style(Style::default().fg(Color::LightRed))
                        .data(vec![
                            (0.0, 7.0),
                            (1.0, 9.0),
                            (2.0, 13.0),
                            (3.0, 17.0),
                            (4.0, 22.0),
                            (5.0, 25.0),
                            (6.0, 28.0),
                            (7.0, 28.0),
                            (8.0, 24.0),
                            (9.0, 19.0),
                            (10.0, 13.0),
                            (11.0, 8.0),
                        ]),
                ])
                .build(),
        );
        assert_eq!(component.props.foreground, Color::Reset);
        assert_eq!(component.props.background, Color::Reset);
        assert_eq!(component.props.visible, true);
        assert_eq!(component.props.borders.borders, Borders::ALL);
        assert_eq!(component.props.borders.variant, BorderType::Double);
        assert_eq!(component.props.borders.color, Color::Yellow);
        assert_eq!(
            *component.props.own.get(PROP_X_BOUNDS).unwrap(),
            PropPayload::Tup2((PropValue::F64(0.0), PropValue::F64(11.0)))
        );
        assert!(component.props.own.get(PROP_X_LABELS).is_some());
        assert!(component.props.own.get(PROP_X_STYLE).is_some());
        assert!(component.props.own.get(PROP_X_TITLE).is_some());
        assert_eq!(
            *component.props.own.get(PROP_Y_BOUNDS).unwrap(),
            PropPayload::Tup2((PropValue::F64(-5.0), PropValue::F64(35.0)))
        );
        assert!(component.props.own.get(PROP_Y_LABELS).is_some());
        assert!(component.props.own.get(PROP_Y_STYLE).is_some());
        assert!(component.props.own.get(PROP_Y_TITLE).is_some());
        assert!(component.props.own.get(PROP_DATA).is_some());
        assert_eq!(
            *component.props.own.get(PROP_DISABLED).unwrap(),
            PropPayload::One(PropValue::Bool(true))
        );
        // focus
        component.active();
        assert_eq!(component.states.focus, false);
        component.blur();
        assert_eq!(component.states.focus, false);
        // Commands
        assert_eq!(component.get_state(), Payload::None);
        // -> Right
        assert_eq!(
            component.on(Event::Key(KeyEvent::from(KeyCode::Right))),
            Msg::OnKey(KeyEvent::from(KeyCode::Right))
        );
        assert_eq!(component.states.cursor, 0);
        // <- Left
        assert_eq!(
            component.on(Event::Key(KeyEvent::from(KeyCode::Left))),
            Msg::OnKey(KeyEvent::from(KeyCode::Left))
        );
        assert_eq!(component.states.cursor, 0);
        // End
        assert_eq!(
            component.on(Event::Key(KeyEvent::from(KeyCode::End))),
            Msg::OnKey(KeyEvent::from(KeyCode::End))
        );
        assert_eq!(component.states.cursor, 0);
        // Home
        assert_eq!(
            component.on(Event::Key(KeyEvent::from(KeyCode::Home))),
            Msg::OnKey(KeyEvent::from(KeyCode::Home))
        );
        assert_eq!(component.states.cursor, 0);
        // other keys
        assert_eq!(
            component.on(Event::Key(KeyEvent::from(KeyCode::Char('a')))),
            Msg::OnKey(KeyEvent::from(KeyCode::Char('a'))),
        );
        // component funcs
        assert_eq!(component.data_len(), 2);
        assert_eq!(component.max_dataset_len(), 12);
        assert_eq!(component.disabled(), true);
        assert_eq!(component.data(2, 4).len(), 2);
        // Add a new record
        assert_eq!(
            component.update(
                ChartPropsBuilder::from(component.get_props())
                    .push_record(0, (0.5, 2.0))
                    .push_record(0, (2.5, 5.5))
                    .push_record(1, (10.2, 11.2))
                    .build()
            ),
            Msg::None
        );
        assert_eq!(component.max_dataset_len(), 14);
        // Pop records
        assert_eq!(
            component.update(
                ChartPropsBuilder::from(component.get_props())
                    .pop_record_back(0)
                    .pop_record_back(1)
                    .pop_record_front(1)
                    .build()
            ),
            Msg::None
        );
        assert_eq!(component.max_dataset_len(), 13);
        // Update and test empty data
        component.states.cursor_at_end(12);
        assert_eq!(
            component.update(
                ChartPropsBuilder::from(component.get_props())
                    .with_data(&[])
                    .build()
            ),
            Msg::None
        );
        assert_eq!(component.max_dataset_len(), 0);
        // Cursor is reset
        assert_eq!(component.states.cursor, 0);
    }

    #[test]
    #[should_panic]
    fn test_components_chart_uninitialized() {
        ChartPropsBuilder::default().push_record(2, (0.4, 0.7));
    }

    #[test]
    #[should_panic]
    fn test_components_chart_uninitialized_2() {
        ChartPropsBuilder::default().push_record(2, (0.4, 0.7));
    }
}