use crate::coordinate::{Col, Row};
use crate::model::DefinedName;
#[derive(Debug, Clone, PartialEq, Default)]
pub struct Chart {
pub name: String,
pub anchor: Anchor,
pub title: Option<Title>,
pub auto_title_deleted: bool,
pub plots: Vec<Plot>,
pub axes: Vec<ChartAxis>,
pub legend: Option<Legend>,
pub markup: ChartMarkup,
pub origin: Option<ChartOrigin>,
}
impl Chart {
#[must_use]
pub fn is_unchanged(&self) -> bool {
self.origin
.as_ref()
.is_some_and(|o| self.same_placement(&o.read) && self.same_content(&o.read))
}
pub(crate) fn settle(&mut self) {
if let Some(mut origin) = self.origin.take() {
origin.read = Box::new(self.clone());
self.origin = Some(origin);
}
}
pub(crate) fn same_placement(&self, other: &Self) -> bool {
self.name == other.name && self.anchor == other.anchor
}
pub(crate) fn same_content(&self, other: &Self) -> bool {
let Self {
name: _,
anchor: _,
title,
auto_title_deleted,
plots,
axes,
legend,
markup,
origin: _,
} = self;
*title == other.title
&& *auto_title_deleted == other.auto_title_deleted
&& *plots == other.plots
&& *axes == other.axes
&& *legend == other.legend
&& *markup == other.markup
}
pub(crate) fn formulas_mut(&mut self) -> Vec<&mut String> {
let mut out = Vec::new();
let titles = self
.title
.iter_mut()
.chain(self.axes.iter_mut().filter_map(|a| a.title.as_mut()));
out.extend(
titles
.filter_map(|t| t.text.as_mut())
.filter_map(ChartText::formula_mut),
);
for series in self.plots.iter_mut().flat_map(|p| &mut p.series) {
out.extend(series.name.as_mut().and_then(ChartText::formula_mut));
for data in [
&mut series.categories,
&mut series.values,
&mut series.bubble_sizes,
] {
out.extend(data.as_mut().and_then(DataSource::formula_mut));
}
}
out
}
pub(crate) fn markups_mut(&mut self) -> Vec<&mut String> {
let ChartMarkup {
before_chart,
before_plot_area,
plot_area_layout,
after_axes,
after_legend,
after_chart,
} = &mut self.markup;
let mut out = vec![
before_chart,
before_plot_area,
plot_area_layout,
after_axes,
after_legend,
after_chart,
];
out.extend(self.title.as_mut().map(|t| &mut t.markup));
out.extend(self.legend.as_mut().map(|l| &mut l.markup));
for axis in &mut self.axes {
let AxisMarkup {
gridlines,
ticks,
tail,
} = &mut axis.markup;
out.extend([gridlines, ticks, tail]);
out.extend(axis.title.as_mut().map(|t| &mut t.markup));
}
for plot in &mut self.plots {
out.push(&mut plot.markup);
for series in &mut plot.series {
let SeriesMarkup {
before_data,
after_data,
} = &mut series.markup;
out.extend([before_data, after_data]);
for data in [
&mut series.categories,
&mut series.values,
&mut series.bubble_sizes,
] {
if let Some(DataSource::Levels { formula, markup }) = data {
out.push(markup);
out.extend(formula.as_mut());
}
}
}
}
out
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct ChartOrigin {
pub(crate) drawing: String,
pub(crate) part: String,
pub(crate) read: Box<Chart>,
pub(crate) root_attributes: String,
pub(crate) prefix: String,
pub(crate) grouped: bool,
}
impl ChartOrigin {
#[must_use]
pub fn part(&self) -> &str {
&self.part
}
#[must_use]
pub fn drawing(&self) -> &str {
&self.drawing
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Anchor {
TwoCell {
from: Marker,
to: Marker,
edit_as: Option<EditAs>,
},
OneCell {
from: Marker,
width: i64,
height: i64,
},
Absolute {
x: i64,
y: i64,
width: i64,
height: i64,
},
}
impl Default for Anchor {
fn default() -> Self {
Self::TwoCell {
from: Marker::default(),
to: Marker {
col: Col::new(8).unwrap_or_default(),
row: Row::new(15).unwrap_or_default(),
..Marker::default()
},
edit_as: None,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct Marker {
pub col: Col,
pub col_offset: i64,
pub row: Row,
pub row_offset: i64,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum EditAs {
TwoCell,
OneCell,
Absolute,
}
impl EditAs {
#[must_use]
pub fn parse(value: &str) -> Option<Self> {
match value {
"twoCell" => Some(Self::TwoCell),
"oneCell" => Some(Self::OneCell),
"absolute" => Some(Self::Absolute),
_ => None,
}
}
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
Self::TwoCell => "twoCell",
Self::OneCell => "oneCell",
Self::Absolute => "absolute",
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub enum ChartText {
Reference {
formula: String,
cache: Option<String>,
},
Text {
text: String,
rich: Option<String>,
},
}
impl ChartText {
#[must_use]
pub fn text(text: impl Into<String>) -> Self {
Self::Text {
text: text.into(),
rich: None,
}
}
#[must_use]
pub fn shown(&self) -> Option<&str> {
match self {
Self::Reference { cache, .. } => cache.as_deref(),
Self::Text { text, .. } => Some(text),
}
}
fn formula_mut(&mut self) -> Option<&mut String> {
match self {
Self::Reference { formula, .. } => Some(formula),
Self::Text { .. } => None,
}
}
}
#[derive(Debug, Clone, PartialEq, Default)]
pub struct Title {
pub text: Option<ChartText>,
pub markup: String,
}
#[derive(Debug, Clone, PartialEq)]
pub struct Plot {
pub kind: PlotKind,
pub vary_colors: Option<bool>,
pub series: Vec<Series>,
pub axis_ids: Vec<u32>,
pub markup: String,
}
impl Plot {
#[must_use]
pub const fn new(kind: PlotKind) -> Self {
Self {
kind,
vary_colors: None,
series: Vec::new(),
axis_ids: Vec::new(),
markup: String::new(),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PlotKind {
Bar {
direction: BarDirection,
grouping: Grouping,
three_d: bool,
},
Line {
grouping: Grouping,
three_d: bool,
},
Area {
grouping: Grouping,
three_d: bool,
},
Pie {
three_d: bool,
},
Doughnut,
OfPie {
bar: bool,
},
Scatter(ScatterStyle),
Radar(RadarStyle),
Bubble,
Stock,
Surface {
three_d: bool,
wireframe: bool,
},
}
impl PlotKind {
#[must_use]
pub const fn has_axes(self) -> bool {
!matches!(self, Self::Pie { .. } | Self::Doughnut | Self::OfPie { .. })
}
#[must_use]
pub const fn plots_xy(self) -> bool {
matches!(self, Self::Scatter(_) | Self::Bubble)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum BarDirection {
#[default]
Column,
Bar,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum Grouping {
#[default]
Standard,
Clustered,
Stacked,
PercentStacked,
}
impl Grouping {
#[must_use]
pub fn parse(value: &str) -> Self {
match value {
"clustered" => Self::Clustered,
"stacked" => Self::Stacked,
"percentStacked" => Self::PercentStacked,
_ => Self::Standard,
}
}
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
Self::Standard => "standard",
Self::Clustered => "clustered",
Self::Stacked => "stacked",
Self::PercentStacked => "percentStacked",
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum ScatterStyle {
None,
Line,
LineMarker,
#[default]
Marker,
Smooth,
SmoothMarker,
}
impl ScatterStyle {
#[must_use]
pub fn parse(value: &str) -> Self {
match value {
"none" => Self::None,
"line" => Self::Line,
"lineMarker" => Self::LineMarker,
"smooth" => Self::Smooth,
"smoothMarker" => Self::SmoothMarker,
_ => Self::Marker,
}
}
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
Self::None => "none",
Self::Line => "line",
Self::LineMarker => "lineMarker",
Self::Marker => "marker",
Self::Smooth => "smooth",
Self::SmoothMarker => "smoothMarker",
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum RadarStyle {
#[default]
Standard,
Marker,
Filled,
}
impl RadarStyle {
#[must_use]
pub fn parse(value: &str) -> Self {
match value {
"marker" => Self::Marker,
"filled" => Self::Filled,
_ => Self::Standard,
}
}
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
Self::Standard => "standard",
Self::Marker => "marker",
Self::Filled => "filled",
}
}
}
#[derive(Debug, Clone, PartialEq, Default)]
pub struct Series {
pub index: u32,
pub order: u32,
pub name: Option<ChartText>,
pub categories: Option<DataSource>,
pub values: Option<DataSource>,
pub bubble_sizes: Option<DataSource>,
pub markup: SeriesMarkup,
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct SeriesMarkup {
pub before_data: String,
pub after_data: String,
}
#[derive(Debug, Clone, PartialEq)]
pub enum DataSource {
Numbers {
formula: Option<String>,
format_code: Option<String>,
count: Option<u32>,
points: Vec<(u32, f64)>,
},
Strings {
formula: Option<String>,
count: Option<u32>,
points: Vec<(u32, String)>,
},
Levels {
formula: Option<String>,
markup: String,
},
}
impl DataSource {
#[must_use]
pub fn numbers(formula: impl Into<String>) -> Self {
Self::Numbers {
formula: Some(formula.into()),
format_code: None,
count: None,
points: Vec::new(),
}
}
#[must_use]
pub fn strings(formula: impl Into<String>) -> Self {
Self::Strings {
formula: Some(formula.into()),
count: None,
points: Vec::new(),
}
}
#[must_use]
pub fn formula(&self) -> Option<&str> {
match self {
Self::Numbers { formula, .. }
| Self::Strings { formula, .. }
| Self::Levels { formula, .. } => formula.as_deref(),
}
}
fn formula_mut(&mut self) -> Option<&mut String> {
match self {
Self::Numbers { formula, .. } | Self::Strings { formula, .. } => formula.as_mut(),
Self::Levels { .. } => None,
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct ChartAxis {
pub kind: AxisKind,
pub id: u32,
pub cross_axis: u32,
pub position: AxisPosition,
pub deleted: bool,
pub reversed: bool,
pub min: Option<f64>,
pub max: Option<f64>,
pub log_base: Option<f64>,
pub title: Option<Title>,
pub number_format: Option<(String, bool)>,
pub markup: AxisMarkup,
}
impl ChartAxis {
#[must_use]
pub fn category(id: u32, cross_axis: u32) -> Self {
Self::new(AxisKind::Category, id, cross_axis, AxisPosition::Bottom)
}
#[must_use]
pub fn value(id: u32, cross_axis: u32) -> Self {
Self::new(AxisKind::Value, id, cross_axis, AxisPosition::Left)
}
#[must_use]
pub fn new(kind: AxisKind, id: u32, cross_axis: u32, position: AxisPosition) -> Self {
Self {
kind,
id,
cross_axis,
position,
deleted: false,
reversed: false,
min: None,
max: None,
log_base: None,
title: None,
number_format: None,
markup: AxisMarkup::default(),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct AxisMarkup {
pub gridlines: String,
pub ticks: String,
pub tail: String,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AxisKind {
Category,
Value,
Date,
Series,
}
impl AxisKind {
#[must_use]
pub const fn element(self) -> &'static str {
match self {
Self::Category => "catAx",
Self::Value => "valAx",
Self::Date => "dateAx",
Self::Series => "serAx",
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum AxisPosition {
#[default]
Bottom,
Left,
Right,
Top,
}
impl AxisPosition {
#[must_use]
pub fn parse(value: &str) -> Self {
match value {
"l" => Self::Left,
"r" => Self::Right,
"t" => Self::Top,
_ => Self::Bottom,
}
}
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
Self::Bottom => "b",
Self::Left => "l",
Self::Right => "r",
Self::Top => "t",
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct Legend {
pub position: LegendPosition,
pub markup: String,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum LegendPosition {
Bottom,
Left,
#[default]
Right,
Top,
TopRight,
}
impl LegendPosition {
#[must_use]
pub fn parse(value: &str) -> Self {
match value {
"b" => Self::Bottom,
"l" => Self::Left,
"t" => Self::Top,
"tr" => Self::TopRight,
_ => Self::Right,
}
}
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
Self::Bottom => "b",
Self::Left => "l",
Self::Right => "r",
Self::Top => "t",
Self::TopRight => "tr",
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct ChartMarkup {
pub before_chart: String,
pub before_plot_area: String,
pub plot_area_layout: String,
pub after_axes: String,
pub after_legend: String,
pub after_chart: String,
}
#[derive(Debug, Clone, PartialEq, Default)]
pub struct ChartEx {
pub name: String,
pub anchor: Anchor,
pub part: String,
pub title: Option<ChartText>,
pub series: Vec<ExSeries>,
pub origin: Option<ChartExOrigin>,
}
impl ChartEx {
#[must_use]
pub fn new(layout: SeriesLayout, dimensions: Vec<Dimension>, anchor: Anchor) -> Self {
Self {
anchor,
series: vec![ExSeries {
layout,
name: None,
hidden: false,
dimensions,
}],
..Self::default()
}
}
#[must_use]
pub fn is_unchanged(&self) -> bool {
self.origin
.as_ref()
.is_some_and(|o| self.same_placement(&o.read) && self.same_content(&o.read))
}
pub(crate) fn same_placement(&self, other: &Self) -> bool {
self.name == other.name && self.anchor == other.anchor
}
pub(crate) fn same_content(&self, other: &Self) -> bool {
self.title == other.title && self.series == other.series
}
pub(crate) fn settle(&mut self) {
if let Some(mut origin) = self.origin.take() {
origin.read = Box::new(self.clone());
self.origin = Some(origin);
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct ChartExOrigin {
pub(crate) drawing: String,
pub(crate) id: u32,
pub(crate) grouped: bool,
pub(crate) read: Box<ChartEx>,
}
impl ChartExOrigin {
#[must_use]
pub fn drawing(&self) -> &str {
&self.drawing
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct ExSeries {
pub layout: SeriesLayout,
pub name: Option<ChartText>,
pub hidden: bool,
pub dimensions: Vec<Dimension>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SeriesLayout {
BoxWhisker,
ClusteredColumn,
Funnel,
ParetoLine,
RegionMap,
Sunburst,
Treemap,
Waterfall,
Unknown,
}
impl SeriesLayout {
#[must_use]
pub fn parse(value: &str) -> Self {
match value {
"boxWhisker" => Self::BoxWhisker,
"clusteredColumn" => Self::ClusteredColumn,
"funnel" => Self::Funnel,
"paretoLine" => Self::ParetoLine,
"regionMap" => Self::RegionMap,
"sunburst" => Self::Sunburst,
"treemap" => Self::Treemap,
"waterfall" => Self::Waterfall,
_ => Self::Unknown,
}
}
#[must_use]
pub const fn as_str(self) -> Option<&'static str> {
Some(match self {
Self::BoxWhisker => "boxWhisker",
Self::ClusteredColumn => "clusteredColumn",
Self::Funnel => "funnel",
Self::ParetoLine => "paretoLine",
Self::RegionMap => "regionMap",
Self::Sunburst => "sunburst",
Self::Treemap => "treemap",
Self::Waterfall => "waterfall",
Self::Unknown => return None,
})
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct Dimension {
pub role: DimensionRole,
pub numeric: bool,
pub formula: Option<String>,
pub levels: Vec<Vec<(u32, String)>>,
}
impl Dimension {
#[must_use]
pub fn reference<'a>(&'a self, names: &'a [DefinedName]) -> Option<&'a str> {
let formula = self.formula.as_deref()?;
Some(
names
.iter()
.find(|n| n.sheet.is_none() && n.name == formula)
.map_or(formula, |n| n.formula.as_str()),
)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DimensionRole {
Categories,
Values,
Sizes,
X,
Y,
ColorValues,
ColorStrings,
EntityIds,
}
impl DimensionRole {
#[must_use]
pub fn parse(value: &str) -> Option<Self> {
Some(match value {
"cat" => Self::Categories,
"val" => Self::Values,
"size" => Self::Sizes,
"x" => Self::X,
"y" => Self::Y,
"colorVal" => Self::ColorValues,
"colorStr" => Self::ColorStrings,
"entityId" => Self::EntityIds,
_ => return None,
})
}
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
Self::Categories => "cat",
Self::Values => "val",
Self::Sizes => "size",
Self::X => "x",
Self::Y => "y",
Self::ColorValues => "colorVal",
Self::ColorStrings => "colorStr",
Self::EntityIds => "entityId",
}
}
}