pub struct DrawingCtx<'a> { /* private fields */ }Expand description
Coordinate conversions and measurements for DrawingTools of one pane.
Implementations§
Source§impl DrawingCtx<'_>
impl DrawingCtx<'_>
Sourcepub fn hit_tolerance(&self) -> f32
pub fn hit_tolerance(&self) -> f32
Distance within which a point hits a line.
Sourcepub fn bar_spacing(&self) -> Px
pub fn bar_spacing(&self) -> Px
Width of one bar.
Sourcepub fn index_of_time(&self, time: Timestamp) -> Option<f64>
pub fn index_of_time(&self, time: Timestamp) -> Option<f64>
Logical index of time, fractional between slots and extrapolated
outside the data; None without data.
Sourcepub fn y_of(&self, price: f64) -> Option<f32>
pub fn y_of(&self, price: f64) -> Option<f32>
y of price; None if it has none (e.g. ≤ 0 on a log scale).
Sourcepub fn time_at(&self, x: f32) -> Option<Timestamp>
pub fn time_at(&self, x: f32) -> Option<Timestamp>
Time at x: interpolated between slots, extrapolated beyond the
data (the inverse of x_of, rounded to the
millisecond); None without data.
§Examples
use std::sync::atomic::{AtomicI64, Ordering};
use ferrochart_core::chart::{SeriesData, SeriesOptions};
use ferrochart_core::data::ValueColumns;
use ferrochart_core::drawing::FrameBuilder;
use ferrochart_core::frame::SeriesKind;
use ferrochart_core::geom::SizeCss;
use ferrochart_core::plugin::{LayerCtx, LayerPlugin, LayerPosition};
use ferrochart_core::testing::MonoMeasurer;
use ferrochart_core::{Chart, ChartOptions};
static TIME: AtomicI64 = AtomicI64::new(0);
struct Probe;
impl LayerPlugin for Probe {
fn position(&self) -> LayerPosition {
LayerPosition::Overlay
}
fn build(&self, ctx: &LayerCtx<'_>, _: &mut FrameBuilder<'_>) {
let Some(c) = ctx.coords() else { return };
// Half way between the bars at 60 s and 120 s.
if let Some(t) = c.x_of(90_000).and_then(|x| c.time_at(x)) {
TIME.store(t, Ordering::Relaxed);
}
}
}
let mut chart = Chart::new(ChartOptions::default());
chart.resize(SizeCss { w: 800.0, h: 400.0 }, 1.0);
let pane = chart.main_pane();
let line = chart.add_series(pane, SeriesKind::Line, SeriesOptions::default())?;
chart.set_data(line, SeriesData::Value(ValueColumns {
time: (0..10).map(|i| i * 60_000).collect(),
value: vec![1.0; 10],
color: None,
}))?;
chart.add_layer(Some(pane), Box::new(Probe))?;
chart.build_frame(0.0, &MonoMeasurer);
assert_eq!(TIME.load(Ordering::Relaxed), 90_000);Sourcepub fn stroke(&self, style: &DrawingStyle) -> StrokeStyle
pub fn stroke(&self, style: &DrawingStyle) -> StrokeStyle
Stroke of style’s lines (whole pixels, at least 1).
Sourcepub fn font(&self, size: Css) -> FontStyleRef
pub fn font(&self, size: Css) -> FontStyleRef
Font of size.
Sourcepub fn text_width(&self, text: &str, font: FontStyleRef) -> f32
pub fn text_width(&self, text: &str, font: FontStyleRef) -> f32
Width of text.
Sourcepub fn line_height(&self, font: FontStyleRef) -> f32
pub fn line_height(&self, font: FontStyleRef) -> f32
Line height of font.
Sourcepub fn write_price(&self, out: &mut impl Write, price: f64)
pub fn write_price(&self, out: &mut impl Write, price: f64)
Writes price with the pane’s precision.
Auto Trait Implementations§
impl<'a> !RefUnwindSafe for DrawingCtx<'a>
impl<'a> !Send for DrawingCtx<'a>
impl<'a> !Sync for DrawingCtx<'a>
impl<'a> !UnwindSafe for DrawingCtx<'a>
impl<'a> Forget for DrawingCtx<'a>
impl<'a> Freeze for DrawingCtx<'a>
impl<'a> Move for DrawingCtx<'a>
impl<'a> Unpin for DrawingCtx<'a>
impl<'a> UnsafeUnpin for DrawingCtx<'a>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more