egui-charts 0.2.0

High-performance financial charting engine for egui — candlesticks, 95 drawing tools, 130+ indicators, and a full design-token theme system
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
//! Time formatting for X-axis labels.
//!
//! The [`TimeFormatter`] trait controls how timestamps are rendered as labels
//! on the time axis.  The library ships with [`DefaultTimeFormatter`],
//! [`LocaleTimeFormatter`], [`RelativeTimeFormatter`], and a
//! [`TimezoneAwareFormatter`] wrapper for automatic timezone conversion.
//!
//! Use [`TimeFormatterBuilder`] for a fluent construction API.

use super::timescale_marks::TickMarkType;
use crate::config::TimezoneMode;
/// Time formatting trait for customizable time axis labels.
/// Reference: lightweight-charts TimeFormatterFn.
use chrono::{DateTime, TimeZone, Utc};

#[cfg(test)]
use chrono::Datelike;

/// Trait for custom time formatting
/// Allows users to customize how time labels are displayed on the axis
pub trait TimeFormatter: Send + Sync {
    /// Format a ts into a display string
    ///
    /// # Arguments
    /// * `time` - The ts to format
    /// * `mark_type` - The type of tick mark (Year, Month, etc.)
    ///
    /// # Returns
    /// Formatted string to display on the time axis
    fn format(&self, time: DateTime<Utc>, mark_type: TickMarkType) -> String;

    /// Clone this formatter into a Box
    fn clone_box(&self) -> Box<dyn TimeFormatter>;
}

/// Default time formatter using standard formats
#[derive(Debug, Clone)]
pub struct DefaultTimeFormatter {
    /// Whether to use 24-hour format
    pub use_24_hour: bool,

    /// Whether to show seconds
    pub show_seconds: bool,
}

impl Default for DefaultTimeFormatter {
    fn default() -> Self {
        Self {
            use_24_hour: true,
            show_seconds: true,
        }
    }
}

impl TimeFormatter for DefaultTimeFormatter {
    fn format(&self, time: DateTime<Utc>, mark_type: TickMarkType) -> String {
        match mark_type {
            TickMarkType::Year => time.format("%Y").to_string(),
            TickMarkType::Month => time.format("%b").to_string(),
            // Show "Jan 15" for day boundaries
            TickMarkType::DayOfMonth => time.format("%b %d").to_string(),
            TickMarkType::Time => {
                // Show just time for intraday marks
                if self.use_24_hour {
                    time.format("%H:%M").to_string()
                } else {
                    time.format("%I:%M %p").to_string()
                }
            }
            TickMarkType::TimeWithSeconds => {
                if !self.show_seconds {
                    // Fall back to Time format if seconds disabled
                    return self.format(time, TickMarkType::Time);
                }

                if self.use_24_hour {
                    time.format("%H:%M:%S").to_string()
                } else {
                    time.format("%I:%M:%S %p").to_string()
                }
            }
        }
    }

    fn clone_box(&self) -> Box<dyn TimeFormatter> {
        Box::new(self.clone())
    }
}

/// Locale-aware time formatter
#[derive(Debug, Clone)]
pub struct LocaleTimeFormatter {
    /// Locale identifier (e.g., "en-US", "ja-JP", "de-DE")
    pub locale: String,

    /// Whether to use 24-hour format
    pub use_24_hour: bool,
}

impl LocaleTimeFormatter {
    /// Create a new locale-aware formatter
    pub fn new(locale: impl Into<String>) -> Self {
        let locale_str = locale.into();
        let use_24_hour = Self::default_24hour_for_locale(&locale_str);
        Self {
            locale: locale_str,
            use_24_hour,
        }
    }

    /// Determine default 24-hour preference for locale
    fn default_24hour_for_locale(locale: &str) -> bool {
        // US, Canada, UK prefer 12-hour
        !matches!(locale, "en-US" | "en-CA" | "en-GB")
    }
}

impl TimeFormatter for LocaleTimeFormatter {
    fn format(&self, time: DateTime<Utc>, mark_type: TickMarkType) -> String {
        // Use locale-specific formatting
        // For now, fall back to default formatter
        // In production, this would use proper locale libraries
        DefaultTimeFormatter {
            use_24_hour: self.use_24_hour,
            show_seconds: true,
        }
        .format(time, mark_type)
    }

    fn clone_box(&self) -> Box<dyn TimeFormatter> {
        Box::new(self.clone())
    }
}

/// Custom time formatter using a closure
pub struct CustomTimeFormatter {
    formatter: Box<dyn Fn(DateTime<Utc>, TickMarkType) -> String + Send + Sync>,
}

