use std::fmt;
use crate::color::Rgba;
use crate::series::{Candle, Change, OutOfOrder, Point, Series, fold_extent};
use crate::time::{TimeZone, Timestamp};
use crate::viewport::Viewport;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct SeriesId(pub(crate) usize);
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct PaneId(pub(crate) usize);
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Mark {
Line,
Area,
Bars,
Candles,
Ohlc,
Volume,
}
impl Mark {
fn holds_points(self) -> bool {
matches!(self, Mark::Line | Mark::Area | Mark::Bars)
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum YRange {
Auto,
AutoFromZero,
Fixed(f64, f64),
}
#[derive(Clone, Debug, PartialEq)]
pub struct Pane {
pub weight: f32,
pub y_range: YRange,
pub title: Option<String>,
}
impl Pane {
pub fn new(weight: f32) -> Pane {
Pane { weight, y_range: YRange::Auto, title: None }
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct SeriesStyle {
pub color: Rgba,
pub width: f32,
pub show_last_value: bool,
}
impl SeriesStyle {
pub fn new(color: Rgba) -> SeriesStyle {
SeriesStyle { color, width: 1.5, show_last_value: false }
}
pub fn with_last_value(mut self) -> SeriesStyle {
self.show_last_value = true;
self
}
pub fn with_width(mut self, width: f32) -> SeriesStyle {
self.width = width;
self
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Theme {
pub background: Rgba,
pub grid: Rgba,
pub axis: Rgba,
pub text: Rgba,
pub up: Rgba,
pub down: Rgba,
pub tag_text: Rgba,
}
impl Theme {
pub const fn dark() -> Theme {
Theme {
background: Rgba::TRANSPARENT,
grid: Rgba::hex(0x2a2e39),
axis: Rgba::hex(0x50535e),
text: Rgba::hex(0xb2b5be),
up: Rgba::hex(0x26a69a),
down: Rgba::hex(0xef5350),
tag_text: Rgba::hex(0x101114),
}
}
pub const fn light() -> Theme {
Theme {
background: Rgba::hex(0xffffff),
grid: Rgba::hex(0xe6e8ee),
axis: Rgba::hex(0xb4b8c2),
text: Rgba::hex(0x3b3f4a),
up: Rgba::hex(0x089981),
down: Rgba::hex(0xf23645),
tag_text: Rgba::hex(0xffffff),
}
}
}
impl Default for Theme {
fn default() -> Self {
Theme::dark()
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ChartError {
UnknownSeries(SeriesId),
UnknownPane(PaneId),
WrongKind(SeriesId),
OutOfOrder(OutOfOrder),
}
impl fmt::Display for ChartError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
ChartError::UnknownSeries(id) => write!(f, "unknown series {}", id.0),
ChartError::UnknownPane(id) => write!(f, "unknown pane {}", id.0),
ChartError::WrongKind(id) => write!(f, "series {} holds a different kind of data", id.0),
ChartError::OutOfOrder(e) => e.fmt(f),
}
}
}
impl std::error::Error for ChartError {}
impl From<OutOfOrder> for ChartError {
fn from(e: OutOfOrder) -> Self {
ChartError::OutOfOrder(e)
}
}
#[derive(Clone, Debug)]
pub(crate) enum Data {
Points(Series<Point>),
Candles(Series<Candle>),
VolumeOf(SeriesId),
}
#[derive(Clone, Debug)]
pub(crate) struct Entry {
pub(crate) name: String,
pub(crate) pane: PaneId,
pub(crate) mark: Mark,
pub(crate) style: SeriesStyle,
pub(crate) data: Data,
pub(crate) visible: bool,
}
#[derive(Clone, Debug)]
pub struct Chart {
pub(crate) panes: Vec<Pane>,
pub(crate) entries: Vec<Entry>,
pub(crate) viewport: Viewport,
pub(crate) theme: Theme,
pub(crate) tz: TimeZone,
capacity: usize,
revision: u64,
}
impl Default for Chart {
fn default() -> Self {
Chart::new()
}
}
impl Chart {
pub const MAIN: PaneId = PaneId(0);
pub fn new() -> Chart {
Chart {
panes: vec![Pane::new(3.0)],
entries: Vec::new(),
viewport: Viewport::default(),
theme: Theme::default(),
tz: TimeZone::Utc,
capacity: 100_000,
revision: 0,
}
}
fn touch(&mut self) {
self.revision += 1;
}
pub fn revision(&self) -> u64 {
self.revision
}
pub fn theme(&self) -> &Theme {
&self.theme
}
pub fn set_theme(&mut self, theme: Theme) {
self.theme = theme;
self.touch();
}
pub fn time_zone(&self) -> TimeZone {
self.tz
}
pub fn set_time_zone(&mut self, tz: TimeZone) {
self.tz = tz;
self.touch();
}
pub fn set_default_capacity(&mut self, capacity: usize) {
self.capacity = capacity.max(1);
}
pub fn add_pane(&mut self, pane: Pane) -> PaneId {
self.panes.push(pane);
self.touch();
PaneId(self.panes.len() - 1)
}
pub fn pane(&self, id: PaneId) -> Option<&Pane> {
self.panes.get(id.0)
}
pub fn pane_mut(&mut self, id: PaneId) -> Option<&mut Pane> {
self.touch();
self.panes.get_mut(id.0)
}
pub fn pane_count(&self) -> usize {
self.panes.len()
}
pub fn add_series(&mut self, pane: PaneId, name: impl Into<String>, mark: Mark, style: SeriesStyle) -> Result<SeriesId, ChartError> {
if pane.0 >= self.panes.len() {
return Err(ChartError::UnknownPane(pane));
}
let data = match mark {
Mark::Line | Mark::Area | Mark::Bars => Data::Points(Series::with_capacity(self.capacity)),
Mark::Candles | Mark::Ohlc => Data::Candles(Series::with_capacity(self.capacity)),
Mark::Volume => return Err(ChartError::WrongKind(SeriesId(self.entries.len()))),
};
Ok(self.push_entry(Entry { name: name.into(), pane, mark, style, data, visible: true }))
}
fn push_entry(&mut self, entry: Entry) -> SeriesId {
self.entries.push(entry);
self.touch();
SeriesId(self.entries.len() - 1)
}
pub fn add_line(&mut self, pane: PaneId, name: impl Into<String>, color: Rgba) -> Result<SeriesId, ChartError> {
self.add_series(pane, name, Mark::Line, SeriesStyle::new(color))
}
pub fn add_candles(&mut self, pane: PaneId, name: impl Into<String>) -> Result<SeriesId, ChartError> {
self.add_series(pane, name, Mark::Candles, SeriesStyle::new(self.theme.text).with_last_value())
}
pub fn add_volume(&mut self, pane: PaneId, candles: SeriesId) -> Result<SeriesId, ChartError> {
if pane.0 >= self.panes.len() {
return Err(ChartError::UnknownPane(pane));
}
match self.entries.get(candles.0).map(|e| &e.data) {
Some(Data::Candles(_)) => {}
Some(_) => return Err(ChartError::WrongKind(candles)),
None => return Err(ChartError::UnknownSeries(candles)),
}
let name = format!("{} volume", self.entries[candles.0].name);
let style = SeriesStyle::new(self.theme.text);
Ok(self.push_entry(Entry { name, pane, mark: Mark::Volume, style, data: Data::VolumeOf(candles), visible: true }))
}
pub fn series_ids(&self) -> impl Iterator<Item = SeriesId> + use<> {
(0..self.entries.len()).map(SeriesId)
}
pub fn series_count(&self) -> usize {
self.entries.len()
}
pub fn name(&self, id: SeriesId) -> Option<&str> {
self.entries.get(id.0).map(|e| e.name.as_str())
}
pub fn mark(&self, id: SeriesId) -> Option<Mark> {
self.entries.get(id.0).map(|e| e.mark)
}
pub fn style(&self, id: SeriesId) -> Option<&SeriesStyle> {
self.entries.get(id.0).map(|e| &e.style)
}
pub fn set_style(&mut self, id: SeriesId, style: SeriesStyle) -> Result<(), ChartError> {
let e = self.entries.get_mut(id.0).ok_or(ChartError::UnknownSeries(id))?;
e.style = style;
self.touch();
Ok(())
}
pub fn set_mark(&mut self, id: SeriesId, mark: Mark) -> Result<(), ChartError> {
let e = self.entries.get_mut(id.0).ok_or(ChartError::UnknownSeries(id))?;
let ok = match e.data {
Data::Points(_) => mark.holds_points(),
Data::Candles(_) => matches!(mark, Mark::Candles | Mark::Ohlc),
Data::VolumeOf(_) => mark == Mark::Volume,
};
if !ok {
return Err(ChartError::WrongKind(id));
}
e.mark = mark;
self.touch();
Ok(())
}
pub fn set_visible(&mut self, id: SeriesId, visible: bool) -> Result<(), ChartError> {
let e = self.entries.get_mut(id.0).ok_or(ChartError::UnknownSeries(id))?;
e.visible = visible;
self.touch();
Ok(())
}
pub fn points(&self, id: SeriesId) -> Option<&Series<Point>> {
match &self.entries.get(id.0)?.data {
Data::Points(s) => Some(s),
_ => None,
}
}
pub fn candles(&self, id: SeriesId) -> Option<&Series<Candle>> {
match &self.entries.get(id.0)?.data {
Data::Candles(s) => Some(s),
Data::VolumeOf(src) => self.candles(*src),
_ => None,
}
}
fn points_series_mut(&mut self, id: SeriesId) -> Result<&mut Series<Point>, ChartError> {
match &mut self.entries.get_mut(id.0).ok_or(ChartError::UnknownSeries(id))?.data {
Data::Points(s) => Ok(s),
_ => Err(ChartError::WrongKind(id)),
}
}
fn candle_series_mut(&mut self, id: SeriesId) -> Result<&mut Series<Candle>, ChartError> {
match &mut self.entries.get_mut(id.0).ok_or(ChartError::UnknownSeries(id))?.data {
Data::Candles(s) => Ok(s),
_ => Err(ChartError::WrongKind(id)),
}
}
pub fn points_mut(&mut self, id: SeriesId) -> Result<&mut Series<Point>, ChartError> {
self.touch();
self.points_series_mut(id)
}
pub fn candles_mut(&mut self, id: SeriesId) -> Result<&mut Series<Candle>, ChartError> {
self.touch();
self.candle_series_mut(id)
}
fn after_data_change(&mut self) {
self.touch();
if self.viewport.is_following()
&& let Some(latest) = self.latest_time()
{
self.viewport.follow_to(latest);
}
}
pub fn push_point(&mut self, id: SeriesId, point: Point) -> Result<Change, ChartError> {
let change = self.points_series_mut(id)?.push(point)?;
self.after_data_change();
Ok(change)
}
pub fn push_candle(&mut self, id: SeriesId, candle: Candle) -> Result<Change, ChartError> {
let change = self.candle_series_mut(id)?.push(candle)?;
self.after_data_change();
Ok(change)
}
pub fn apply_trade(&mut self, id: SeriesId, time: Timestamp, price: f64, size: f64, interval_ms: i64) -> Result<Change, ChartError> {
let change = self.candle_series_mut(id)?.apply_trade(time, price, size, interval_ms)?;
self.after_data_change();
Ok(change)
}
pub fn set_points(&mut self, id: SeriesId, points: Vec<Point>) -> Result<(), ChartError> {
self.points_series_mut(id)?.replace_all(points);
self.after_data_change();
Ok(())
}
pub fn set_candles(&mut self, id: SeriesId, candles: Vec<Candle>) -> Result<(), ChartError> {
self.candle_series_mut(id)?.replace_all(candles);
self.after_data_change();
Ok(())
}
pub fn latest_time(&self) -> Option<Timestamp> {
self.entries
.iter()
.filter_map(|e| match &e.data {
Data::Points(s) => s.last().map(|p| p.time),
Data::Candles(s) => s.last().map(|c| c.time + s.interval().unwrap_or(0)),
Data::VolumeOf(_) => None,
})
.max()
}
pub fn earliest_time(&self) -> Option<Timestamp> {
self.entries
.iter()
.filter_map(|e| match &e.data {
Data::Points(s) => s.first().map(|p| p.time),
Data::Candles(s) => s.first().map(|c| c.time),
Data::VolumeOf(_) => None,
})
.min()
}
pub fn viewport(&self) -> &Viewport {
&self.viewport
}
pub fn viewport_mut(&mut self) -> &mut Viewport {
self.touch();
&mut self.viewport
}
pub fn show_latest(&mut self, span: i64) {
let end = self.latest_time().unwrap_or(0);
self.viewport.set_range(end - span, end);
self.viewport.set_following(true);
self.viewport.follow_to(end);
self.touch();
}
pub fn fit_all(&mut self) {
if let (Some(a), Some(b)) = (self.earliest_time(), self.latest_time()) {
self.viewport.set_range(a, b.max(a + 1));
self.viewport.set_following(true);
self.viewport.follow_to(b);
}
self.touch();
}
fn refollow_if_at_edge(&mut self) {
if let Some(latest) = self.latest_time()
&& self.viewport.end() >= latest
{
self.viewport.set_following(true);
self.viewport.follow_to(latest);
}
}
pub fn pan_pixels(&mut self, dx: f64, width: f64) {
self.viewport.pan_pixels(dx, width);
self.refollow_if_at_edge();
self.touch();
}
pub fn zoom_at(&mut self, factor: f64, x: f64, width: f64) {
if self.viewport.is_following() {
self.viewport.zoom(factor, 1.0);
if let Some(latest) = self.latest_time() {
self.viewport.follow_to(latest);
}
} else {
self.viewport.zoom_at_pixel(factor, x, width);
self.refollow_if_at_edge();
}
self.touch();
}
pub(crate) fn entry_extent(&self, e: &Entry, start: Timestamp, end: Timestamp) -> Option<(f64, f64)> {
match &e.data {
Data::Points(s) => {
let ext = s.extent(start, end)?;
Some(if e.mark == Mark::Bars { (ext.0.min(0.0), ext.1.max(0.0)) } else { ext })
}
Data::Candles(s) => s.extent(start, end),
Data::VolumeOf(src) => {
let s = self.candles(*src)?;
fold_extent(s.range(start, end).map(|c| (0.0, c.volume)))
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn kinds_are_checked() {
let mut c = Chart::new();
let line = c.add_line(Chart::MAIN, "price", Rgba::hex(0xffffff)).unwrap();
let candles = c.add_candles(Chart::MAIN, "BTC").unwrap();
assert_eq!(c.push_candle(line, Candle::from_trade(0, 1.0, 1.0)), Err(ChartError::WrongKind(line)));
assert_eq!(c.push_point(candles, Point::new(0, 1.0)), Err(ChartError::WrongKind(candles)));
assert_eq!(c.add_volume(Chart::MAIN, line), Err(ChartError::WrongKind(line)));
assert_eq!(c.add_line(PaneId(9), "x", Rgba::hex(0)), Err(ChartError::UnknownPane(PaneId(9))));
let vol = c.add_volume(Chart::MAIN, candles).unwrap();
assert_eq!(c.name(vol), Some("BTC volume"));
assert_eq!(c.set_mark(line, Mark::Candles), Err(ChartError::WrongKind(line)));
c.set_mark(line, Mark::Area).unwrap();
c.push_candle(candles, Candle::new(60, 1.0, 2.0, 0.5, 1.5, 7.0)).unwrap();
assert_eq!(c.candles(vol).unwrap().len(), 1);
}
#[test]
fn live_data_scrolls_while_following() {
let mut c = Chart::new();
let line = c.add_line(Chart::MAIN, "p", Rgba::hex(0xffffff)).unwrap();
c.viewport_mut().set_right_margin(0.0);
c.show_latest(1000);
for t in 0..=5000 {
c.push_point(line, Point::new(t, t as f64)).unwrap();
}
assert_eq!((c.viewport().start(), c.viewport().end()), (4000, 5000));
c.pan_pixels(100.0, 1000.0);
assert!(!c.viewport().is_following());
c.push_point(line, Point::new(5001, 0.0)).unwrap();
assert_eq!(c.viewport().end(), 4900);
c.pan_pixels(-200.0, 1000.0);
assert!(c.viewport().is_following());
assert_eq!(c.viewport().end(), 5001);
}
#[test]
fn revision_bumps() {
let mut c = Chart::new();
let r0 = c.revision();
let id = c.add_line(Chart::MAIN, "p", Rgba::hex(0)).unwrap();
c.push_point(id, Point::new(1, 1.0)).unwrap();
assert!(c.revision() > r0);
}
}