Functional Weave
Code in Python

charts.axis@1.0.1

impl/rust/number_axis.rs

1,447 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_to_value};  ← axisFromTicks, another function of this group · built into the same file, even by a slim install
use super::charts_format_format_tick::format_tick;
use super::charts_scale_linear_scale::linear_scale;
use super::charts_ticks_nice_ticks::nice_ticks;
use super::charts_ticks_tick_step::tick_step;

/// A linear number axis: nice ticks, placed by linear_scale, labelled for the step.
///
/// # Panics
/// Panics on a domain without two different ends, a count below 1, or an
/// unknown orient.
pub fn number_axis(domain: &[f64], 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 = domain[0].min(domain[1]);
    let hi = domain[0].max(domain[1]);
    let values = nice_ticks(domain[0], domain[1], count);
    let step = tick_step(lo, hi, count);
    let positions: Vec<f64> = values.iter().map(|v| linear_scale(domain, range, *v, false)).collect();
    let labels: Vec<String> = values.iter().map(|v| format_tick(*v, step)).collect();
    axis_from_ticks(&positions, &labels, range, orient, position, tick_size)
}

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