impl CustomTimeFormatter {
    /// Create a new custom formatter from a closure
    pub fn new<F>(formatter: F) -> Self
    where
        F: Fn(DateTime<Utc>, TickMarkType) -> String + Send + Sync + 'static,
    {
        Self {
            formatter: Box::new(formatter),
        }
    }
}

impl TimeFormatter for CustomTimeFormatter {
    fn format(&self, time: DateTime<Utc>, mark_type: TickMarkType) -> String {
        (self.formatter)(time, mark_type)
    }

    fn clone_box(&self) -> Box<dyn TimeFormatter> {
        // Note: Cannot clone closures, so this returns default
        // Users should manage their custom formatters explicitly
        Box::new(DefaultTimeFormatter::default())
    }
}

/// Relative time formatter (e.g., "2h ago", "yesterday")
#[derive(Debug, Clone)]
pub struct RelativeTimeFormatter {
    /// Reference time for relative calculations (usually "now")
    pub reference_time: DateTime<Utc>,
}

impl RelativeTimeFormatter {
    /// Create a new relative time formatter
    pub fn new(reference_time: DateTime<Utc>) -> Self {
        Self { reference_time }
    }

    /// Create a formatter relative to current time
    pub fn now() -> Self {
        Self {
            reference_time: Utc::now(),
        }
    }
}

impl TimeFormatter for RelativeTimeFormatter {
    fn format(&self, time: DateTime<Utc>, _mark_type: TickMarkType) -> String {
        let diff = self.reference_time.signed_duration_since(time);

        if diff.num_seconds() < 0 {
            return "future".to_string();
        }

        let seconds = diff.num_seconds();

        if seconds < 60 {
            format!("{seconds}s ago")
        } else if seconds < 3600 {
            format!("{}m ago", seconds / 60)
        } else if seconds < 86400 {
            format!("{}h ago", seconds / 3600)
        } else if seconds < 604800 {
            format!("{}d ago", seconds / 86400)
        } else if seconds < 2592000 {
            format!("{}w ago", seconds / 604800)
        } else if seconds < 31536000 {
            format!("{}mo ago", seconds / 2592000)
        } else {
            format!("{}y ago", seconds / 31536000)
        }
    }

    fn clone_box(&self) -> Box<dyn TimeFormatter> {
        Box::new(self.clone())
    }
}

/// Timezone-aware formatter wrapper
/// Converts ts to target timezone before formatting
pub struct TimezoneAwareFormatter {
    inner: Box<dyn TimeFormatter>,
    timezone: TimezoneMode,
}

impl Clone for TimezoneAwareFormatter {
    fn clone(&self) -> Self {
        Self {
            inner: self.inner.clone_box(),
            timezone: self.timezone.clone(),
        }
    }
}

impl TimezoneAwareFormatter {
    /// Create a new timezone-aware formatter
    pub fn new(inner: Box<dyn TimeFormatter>, timezone: TimezoneMode) -> Self {
        Self { inner, timezone }
    }

    /// Convert UTC ts to target timezone
    fn convert_timezone(&self, utc_time: DateTime<Utc>) -> DateTime<Utc> {
        use chrono::Datelike;
        use chrono::Timelike;

        match &self.timezone {
            TimezoneMode::Utc => utc_time,
            TimezoneMode::Local => {
                // Convert to local, then back to UTC representation for formatting
                let local = utc_time.with_timezone(&chrono::Local);
                Utc.with_ymd_and_hms(
                    local.year(),
                    local.month(),
                    local.day(),
                    local.hour(),
                    local.minute(),
                    local.second(),
                )
                .single()
                .unwrap_or(utc_time)
            }
            TimezoneMode::Timezone(tz) | TimezoneMode::Exchange(tz) => {
                // Convert to target timezone
                let converted = utc_time.with_timezone(tz);
                Utc.with_ymd_and_hms(
                    converted.year(),
                    converted.month(),
                    converted.day(),
                    converted.hour(),
                    converted.minute(),
                    converted.second(),
                )
                .single()
                .unwrap_or(utc_time)
            }
        }
    }
}

impl TimeFormatter for TimezoneAwareFormatter {
    fn format(&self, time: DateTime<Utc>, mark_type: TickMarkType) -> String {
        let converted = self.convert_timezone(time);
        self.inner.format(converted, mark_type)
    }

    fn clone_box(&self) -> Box<dyn TimeFormatter> {
        Box::new(self.clone())
    }
}

/// Builder for creating time formatters
pub struct TimeFormatterBuilder {
    use_24_hour: bool,
    show_seconds: bool,
    locale: Option<String>,
    timezone: Option<TimezoneMode>,
}

impl TimeFormatterBuilder {
    /// Create a new formatter builder
    pub fn new() -> Self {
        Self {
            use_24_hour: true,
            show_seconds: true,
            locale: None,
            timezone: None,
        }
    }

