Skip to main content

Chart

Struct Chart 

Source
pub struct Chart { /* private fields */ }
Expand description

A renderer-independent chart: panes stacked vertically over one time axis, each with its own series and y axis.

Push data as it arrives; layout turns the visible part into a Scene that a backend draws. Every change bumps revision.

Implementations§

Source§

impl Chart

Source

pub const MAIN: PaneId

The main pane, created with the chart.

Source

pub fn new() -> Chart

A chart with one auto-ranged pane, the dark theme, UTC labels and 100 000-sample series.

Examples found in repository?
examples/terminal_snapshot.rs (line 11)
10fn main() {
11    let mut chart = Chart::new();
12    let candles = chart.add_candles(Chart::MAIN, "XYZ").unwrap();
13    let volume = chart.add_pane(Pane { weight: 1.0, y_range: YRange::AutoFromZero, title: Some("Volume".into()) });
14    chart.add_volume(volume, candles).unwrap();
15    let mut price = 100.0f64;
16    for i in 0..90 {
17        let open = price;
18        price += ((i as f64) * 0.7).sin() * 1.5;
19        let (hi, lo) = (open.max(price) + 0.6, open.min(price) - 0.6);
20        chart.push_candle(candles, Candle::new(i * MINUTE, open, hi, lo, price, 20.0 + (i % 7) as f64 * 5.0)).unwrap();
21    }
22    chart.show_latest(60 * MINUTE);
23
24    let area = Rect::new(0, 0, 80, 24);
25    let mut buf = Buffer::empty(area);
26    ChartWidget::new(&chart).render(area, &mut buf);
27    for y in 0..area.height {
28        let line: String = (0..area.width).map(|x| buf[(x, y)].symbol().to_string()).collect();
29        println!("{}", line.trim_end());
30    }
31}
Source

pub fn revision(&self) -> u64

Bumped on every change, including viewport and style changes.

Source

pub fn theme(&self) -> &Theme

The theme.

Source

pub fn set_theme(&mut self, theme: Theme)

Changes the theme.

Source

pub fn time_zone(&self) -> TimeZone

The time zone used for time labels.

Source

pub fn set_time_zone(&mut self, tz: TimeZone)

Changes the time zone used for time labels.

Source

pub fn set_default_capacity(&mut self, capacity: usize)

The capacity given to series added from now on.

Source

pub fn add_pane(&mut self, pane: Pane) -> PaneId

Adds a pane below the existing ones, with relative height weight (the main pane has 3).

Examples found in repository?
examples/terminal_snapshot.rs (line 13)
10fn main() {
11    let mut chart = Chart::new();
12    let candles = chart.add_candles(Chart::MAIN, "XYZ").unwrap();
13    let volume = chart.add_pane(Pane { weight: 1.0, y_range: YRange::AutoFromZero, title: Some("Volume".into()) });
14    chart.add_volume(volume, candles).unwrap();
15    let mut price = 100.0f64;
16    for i in 0..90 {
17        let open = price;
18        price += ((i as f64) * 0.7).sin() * 1.5;
19        let (hi, lo) = (open.max(price) + 0.6, open.min(price) - 0.6);
20        chart.push_candle(candles, Candle::new(i * MINUTE, open, hi, lo, price, 20.0 + (i % 7) as f64 * 5.0)).unwrap();
21    }
22    chart.show_latest(60 * MINUTE);
23
24    let area = Rect::new(0, 0, 80, 24);
25    let mut buf = Buffer::empty(area);
26    ChartWidget::new(&chart).render(area, &mut buf);
27    for y in 0..area.height {
28        let line: String = (0..area.width).map(|x| buf[(x, y)].symbol().to_string()).collect();
29        println!("{}", line.trim_end());
30    }
31}
Source

pub fn pane(&self, id: PaneId) -> Option<&Pane>

A pane’s settings.

Source

pub fn pane_mut(&mut self, id: PaneId) -> Option<&mut Pane>

Edits a pane’s settings.

Source

pub fn pane_count(&self) -> usize

The number of panes.

Source

pub fn add_series( &mut self, pane: PaneId, name: impl Into<String>, mark: Mark, style: SeriesStyle, ) -> Result<SeriesId, ChartError>

Adds a series. Volume series take their data from candles via add_volume instead.

Source

pub fn add_line( &mut self, pane: PaneId, name: impl Into<String>, color: Rgba, ) -> Result<SeriesId, ChartError>

Adds a line series in color to pane.

Source

pub fn add_candles( &mut self, pane: PaneId, name: impl Into<String>, ) -> Result<SeriesId, ChartError>

Adds a candlestick series to pane, with a last-value tag.

