egui_charts/config/timescale.rs
1//! Time-scale (X-axis) options.
2//!
3//! [`TimeScaleOptions`] configures bar spacing, scroll constraints, edge
4//! fixing, crosshair index eligibility, time-label visibility and spacing, the axis size and
5//! border, and the [`TimezoneMode`] used to convert UTC timestamps before rendering axis labels.
6
7use crate::scales::TimezoneMode;
8use egui::Color32;
9
10/// Time Scale Options.
11/// Controls bar spacing, scrolling constraints, and time display.
12#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
13pub struct TimeScaleOptions {
14 /// The margin space in bars from the right side of the chart, kept
15 /// whenever the view returns to the live edge.
16 /// Default: 2.5
17 pub right_offset: f32,
18
19 /// The margin space in pixels from the right side of the chart.
20 /// This option has priority over right_offset.
21 /// Default: None
22 pub right_offset_pixels: Option<f32>,
23
24 /// The space between bars in pixels.
25 /// Default: 6.0
26 pub bar_spacing: f32,
27
28 /// The min space between bars in pixels, floored at 0.000001 even when set to zero.
29 /// Non-finite values use the default.
30 /// Default: 0.5
31 pub min_bar_spacing: f32,
32
33 /// The max space between bars in pixels.
34 /// Non-positive or non-finite values are unlimited; positive limits have a 0.000001 floor.
35 /// Default: 0.0
36 pub max_bar_spacing: f32,
37
38 /// Prevent scrolling to the left of the first bar: the view stops with the first bar at the
39 /// left edge, and when every bar fits the empty space is on the right.
40 /// Default: false
41 pub fix_left_edge: bool,
42
43 /// Prevent scrolling to the right of the most recent bar.
44 /// Default: false
45 pub fix_right_edge: bool,
46
47 /// Prevent changing the visible time range during chart resizing.
48 /// Default: false
49 pub lock_visible_time_range_on_resize: bool,
50
51 /// Keep the right offset when zooming with the mouse wheel, a pinch or the keyboard, so the
52 /// bar at the right edge stays in place instead of the bar under the pointer.
53 /// Default: false
54 pub right_bar_stays_on_scroll: bool,
55
56 /// Shift the visible range to the right (into the future) by the number of new bars when new data is added.
57 /// Note that this only applies when the last bar is visible.
58 /// Default: true
59 pub shift_visible_range_on_new_bar: bool,
60
61 /// Allow the visible range to be shifted to the right when a new bar is added which
62 /// is replacing an existing whitespace time point on the chart.
63 /// Default: false
64 pub allow_shift_visible_range_on_whitespace_replacement: bool,
65
66 /// Exclude slots with no values in any bar series or study plot from crosshair selection.
67 /// The nearest eligible slot is selected; equal distances prefer the later slot.
68 /// Stored data and time-axis marks are unchanged. Default: false.
69 #[serde(default)]
70 pub ignore_whitespace_indices: bool,
71
72 /// Exclude slots whose values belong only to hidden series or study plots from crosshair
73 /// selection. Slots with no values are controlled by `ignore_whitespace_indices`.
74 /// With both filters enabled, any visible value keeps a slot eligible. If no eligible
75 /// slot is in view, no crosshair is shown or reported. Default: false.
76 #[serde(default)]
77 pub ignore_hidden_indices: bool,
78
79 /// Include the clock in intraday time-scale and crosshair labels.
80 /// Default: true
81 pub time_visible: bool,
82
83 /// Show seconds in the time scale and vertical crosshair label in hh:mm:ss format for intraday data.
84 /// Default: true
85 pub seconds_visible: bool,
86
87 /// Draw small vertical line on time axis labels.
88 /// Default: false
89 pub ticks_visible: bool,
90
91 /// Changes horizontal scale marks generation.
92 /// With this flag equal to true, marks of the same weight are either all drawn or none are drawn at all.
93 /// Default: false
94 pub uniform_distribution: bool,
95
96 /// Allow major time scale labels (day, month and year boundaries) to be rendered in a
97 /// bolder font weight.
98 /// Default: true
99 pub allow_bold_labels: bool,
100
101 /// The longest expected tick mark label in characters, which sets the minimum distance
102 /// between time scale labels; raise it for a custom formatter with longer labels.
103 /// Zero is treated as the default.
104 /// Default: 8
105 pub tick_mark_max_character_length: usize,
106
107 /// The minimum height of the time scale in points. The time scale is taller when its
108 /// labels need more space, and has no height while time labels are hidden.
109 /// Default: 0.0
110 pub minimum_height: f32,
111
112 /// Draw a line along the top edge of the time scale.
113 /// Default: false
114 pub border_visible: bool,
115
116 /// The colour of the time scale border; `None` uses the theme's border colour.
117 /// Default: None
118 pub border_color: Option<Color32>,
119
120 /// Timezone mode for displaying ts
121 /// Default: UTC
122 pub timezone: TimezoneMode,
123}
124
125impl Default for TimeScaleOptions {
126 fn default() -> Self {
127 Self {
128 right_offset: 2.5, // Default: keep 2-3 bars of whitespace on the right
129 right_offset_pixels: None,
130 bar_spacing: 6.0,
131 min_bar_spacing: 0.5,
132 max_bar_spacing: 0.0,
133 fix_left_edge: false, // Allow scrolling past the first bar
134 fix_right_edge: false, // Allow scrolling past latest bar
135 lock_visible_time_range_on_resize: false,
136 right_bar_stays_on_scroll: false,
137 shift_visible_range_on_new_bar: true,
138 allow_shift_visible_range_on_whitespace_replacement: false,
139 ignore_whitespace_indices: false,
140 ignore_hidden_indices: false,
141 time_visible: true,
142 seconds_visible: true,
143 ticks_visible: false,
144 uniform_distribution: false,
145 allow_bold_labels: true,
146 tick_mark_max_character_length: 8,
147 minimum_height: 0.0,
148 border_visible: false,
149 border_color: None,
150 timezone: TimezoneMode::default(),
151 }
152 }
153}