pub trait LayerPlugin: Send {
// Required methods
fn position(&self) -> LayerPosition;
fn build(&self, ctx: &LayerCtx<'_>, out: &mut FrameBuilder<'_>);
}Expand description
Extra drawing on a pane or on the whole chart.
build runs in every frame and must not allocate once warmed up (the
frame’s pools keep their capacity). When the plugin’s own content
changes, call crate::Chart::invalidate_layer.
§Examples
use ferrochart_core::drawing::{FrameBuilder, Label};
use ferrochart_core::frame::TextAlign;
use ferrochart_core::geom::{Css, PointPx, Rgba};
use ferrochart_core::plugin::{LayerCtx, LayerPlugin, LayerPosition};
use ferrochart_core::testing::MonoMeasurer;
use ferrochart_core::{Chart, ChartOptions};
struct Watermark;
impl LayerPlugin for Watermark {
fn position(&self) -> LayerPosition {
LayerPosition::Background
}
fn build(&self, ctx: &LayerCtx<'_>, out: &mut FrameBuilder<'_>) {
let r = ctx.rect();
let center = PointPx { x: r.x + r.w / 2.0, y: r.y + r.h / 2.0 };
let label = Label {
font: ctx.font(Css(48.0)),
color: Rgba::hex(0x80_8080).with_alpha(0x40),
align: TextAlign::Center,
bg: None,
};
out.text(center, "BTCUSD", label);
}
}
let mut chart = Chart::new(ChartOptions::default());
let id = chart.add_layer(None, Box::new(Watermark))?;
chart.build_frame(0.0, &MonoMeasurer);
chart.remove_layer(id)?;Required Methods§
Sourcefn position(&self) -> LayerPosition
fn position(&self) -> LayerPosition
Where it draws.
Sourcefn build(&self, ctx: &LayerCtx<'_>, out: &mut FrameBuilder<'_>)
fn build(&self, ctx: &LayerCtx<'_>, out: &mut FrameBuilder<'_>)
Writes the primitives (clipped to LayerCtx::rect).
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".