use ratatui::style::Color;
use super::DataSeries;
pub const DEFAULT_PALETTE: &[Color] = &[
Color::Rgb(31, 119, 180), Color::Rgb(255, 127, 14), Color::Rgb(44, 160, 44), Color::Rgb(214, 39, 40), Color::Rgb(148, 103, 189), Color::Rgb(140, 86, 75), Color::Rgb(227, 119, 194), Color::Rgb(127, 127, 127), Color::Rgb(188, 189, 34), Color::Rgb(23, 190, 207), Color::Rgb(174, 199, 232), Color::Rgb(255, 187, 120), Color::Rgb(152, 223, 138), Color::Rgb(255, 152, 150), Color::Rgb(197, 176, 213), Color::Rgb(196, 156, 148), Color::Rgb(247, 182, 210), Color::Rgb(199, 199, 199), Color::Rgb(219, 219, 141), Color::Rgb(158, 218, 229), ];
pub fn chart_palette_color(index: usize) -> Color {
DEFAULT_PALETTE[index % DEFAULT_PALETTE.len()]
}
impl DataSeries {
pub fn new(label: impl Into<String>, values: Vec<f64>) -> Self {
Self {
label: label.into(),
values,
color: DEFAULT_PALETTE[0],
x_values: None,
upper_bound: None,
lower_bound: None,
}
}
pub fn xy(label: impl Into<String>, x: Vec<f64>, y: Vec<f64>) -> Self {
Self {
label: label.into(),
values: y,
color: DEFAULT_PALETTE[0],
x_values: Some(x),
upper_bound: None,
lower_bound: None,
}
}
pub fn with_color(mut self, color: Color) -> Self {
self.color = color;
self
}
pub fn with_x_values(mut self, x: Vec<f64>) -> Self {
self.x_values = Some(x);
self
}
pub fn with_upper_bound(mut self, upper: Vec<f64>) -> Self {
self.upper_bound = Some(upper);
self
}
pub fn with_lower_bound(mut self, lower: Vec<f64>) -> Self {
self.lower_bound = Some(lower);
self
}
pub fn with_bounds(mut self, lower: Vec<f64>, upper: Vec<f64>) -> Self {
self.lower_bound = Some(lower);
self.upper_bound = Some(upper);
self
}
pub fn label(&self) -> &str {
&self.label
}
pub fn values(&self) -> &[f64] {
&self.values
}
pub fn color(&self) -> Color {
self.color
}
pub fn x_values(&self) -> Option<&[f64]> {
self.x_values.as_deref()
}
pub fn upper_bound(&self) -> Option<&[f64]> {
self.upper_bound.as_deref()
}
pub fn lower_bound(&self) -> Option<&[f64]> {
self.lower_bound.as_deref()
}
pub fn push(&mut self, value: f64) {
self.values.push(value);
}
pub fn push_bounded(&mut self, value: f64, max_len: usize) {
self.values.push(value);
while self.values.len() > max_len {
self.values.remove(0);
}
}
pub fn min(&self) -> f64 {
self.values.iter().copied().reduce(f64::min).unwrap_or(0.0)
}
pub fn max(&self) -> f64 {
self.values.iter().copied().reduce(f64::max).unwrap_or(0.0)
}
pub fn last(&self) -> Option<f64> {
self.values.last().copied()
}
pub fn len(&self) -> usize {
self.values.len()
}
pub fn is_empty(&self) -> bool {
self.values.is_empty()
}
pub fn clear(&mut self) {
self.values.clear();
}
pub fn set_label(&mut self, label: impl Into<String>) {
self.label = label.into();
}
pub fn set_color(&mut self, color: Color) {
self.color = color;
}
pub fn set_x_values(&mut self, x: Option<Vec<f64>>) {
self.x_values = x;
}
pub fn set_upper_bound(&mut self, upper: Option<Vec<f64>>) {
self.upper_bound = upper;
}
pub fn set_lower_bound(&mut self, lower: Option<Vec<f64>>) {
self.lower_bound = lower;
}
}