use crate::core::{Color, Point, Rect};
#[derive(Debug, Clone)]
pub struct DataPoint {
pub x: f64,
pub y: f64,
pub label: Option<String>,
}
#[derive(Debug, Clone)]
pub struct ChartSeries {
pub name: String,
pub data: Vec<DataPoint>,
pub color: Color,
pub visible: bool,
}
pub enum ChartType {
Line,
Bar,
Pie,
Scatter,
Area,
}
impl ChartType {
pub fn create_chart(&self) -> Box<dyn Chart> {
match self {
ChartType::Line => Box::new(crate::chart::charts::LineChart::new()),
ChartType::Bar => Box::new(crate::chart::charts::BarChart::new()),
ChartType::Pie => Box::new(crate::chart::charts::PieChart::new()),
ChartType::Scatter => Box::new(crate::chart::charts::ScatterChart::new()),
ChartType::Area => Box::new(crate::chart::charts::AreaChart::new()),
}
}
}
impl std::fmt::Display for ChartType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ChartType::Line => write!(f, "Line"),
ChartType::Bar => write!(f, "Bar"),
ChartType::Pie => write!(f, "Pie"),
ChartType::Scatter => write!(f, "Scatter"),
ChartType::Area => write!(f, "Area"),
}
}
}
pub trait Chart {
fn add_series(&mut self, series: ChartSeries);
fn remove_series(&mut self, name: &str);
fn clear_series(&mut self);
fn set_title(&mut self, title: String);
fn set_x_axis_label(&mut self, label: String);
fn set_y_axis_label(&mut self, label: String);
fn draw(&self, rect: Rect, context: &mut dyn ChartContext);
}
pub trait ChartContext {
fn draw_line(&mut self, from: Point, to: Point, width: f32, color: Color);
fn draw_rect(&mut self, rect: Rect, color: Color);
fn draw_text(&mut self, text: &str, pos: Point, font_size: f32, color: Color);
fn draw_circle(&mut self, center: Point, radius: f32, color: Color);
fn draw_polygon(&mut self, points: &[Point], color: Color);
fn draw_path_segment(&mut self, start: Point, end: Point, width: f32, color: Color);
fn draw_arc(
&mut self,
center: Point,
radius: f32,
start_angle: f64,
end_angle: f64,
color: Color,
);
fn draw_path(&mut self, points: &[Point], width: f32, color: Color);
fn draw_ellipse(&mut self, center: Point, radius_x: f32, radius_y: f32, color: Color);
fn set_fill_color(&mut self, color: Color);
fn set_stroke_color(&mut self, color: Color);
}