    /// Set 24-hour format
    pub fn with_24_hour(mut self, use_24_hour: bool) -> Self {
        self.use_24_hour = use_24_hour;
        self
    }

    /// Set seconds display
    pub fn with_seconds(mut self, show_seconds: bool) -> Self {
        self.show_seconds = show_seconds;
        self
    }

    /// Set locale
    pub fn with_locale(mut self, locale: impl Into<String>) -> Self {
        self.locale = Some(locale.into());
        self
    }

    /// Set timezone for conversion
    pub fn with_timezone(mut self, timezone: TimezoneMode) -> Self {
        self.timezone = Some(timezone);
        self
    }

    /// Build the formatter
    pub fn build(self) -> Box<dyn TimeFormatter> {
        let base: Box<dyn TimeFormatter> = if let Some(locale) = self.locale {
            Box::new(LocaleTimeFormatter {
                locale,
                use_24_hour: self.use_24_hour,
            })
        } else {
            Box::new(DefaultTimeFormatter {
                use_24_hour: self.use_24_hour,
                show_seconds: self.show_seconds,
            })
        };

        // Wrap with timezone converter if specified
        if let Some(tz) = self.timezone {
            Box::new(TimezoneAwareFormatter::new(base, tz))
        } else {
            base
        }
    }
}

impl Default for TimeFormatterBuilder {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use chrono::TimeZone;

    #[test]
    fn test_default_formatter() {
        let formatter = DefaultTimeFormatter::default();
        let time = Utc.with_ymd_and_hms(2024, 6, 15, 14, 30, 45).unwrap();

        assert_eq!(formatter.format(time, TickMarkType::Year), "2024");
        assert_eq!(formatter.format(time, TickMarkType::Month), "Jun");
        assert_eq!(formatter.format(time, TickMarkType::Time), "14:30");
        assert_eq!(
            formatter.format(time, TickMarkType::TimeWithSeconds),
            "14:30:45"
        );
    }

    #[test]
    fn test_12_hour_format() {
        let formatter = DefaultTimeFormatter {
            use_24_hour: false,
            show_seconds: true,
        };
        let time = Utc.with_ymd_and_hms(2024, 6, 15, 14, 30, 45).unwrap();

        let result = formatter.format(time, TickMarkType::Time);
        assert!(result.contains("PM"));
    }

    #[test]
    fn test_relative_formatter() {
        let reference = Utc.with_ymd_and_hms(2024, 6, 15, 14, 30, 0).unwrap();
        let formatter = RelativeTimeFormatter::new(reference);

        let time = Utc.with_ymd_and_hms(2024, 6, 15, 14, 28, 0).unwrap();
        assert_eq!(formatter.format(time, TickMarkType::Time), "2m ago");

        let time = Utc.with_ymd_and_hms(2024, 6, 15, 12, 30, 0).unwrap();
        assert_eq!(formatter.format(time, TickMarkType::Time), "2h ago");
    }

    #[test]
    fn test_custom_formatter() {
        let formatter = CustomTimeFormatter::new(|time, mark_type| match mark_type {
            TickMarkType::Year => format!("Year {}", time.year()),
            _ => time.format("%Y-%m-%d").to_string(),
        });

        let time = Utc.with_ymd_and_hms(2024, 6, 15, 14, 30, 0).unwrap();
        assert_eq!(formatter.format(time, TickMarkType::Year), "Year 2024");
        assert_eq!(formatter.format(time, TickMarkType::Month), "2024-06-15");
    }

    #[test]
    fn test_formatter_builder() {
        let formatter = TimeFormatterBuilder::new()
            .with_24_hour(false)
            .with_seconds(false)
            .build();

        let time = Utc.with_ymd_and_hms(2024, 6, 15, 14, 30, 45).unwrap();
        let result = formatter.format(time, TickMarkType::Time);
        assert!(result.contains("PM"));
    }

    #[test]
    fn test_timezone_conversion_utc() {
        use crate::config::TimezoneMode;

        let formatter = TimeFormatterBuilder::new()
            .with_24_hour(true)
            .with_seconds(false)
            .with_timezone(TimezoneMode::Utc)
            .build();

        let time = Utc.with_ymd_and_hms(2024, 6, 15, 14, 30, 0).unwrap();
        let result = formatter.format(time, TickMarkType::Time);
        assert_eq!(result, "14:30");
    }

    #[test]
    fn test_timezone_conversion_ny() {
        use crate::config::TimezoneMode;
        use chrono_tz::America::New_York;

        let formatter = TimeFormatterBuilder::new()
            .with_24_hour(true)
            .with_seconds(false)
            .with_timezone(TimezoneMode::Timezone(New_York))
            .build();

        // June 15, 2024 14:30 UTC = 10:30 EDT (UTC-4)
        let time = Utc.with_ymd_and_hms(2024, 6, 15, 14, 30, 0).unwrap();
        let result = formatter.format(time, TickMarkType::Time);
        assert_eq!(result, "10:30");
    }

