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
impl TimeScale
Sourcepub fn bar_spacing(&self) -> f64
pub fn bar_spacing(&self) -> f64
Width of one bar in physical pixels.
Sourcepub fn right_offset(&self) -> f64
pub fn right_offset(&self) -> f64
Empty space right of the last bar, in bars (may be negative).
Sourcepub fn effective_min_spacing(&self) -> f64
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.
Sourcepub fn set_bar_spacing(&mut self, px: f64)
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.
Sourcepub fn set_right_offset(&mut self, bars: f64)
pub fn set_right_offset(&mut self, bars: f64)
Sets the right offset in bars; non-finite values are ignored.
Sourcepub fn scroll_by(&mut self, dx: Px)
pub fn scroll_by(&mut self, dx: Px)
Pans by dx (positive = content moves right, showing earlier bars).
Sourcepub fn zoom(&mut self, anchor_x: Px, factor: f64)
pub fn zoom(&mut self, anchor_x: Px, factor: f64)
Zooms by factor (> 1 zooms in) keeping the bar under anchor_x fixed.
Sourcepub fn fit_content(&mut self)
pub fn fit_content(&mut self)
Shows all bars.
Sourcepub fn set_visible_range(
&mut self,
from: LogicalIndex,
to: LogicalIndex,
) -> Result<()>
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());Sourcepub fn visible_range(&self) -> (LogicalIndex, LogicalIndex)
pub fn visible_range(&self) -> (LogicalIndex, LogicalIndex)
Visible logical range (from, to) (left and right plot edges).
Sourcepub fn index_to_x(&self, i: LogicalIndex) -> Px
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);Sourcepub fn x_to_index(&self, x: Px) -> LogicalIndex
pub fn x_to_index(&self, x: Px) -> LogicalIndex
Logical index at x (relative to the plot’s left edge).