Functional Weave
Code in TypeScript

charts.axis@1.0.1

impl/python/time_axis.py

1,164 bytes · the Python 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.

from typing import Sequence

from .charts_axis_axis_from_ticks import axis_from_ticks  ← axisFromTicks, another function of this group · built into the same file, even by a slim install
from .charts_axis_types import Axis, AxisOrient
from .charts_format_format_date import format_date
from .charts_scale_time_scale import time_scale
from .charts_ticks_time_tick_interval import time_tick_interval
from .charts_ticks_time_ticks import time_ticks


def time_axis(
    domain: Sequence[str],
    range: Sequence[float],  # noqa: A002 - the manifest names it range
    count: int,
    orient: AxisOrient,
    position: float,
    tick_size: float,
) -> Axis:
    """A date axis: a calendar interval for about count ticks, labelled for that interval."""
    if len(domain) != 2:
        raise ValueError("domain must have exactly 2 values, [from, to]; got %d" % len(domain))
    lo = min(domain[0], domain[1])
    hi = max(domain[0], domain[1])
    chosen = time_tick_interval(lo, hi, count)
    values = time_ticks(lo, hi, chosen.interval, chosen.step)
    positions = [time_scale(domain, range, v, False) for v in values]
    labels = [format_date(v, chosen.interval) for v in values]
    return axis_from_ticks(positions, labels, range, orient, position, tick_size)