use crate::styles::numbers::NumberFormat;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct CategoryAxis;
impl CategoryAxis {
#[allow(clippy::new_ret_no_self)]
pub fn new() -> Axis {
Axis::category()
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct ValueAxis;
impl ValueAxis {
#[allow(clippy::new_ret_no_self)]
pub fn new() -> Axis {
Axis::value()
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct Axis {
pub auto_axis: bool,
pub raw_min: f64,
pub raw_max: f64,
pub raw_unit: Option<f64>,
pub title: String,
pub position: String,
pub tick_label_position: String,
pub crosses: String,
pub auto: bool,
pub label_align: Option<String>,
pub label_offset: Option<i64>,
pub cross_between: String,
pub orientation: String,
pub id: u32,
pub cross: u32,
pub number_format: NumberFormat,
pub delete_axis: bool,
pub major_gridlines: bool,
pub axis_type: String,
}
impl Default for Axis {
fn default() -> Self {
Axis {
auto_axis: true,
raw_min: 0.0,
raw_max: 0.0,
raw_unit: None,
title: String::new(),
position: "b".to_string(),
tick_label_position: "nextTo".to_string(),
crosses: "autoZero".to_string(),
auto: true,
label_align: Some("ctr".to_string()),
label_offset: Some(100),
cross_between: "midCat".to_string(),
orientation: "minMax".to_string(),
id: 60_871_424,
cross: 60_873_344,
number_format: NumberFormat::default(),
delete_axis: false,
major_gridlines: false,
axis_type: "catAx".to_string(),
}
}
}
impl Axis {
pub const POSITION_BOTTOM: &'static str = "b";
pub const POSITION_LEFT: &'static str = "l";
pub const ORIENTATION_MIN_MAX: &'static str = "minMax";
pub fn category() -> Self {
Axis::default()
}
pub fn is_value_axis(&self) -> bool {
self.axis_type == "valAx"
}
pub fn value() -> Self {
Axis {
position: Axis::POSITION_LEFT.to_string(),
major_gridlines: true,
auto: false,
cross_between: "between".to_string(),
axis_type: "valAx".to_string(),
id: 60_873_344,
cross: 60_871_424,
..Axis::default()
}
}
pub fn new(auto_axis: bool) -> Self {
Axis {
auto_axis,
..Axis::default()
}
}
pub fn min(&self) -> f64 {
if self.auto_axis {
self.scaled_bounds().0
} else {
self.raw_min
}
}
pub fn max(&self) -> f64 {
if self.auto_axis {
self.scaled_bounds().1
} else {
self.raw_max
}
}
pub fn scaled_bounds(&self) -> (f64, f64) {
self.max_min().bounds
}
pub fn set_min(&mut self, value: f64) {
self.raw_min = value;
}
pub fn set_max(&mut self, value: f64) {
self.raw_max = value;
}
pub fn unit(&self) -> f64 {
self.max_min().unit
}
pub fn set_unit(&mut self, value: f64) {
self.raw_unit = Some(value);
}
pub fn max_min(&self) -> AxisScale {
let length = self.raw_max - self.raw_min;
if length == 0.0 {
return AxisScale {
bounds: (self.raw_min, self.raw_max),
unit: 1.0,
};
}
let sign = if length < 0.0 { -1.0 } else { 1.0 };
let zoom = less_than_one(length).unwrap_or(1.0);
let value = length * zoom;
let mut value = (value.abs() * 1.1).ceil();
value *= sign;
let log = value.abs().log10();
let exp = log.trunc();
let mant = log - exp;
let mut unit = (10f64.powf(mant).ceil() * 10f64.powf(exp - 1.0)).ceil();
let value = (value / unit).ceil() * unit;
unit /= zoom;
if value / unit > 9.0 {
unit *= 2.0;
}
let scale = value / length;
let mini = (self.raw_min * scale).floor() / zoom;
let maxi = (self.raw_max * scale).ceil() / zoom;
AxisScale {
bounds: (mini, maxi),
unit,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct AxisScale {
pub bounds: (f64, f64),
pub unit: f64,
}
pub fn less_than_one(value: f64) -> Option<f64> {
let value = value.abs();
if value < 1.0 && value > 0.0 {
let exp = value.log10().abs().trunc();
Some(10f64.powf(exp + 1.0))
} else {
None
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn category_axis_defaults() {
let axis = Axis::category();
assert_eq!(axis.position, "b");
assert_eq!(axis.tick_label_position, "nextTo");
assert_eq!(axis.crosses, "autoZero");
assert!(axis.auto);
assert_eq!(axis.label_align.as_deref(), Some("ctr"));
assert_eq!(axis.label_offset, Some(100));
assert_eq!(axis.cross_between, "midCat");
assert_eq!(axis.axis_type, "catAx");
assert_eq!(axis.id, 60_871_424);
}
#[test]
fn value_axis_defaults() {
let axis = Axis::value();
assert_eq!(axis.position, "l");
assert!(!axis.auto);
assert_eq!(axis.cross_between, "between");
assert_eq!(axis.axis_type, "valAx");
assert_eq!(axis.id, 60_873_344);
assert!(axis.major_gridlines);
}
#[test]
fn manual_axis_returns_raw_bounds() {
let mut axis = Axis::new(false);
axis.set_min(5.0);
axis.set_max(50.0);
assert_eq!(axis.min(), 5.0);
assert_eq!(axis.max(), 50.0);
}
#[test]
fn auto_axis_pads_the_range() {
let mut axis = Axis::new(true);
axis.set_min(0.0);
axis.set_max(100.0);
let scale = axis.max_min();
let (min, unit) = (scale.bounds.0, scale.unit);
assert!(min <= 0.0);
assert!(unit > 0.0);
let (lo, hi) = (axis.min(), axis.max());
assert!(((hi - lo) / unit).ceil() <= 10.0);
}
#[test]
fn auto_axis_pads_small_ranges() {
let mut axis = Axis::new(true);
axis.set_min(0.0);
axis.set_max(0.001);
let scale = axis.max_min();
let unit = scale.unit;
assert_eq!(scale.bounds.0, 0.0);
assert!((unit - 0.0004).abs() < 1e-12, "unit was {unit}");
assert!(axis.max() > 0.001, "the maximum must cover the data");
}
#[test]
fn flat_series_gets_a_usable_unit() {
let mut axis = Axis::new(true);
axis.set_min(10.0);
axis.set_max(10.0);
let scale = axis.max_min();
let (min, unit) = (scale.bounds.0, scale.unit);
assert!(unit > 0.0);
assert!(min.is_finite());
}
#[test]
fn less_than_one_rescaling() {
assert_eq!(less_than_one(0.5), Some(10.0));
assert_eq!(less_than_one(0.05), Some(100.0));
assert_eq!(less_than_one(5.0), None);
assert_eq!(less_than_one(0.0), None);
assert_eq!(less_than_one(-0.5), Some(10.0));
}
}