pub(crate) mod category;
mod category_tests;
pub(crate) mod date;
mod date_tests;
pub(crate) mod value;
mod value_ext;
mod value_tests;
pub use category::CategoryAxis;
pub use date::DateAxis;
pub use value::ValueAxis;
use crate::text::font::Font;
use crate::text::TextFrame;
use super::chart::ChartFormat;
#[derive(Debug, Clone)]
pub struct AxisTitle {
pub text_frame: Option<TextFrame>,
has_text_frame: bool,
pub format: Option<ChartFormat>,
}
impl AxisTitle {
#[must_use]
pub const fn new() -> Self {
Self {
text_frame: None,
has_text_frame: false,
format: None,
}
}
#[must_use]
pub fn from_text(text: &str) -> Self {
let mut tf = TextFrame::new();
tf.set_text(text);
Self {
text_frame: Some(tf),
has_text_frame: true,
format: None,
}
}
#[must_use]
pub const fn has_text_frame(&self) -> bool {
self.has_text_frame
}
#[must_use]
pub const fn text_frame(&self) -> Option<&TextFrame> {
self.text_frame.as_ref()
}
pub fn text_frame_mut(&mut self) -> &mut TextFrame {
self.has_text_frame = true;
self.text_frame.get_or_insert_with(TextFrame::new)
}
pub fn set_text_frame(&mut self, text_frame: TextFrame) {
self.has_text_frame = true;
self.text_frame = Some(text_frame);
}
#[must_use]
pub fn text(&self) -> Option<String> {
self.text_frame.as_ref().map(TextFrame::text)
}
}
impl Default for AxisTitle {
fn default() -> Self {
Self::new()
}
}
#[derive(Debug, Clone)]
pub struct TickLabels {
font: Option<Font>,
pub number_format: Option<String>,
pub number_format_is_linked: bool,
pub offset: Option<u32>,
}
impl TickLabels {
#[must_use]
pub const fn new() -> Self {
Self {
font: None,
number_format: None,
number_format_is_linked: true,
offset: None,
}
}
#[must_use]
pub const fn font(&self) -> Option<&Font> {
self.font.as_ref()
}
pub fn font_mut(&mut self) -> &mut Font {
self.font.get_or_insert_with(Font::new)
}
}
impl Default for TickLabels {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_axis_title_new() {
let at = AxisTitle::new();
assert!(!at.has_text_frame());
assert!(at.text_frame().is_none());
assert!(at.text().is_none());
assert!(at.format.is_none());
}
#[test]
fn test_axis_title_from_text() {
let at = AxisTitle::from_text("Revenue");
assert!(at.has_text_frame());
assert_eq!(at.text(), Some("Revenue".to_string()));
}
#[test]
fn test_axis_title_text_frame_mut() {
let mut at = AxisTitle::new();
assert!(!at.has_text_frame());
at.text_frame_mut().set_text("New Title");
assert!(at.has_text_frame());
assert_eq!(at.text(), Some("New Title".to_string()));
}
#[test]
fn test_axis_title_set_text_frame() {
let mut at = AxisTitle::new();
let mut tf = TextFrame::new();
tf.set_text("Custom Title");
at.set_text_frame(tf);
assert!(at.has_text_frame());
assert_eq!(at.text(), Some("Custom Title".to_string()));
}
#[test]
fn test_tick_labels_new() {
let tl = TickLabels::new();
assert!(tl.font().is_none());
assert!(tl.number_format.is_none());
assert!(tl.number_format_is_linked);
assert!(tl.offset.is_none());
}
#[test]
fn test_tick_labels_font_mut() {
let mut tl = TickLabels::new();
tl.font_mut().bold = Some(true);
assert!(tl.font().is_some());
assert_eq!(tl.font().unwrap().bold, Some(true));
}
#[test]
fn test_tick_labels_properties() {
let mut tl = TickLabels::new();
tl.number_format = Some("#,##0".to_string());
tl.number_format_is_linked = false;
tl.offset = Some(100);
assert_eq!(tl.number_format.as_deref(), Some("#,##0"));
assert!(!tl.number_format_is_linked);
assert_eq!(tl.offset, Some(100));
}
}