Skip to main content

densa_chart/
model.rs

1use densa_draw::Canvas;
2
3use crate::{Axis, AxisKind, ChartPalette, Plot};
4
5/// The data of a chart widget (`densa_widgets::Chart`): panes stacked top to bottom on one
6/// horizontal axis, each drawing its own series (candles over a line, volume bars below).
7///
8/// The widget decides what part of `x` shows (it follows the newest data until the user
9/// pans or zooms) and asks each pane for its vertical axis over that part, so panes scale to
10/// what is on screen. Painting goes through the series functions of this crate, which
11/// allocate nothing.
12pub trait ChartModel {
13    /// What the horizontal axis counts.
14    fn x_kind(&self) -> AxisKind {
15        AxisKind::Time
16    }
17
18    /// The lowest and highest `x` of the data, or `None` while there is none.
19    fn x_extent(&self) -> Option<(f64, f64)>;
20
21    /// The distance between two points (a candle's period), if the data has one: the
22    /// widget zooms in no further than a few of them.
23    fn x_step(&self) -> Option<f64> {
24        None
25    }
26
27    /// How many panes, top to bottom (one is drawn even for zero).
28    fn panes(&self) -> usize {
29        1
30    }
31
32    /// Pane `pane`'s share of the height, against the others' (above zero).
33    fn pane_weight(&self, pane: usize) -> f32 {
34        let _ = pane;
35        1.0
36    }
37
38    /// The vertical axis of pane `pane` while `x` shows: what its series need, with room
39    /// around them (see [`Axis::padded`]); `None` if it has nothing there.
40    fn y_axis(&self, pane: usize, x: &Axis) -> Option<Axis>;
41
42    /// Draws the series of pane `pane` onto `plot`, in `palette`'s colors.
43    fn paint(&self, pane: usize, plot: &Plot, canvas: &mut Canvas<'_>, palette: &ChartPalette);
44
45    /// Called before every frame is painted, to catch up with new data. Does nothing by
46    /// default.
47    fn prepare(&mut self) {}
48
49    /// What the chart shows, for screen readers ("ACME, one-minute candles, last 182.40").
50    /// Asked only while one is listening. Empty by default.
51    fn describe(&self) -> String {
52        String::new()
53    }
54}