Skip to main content

ChartMapping

Struct ChartMapping 

Source
pub struct ChartMapping {
    pub rect: Rect,
    pub bar_spacing: f32,
    pub start_idx: usize,
    pub base_idx: usize,
    pub right_offset: f64,
    pub price: PriceMap,
}
Expand description

Unified coordinate mapping for charts and indicator panes.

Converts between:

  • Bar indices ↔ X screen coordinates
  • Prices ↔ Y screen coordinates

Share the mapping with overlays and hit tests to keep them aligned with the pane.

Fields§

§rect: Rect

The chart rectangle (rendering area)

§bar_spacing: f32

Spacing between bars in egui logical points.

§start_idx: usize

Index of the first visible bar

§base_idx: usize

Index of the final bar in the series, used as the horizontal anchor.

§right_offset: f64

Right-side offset for chart scrolling (in bar units)

§price: PriceMap

Price-to-y mapping of the pane; it carries its own vertical extent, which need not match rect

Implementations§

Source§

impl ChartMapping

Source

pub fn new( rect: Rect, bar_spacing: f32, start_idx: usize, base_idx: usize, right_offset: impl Into<f64>, price: PriceMap, ) -> Self

Create a new ChartMapping with all parameters.

Source

pub fn idx_to_x(&self, bar_idx: usize) -> f32

Convert bar index to X screen coordinate.

This is the canonical formula used everywhere.

Source

pub fn x_to_idx(&self, x: f32) -> usize

Convert X screen coordinate to bar index (inverse of idx_to_x).

A degenerate bar_spacing (zero or sub-epsilon) has no inverse, so the anchor index is returned rather than producing an infinite/NaN index.

Source

pub fn x_to_idx_f32(&self, x: f32) -> f32

Convert X screen coordinate to fractional bar index.

Useful for precise positioning (e.g., crosshair snapping).

A degenerate bar_spacing (zero or sub-epsilon) has no inverse, so the anchor index is returned rather than producing an infinite/NaN index.

Source

pub fn x_to_idx_f64(&self, x: f32) -> f64

Convert X to a fractional bar index, retaining sub-bar precision on large histories. A degenerate spacing returns the anchor, as in Self::x_to_idx_f32.

Source

pub fn is_x_visible(&self, x: f32) -> bool

Check if an X coordinate is within the visible chart bounds.

Source

pub fn local_idx_at_x(&self, x: f32, visible_len: usize) -> Option<usize>

Resolve the bar directly under a cursor X coordinate to a local index into the visible data slice of length visible_len.

Returns None when there is no data, or when the cursor falls before the first bar or after the last bar (the right-side empty scroll margin), so callers never index past the slice or read a stale neighbour. Resolution uses Self::x_to_idx_f64, the exact inverse of Self::idx_to_x, so the bar at its own centre x resolves to itself and the readout describes the same bar the crosshair snaps to.

Source

pub fn bar_width(&self) -> f32

Calculate the bar width based on spacing.

Source

pub fn price_to_y(&self, price: f64) -> f32

Convert price to Y screen coordinate.

Source

pub fn y_to_price(&self, y: f32) -> f64

Convert Y screen coordinate to price (inverse of price_to_y).

Source

pub fn is_y_visible(&self, y: f32) -> bool

Check if a Y coordinate is within the visible chart bounds.

Source

pub fn to_screen(&self, bar_idx: usize, price: f64) -> Pos2

Convert bar index and price to screen position.

Source

pub fn from_screen(&self, pos: Pos2) -> (usize, f64)

Convert screen position to bar index and price.

Source

pub fn is_visible(&self, pos: Pos2) -> bool

Check if a screen position is within the visible chart bounds.

Source

pub fn with_rect(self, rect: Rect) -> Self

Create a new ChartMapping with a different rect (for sub-regions).

Only the x mapping and the bounds checks follow the new rect: price still maps onto its own pane.

Trait Implementations§

Source§

impl Clone for ChartMapping

Source§

fn clone(&self) -> ChartMapping

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 ChartMapping

Source§

impl Debug for ChartMapping

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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
§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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, 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<T> MaybeSend for T
where T: Send + Sync + ?Sized,

§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

§

impl<T> SerializableAny for T
where T: 'static + Any + Clone + for<'a> Send + Sync,

§

impl<T, S> SimdFrom<T, S> for T
where S: Simd,

§

fn simd_from(_simd: S, value: T) -> T

§

impl<F, T, S> SimdInto<T, S> for F
where T: SimdFrom<F, S>, S: Simd,

§

fn simd_into(self, simd: S) -> T

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