Examples found in repository?
examples/terminal_snapshot.rs (line 12)
10fn main() {
11    let mut chart = Chart::new();
12    let candles = chart.add_candles(Chart::MAIN, "XYZ").unwrap();
13    let volume = chart.add_pane(Pane { weight: 1.0, y_range: YRange::AutoFromZero, title: Some("Volume".into()) });
14    chart.add_volume(volume, candles).unwrap();
15    let mut price = 100.0f64;
16    for i in 0..90 {
17        let open = price;
18        price += ((i as f64) * 0.7).sin() * 1.5;
19        let (hi, lo) = (open.max(price) + 0.6, open.min(price) - 0.6);
20        chart.push_candle(candles, Candle::new(i * MINUTE, open, hi, lo, price, 20.0 + (i % 7) as f64 * 5.0)).unwrap();
21    }
22    chart.show_latest(60 * MINUTE);
23
24    let area = Rect::new(0, 0, 80, 24);
25    let mut buf = Buffer::empty(area);
26    ChartWidget::new(&chart).render(area, &mut buf);
27    for y in 0..area.height {
28        let line: String = (0..area.width).map(|x| buf[(x, y)].symbol().to_string()).collect();
29        println!("{}", line.trim_end());
30    }
31}
Source

pub fn add_volume( &mut self, pane: PaneId, candles: SeriesId, ) -> Result<SeriesId, ChartError>

Adds volume bars for the candle series candles to pane (usually a pane of its own with YRange::AutoFromZero). The volume shares the candles’ data.

Examples found in repository?
examples/terminal_snapshot.rs (line 14)
10fn main() {
11    let mut chart = Chart::new();
12    let candles = chart.add_candles(Chart::MAIN, "XYZ").unwrap();
13    let volume = chart.add_pane(Pane { weight: 1.0, y_range: YRange::AutoFromZero, title: Some("Volume".into()) });
14    chart.add_volume(volume, candles).unwrap();
15    let mut price = 100.0f64;
16    for i in 0..90 {
17        let open = price;
18        price += ((i as f64) * 0.7).sin() * 1.5;
19        let (hi, lo) = (open.max(price) + 0.6, open.min(price) - 0.6);
20        chart.push_candle(candles, Candle::new(i * MINUTE, open, hi, lo, price, 20.0 + (i % 7) as f64 * 5.0)).unwrap();
21    }
22    chart.show_latest(60 * MINUTE);
23
24    let area = Rect::new(0, 0, 80, 24);
25    let mut buf = Buffer::empty(area);
26    ChartWidget::new(&chart).render(area, &mut buf);
27    for y in 0..area.height {
28        let line: String = (0..area.width).map(|x| buf[(x, y)].symbol().to_string()).collect();
29        println!("{}", line.trim_end());
30    }
31}
Source

pub fn series_ids(&self) -> impl Iterator<Item = SeriesId> + use<>

Every series id, in the order they were added.

Source

pub fn series_count(&self) -> usize

The number of series.

Source

pub fn name(&self, id: SeriesId) -> Option<&str>

A series’ name.

Source

pub fn mark(&self, id: SeriesId) -> Option<Mark>

How a series is drawn.

Source

pub fn style(&self, id: SeriesId) -> Option<&SeriesStyle>

A series’ style.

Source

pub fn set_style( &mut self, id: SeriesId, style: SeriesStyle, ) -> Result<(), ChartError>

Changes a series’ style.

Source

pub fn set_mark(&mut self, id: SeriesId, mark: Mark) -> Result<(), ChartError>

Changes the mark of a series between kinds that hold the same data (line, area and bars; or candles and OHLC).

Source

pub fn set_visible( &mut self, id: SeriesId, visible: bool, ) -> Result<(), ChartError>

Shows or hides a series.

Source

pub fn points(&self, id: SeriesId) -> Option<&Series<Point>>

A point series’ data.

Source

pub fn candles(&self, id: SeriesId) -> Option<&Series<Candle>>

A candle series’ data (also for a volume series, which shares it).

Source

pub fn points_mut( &mut self, id: SeriesId, ) -> Result<&mut Series<Point>, ChartError>

Edits a point series directly (bulk loads, capacity changes).

Source

pub fn candles_mut( &mut self, id: SeriesId, ) -> Result<&mut Series<Candle>, ChartError>

Edits a candle series directly.

Source

pub fn push_point( &mut self, id: SeriesId, point: Point, ) -> Result<Change, ChartError>

Appends a point (or replaces the newest one with the same time).

Source

pub fn push_candle( &mut self, id: SeriesId, candle: Candle, ) -> Result<Change, ChartError>

Appends a candle (or replaces the newest one with the same time).

