Functional Weave
Code in Rust

charts.axis@1.0.0

impl/rust/band_axis.rs

1,073 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_scale_band_scale::band_scale;

/// A category axis: one tick at the centre of each band, labelled with the category.
///
/// # Panics
/// Panics on an empty or duplicated domain, padding outside 0..=1, or an
/// unknown orient.
pub fn band_axis(
    domain: &[String],
    range: &[f64],
    padding_inner: f64,
    padding_outer: f64,
    orient: &str,
    position: f64,
    tick_size: f64,
) -> Axis {
    let positions: Vec<f64> = domain
        .iter()
        .map(|v| band_scale(domain, range, v, padding_inner, padding_outer, 0.5).center)
        .collect();
    axis_from_ticks(&positions, domain, range, orient, position, tick_size)
}

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