Functional Weave
Code in Python

charts.axis@1.0.1

impl/rust/time_axis.rs

1,540 bytes · the Rust implementation · view raw

Imports name this capability’s declared dependencies, which fune builds next to it in your project; each one links to its page.

use super::funejson::Value;  ← the fune runtime: the JSON value the test vectors use; fune build keeps it only where a signature takes one
use super::charts_axis_axis_from_ticks::{axis_floats, axis_from_ticks, axis_strings, axis_to_value};  ← axisFromTicks, another function of this group · built into the same file, even by a slim install
use super::charts_format_format_date::format_date;
use super::charts_scale_time_scale::time_scale;
use super::charts_ticks_time_tick_interval::time_tick_interval;
use super::charts_ticks_time_ticks::time_ticks;

/// A date axis: a calendar interval for about `count` ticks, labelled for that interval.
///
/// # Panics
/// Panics on a domain without two different dates, a count below 1, or an
/// unknown orient.
pub fn time_axis(domain: &[String], range: &[f64], count: i64, orient: &str, position: f64, tick_size: f64) -> Axis {
    if domain.len() != 2 {
        panic!("domain must have exactly 2 values, [from, to]; got {}", domain.len());
    }
    let (lo, hi) = if domain[0] < domain[1] { (&domain[0], &domain[1]) } else { (&domain[1], &domain[0]) };
    let chosen = time_tick_interval(lo, hi, count);
    let values = time_ticks(lo, hi, &chosen.interval, chosen.step);
    let positions: Vec<f64> = values.iter().map(|v| time_scale(domain, range, v, false)).collect();
    let labels: Vec<String> = values.iter().map(|v| format_date(v, &chosen.interval)).collect();
    axis_from_ticks(&positions, &labels, range, orient, position, tick_size)
}

pub fn fune_vector(args: &[Value]) -> Value {
    axis_to_value(&time_axis(
        &axis_strings(&args[0]),
        &axis_floats(&args[1]),
        args[2].as_i64(),
        args[3].as_str(),
        args[4].as_f64(),
        args[5].as_f64(),
    ))
}