Skip to main content

TimeScale

Struct TimeScale 

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

The global time scale.

x(i) = width - (last_index + right_offset - i + 0.5) * bar_spacing is the center of bar i in physical pixels relative to the plot’s left edge.

§Examples

use ferrochart_core::{Chart, ChartOptions};
use ferrochart_core::geom::Px;

let mut chart = Chart::new(ChartOptions::default());
let ts = chart.time_scale();
ts.set_bar_spacing(8.0);
ts.scroll_by(Px(16.0)); // drag right by two bars
assert_eq!(ts.bar_spacing(), 8.0);

Implementations§

Source§

impl TimeScale

Source

pub fn bar_spacing(&self) -> f64

Width of one bar in physical pixels.

Source

pub fn right_offset(&self) -> f64

Empty space right of the last bar, in bars (may be negative).

Source

pub fn effective_min_spacing(&self) -> f64

Smallest allowed bar spacing: min(min_bar_spacing, 0.9 · width / bars), so that all data can always be shown at once.

Source

pub fn set_bar_spacing(&mut self, px: f64)

Sets the bar spacing (physical pixels), clamped to the allowed range; non-finite values are ignored.

Source

pub fn set_right_offset(&mut self, bars: f64)

Sets the right offset in bars; non-finite values are ignored.

Source

pub fn scroll_by(&mut self, dx: Px)

Pans by dx (positive = content moves right, showing earlier bars).

Source

pub fn zoom(&mut self, anchor_x: Px, factor: f64)

Zooms by factor (> 1 zooms in) keeping the bar under anchor_x fixed.

Source

pub fn fit_content(&mut self)

Shows all bars.

Source

pub fn set_visible_range( &mut self, from: LogicalIndex, to: LogicalIndex, ) -> Result<()>

Shows logical indices from..to across the plot (the left and right edges), as far as the spacing limits allow (the right edge is kept). Like fit_content it waits for the plot width, so it may be called before the first frame or together with a resize; panning or zooming drops it.

§Errors

Error::InvalidArgument unless from and to are finite and from < to; the view is unchanged.

§Examples
use ferrochart_core::{Chart, ChartOptions};
use ferrochart_core::chart::{SeriesData, SeriesOptions};
use ferrochart_core::frame::SeriesKind;
use ferrochart_core::geom::SizeCss;
use ferrochart_core::testing::{random_walk, MonoMeasurer};

let mut chart = Chart::new(ChartOptions::default());
chart.resize(SizeCss { w: 800.0, h: 450.0 }, 1.0);
let pane = chart.main_pane();
let candles = chart.add_series(pane, SeriesKind::Candlestick, SeriesOptions::default())?;
chart.set_data(candles, SeriesData::Ohlc(random_walk(1, 500, 0, 60_000)?))?;
// Before the first frame: applied once the layout is known.
chart.time_scale().set_visible_range(100.0, 200.0)?;
chart.build_frame(0.0, &MonoMeasurer);
let (from, to) = chart.time_scale().visible_range();
assert!((from - 100.0).abs() < 1e-6 && (to - 200.0).abs() < 1e-6);
assert!(chart.time_scale().set_visible_range(5.0, 5.0).is_err());
Source

pub fn visible_range(&self) -> (LogicalIndex, LogicalIndex)

Visible logical range (from, to) (left and right plot edges).

Source

pub fn index_to_x(&self, i: LogicalIndex) -> Px

x of the center of logical index i, relative to the plot’s left edge.

§Examples
use ferrochart_core::{Chart, ChartOptions};

let mut chart = Chart::new(ChartOptions::default());
let ts = chart.time_scale();
let x = ts.index_to_x(3.0);
assert!((ts.x_to_index(x) - 3.0).abs() < 1e-3);
Source

pub fn x_to_index(&self, x: Px) -> LogicalIndex

Logical index at x (relative to the plot’s left edge).

Trait Implementations§

Source§

impl Clone for TimeScale

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 Debug for TimeScale

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