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|, sovsits in the middle (0..1whenvis 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
impl Scale
Sourcepub fn new(a: f64, b: f64) -> Self
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.
Sourcepub fn from_values(values: impl IntoIterator<Item = f64>) -> Self
pub fn from_values(values: impl IntoIterator<Item = f64>) -> Self
The range of the finite values in values.
Sourcepub fn bounds(self, min: Option<f64>, max: Option<f64>) -> Self
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.
Sourcepub fn include_zero(self) -> Self
pub fn include_zero(self) -> Self
Stretch the scale so it contains 0, as bars need.
Sourcepub fn nice(self, count: usize) -> Self
pub fn nice(self, count: usize) -> Self
Widen the bounds outward to multiples of the tick step that
ticks would use for count ticks.
Sourcepub fn span(&self) -> f64
pub fn span(&self) -> f64
max - min, always above zero. Bounds near ±f64::MAX would
overflow it; it is then f64::MAX.
Sourcepub fn normalize(&self, value: f64) -> Option<f64>
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.
Sourcepub fn step(&self, count: usize) -> f64
pub fn step(&self, count: usize) -> f64
The tick step for about count ticks: 1, 2 or 5 times a power of ten.