use crate::enums::chart::XlChartType;
use crate::error::PptxResult;
use super::super::xmlwriter::ChartXmlWriter;
#[derive(Debug, Clone)]
pub struct XyChartData {
series: Vec<XySeriesData>,
number_format: String,
}
impl XyChartData {
#[must_use]
pub fn new() -> Self {
Self {
series: Vec::new(),
number_format: "General".to_string(),
}
}
pub fn add_series(&mut self, name: &str) -> &mut XySeriesData {
let index = self.series.len();
self.series.push(XySeriesData {
name: name.to_string(),
data_points: Vec::new(),
number_format: None,
index,
});
let idx = self.series.len() - 1;
&mut self.series[idx]
}
#[must_use]
pub fn series(&self) -> &[XySeriesData] {
&self.series
}
#[must_use]
pub fn number_format(&self) -> &str {
&self.number_format
}
pub fn to_xml(&self, chart_type: XlChartType) -> PptxResult<String> {
ChartXmlWriter::write_xy(self, chart_type)
}
}
impl Default for XyChartData {
fn default() -> Self {
Self::new()
}
}
#[derive(Debug, Clone)]
pub struct XySeriesData {
name: String,
data_points: Vec<XyDataPoint>,
number_format: Option<String>,
index: usize,
}
impl XySeriesData {
pub fn add_data_point(&mut self, x: f64, y: f64) {
self.data_points.push(XyDataPoint { x, y });
}
#[must_use]
pub fn name(&self) -> &str {
&self.name
}
#[must_use]
pub fn data_points(&self) -> &[XyDataPoint] {
&self.data_points
}
#[must_use]
pub fn x_values(&self) -> Vec<f64> {
self.data_points.iter().map(|dp| dp.x).collect()
}
#[must_use]
pub fn y_values(&self) -> Vec<f64> {
self.data_points.iter().map(|dp| dp.y).collect()
}
#[must_use]
pub const fn index(&self) -> usize {
self.index
}
#[must_use]
pub fn number_format(&self) -> Option<&str> {
self.number_format.as_deref()
}
}
#[derive(Debug, Clone, Copy)]
pub struct XyDataPoint {
pub x: f64,
pub y: f64,
}