Skip to main content

CustomSeries

Trait CustomSeries 

Source
pub trait CustomSeries: Send + Any {
    // Required methods
    fn kind(&self) -> &'static str;
    fn axis_mode(&self) -> AxisMode;
    fn times(&self) -> &[Timestamp];
    fn extremes(&self, from: usize, to: usize) -> Option<(f64, f64)>;
    fn build(
        &self,
        ctx: &SeriesCtx<'_>,
        from: usize,
        to: usize,
        out: &mut FrameBuilder<'_>,
    );

    // Provided methods
    fn last_value(&self) -> Option<f64> { ... }
    fn crosshair_value(&self, point: usize) -> Option<f64> { ... }
    fn native_params(&self, ctx: &SeriesCtx<'_>, params: &mut [f32; 8]) { ... }
}
Expand description

A series that owns its data.

Core asks it for its times and extremes and lets it draw the visible points; the data is changed through crate::Chart::update_custom, which tells core what changed. Point i is element i of times.

§Examples

use ferrochart_core::data::{DataChange, Timestamp};
use ferrochart_core::drawing::FrameBuilder;
use ferrochart_core::geom::RectPx;
use ferrochart_core::plugin::{AxisMode, CustomSeries, MinMaxIndex, SeriesCtx};
use ferrochart_core::chart::SeriesOptions;
use ferrochart_core::testing::MonoMeasurer;
use ferrochart_core::{Chart, ChartOptions};

/// A vertical bar from `lo` to `hi` per point.
#[derive(Default)]
struct Ranges {
    time: Vec<Timestamp>,
    range: Vec<(f64, f64)>,
    index: MinMaxIndex,
}

impl Ranges {
    fn push(&mut self, t: Timestamp, lo: f64, hi: f64) -> DataChange {
        self.time.push(t);
        self.range.push((lo, hi));
        let _ = self.index.push(lo, hi);
        DataChange::Append { count: 1 }
    }
}

impl CustomSeries for Ranges {
    fn kind(&self) -> &'static str {
        "example.ranges"
    }
    fn axis_mode(&self) -> AxisMode {
        AxisMode::Slots
    }
    fn times(&self) -> &[Timestamp] {
        &self.time
    }
    fn extremes(&self, from: usize, to: usize) -> Option<(f64, f64)> {
        self.index.query(from, to)
    }
    fn build(&self, ctx: &SeriesCtx<'_>, from: usize, to: usize, out: &mut FrameBuilder<'_>) {
        let w = (ctx.bar_spacing().0 * 0.6).max(1.0);
        for i in from..to {
            let (lo, hi) = self.range[i];
            let (Some(x), Some(y0), Some(y1)) =
                (ctx.x_of_point(i), ctx.coords().y_of(hi), ctx.coords().y_of(lo))
            else {
                continue;
            };
            let rect = RectPx { x: x - w / 2.0, y: y0, w, h: (y1 - y0).max(1.0) };
            out.rect(rect, ctx.style().up);
        }
    }
}

let mut chart = Chart::new(ChartOptions::default());
let id = chart.add_custom_series(
    chart.main_pane(),
    Box::new(Ranges::default()),
    SeriesOptions::default(),
)?;
chart.update_custom(id, |r: &mut Ranges| r.push(60_000, 1.0, 2.0))?;
chart.update_custom(id, |r: &mut Ranges| r.push(120_000, 1.5, 3.0))?;
assert_eq!(chart.custom_series::<Ranges>(id).map(|r| r.time.len()), Some(2));
chart.build_frame(0.0, &MonoMeasurer);

Required Methods§

Source

fn kind(&self) -> &'static str

Kind name, e.g. "orderflow.heatmap"; renderers that draw it natively declare this name.

Source

fn axis_mode(&self) -> AxisMode

How the points are placed on the time axis; read once, when added.

Source

fn times(&self) -> &[Timestamp]

Times of the points, strictly increasing.

Source

fn extremes(&self, from: usize, to: usize) -> Option<(f64, f64)>

Price extremes of points from..to, for auto scaling. Called every frame, so it must be O(log n) (see crate::plugin::MinMaxIndex).

Source

fn build( &self, ctx: &SeriesCtx<'_>, from: usize, to: usize, out: &mut FrameBuilder<'_>, )

Portable path: writes the primitives of points from..to (the visible ones and one more on each side). Must not allocate once warmed up.

Provided Methods§

Source

fn last_value(&self) -> Option<f64>

Value of the last-value label; None hides it.

Source

fn crosshair_value(&self, point: usize) -> Option<f64>

Value of point point for the crosshair and magnet.

Source

fn native_params(&self, ctx: &SeriesCtx<'_>, params: &mut [f32; 8])

Parameters of the native GPU path, e.g. a color-scale range.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§