Skip to main content

DrawingCtx

Struct DrawingCtx 

Source
pub struct DrawingCtx<'a> { /* private fields */ }
Expand description

Coordinate conversions and measurements for DrawingTools of one pane.

Implementations§

Source§

impl DrawingCtx<'_>

Source

pub fn plot(&self) -> RectPx

The pane’s plot rectangle.

Source

pub fn dpr(&self) -> f32

Device pixel ratio.

Source

pub fn px(&self, v: Css) -> Px

v in physical pixels.

Source

pub fn hit_tolerance(&self) -> f32

Distance within which a point hits a line.

Source

pub fn bar_spacing(&self) -> Px

Width of one bar.

Source

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.

Source

pub fn x_of(&self, time: Timestamp) -> Option<f32>

x of time.

Source

pub fn y_of(&self, price: f64) -> Option<f32>

y of price; None if it has none (e.g. ≤ 0 on a log scale).

Source

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

Price at y.

Source

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);
Source

pub fn point(&self, a: Anchor) -> Option<PointPx>

Screen position of a.

Source

pub fn stroke(&self, style: &DrawingStyle) -> StrokeStyle

Stroke of style’s lines (whole pixels, at least 1).

Source

pub fn font(&self, size: Css) -> FontStyleRef

Font of size.

Source

pub fn text_width(&self, text: &str, font: FontStyleRef) -> f32

Width of text.

Source

pub fn line_height(&self, font: FontStyleRef) -> f32

Line height of font.

Source

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