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
pub mod season;
pub mod moving_median;
pub mod merger;
pub mod grouper;

pub use season::Season;
pub use moving_median::MovingMedian;
pub use merger::Merger;
pub use grouper::Grouper;

use crate::plotable::Plotable;

use std::fmt;

use plotlib::view::{View, CategoricalView, ContinuousView};
use plotlib::repr::{Plot, CategoricalRepresentation};
use plotlib::style::{PointStyle, LineStyle, PointMarker};

/// Holds the data of the series
/// its implied that every unit of data represents a unit of time
#[derive(Clone)]
pub struct TimeSeries {
    data: Vec<(f64, f64)>,
    dom_ran: Option<(String, String)>,
    style: Style,
}

impl TimeSeries {

    /// Creates a new Timeseries with the data given for the y axis.
    pub fn new(data: Vec<f64>) -> Self {
        let mut v = Vec::new();
        for i in 0..data.len() {
            v.push(((i + 1) as f64, data[i]));
        }
        Self {
            data: v,
            dom_ran: None,
            style: Default::default(),
        }
    }

    pub fn from_pairs_vec(pairs: Vec<(f64, f64)>) -> Self {
        /*let mut v = pairs.iter().map(|pair| pair.1).collect();
        let mut d = pairs.iter().map(|pair| pair.0).collect();
        let mut series = TimeSeries::new(v);*/
        Self {
            data: pairs,
            dom_ran: None,
            style: Default::default(),
        }
    }

    /// Returns all (x,y) values
    pub fn get_data(&self) ->Vec<(f64, f64)> {
        /*let mut vec = Vec::new();
        for i in 0..self.data.len() {
            vec.push(
                ((i + 1 as usize) as f64, self.data[i] as f64)
            );
        }*/

        self.data.clone()
    }

    /// returns the y value of this time series
    /// at the given x value
    pub fn get_range_at(&self, u: usize) -> f64 {
        for (x, y) in self.data.iter() {
            if *x as usize == u {
                return *y;
            }
        }
        panic!(format!("domain value {} doesn't exist in the time series", u));
    }

    /// returns the y value at the nth position
    pub fn get_range_at_ord(&self, n: usize) -> f64 {
        self.get_data()[n].1
    }

    /// adds the given data at the end of this timeseries
    pub fn push(&mut self, data: f64) {
        let dom_last = self.data[self.data.len() - 1].0; //gets last domain number
        self.data.push(((dom_last + 1 as f64) , data)); //last domain + 1
    }

    pub fn with_push(mut self, data: f64) -> Self {
        self.push(data);
        self
    }

    /// Returns all y values
    pub fn get_range(&self) -> Vec<f64> {
        self.data.iter().map(|(x, y)| *y).collect()
    }

    pub fn style(&self) -> Style {
        self.style.clone()
    }

    pub fn style_mut(&mut self) -> &mut Style {
        &mut self.style
    }

    pub fn len(&self) -> usize {
        self.data.len()
    }
}

#[derive(Clone)]
pub struct Style{
    pub point: PointStyle,
    pub line: LineStyle,
}

impl Style {
    pub fn from_color(color: &str) -> Self {
        let mut style: Style = Default::default();
        style.point = style.point.colour(color);
        style.line = style.line.colour(color);
        style
    }
}

impl Default for Style {
    fn default() -> Self {
        Self {
            point: PointStyle::new().colour("#000000").marker(PointMarker::Circle),
            line: LineStyle::new().colour("#000000")
        }
    }
}


impl Plotable for TimeSeries {
    fn plot(&self) -> Box<dyn View> {
        let plot = self.as_plot();
        let mut view = ContinuousView::new()
        .add(plot);
        Box::new(view)
    }

    fn as_plot(&self) -> Plot {
        let mut plot = Plot::new(self.get_data());
        plot.point_style(self.style().point).line_style(self.style().line)
    }
}


/// Un Mes del año.
#[derive(Copy, Clone, Debug)]
pub enum Mes {
    Enero,
    Febrero,
    Marzo,
    Abril,
    Mayo,
    Junio,
    Julio,
    Agosto,
    Septiembre,
    Octubre,
    Noviembre,
    Diciembre

}

impl fmt::Display for Mes {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{:?}", self)
    }
}


impl From<u32> for Mes {
    fn from(v: u32) -> Self {
        
        match v {
            0 => Self::Enero,
            1 => Self::Febrero,
            2 => Self::Marzo,
            3 => Self::Abril,
            4 => Self::Mayo,
            5 => Self::Junio,
            6 => Self::Julio,
            7 => Self::Agosto,
            8 => Self::Septiembre,
            9 => Self::Octubre,
            10 => Self::Noviembre,
            11 => Self::Diciembre,
            _ => panic!(format!("Número de mes inválido: {}", v))
        }
    }
}

impl From<&str> for Mes {
    fn from(v: &str) -> Self {
        let v = v.to_lowercase();
        match v.as_str() {
           "enero" => Self::Enero,
           "febrero" => Self::Febrero,
           "marzo" => Self::Marzo,
           "abril" => Self::Abril,
           "mayo" => Self::Mayo,
           "junio" => Self::Junio,
           "julio" => Self::Julio,
           "agosto" => Self::Agosto,
           "septiembre" => Self::Septiembre,
           "octubre" => Self::Octubre,
           "noviembre" => Self::Noviembre,
           "diciembre" => Self::Diciembre,
           _ => panic!(format!("Mes inválido: {}", v))
        }
    }
}