Examples found in repository?
examples/terminal_snapshot.rs (line 20)
10fn main() {
11    let mut chart = Chart::new();
12    let candles = chart.add_candles(Chart::MAIN, "XYZ").unwrap();
13    let volume = chart.add_pane(Pane { weight: 1.0, y_range: YRange::AutoFromZero, title: Some("Volume".into()) });
14    chart.add_volume(volume, candles).unwrap();
15    let mut price = 100.0f64;
16    for i in 0..90 {
17        let open = price;
18        price += ((i as f64) * 0.7).sin() * 1.5;
19        let (hi, lo) = (open.max(price) + 0.6, open.min(price) - 0.6);
20        chart.push_candle(candles, Candle::new(i * MINUTE, open, hi, lo, price, 20.0 + (i % 7) as f64 * 5.0)).unwrap();
21    }
22    chart.show_latest(60 * MINUTE);
23
24    let area = Rect::new(0, 0, 80, 24);
25    let mut buf = Buffer::empty(area);
26    ChartWidget::new(&chart).render(area, &mut buf);
27    for y in 0..area.height {
28        let line: String = (0..area.width).map(|x| buf[(x, y)].symbol().to_string()).collect();
29        println!("{}", line.trim_end());
30    }
31}
Source

pub fn apply_trade( &mut self, id: SeriesId, time: Timestamp, price: f64, size: f64, interval_ms: i64, ) -> Result<Change, ChartError>

Folds a trade into a candle series with interval_ms bars.

Source

pub fn set_points( &mut self, id: SeriesId, points: Vec<Point>, ) -> Result<(), ChartError>

Replaces a point series’ contents (history loads); sorts by time.

Source

pub fn set_candles( &mut self, id: SeriesId, candles: Vec<Candle>, ) -> Result<(), ChartError>

Replaces a candle series’ contents (history loads); sorts by time.

Source

pub fn latest_time(&self) -> Option<Timestamp>

The newest sample time across all series.

Source

pub fn earliest_time(&self) -> Option<Timestamp>

The oldest sample time across all series.

Source

pub fn viewport(&self) -> &Viewport

The viewport.

Source

pub fn viewport_mut(&mut self) -> &mut Viewport

Edits the viewport directly.

Source

pub fn show_latest(&mut self, span: i64)

Shows the last span milliseconds of data and follows new data.

Examples found in repository?
examples/terminal_snapshot.rs (line 22)
10fn main() {
11    let mut chart = Chart::new();
12    let candles = chart.add_candles(Chart::MAIN, "XYZ").unwrap();
13    let volume = chart.add_pane(Pane { weight: 1.0, y_range: YRange::AutoFromZero, title: Some("Volume".into()) });
14    chart.add_volume(volume, candles).unwrap();
15    let mut price = 100.0f64;
16    for i in 0..90 {
17        let open = price;
18        price += ((i as f64) * 0.7).sin() * 1.5;
19        let (hi, lo) = (open.max(price) + 0.6, open.min(price) - 0.6);
20        chart.push_candle(candles, Candle::new(i * MINUTE, open, hi, lo, price, 20.0 + (i % 7) as f64 * 5.0)).unwrap();
21    }
22    chart.show_latest(60 * MINUTE);
23
24    let area = Rect::new(0, 0, 80, 24);
25    let mut buf = Buffer::empty(area);
26    ChartWidget::new(&chart).render(area, &mut buf);
27    for y in 0..area.height {
28        let line: String = (0..area.width).map(|x| buf[(x, y)].symbol().to_string()).collect();
29        println!("{}", line.trim_end());
30    }
31}
Source

pub fn fit_all(&mut self)

Shows all data.

Source

pub fn pan_pixels(&mut self, dx: f64, width: f64)

Pans for a drag of dx pixels across a plot width pixels wide (dragging right shows earlier data). Panning back to the newest data resumes following.

Source

pub fn zoom_at(&mut self, factor: f64, x: f64, width: f64)

Zooms by factor (below 1 zooms in) around pixel x of a plot width pixels wide. While following, the newest data stays at the right edge.

Source§

impl Chart

Source

pub fn layout(&self, size: Size, metrics: &Metrics) -> Scene

Lays out the visible data for a surface of size, with text measured by metrics.

Trait Implementations§

Source§

impl Clone for Chart

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Chart

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Chart

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl Forget for Chart

§

impl Freeze for Chart

§

impl Move for Chart

§

impl RefUnwindSafe for Chart

§

impl Send for Chart

§

impl Sync for Chart

§

impl Unpin for Chart

§

impl UnsafeUnpin for Chart

§

impl UnwindSafe for Chart

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.