Skip to main content

Module primitives

Module primitives 

Source
Expand description

Custom drawing attached to a series: primitives, and the renderers of custom series.

A Primitive is host code that the chart calls while it draws a frame. It is attached to a series with Chart::attach_primitive and draws in the pane of that series, on its price scale. A primitive can draw behind the series, above them, or above the drawings as well (PrimitivePass), draw inside the price and time axes and put labels on them, widen the price range its scale fits, take part in hit testing with its own pointer cursor, and follow the bars of its series through lifecycle hooks.

A custom series is a study that supplies the values (legend, scale, crosshair and last-value tag) together with a primitive that draws its points in place of the study’s plots and hit tests them: Chart::add_custom_series.

Every hook has a default that does nothing, so a primitive implements only what it needs. examples/primitives.rs shades the regular session behind the bars with labels on the time axis, and adds a custom series that draws the range of each bar in a pane of its own.

use egui::{Color32, Painter};
use egui_charts::chart::primitives::{Primitive, PrimitivePass, PrimitiveView};
use egui_charts::widget::{Chart, SeriesId};

/// Shades the bars whose close is above `level`.
struct AboveLevel {
    level: f64,
}

impl Primitive for AboveLevel {
    fn draw(&self, painter: &Painter, view: &PrimitiveView<'_>, pass: PrimitivePass) {
        if pass != PrimitivePass::Below {
            return;
        }
        let rect = view.mapping.rect;
        let half = view.mapping.bar_spacing / 2.0;
        for index in view.visible.clone() {
            if view.bars[index].close > self.level {
                let x = view.index_to_x(index as f64);
                painter.rect_filled(
                    egui::Rect::from_x_y_ranges(x - half..=x + half, rect.y_range()),
                    0.0,
                    Color32::from_rgba_unmultiplied(41, 98, 255, 30),
                );
            }
        }
    }
}

let id = chart.attach_primitive(SeriesId::MAIN, AboveLevel { level: 100.0 });

Structs§

PriceAxisLabel
A label a primitive puts on the price axis of its pane, on a tag in color.
PrimitiveId
Identifies a primitive attached to a chart, for Chart::detach_primitive and pointer targets.
PrimitiveView
The pane and scale a primitive draws on in the current frame.
TimeAxisLabel
A label a primitive puts on the time axis, on a tag in color.

Enums§

PrimitivePass
When a primitive draws in its pane.

Traits§

Primitive
Host drawing attached to a series, called by the chart while it draws each frame.