Skip to main content

egui_charts/model/
time_scale_event.rs

1//! Host-supplied events displayed beside the time axis.
2
3use chrono::{DateTime, Utc};
4use serde::{Deserialize, Serialize};
5
6/// Category of a host-supplied time-axis event.
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
8#[non_exhaustive]
9pub enum EventKind {
10    /// Company earnings announcement.
11    Earnings,
12    /// Cash or stock dividend.
13    Dividend,
14    /// Stock split or reverse split.
15    Split,
16    /// Economic release or scheduled announcement.
17    Economic,
18    /// News headline.
19    News,
20    /// Trading idea published by the host.
21    Idea,
22}
23
24impl EventKind {
25    /// Short label drawn inside the event badge.
26    pub fn label(self) -> &'static str {
27        match self {
28            Self::Earnings => "E",
29            Self::Dividend => "D",
30            Self::Split => "S",
31            Self::Economic => "Ec",
32            Self::News => "N",
33            Self::Idea => "I",
34        }
35    }
36}
37
38/// An event supplied by the host, which owns its data and any external service.
39#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
40pub struct TimeScaleEvent {
41    /// Stable host identity, returned by the chart when the badge is clicked.
42    pub id: String,
43    /// Scheduled or actual event time in UTC.
44    pub time: DateTime<Utc>,
45    /// Category used by the event visibility controls.
46    pub kind: EventKind,
47    /// Headline shown in the hover popup.
48    pub title: String,
49    /// Host-formatted details shown beneath the headline, including any units.
50    pub details: String,
51}
52
53/// Visibility of each time-axis event category.
54#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
55#[serde(default)]
56pub struct EventVisibility {
57    /// Show dividend events.
58    pub show_dividends: bool,
59    /// Show stock splits.
60    pub show_splits: bool,
61    /// Show earnings announcements.
62    pub show_earnings: bool,
63    /// Draw a dashed vertical break at each earnings announcement.
64    pub show_earnings_breaks: bool,
65    /// Show economic releases.
66    pub show_economic_events: bool,
67    /// Show news headlines.
68    pub show_news: bool,
69    /// Ask the host to raise a notification for each news headline it supplies. The host owns
70    /// delivery; the chart does not read this.
71    pub show_news_notifications: bool,
72    /// Show trading ideas the host supplies.
73    pub show_ideas: bool,
74}
75
76impl Default for EventVisibility {
77    fn default() -> Self {
78        Self {
79            show_dividends: true,
80            show_splits: true,
81            show_earnings: false,
82            show_earnings_breaks: false,
83            show_economic_events: false,
84            show_news: true,
85            show_news_notifications: false,
86            show_ideas: true,
87        }
88    }
89}
90
91impl EventVisibility {
92    /// Whether events in this category are displayed.
93    pub fn shows(&self, kind: EventKind) -> bool {
94        match kind {
95            EventKind::Dividend => self.show_dividends,
96            EventKind::Split => self.show_splits,
97            EventKind::Earnings => self.show_earnings,
98            EventKind::Economic => self.show_economic_events,
99            EventKind::News => self.show_news,
100            EventKind::Idea => self.show_ideas,
101        }
102    }
103}