Skip to main content

gpui_base/plot/
scale.rs

1mod band;
2mod linear;
3mod ordinal;
4mod point;
5mod sealed;
6
7pub use band::ScaleBand;
8pub use linear::ScaleLinear;
9pub use ordinal::ScaleOrdinal;
10pub use point::ScalePoint;
11
12use num_traits::{Num, ToPrimitive};
13
14/// A number a [`ScaleLinear`] and the charts built on it can place: `f32`,
15/// `f64`, and `rust_decimal::Decimal` with the `decimal` feature.
16///
17/// Sealed: integer types are left out on purpose, because a linear scale
18/// divides values and integer division would truncate every position.
19pub trait PlotValue: sealed::Sealed + Copy + PartialOrd + Num + ToPrimitive + 'static {}
20
21impl PlotValue for f32 {}
22impl PlotValue for f64 {}
23#[cfg(feature = "decimal")]
24impl PlotValue for rust_decimal::Decimal {}
25
26pub trait Scale<T> {
27    /// Get the tick of the scale.
28    fn tick(&self, value: &T) -> Option<f32>;
29
30    /// The index of the domain value whose position is nearest `tick`; `0` for
31    /// an empty domain or a scale without discrete positions.
32    fn nearest_index(&self, _tick: f32) -> usize {
33        0
34    }
35}