Skip to main content

Scale

Struct Scale 

Source
pub struct Scale { /* private fields */ }
Expand description

A linear scale from min to max.

Every chart maps its values through one. Scale::from_values skips NaN and infinite values and never returns an empty range, so the charts never divide by zero:

  • no finite values: 0..1;
  • every value equal to v: v - |v| .. v + |v|, so v sits in the middle (0..1 when v is 0);
  • otherwise the smallest and largest value.
use rich_ext::chart::Scale;

let scale = Scale::from_values([3.0, f64::NAN, 17.0, 9.0]);
assert_eq!((scale.min(), scale.max()), (3.0, 17.0));
assert_eq!(scale.nice(5).ticks(5), vec![0.0, 5.0, 10.0, 15.0, 20.0]);
assert_eq!(scale.normalize(10.0), Some(0.5));

// Explicit bounds win, in either order.
assert_eq!(Scale::new(10.0, -10.0).normalize(0.0), Some(0.5));

Implementations§

Source§

impl Scale

Source

pub fn new(a: f64, b: f64) -> Self

A scale between two bounds, in either order. Non-finite bounds fall back to 0..1; equal bounds are widened as in from_values.

Source

pub fn from_values(values: impl IntoIterator<Item = f64>) -> Self

The range of the finite values in values.

Source

pub fn bounds(self, min: Option<f64>, max: Option<f64>) -> Self

Override either bound. A bound left None keeps its value; if the result is empty or reversed it is fixed up as new does.

Source

pub fn include_zero(self) -> Self

Stretch the scale so it contains 0, as bars need.

Source

pub fn nice(self, count: usize) -> Self

Widen the bounds outward to multiples of the tick step that ticks would use for count ticks.

Source

pub fn min(&self) -> f64

The lower bound.

Source

pub fn max(&self) -> f64

The upper bound.

Source

pub fn span(&self) -> f64

max - min, always above zero. Bounds near ±f64::MAX would overflow it; it is then f64::MAX.

Source

pub fn normalize(&self, value: f64) -> Option<f64>

Where value falls, from 0 (at min) to 1 (at max), clamped. None for NaN and infinities.

Source

pub fn step(&self, count: usize) -> f64

The tick step for about count ticks: 1, 2 or 5 times a power of ten.

Source

pub fn ticks(&self, count: usize) -> Vec<f64>

Round values inside the scale, about count of them (at least 2), spaced by step. They include the bounds only when the bounds are themselves multiples of the step; use nice first for ticks that span the whole scale.

Source

pub fn ticks_every(&self, step: f64) -> Vec<f64>

The multiples of step inside the scale. After nice the range is wider, so its own step may differ; pass the step of the range before widening to keep the ticks the bounds were rounded to.

Trait Implementations§

Source§

impl Clone for Scale

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for Scale

Source§

impl Debug for Scale

Source§

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

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

impl Default for Scale

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl PartialEq for Scale

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Scale

Auto Trait Implementations§

§

impl Freeze for Scale

§

impl RefUnwindSafe for Scale

§

impl Send for Scale

§

impl Sync for Scale

§

impl Unpin for Scale

§

impl UnsafeUnpin for Scale

§

impl UnwindSafe for Scale

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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> 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