    #[test]
    fn test_dst_spring_forward() {
        use crate::config::TimezoneMode;
        use chrono_tz::America::New_York;

        let formatter = TimeFormatterBuilder::new()
            .with_24_hour(true)
            .with_seconds(true)
            .with_timezone(TimezoneMode::Exchange(New_York))
            .build();

        // 2024 DST transition: March 10, 2:00 AM -> 3:00 AM
        // Before DST: March 10, 2024 06:00 UTC = March 10, 2024 01:00 EST (UTC-5)
        let before_dst = Utc.with_ymd_and_hms(2024, 3, 10, 6, 0, 0).unwrap();
        let result_before = formatter.format(before_dst, TickMarkType::TimeWithSeconds);
        assert_eq!(result_before, "01:00:00");

        // After DST: March 10, 2024 08:00 UTC = March 10, 2024 04:00 EDT (UTC-4)
        let after_dst = Utc.with_ymd_and_hms(2024, 3, 10, 8, 0, 0).unwrap();
        let result_after = formatter.format(after_dst, TickMarkType::TimeWithSeconds);
        assert_eq!(result_after, "04:00:00");
    }

    #[test]
    fn test_dst_fall_back() {
        use crate::config::TimezoneMode;
        use chrono_tz::America::New_York;

        let formatter = TimeFormatterBuilder::new()
            .with_24_hour(true)
            .with_seconds(true)
            .with_timezone(TimezoneMode::Exchange(New_York))
            .build();

        // 2024 DST transition: November 3, 2:00 AM -> 1:00 AM
        // Before DST end: November 3, 2024 05:00 UTC = November 3, 2024 01:00 EDT (UTC-4)
        let before_dst = Utc.with_ymd_and_hms(2024, 11, 3, 5, 0, 0).unwrap();
        let result_before = formatter.format(before_dst, TickMarkType::TimeWithSeconds);
        assert_eq!(result_before, "01:00:00");

        // After DST end: November 3, 2024 07:00 UTC = November 3, 2024 02:00 EST (UTC-5)
        let after_dst = Utc.with_ymd_and_hms(2024, 11, 3, 7, 0, 0).unwrap();
        let result_after = formatter.format(after_dst, TickMarkType::TimeWithSeconds);
        assert_eq!(result_after, "02:00:00");
    }

    #[test]
    fn test_timezone_london() {
        use crate::config::TimezoneMode;

        let formatter = TimeFormatterBuilder::new()
            .with_24_hour(true)
            .with_seconds(false)
            .with_timezone(TimezoneMode::lse())
            .build();

        // June 15, 2024 14:30 UTC = 15:30 BST (UTC+1)
        let time = Utc.with_ymd_and_hms(2024, 6, 15, 14, 30, 0).unwrap();
        let result = formatter.format(time, TickMarkType::Time);
        assert_eq!(result, "15:30");

        // January 15, 2024 14:30 UTC = 14:30 GMT (UTC+0)
        let time_winter = Utc.with_ymd_and_hms(2024, 1, 15, 14, 30, 0).unwrap();
        let result_winter = formatter.format(time_winter, TickMarkType::Time);
        assert_eq!(result_winter, "14:30");
    }

    #[test]
    fn test_timezone_tokyo() {
        use crate::config::TimezoneMode;

        let formatter = TimeFormatterBuilder::new()
            .with_24_hour(true)
            .with_seconds(false)
            .with_timezone(TimezoneMode::jse())
            .build();

        // June 15, 2024 14:30 UTC = June 15, 2024 23:30 JST (UTC+9)
        // Note: Japan doesn't use DST
        let time = Utc.with_ymd_and_hms(2024, 6, 15, 14, 30, 0).unwrap();
        let result = formatter.format(time, TickMarkType::Time);
        assert_eq!(result, "23:30");
    }

    #[test]
    fn test_timezone_clone() {
        use crate::config::TimezoneMode;

        let formatter1 = TimeFormatterBuilder::new()
            .with_24_hour(true)
            .with_seconds(false)
            .with_timezone(TimezoneMode::nyse())
            .build();

        // Test that clone_box works correctly
        let formatter2 = formatter1.clone_box();

        let time = Utc.with_ymd_and_hms(2024, 6, 15, 14, 30, 0).unwrap();
        let result1 = formatter1.format(time, TickMarkType::Time);
        let result2 = formatter2.format(time, TickMarkType::Time);

        assert_eq!(result1, result2);
        assert_eq!(result1, "10:30");
    }
}