Skip to main content

Axis

Struct Axis 

Source
pub struct Axis<D, Theme = Theme> { /* private fields */ }
Expand description

An axis that maps data values to screen coordinates.

The Axis struct is responsible for:

  1. Defining the scale (linear, log, etc.) for mapping data to pixels.
  2. Configuring visual elements like ticks, grid lines, and labels.
  3. Handling layout and rendering of the axis and its interactive marker.

§Example

use iced_aksel::{Axis, axis::{Position, TickResult, TickContext}, scale::Linear};

let axis = Axis::new(Linear::new(0.0, 100.0), Position::Bottom)
    .with_thickness(40.0)
    .with_tick_renderer(|ctx: TickContext<f64>| TickResult {
        tick_line: Some(ctx.tickline()),
        grid_line: Some(ctx.gridline()),
        label: Some(ctx.label(format!("{:.2}", ctx.tick.value))),
        ..Default::default()
    });

Implementations§

Source§

impl<D: Float, Theme> Axis<D, Theme>

Source

pub fn new( scale: impl Scale<Domain = D, Normalized = f32> + 'static, position: Position, ) -> Self

Creates a new Axis with the given scale and position.

By default, the axis will render:

  • Major ticks with labels
  • Minor ticks (smaller lines)
  • Grid lines aligned with major ticks
Source

pub fn with_thickness<P: Into<Pixels>>(self, thickness: P) -> Self

Sets the reserved thickness of the axis in pixels.

This determines the space reserved for the axis in the chart layout. Increase this if your labels are being clipped or overlapping with the chart area.

Source

pub fn style<F>(self, style_fn: F) -> Self
where F: FnMut(&mut AxisStyle) + 'static,

Adds a function that overrides the default styling coming from chart.

If you just want to set a general style for the chart in general, see crate::Chart instead.

Source

pub fn with_tick_renderer<F>(self, renderer: F) -> Self
where F: FnMut(TickContext<'_, D, Theme>) -> TickResult + 'static,

Sets a custom renderer for ticks.

This function gives you full control over which ticks render lines, grids, or labels.

§Example
iced_aksel::axis::Position::Bottom);
axis.with_tick_renderer(|ctx: TickContext<f64>| {
    if ctx.tick.level == 0 {
        TickResult::with_label(ctx.label(format!("{:.1}", ctx.tick.value)))
    } else {
        TickResult::default() // Empty tick result
    }
});
Source

pub const fn without_grid(self) -> Self

Disables grid line rendering for this axis.

Source

pub fn skip_overlapping_labels(self, min_gap_px: f32) -> Self

Configures the axis to skip labels that would overlap.

min_gap_px specifies the minimum distance in pixels required between labels.

Source

pub fn with_custom_label_policy<F>(self, policy: F) -> Self
where F: for<'a> Fn(LabelDecisionContext<'a, D>) -> LabelDecision + 'static,

Sets a custom policy for determining which labels to render.

Useful for advanced collision detection or custom filtering logic.

Source

pub const fn invisible(self) -> Self

Makes the axis invisible.

It will still occupy layout space (defined by thickness) but will not render any ticks, lines, or labels. To remove it from layout entirely, set thickness to 0.

Source

pub fn set_skip_overlapping_labels(&mut self, min_gap_px: f32)

Configures the axis to skip labels that would overlap.

min_gap_px specifies the minimum distance in pixels required between labels.

Source

pub fn set_tick_renderer<F>(&mut self, renderer: F)
where F: Fn(TickContext<'_, D, Theme>) -> TickResult + 'static,

Updates the tick renderer in-place.

Source

pub const fn set_visibility(&mut self, visible: bool)

Sets the visibility of the axis.

Source

pub fn set_thickness<P: Into<Pixels>>(&mut self, thickness: P)

Updates the thickness of the axis in-place.

Source

pub const fn is_visible(&self) -> bool

Returns true if the axis is currently visible.

Source

pub fn domain(&self) -> (&D, &D)

Returns the data domain (min, max) of the axis.

Source

pub const fn position(&self) -> &Position

Returns the layout position of the axis.

Source

pub fn orientation(&self) -> Orientation

Returns the orientation (Horizontal/Vertical) based on the position.

Source

pub const fn thickness(&self) -> Pixels

Returns the current thickness of the axis.

Trait Implementations§

Source§

impl<D, Theme> Debug for Axis<D, Theme>

Source§

fn fmt(&self, __f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<D: Float, Theme> Deref for Axis<D, Theme>

Source§

type Target = dyn Scale<Normalized = f32, Domain = D>

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl<D: Float, Theme> DerefMut for Axis<D, Theme>

Source§

fn deref_mut(&mut self) -> &mut Self::Target

Mutably dereferences the value.

Auto Trait Implementations§

§

impl<D, Theme = Theme> !Freeze for Axis<D, Theme>

§

impl<D, Theme = Theme> !RefUnwindSafe for Axis<D, Theme>

§

impl<D, Theme = Theme> !Send for Axis<D, Theme>

§

impl<D, Theme = Theme> !Sync for Axis<D, Theme>

§

impl<D, Theme> Unpin for Axis<D, Theme>

§

impl<D, Theme> UnsafeUnpin for Axis<D, Theme>

§

impl<D, Theme = Theme> !UnwindSafe for Axis<D, Theme>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> Downcast<T> for T

Source§

fn downcast(&self) -> &T

Source§

impl<T> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
Source§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> Upcast<T> for T

Source§

fn upcast(&self) -> Option<&T>

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more