use std::fmt;
use plotlib::repr::Plot;
use plotlib::style::{LineStyle, PointStyle, LineJoin};
use crate::plotable::Plotable;
pub trait Data {
fn season(&self, n: usize) -> Vec<(f64, f64)> {
let mut v = Vec::new();
let low = self.season_length() * (n - 1);
let mut high = low + self.season_length();
if high > self.as_vec().len() { high = self.as_vec().len()
}
for i in low..high {
v.push(self.as_vec()[i]);
}
v
}
fn season_length(&self) -> usize;
fn as_vec(&self) -> Vec<(f64, f64)>;
fn seasons(&self) -> usize {
(self.as_vec().len() as f64 / self.season_length() as f64).ceil() as usize
}
fn get(&self, i: usize) -> (f64, f64) {
self.as_vec()[i]
}
}
pub struct TablaDemanda {
elements: Vec<f64>,
}
impl TablaDemanda {
pub fn new(elements: Vec<f64>) -> Self {
Self {
elements: elements,
}
}
}
impl Data for TablaDemanda {
fn season_length(&self) -> usize {
12
}
fn as_vec(&self) -> Vec<(f64, f64)> {
let mut v = Vec::new();
for i in 0..self.elements.len() {
v.push((i as f64, self.elements[i] as f64));
}
v
}
}
impl Plotable for TablaDemanda {
fn plot(&self) -> Plot {
Plot::new(self.as_vec())
.point_style(PointStyle::new()
.colour("#000000")
).line_style(LineStyle::new().linejoin(LineJoin::Round))
}
}
impl fmt::Display for TablaDemanda {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let mut s = String::new();
for i in 1..self.seasons() + 1 {
s += format!("season: {}\n", i).as_str();
for e in self.season(i).iter() {
s += format!("{} -> {}\n", e.0, e.1).as_str();
}
}
write!(f, "{}", s)
}
}