use crate::primitives::Color;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ArrowStyle {
None,
Simple,
Fancy,
}
use crate::primitives::{HAlign, VAlign};
#[derive(Debug, Clone)]
pub struct TextAnnotation {
pub text: String,
pub x: f64,
pub y: f64,
pub fontsize: Option<f64>,
pub color: Option<Color>,
pub ha: HAlign,
pub va: VAlign,
pub rotation: f64,
}
impl TextAnnotation {
pub fn fontsize(&mut self, size: f64) -> &mut Self {
self.fontsize = Some(size);
self
}
pub fn color(&mut self, color: Color) -> &mut Self {
self.color = Some(color);
self
}
pub fn ha(&mut self, ha: HAlign) -> &mut Self {
self.ha = ha;
self
}
pub fn va(&mut self, va: VAlign) -> &mut Self {
self.va = va;
self
}
pub fn rotation(&mut self, degrees: f64) -> &mut Self {
self.rotation = degrees;
self
}
}
#[derive(Debug, Clone)]
pub struct Annotation {
pub text: String,
pub xy: (f64, f64),
pub xytext: (f64, f64),
pub fontsize: Option<f64>,
pub color: Option<Color>,
pub ha: HAlign,
pub va: VAlign,
pub arrowstyle: ArrowStyle,
pub arrow_color: Option<Color>,
}
impl Annotation {
pub fn fontsize(&mut self, size: f64) -> &mut Self {
self.fontsize = Some(size);
self
}
pub fn color(&mut self, color: Color) -> &mut Self {
self.color = Some(color);
self
}
pub fn ha(&mut self, ha: HAlign) -> &mut Self {
self.ha = ha;
self
}
pub fn va(&mut self, va: VAlign) -> &mut Self {
self.va = va;
self
}
pub fn arrowstyle(&mut self, style: ArrowStyle) -> &mut Self {
self.arrowstyle = style;
self
}
pub fn arrow_color(&mut self, color: Color) -> &mut Self {
self.arrow_color = Some(color);
self
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn text_annotation_defaults() {
let mut t = TextAnnotation {
text: "hello".to_string(),
x: 1.0,
y: 2.0,
fontsize: None,
color: None,
ha: HAlign::Left,
va: VAlign::Baseline,
rotation: 0.0,
};
assert_eq!(t.text, "hello");
assert_eq!(t.x, 1.0);
assert_eq!(t.y, 2.0);
assert!(t.fontsize.is_none());
assert!(t.color.is_none());
assert_eq!(t.ha, HAlign::Left);
assert_eq!(t.va, VAlign::Baseline);
assert!((t.rotation - 0.0).abs() < f64::EPSILON);
t.fontsize(14.0).color(Color::TAB_RED).ha(HAlign::Center).va(VAlign::Top).rotation(45.0);
assert_eq!(t.fontsize, Some(14.0));
assert_eq!(t.color, Some(Color::TAB_RED));
assert_eq!(t.ha, HAlign::Center);
assert_eq!(t.va, VAlign::Top);
assert!((t.rotation - 45.0).abs() < f64::EPSILON);
}
#[test]
fn annotation_defaults() {
let mut a = Annotation {
text: "peak".to_string(),
xy: (1.0, 2.0),
xytext: (3.0, 4.0),
fontsize: None,
color: None,
ha: HAlign::Center,
va: VAlign::Bottom,
arrowstyle: ArrowStyle::None,
arrow_color: None,
};
assert_eq!(a.text, "peak");
assert_eq!(a.xy, (1.0, 2.0));
assert_eq!(a.xytext, (3.0, 4.0));
assert_eq!(a.arrowstyle, ArrowStyle::None);
a.arrowstyle(ArrowStyle::Simple).arrow_color(Color::TAB_BLUE).fontsize(12.0);
assert_eq!(a.arrowstyle, ArrowStyle::Simple);
assert_eq!(a.arrow_color, Some(Color::TAB_BLUE));
assert_eq!(a.fontsize, Some(12.0));
}
#[test]
fn arrow_style_equality() {
assert_eq!(ArrowStyle::None, ArrowStyle::None);
assert_eq!(ArrowStyle::Simple, ArrowStyle::Simple);
assert_eq!(ArrowStyle::Fancy, ArrowStyle::Fancy);
assert_ne!(ArrowStyle::None, ArrowStyle::Simple);
assert_ne!(ArrowStyle::Simple, ArrowStyle::Fancy);
}
}