use std::collections::HashMap;
#[derive(Clone, Debug, PartialEq)]
pub enum ChartType {
Bar,
Column,
Line,
Pie,
Doughnut,
Area,
Scatter,
Radar,
Stock,
Surface,
Bubble,
}
impl ChartType {
pub fn xml_type(&self) -> &'static str {
match self {
ChartType::Bar => "barChart",
ChartType::Column => "barChart",
ChartType::Line => "lineChart",
ChartType::Pie => "pieChart",
ChartType::Doughnut => "doughnutChart",
ChartType::Area => "areaChart",
ChartType::Scatter => "scatterChart",
ChartType::Radar => "radarChart",
ChartType::Stock => "stockChart",
ChartType::Surface => "surfaceChart",
ChartType::Bubble => "bubbleChart",
}
}
}
#[derive(Clone, Debug, Default, PartialEq)]
pub enum BarGrouping {
#[default]
Clustered,
Stacked,
PercentStacked,
}
#[derive(Clone, Debug, Default, PartialEq)]
pub enum BarDirection {
Bar,
#[default]
Col,
}
#[derive(Clone, Debug)]
pub struct ChartSeries {
pub name: Option<String>,
pub categories: Option<String>,
pub values: String,
pub fill_color: Option<String>,
pub line_color: Option<String>,
pub marker_style: Option<String>,
}
impl ChartSeries {
pub fn new<S: Into<String>>(values: S) -> Self {
ChartSeries {
name: None,
categories: None,
values: values.into(),
fill_color: None,
line_color: None,
marker_style: None,
}
}
pub fn with_name<S: Into<String>>(mut self, name: S) -> Self {
self.name = Some(name.into());
self
}
pub fn with_categories<S: Into<String>>(mut self, categories: S) -> Self {
self.categories = Some(categories.into());
self
}
pub fn with_fill_color<S: Into<String>>(mut self, color: S) -> Self {
self.fill_color = Some(color.into());
self
}
}
#[derive(Clone, Debug, Default)]
pub struct ChartAxis {
pub title: Option<String>,
pub min_val: Option<f64>,
pub max_val: Option<f64>,
pub major_unit: Option<f64>,
pub minor_unit: Option<f64>,
pub number_format: Option<String>,
pub gridlines: bool,
pub position: Option<String>,
}
impl ChartAxis {
pub fn new() -> Self {
Self::default()
}
pub fn with_title<S: Into<String>>(mut self, title: S) -> Self {
self.title = Some(title.into());
self
}
pub fn with_range(mut self, min: f64, max: f64) -> Self {
self.min_val = Some(min);
self.max_val = Some(max);
self
}
}
#[derive(Clone, Debug, Default)]
pub struct ChartLegend {
pub position: String,
pub visible: bool,
}
impl ChartLegend {
pub fn new() -> Self {
ChartLegend {
position: "r".to_string(),
visible: true,
}
}
pub fn with_position<S: Into<String>>(mut self, position: S) -> Self {
self.position = position.into();
self
}
pub fn with_visible(mut self, visible: bool) -> Self {
self.visible = visible;
self
}
}
#[derive(Clone, Debug, Default)]
pub struct ChartTitle {
pub text: Option<String>,
pub font_size: Option<f64>,
pub bold: bool,
}
impl ChartTitle {
pub fn new<S: Into<String>>(text: S) -> Self {
ChartTitle {
text: Some(text.into()),
font_size: None,
bold: false,
}
}
}
#[derive(Clone, Debug)]
pub struct ChartAnchor {
pub from_cell: String,
pub from_col_offset: u32,
pub from_row_offset: u32,
pub to_cell: Option<String>,
pub to_col_offset: u32,
pub to_row_offset: u32,
}
impl ChartAnchor {
pub fn at<S: Into<String>>(cell: S) -> Self {
ChartAnchor {
from_cell: cell.into(),
from_col_offset: 0,
from_row_offset: 0,
to_cell: None,
to_col_offset: 0,
to_row_offset: 0,
}
}
pub fn with_size<S: Into<String>>(mut self, to_cell: S) -> Self {
self.to_cell = Some(to_cell.into());
self
}
}
#[derive(Clone, Debug)]
pub struct Chart {
pub chart_type: ChartType,
pub title: Option<ChartTitle>,
pub series: Vec<ChartSeries>,
pub x_axis: Option<ChartAxis>,
pub y_axis: Option<ChartAxis>,
pub legend: Option<ChartLegend>,
pub anchor: Option<ChartAnchor>,
pub width: u32,
pub height: u32,
pub bar_grouping: BarGrouping,
pub bar_direction: BarDirection,
pub properties: HashMap<String, String>,
}
impl Chart {
pub fn new(chart_type: ChartType) -> Self {
Chart {
chart_type,
title: None,
series: Vec::new(),
x_axis: None,
y_axis: None,
legend: Some(ChartLegend::new()),
anchor: None,
width: 4572000, height: 2743200, bar_grouping: BarGrouping::default(),
bar_direction: BarDirection::default(),
properties: HashMap::new(),
}
}
pub fn bar() -> Self {
let mut chart = Self::new(ChartType::Bar);
chart.bar_direction = BarDirection::Bar;
chart
}
pub fn column() -> Self {
let mut chart = Self::new(ChartType::Column);
chart.bar_direction = BarDirection::Col;
chart
}
pub fn line() -> Self {
Self::new(ChartType::Line)
}
pub fn pie() -> Self {
Self::new(ChartType::Pie)
}
pub fn scatter() -> Self {
Self::new(ChartType::Scatter)
}
pub fn area() -> Self {
Self::new(ChartType::Area)
}
pub fn with_title<S: Into<String>>(mut self, title: S) -> Self {
self.title = Some(ChartTitle::new(title));
self
}
pub fn add_series(&mut self, series: ChartSeries) {
self.series.push(series);
}
pub fn with_x_axis(mut self, axis: ChartAxis) -> Self {
self.x_axis = Some(axis);
self
}
pub fn with_y_axis(mut self, axis: ChartAxis) -> Self {
self.y_axis = Some(axis);
self
}
pub fn with_legend(mut self, legend: ChartLegend) -> Self {
self.legend = Some(legend);
self
}
pub fn with_anchor(mut self, anchor: ChartAnchor) -> Self {
self.anchor = Some(anchor);
self
}
pub fn with_size_inches(mut self, width: f64, height: f64) -> Self {
self.width = (width * 914400.0) as u32;
self.height = (height * 914400.0) as u32;
self
}
pub fn with_grouping(mut self, grouping: BarGrouping) -> Self {
self.bar_grouping = grouping;
self
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_create_bar_chart() {
let chart = Chart::bar().with_title("Sales Data");
assert_eq!(chart.chart_type, ChartType::Bar);
assert!(chart.title.is_some());
}
#[test]
fn test_create_line_chart() {
let chart = Chart::line().with_title("Trends");
assert_eq!(chart.chart_type, ChartType::Line);
}
#[test]
fn test_add_series() {
let mut chart = Chart::bar();
chart.add_series(
ChartSeries::new("Sheet1!$B$2:$B$10")
.with_name("Revenue")
.with_categories("Sheet1!$A$2:$A$10"),
);
assert_eq!(chart.series.len(), 1);
assert_eq!(chart.series[0].name, Some("Revenue".to_string()));
}
#[test]
fn test_chart_axes() {
let chart = Chart::bar()
.with_x_axis(ChartAxis::new().with_title("Categories"))
.with_y_axis(ChartAxis::new().with_title("Values").with_range(0.0, 100.0));
assert!(chart.x_axis.is_some());
assert!(chart.y_axis.is_some());
assert_eq!(chart.y_axis.as_ref().unwrap().min_val, Some(0.0));
}
#[test]
fn test_chart_size() {
let chart = Chart::pie().with_size_inches(6.0, 4.0);
assert_eq!(chart.width, 5486400);
}
#[test]
fn test_chart_type_xml() {
assert_eq!(ChartType::Bar.xml_type(), "barChart");
assert_eq!(ChartType::Line.xml_type(), "lineChart");
assert_eq!(ChartType::Pie.xml_type(), "pieChart");
}
}
#[cfg(test)]
mod coverage_tests {
use super::*;
#[test]
fn chart_type_xml_names() {
assert_eq!(ChartType::Column.xml_type(), "barChart");
assert_eq!(ChartType::Area.xml_type(), "areaChart");
assert_eq!(ChartType::Doughnut.xml_type(), "doughnutChart");
assert_eq!(ChartType::Scatter.xml_type(), "scatterChart");
assert_eq!(ChartType::Radar.xml_type(), "radarChart");
assert_eq!(ChartType::Bubble.xml_type(), "bubbleChart");
}
#[test]
fn constructors_and_builders() {
assert_eq!(Chart::area().chart_type, ChartType::Area);
assert_eq!(Chart::scatter().chart_type, ChartType::Scatter);
assert_eq!(Chart::pie().chart_type, ChartType::Pie);
let chart = Chart::column()
.with_x_axis(ChartAxis::new().with_title("Cats"))
.with_y_axis(ChartAxis::new().with_title("Vals").with_range(0.0, 50.0))
.with_grouping(BarGrouping::Stacked)
.with_size_inches(6.0, 4.0)
.with_anchor(ChartAnchor::at("B2").with_size("F20"));
assert!(chart.x_axis.is_some() && chart.y_axis.is_some());
assert_eq!(chart.bar_grouping, BarGrouping::Stacked);
assert_eq!(chart.width, 5486400);
let a = chart.anchor.unwrap();
assert_eq!(a.from_cell, "B2");
assert_eq!(a.to_cell.as_deref(), Some("F20"));
}
#[test]
fn axis_range_sets_min_max() {
let ax = ChartAxis::new().with_range(1.0, 9.0);
assert_eq!(ax.min_val, Some(1.0));
assert_eq!(ax.max_val, Some(9.0));
}
#[test]
fn series_with_fill_color() {
let s = ChartSeries::new("S!$B$1:$B$3")
.with_name("Rev")
.with_categories("S!$A$1:$A$3")
.with_fill_color("FF0000");
assert_eq!(s.fill_color.as_deref(), Some("FF0000"));
assert_eq!(s.name.as_deref(), Some("Rev"));
}
#[test]
fn legend_builder() {
let l = ChartLegend::new().with_position("t").with_visible(false);
assert_eq!(l.position, "t");
assert!(!l.visible);
}
}