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
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
//! Separate-pane indicator rendering with direct egui painting.
//!
//! This module renders oscillator and multi-line indicators (RSI, MACD, Stochastic,
//! etc.) in dedicated panels below the main chart. It paints directly through
//! [`egui::Painter`] to ensure pixel-perfect x-axis alignment with the main
//! candlestick chart.
//!
//! # Usage
//!
//! Indicator panes are typically managed by [`super::Chart::show_with_indicators`], but
//! they can also be used standalone:
//!
//! ```rust,ignore
//! use egui_charts::widget::indicator_pane::{IndicatorPane, IndicatorPaneConfig};
//!
//! let config = IndicatorPaneConfig::rsi();
//! let mut pane = IndicatorPane::with_config(config);
//! pane.show_aligned(ui, &rsi_indicator, &bars, visible_range, coords);
//! ```
//!
//! # Coordinate Alignment
//!
//! The [`IndicatorCoordParams`] struct captures the main chart's bar spacing,
//! right offset, and base index so that indicator panes draw their data points
//! at exactly the same x-coordinates as the corresponding candlesticks above.

use crate::ext::HasDesignTokens;
use crate::model::Bar;
use crate::studies::{Indicator, IndicatorValue};
use crate::styles::typography;
use crate::tokens::DESIGN_TOKENS;
use egui::{Color32, Pos2, Rect, Response, Sense, Stroke, Ui};
use std::ops::Range;

/// Chart layout constants matching main chart (from ChartConfig defaults)
mod layout {
    /// Left padding (matches ChartConfig::padding default)
    pub const LEFT_PADDING: f32 = 40.0;
    /// Right padding (matches ChartConfig::padding * 2.0)
    pub const RIGHT_PADDING: f32 = 80.0;
    /// Minimum pixel spacing between vertical grid lines
    pub const MIN_GRID_SPACING: f32 = 80.0;
}

/// Indicator panel sizing constants
mod indicator_sizing {
    /// Standard panel height for oscillators (RSI, Stochastic)
    pub const OSCILLATOR_HEIGHT: f32 = 120.0;
    /// Standard panel height for multi-line indicators (MACD)
    pub const MULTI_LINE_HEIGHT: f32 = 150.0;
}

/// Configuration for an indicator panel's visual appearance and behavior.
///
/// Controls the panel height, y-axis range, zone lines (overbought/oversold),
/// legend display, and grid visibility. Pre-built configurations are available
/// for common indicators via [`IndicatorPaneConfig::rsi`],
/// [`IndicatorPaneConfig::macd`], and [`IndicatorPaneConfig::stochastic`].
#[derive(Debug, Clone)]
pub struct IndicatorPaneConfig {
    /// Panel height in pixels
    pub height: f32,
    /// Fixed y-axis range (None = auto-scale)
    pub y_range: Option<(f64, f64)>,
    /// Show horizontal zone lines (e.g., RSI 70/30)
    pub show_zones: bool,
    /// Zone line levels and colors
    pub zones: Vec<(f64, Color32, &'static str)>,
    /// Show legend
    pub show_legend: bool,
    /// Show grid
    pub show_grid: bool,
}

impl Default for IndicatorPaneConfig {
    fn default() -> Self {
        Self {
            height: DESIGN_TOKENS.sizing.panel.bottom_default_height,
            y_range: None,
            show_zones: false,
            zones: Vec::new(),
            show_legend: true,
            show_grid: true,
        }
    }
}

impl IndicatorPaneConfig {
    /// Create config for RSI indicator
    pub fn rsi() -> Self {
        Self {
            height: indicator_sizing::OSCILLATOR_HEIGHT,
            y_range: Some((0.0, 100.0)),
            show_zones: true,
            zones: vec![
                (70.0, DESIGN_TOKENS.semantic.extended.bearish, "Overbought"),
                (50.0, DESIGN_TOKENS.semantic.extended.chart_text, "Neutral"),
                (30.0, DESIGN_TOKENS.semantic.extended.bullish, "Oversold"),
            ],
            show_legend: true,
            show_grid: true,
        }
    }

    /// Create config for MACD indicator
    pub fn macd() -> Self {
        Self {
            height: indicator_sizing::MULTI_LINE_HEIGHT,
            y_range: None, // Auto-scale
            show_zones: true,
            zones: vec![(0.0, DESIGN_TOKENS.semantic.extended.chart_text, "Zero Line")],
            show_legend: true,
            show_grid: true,
        }
    }

    /// Create config for Stochastic indicator
    pub fn stochastic() -> Self {
        Self {
            height: indicator_sizing::OSCILLATOR_HEIGHT,
            y_range: Some((0.0, 100.0)),
            show_zones: true,
            zones: vec![
                (80.0, DESIGN_TOKENS.semantic.extended.bearish, "Overbought"),
                (20.0, DESIGN_TOKENS.semantic.extended.bullish, "Oversold"),
            ],
            show_legend: true,
            show_grid: true,
        }
    }
}

// Re-export ChartMapping from coords module for x-axis alignment
pub use crate::chart::coords::ChartMapping;

/// Coordinate parameters for aligning indicator panes with the main chart.
///
/// Wraps the parameters needed to construct a [`ChartMapping`] so that
/// indicator data points are drawn at the same x-positions as their
/// corresponding bars in the main chart. Obtained from the chart's time scale
/// after rendering.
#[derive(Clone, Copy, Debug)]
pub struct IndicatorCoordParams {
    /// Bar spacing in pixels
    pub bar_spacing: f32,
    /// Right offset (bars to the right of last data point)
    pub right_offset: f32,
    /// Base index (index of last data point)
    pub base_idx: usize,
    /// Start index (index of first visible bar)
    pub start_idx: usize,
}

impl IndicatorCoordParams {
    /// Creates coordinate parameters from the main chart's time-scale state.
    ///
    /// These values are typically obtained from the chart after rendering via
    /// [`super::Chart::get_chart_mapping`] or the internal time-scale accessors.
    pub fn new(bar_spacing: f32, right_offset: f32, base_idx: usize, start_idx: usize) -> Self {
        Self {
            bar_spacing,
            right_offset,
            base_idx,
            start_idx,
        }
    }

    /// Creates a [`ChartMapping`] from these params, the panel's screen rect, and y-range.
    ///
    /// The resulting mapping converts bar indices to x-pixel positions and
    /// price/indicator values to y-pixel positions within the given rect.
    pub fn to_mapping(&self, rect: Rect, y_min: f64, y_max: f64) -> ChartMapping {
        ChartMapping::new(
            rect,
            self.bar_spacing,
            self.start_idx,
            self.base_idx,
            self.right_offset,
            y_min,
            y_max,
        )
    }
}

/// Layout and interaction results from rendering one interactive indicator pane.
///
/// Returned by [`IndicatorPane::show_aligned_interactive`]. Besides the rects
/// and y-axis range used for click hit-testing, it carries the [`Response`] for
/// the legend's close "x" so the host can offer one-click pane removal.
pub struct PaneInteraction {
    /// Full panel rect, including the y-axis label gutter.
    pub panel_rect: Rect,
    /// Chart drawing area rect, excluding the y-axis label gutter.
    pub chart_rect: Rect,
    /// Y-axis minimum value used for this frame.
    pub y_min: f64,
    /// Y-axis maximum value used for this frame.
    pub y_max: f64,
    /// Interaction response for the whole pane (line selection, hover).
    pub response: Response,
    /// Interaction response for the legend's close "x", or `None` when the pane
    /// shows no legend and therefore exposes no remove affordance.
    pub close_response: Option<Response>,
}

/// A separate-pane indicator panel rendered via direct egui painting.
///
/// Each `IndicatorPane` renders one indicator (RSI, MACD, etc.) in its own
/// rectangular area below the main chart. The pane includes:
/// - Indicator line(s) with proper colors
/// - Horizontal zone lines (e.g., RSI 70/30 overbought/oversold)
/// - Semi-transparent zone fills for overbought/oversold regions
/// - Y-axis labels on the right side
/// - A legend showing the indicator name and current value
/// - Optional vertical grid lines aligned with the main chart
pub struct IndicatorPane {
    config: IndicatorPaneConfig,
}

impl IndicatorPane {
    /// Create a new indicator panel with custom configuration
    pub fn with_config(config: IndicatorPaneConfig) -> Self {
        Self { config }
    }

    /// Render the indicator panel with proper x-axis alignment
    pub fn show(
        &mut self,
        ui: &mut Ui,
        indicator: &dyn Indicator,
        bars: &[Bar],
        visible_range: Range<usize>,
    ) {
        if !indicator.is_visible() {
            return;
        }

        // Get chart background and text colors from theme
        let chart_bg = ui.chart_bg();
        let text_color = ui.style().visuals.text_color();
        let grid_color = ui.chart_grid();

        // Get live theme colors for zones
        let bullish = ui.bullish_color();
        let bearish = ui.bearish_color();

        // Allocate the panel area
        let available_width = ui.available_width();
        let panel_size = egui::vec2(available_width, self.config.height);
        let (rect, _response) = ui.allocate_exact_size(panel_size, Sense::hover());

        if !ui.is_rect_visible(rect) {
            return;
        }

        let painter = ui.painter();

        // Fill background
        painter.rect_filled(rect, 0.0, chart_bg);

        // Calculate drawing area to match main chart layout
        let left_padding = layout::LEFT_PADDING;
        let right_padding = layout::RIGHT_PADDING;
        let chart_rect = Rect::from_min_max(
            Pos2::new(rect.min.x + left_padding, rect.min.y),
            Pos2::new(rect.max.x - right_padding, rect.max.y),
        );

        // Determine y-axis range
        let (y_min, y_max) = self.calculate_y_range(indicator, &visible_range);

        // Draw zone fills (colored bands)
        if self.config.show_zones && self.config.zones.len() >= 2 {
            self.draw_zone_fills(painter, chart_rect, y_min, y_max, bullish, bearish);
        }

        // Draw horizontal grid lines and zone lines
        self.draw_horizontal_lines(
            painter, chart_rect, y_min, y_max, grid_color, bullish, bearish,
        );

        // Draw indicator lines
        self.draw_indicator_lines(
            painter,
            chart_rect,
            indicator,
            bars,
            &visible_range,
            y_min,
            y_max,
        );

        // Draw y-axis labels
        self.draw_y_axis_labels(painter, rect, chart_rect, y_min, y_max, text_color);

        // Draw indicator legend at top-left. The non-interactive path cannot
        // host a clickable close affordance, so no remove "x" is drawn here.
        self.draw_legend(painter, chart_rect, indicator, text_color, None, false);
    }

    /// Render with explicit coordinate parameters for perfect x-axis alignment.
    ///
    /// Uses [`IndicatorCoordParams`] from the main chart to ensure that each
    /// indicator data point is drawn at the exact same x-coordinate as its
    /// corresponding bar in the price chart above.
    pub fn show_aligned(
        &mut self,
        ui: &mut Ui,
        indicator: &dyn Indicator,
        _bars: &[Bar],
        visible_range: Range<usize>,
        coords: IndicatorCoordParams,
    ) {
        if !indicator.is_visible() {
            return;
        }

        // Get chart background and text colors from theme
        let chart_bg = ui.chart_bg();
        let text_color = ui.style().visuals.text_color();
        let grid_color = ui.chart_grid();

        // Get live theme colors for zones
        let bullish = ui.bullish_color();
        let bearish = ui.bearish_color();

        // Allocate the panel area
        let available_width = ui.available_width();
        let panel_size = egui::vec2(available_width, self.config.height);
        let (rect, _response) = ui.allocate_exact_size(panel_size, Sense::hover());

        if !ui.is_rect_visible(rect) {
            return;
        }

        let painter = ui.painter();

        // Fill background
        painter.rect_filled(rect, 0.0, chart_bg);

        // Calculate drawing area to match main chart layout
        let left_padding = layout::LEFT_PADDING;
        let right_padding = layout::RIGHT_PADDING;
        let chart_rect = Rect::from_min_max(
            Pos2::new(rect.min.x + left_padding, rect.min.y),
            Pos2::new(rect.max.x - right_padding, rect.max.y),
        );

        // Determine y-axis range
        let (y_min, y_max) = self.calculate_y_range(indicator, &visible_range);

        // Draw zone fills (colored bands)
        if self.config.show_zones && self.config.zones.len() >= 2 {
            self.draw_zone_fills(painter, chart_rect, y_min, y_max, bullish, bearish);
        }

        // Draw horizontal grid lines and zone lines
        self.draw_horizontal_lines(
            painter, chart_rect, y_min, y_max, grid_color, bullish, bearish,
        );

        // Draw vertical grid lines (aligned with main chart)
        if self.config.show_grid {
            self.draw_vertical_grid_aligned(painter, chart_rect, &coords, grid_color);
        }

        // Draw indicator lines with aligned coordinates
        self.draw_indicator_lines_aligned(
            painter,
            chart_rect,
            indicator,
            &visible_range,
            y_min,
            y_max,
            &coords,
        );

        // Draw y-axis labels
        self.draw_y_axis_labels(painter, rect, chart_rect, y_min, y_max, text_color);

        // Draw indicator legend at top-left. The non-interactive path cannot
        // host a clickable close affordance, so no remove "x" is drawn here.
        self.draw_legend(painter, chart_rect, indicator, text_color, None, false);
    }

    /// Render with coordinate alignment and return layout info for hit testing.
    ///
    /// Like [`IndicatorPane::show_aligned`], but returns the panel and chart
    /// rects, y-axis range, and an interactive [`egui::Response`]. This allows
    /// the caller to implement click-on-line selection, tooltip display, or
    /// other interactive features on indicator panes.
    ///
    /// Returns `None` if the indicator is not visible.
    ///
    /// The returned [`PaneInteraction`] also carries the [`Response`] for the
    /// legend's close "x" (when the pane shows a legend), so the host can offer
    /// TradingView-style one-click pane removal.
    pub fn show_aligned_interactive(
        &mut self,
        ui: &mut Ui,
        indicator: &dyn Indicator,
        _bars: &[Bar],
        visible_range: Range<usize>,
        coords: IndicatorCoordParams,
    ) -> Option<PaneInteraction> {
        if !indicator.is_visible() {
            return None;
        }

        // Get chart background and text colors from theme
        let chart_bg = ui.chart_bg();
        let text_color = ui.style().visuals.text_color();
        let grid_color = ui.chart_grid();

        // Get live theme colors for zones
        let bullish = ui.bullish_color();
        let bearish = ui.bearish_color();

        // Allocate the panel area with click sense
        let available_width = ui.available_width();
        let panel_size = egui::vec2(available_width, self.config.height);
        let (rect, response) = ui.allocate_exact_size(panel_size, Sense::click());

        if !ui.is_rect_visible(rect) {
            return None;
        }

        let painter = ui.painter();

        // Fill background
        painter.rect_filled(rect, 0.0, chart_bg);

        // Calculate drawing area to match main chart layout
        let left_padding = layout::LEFT_PADDING;
        let right_padding = layout::RIGHT_PADDING;
        let chart_rect = Rect::from_min_max(
            Pos2::new(rect.min.x + left_padding, rect.min.y),
            Pos2::new(rect.max.x - right_padding, rect.max.y),
        );

        // Determine y-axis range
        let (y_min, y_max) = self.calculate_y_range(indicator, &visible_range);

        // Draw zone fills (colored bands)
        if self.config.show_zones && self.config.zones.len() >= 2 {
            self.draw_zone_fills(painter, chart_rect, y_min, y_max, bullish, bearish);
        }

        // Draw horizontal grid lines and zone lines
        self.draw_horizontal_lines(
            painter, chart_rect, y_min, y_max, grid_color, bullish, bearish,
        );

        // Draw vertical grid lines (aligned with main chart)
        if self.config.show_grid {
            self.draw_vertical_grid_aligned(painter, chart_rect, &coords, grid_color);
        }

        // Draw indicator lines with aligned coordinates
        self.draw_indicator_lines_aligned(
            painter,
            chart_rect,
            indicator,
            &visible_range,
            y_min,
            y_max,
            &coords,
        );

        // Draw y-axis labels
        self.draw_y_axis_labels(painter, rect, chart_rect, y_min, y_max, text_color);

        // Reserve the legend's close "x" and make it independently clickable so
        // it can surface a remove request without stealing pane-line clicks.
        let close_rect = self.legend_close_rect(ui.painter(), chart_rect, indicator);
        let close_response = close_rect
            .map(|cr| ui.interact(cr, response.id.with("indicator_pane_close"), Sense::click()));
        let close_hovered = close_response.as_ref().is_some_and(|r| r.hovered());
        if close_hovered {
            ui.ctx().set_cursor_icon(egui::CursorIcon::PointingHand);
        }

        // Draw indicator legend (and the close glyph) at top-left.
        self.draw_legend(
            ui.painter(),
            chart_rect,
            indicator,
            text_color,
            close_rect,
            close_hovered,
        );

        // Return panel info for hit testing
        Some(PaneInteraction {
            panel_rect: rect,
            chart_rect,
            y_min,
            y_max,
            response,
            close_response,
        })
    }

    fn calculate_y_range(
        &self,
        indicator: &dyn Indicator,
        visible_range: &Range<usize>,
    ) -> (f64, f64) {
        if let Some((min, max)) = self.config.y_range {
            return (min, max);
        }

        // Auto-scale based on visible indicator values
        let values = indicator.values();
        let mut min_val = f64::MAX;
        let mut max_val = f64::MIN;

        for i in visible_range.clone() {
            if let Some(value) = values.get(i) {
                match value {
                    IndicatorValue::Single(v) => {
                        min_val = min_val.min(*v);
                        max_val = max_val.max(*v);
                    }
                    IndicatorValue::Multiple(vals) => {
                        for v in vals {
                            min_val = min_val.min(*v);
                            max_val = max_val.max(*v);
                        }
                    }
                    IndicatorValue::None => {}
                }
            }
        }

        if min_val == f64::MAX {
            (0.0, 100.0) // Default fallback
        } else {
            // Add 10% padding
            let range = max_val - min_val;
            let padding = range * 0.1;
            (min_val - padding, max_val + padding)
        }
    }

    fn draw_zone_fills(
        &self,
        painter: &egui::Painter,
        rect: Rect,
        y_min: f64,
        y_max: f64,
        bullish: Color32,
        bearish: Color32,
    ) {
        // For RSI-style indicators, fill zones with semi-transparent colors
        // Overbought zone (above 70) - bearish color
        // Oversold zone (below 30) - bullish color

        for (level, color, label) in &self.config.zones {
            let live_color = if *color == DESIGN_TOKENS.semantic.extended.bullish {
                bullish
            } else if *color == DESIGN_TOKENS.semantic.extended.bearish {
                bearish
            } else {
                continue; // Skip neutral zones for fills
            };

            let y = self.value_to_y(*level, rect, y_min, y_max);

            // Create semi-transparent fill color
            let fill_color = Color32::from_rgba_unmultiplied(
                live_color.r(),
                live_color.g(),
                live_color.b(),
                25, // Very transparent
            );

            if label.contains("Overbought") || label.contains("above") {
                // Fill from this level to top
                let fill_rect =
                    Rect::from_min_max(Pos2::new(rect.min.x, rect.min.y), Pos2::new(rect.max.x, y));
                painter.rect_filled(fill_rect, 0.0, fill_color);
            } else if label.contains("Oversold") || label.contains("below") {
                // Fill from this level to bottom
                let fill_rect =
                    Rect::from_min_max(Pos2::new(rect.min.x, y), Pos2::new(rect.max.x, rect.max.y));
                painter.rect_filled(fill_rect, 0.0, fill_color);
            }
        }
    }

    fn draw_horizontal_lines(
        &self,
        painter: &egui::Painter,
        rect: Rect,
        y_min: f64,
        y_max: f64,
        grid_color: Color32,
        bullish: Color32,
        bearish: Color32,
    ) {
        // Draw zone lines with dashed style
        if self.config.show_zones {
            for (level, color, _label) in &self.config.zones {
                let live_color = if *color == DESIGN_TOKENS.semantic.extended.bullish {
                    bullish
                } else if *color == DESIGN_TOKENS.semantic.extended.bearish {
                    bearish
                } else {
                    *color
                };

                let y = self.value_to_y(*level, rect, y_min, y_max);
                if y >= rect.min.y && y <= rect.max.y {
                    // Draw dashed line
                    self.draw_dashed_line(
                        painter,
                        Pos2::new(rect.min.x, y),
                        Pos2::new(rect.max.x, y),
                        live_color,
                        1.0,
                        5.0,
                        3.0,
                    );
                }
            }
        }

        // Draw subtle grid lines
        if self.config.show_grid {
            let range = y_max - y_min;
            let step = self.calculate_nice_step(range, 5);

            let mut y_val = (y_min / step).ceil() * step;
            while y_val <= y_max {
                let y = self.value_to_y(y_val, rect, y_min, y_max);
                if y >= rect.min.y && y <= rect.max.y {
                    painter.line_segment(
                        [Pos2::new(rect.min.x, y), Pos2::new(rect.max.x, y)],
                        Stroke::new(DESIGN_TOKENS.stroke.extra_thin, grid_color),
                    );
                }
                y_val += step;
            }
        }
    }

    /// Draw vertical grid lines aligned with main chart
    /// Uses the same coordinate system as the main chart for seamless grid appearance
    fn draw_vertical_grid_aligned(
        &self,
        painter: &egui::Painter,
        rect: Rect,
        coords: &IndicatorCoordParams,
        grid_color: Color32,
    ) {
        // Create mapping for coordinate conversion (y range doesn't matter for grid)
        let mapping = coords.to_mapping(rect, 0.0, 1.0);

        // Calculate grid interval based on bar spacing (same algorithm as main chart)
        let min_pixel_spacing = layout::MIN_GRID_SPACING;
        let bars_per_grid = (min_pixel_spacing / coords.bar_spacing).ceil().max(1.0) as usize;

        // Round to nice intervals: 1, 2, 5, 10, 20, 50, 100, etc.
        let bars_per_grid = Self::nice_interval(bars_per_grid);

        // Calculate visible bar range
        let chart_width = rect.width();
        let visible_bars = (chart_width / coords.bar_spacing).ceil() as usize + 2;

        // Use start_idx from coords
        let start_idx = coords.start_idx;

        // Find the first grid line that's visible
        let first_visible_idx = if start_idx > 0 {
            (start_idx / bars_per_grid) * bars_per_grid
        } else {
            0
        };

        // Draw grid lines
        for i in 0..=(visible_bars / bars_per_grid + 2) {
            let grid_idx = first_visible_idx + i * bars_per_grid;

            // Convert bar index to x coord using ChartMapping
            let x = mapping.idx_to_x(grid_idx);

            // Skip if outside visible area
            if x < rect.min.x - 1.0 || x > rect.max.x + 1.0 {
                continue;
            }

            painter.line_segment(
                [Pos2::new(x, rect.min.y), Pos2::new(x, rect.max.y)],
                Stroke::new(DESIGN_TOKENS.stroke.extra_thin, grid_color),
            );
        }
    }

    /// Round to a nice interval (1, 2, 5, 10, 20, 50, 100, etc.)
    /// Uses the same fixed breakpoints as the main chart for consistent grid alignment
    fn nice_interval(raw: usize) -> usize {
        if raw <= 1 {
            return 1;
        }
        if raw <= 2 {
            return 2;
        }
        if raw <= 5 {
            return 5;
        }
        if raw <= 10 {
            return 10;
        }
        if raw <= 20 {
            return 20;
        }
        if raw <= 50 {
            return 50;
        }
        if raw <= 100 {
            return 100;
        }
        if raw <= 200 {
            return 200;
        }
        if raw <= 500 {
            return 500;
        }
        1000
    }

    fn draw_indicator_lines(
        &self,
        painter: &egui::Painter,
        rect: Rect,
        indicator: &dyn Indicator,
        bars: &[Bar],
        visible_range: &Range<usize>,
        y_min: f64,
        y_max: f64,
    ) {
        let values = indicator.values();
        let colors = indicator.colors();
        let line_cnt = indicator.line_cnt();

        // Calculate bar spacing from rect and visible range
        let visible_bars = visible_range.end.saturating_sub(visible_range.start);
        if visible_bars == 0 {
            return;
        }
        let bar_spacing = rect.width() / visible_bars as f32;

        for line_idx in 0..line_cnt {
            let color = colors.get(line_idx).copied().unwrap_or(Color32::WHITE);
            let mut points = Vec::new();

            for i in visible_range.clone() {
                if i >= bars.len() || i >= values.len() {
                    break;
                }

                let val = match &values[i] {
                    IndicatorValue::Single(v) => Some(*v),
                    IndicatorValue::Multiple(vals) => vals.get(line_idx).copied(),
                    IndicatorValue::None => None,
                };

                if let Some(v) = val {
                    // X position based on position in visible range
                    let local_idx = i - visible_range.start;
                    let x = rect.min.x + (local_idx as f32 + 0.5) * bar_spacing;
                    let y = self.value_to_y(v, rect, y_min, y_max);

                    if x >= rect.min.x && x <= rect.max.x {
                        points.push(Pos2::new(x, y));
                    }
                }
            }

            // Draw connected line segments
            if points.len() > 1 {
                for i in 0..points.len() - 1 {
                    painter.line_segment(
                        [points[i], points[i + 1]],
                        Stroke::new(DESIGN_TOKENS.stroke.medium, color),
                    );
                }
            }
        }
    }

    fn draw_indicator_lines_aligned(
        &self,
        painter: &egui::Painter,
        rect: Rect,
        indicator: &dyn Indicator,
        visible_range: &Range<usize>,
        y_min: f64,
        y_max: f64,
        coords: &IndicatorCoordParams,
    ) {
        let values = indicator.values();
        let colors = indicator.colors();
        let line_cnt = indicator.line_cnt();

        // Create mapping for coordinate conversion
        let mapping = coords.to_mapping(rect, y_min, y_max);

        for line_idx in 0..line_cnt {
            let color = colors.get(line_idx).copied().unwrap_or(Color32::WHITE);
            let mut points = Vec::new();

            for i in visible_range.clone() {
                if i >= values.len() {
                    break;
                }

                let val = match &values[i] {
                    IndicatorValue::Single(v) => Some(*v),
                    IndicatorValue::Multiple(vals) => vals.get(line_idx).copied(),
                    IndicatorValue::None => None,
                };

                if let Some(v) = val {
                    // Use the same coordinate formula as main chart
                    let x = mapping.idx_to_x(i);
                    let y = self.value_to_y(v, rect, y_min, y_max);

                    if x >= rect.min.x && x <= rect.max.x {
                        points.push(Pos2::new(x, y));
                    }
                }
            }

            // Draw connected line segments
            if points.len() > 1 {
                for i in 0..points.len() - 1 {
                    painter.line_segment(
                        [points[i], points[i + 1]],
                        Stroke::new(DESIGN_TOKENS.stroke.medium, color),
                    );
                }
            }
        }
    }

    fn draw_y_axis_labels(
        &self,
        painter: &egui::Painter,
        full_rect: Rect,
        chart_rect: Rect,
        y_min: f64,
        y_max: f64,
        text_color: Color32,
    ) {
        let range = y_max - y_min;
        let step = self.calculate_nice_step(range, 5);
        let label_x = chart_rect.max.x + DESIGN_TOKENS.spacing.sm;

        let mut y_val = (y_min / step).ceil() * step;
        while y_val <= y_max {
            let y = self.value_to_y(y_val, chart_rect, y_min, y_max);
            if y >= full_rect.min.y + 10.0 && y <= full_rect.max.y - 10.0 {
                let label = format!("{y_val:.2}");
                painter.text(
                    Pos2::new(label_x, y),
                    egui::Align2::LEFT_CENTER,
                    label,
                    egui::FontId::proportional(typography::XS),
                    text_color,
                );
            }
            y_val += step;
        }
    }

    /// Builds the legend text (indicator name plus its current value) shown at
    /// the pane's top-left.
    fn legend_text(&self, indicator: &dyn Indicator) -> String {
        let values = indicator.values();
        let mut legend_parts = vec![indicator.name().to_string()];

        if let Some(last_value) = values.last() {
            match last_value {
                IndicatorValue::Single(v) => {
                    legend_parts.push(format!("{v:.2}"));
                }
                IndicatorValue::Multiple(vals) => {
                    for v in vals {
                        legend_parts.push(format!("{v:.2}"));
                    }
                }
                IndicatorValue::None => {}
            }
        }

        legend_parts.join(" ")
    }

    /// Computes the screen rect of the close "x" trailing the legend text for a
    /// pane drawn into `rect`, or `None` when the legend (and thus the remove
    /// affordance) is hidden. Layout is measured through `painter` so the hit
    /// region matches the painted glyph exactly.
    fn legend_close_rect(
        &self,
        painter: &egui::Painter,
        rect: Rect,
        indicator: &dyn Indicator,
    ) -> Option<Rect> {
        if !self.config.show_legend {
            return None;
        }

        let galley = painter.layout_no_wrap(
            self.legend_text(indicator),
            egui::FontId::proportional(typography::SM),
            Color32::WHITE,
        );
        let text_left = rect.min.x + DESIGN_TOKENS.spacing.sm;
        let center_y = rect.min.y + DESIGN_TOKENS.spacing.xl;
        let glyph_size = typography::SM;
        let center = Pos2::new(
            text_left + galley.size().x + DESIGN_TOKENS.spacing.md,
            center_y,
        );
        let pad = DESIGN_TOKENS.spacing.sm;
        Some(Rect::from_center_size(
            center,
            egui::vec2(glyph_size + pad, glyph_size + pad),
        ))
    }

    /// Draws the indicator legend (name + current value) at the pane's
    /// top-left, plus a subtle TradingView-style close "x" inside
    /// `close_rect` when one was reserved by [`Self::legend_close_rect`]. The
    /// glyph dims at rest and brightens to the text color when `close_hovered`,
    /// so it reads as clickable without competing with the indicator readout.
    fn draw_legend(
        &self,
        painter: &egui::Painter,
        rect: Rect,
        indicator: &dyn Indicator,
        text_color: Color32,
        close_rect: Option<Rect>,
        close_hovered: bool,
    ) {
        if !self.config.show_legend {
            return;
        }

        painter.text(
            Pos2::new(
                rect.min.x + DESIGN_TOKENS.spacing.sm,
                rect.min.y + DESIGN_TOKENS.spacing.xl,
            ),
            egui::Align2::LEFT_CENTER,
            self.legend_text(indicator),
            egui::FontId::proportional(typography::SM),
            text_color,
        );

        if let Some(close_rect) = close_rect {
            let glyph_color = if close_hovered {
                text_color
            } else {
                Color32::from_rgba_unmultiplied(text_color.r(), text_color.g(), text_color.b(), 140)
            };
            painter.text(
                close_rect.center(),
                egui::Align2::CENTER_CENTER,
                "\u{00d7}",
                egui::FontId::proportional(typography::SM),
                glyph_color,
            );
        }
    }

    fn value_to_y(&self, value: f64, rect: Rect, y_min: f64, y_max: f64) -> f32 {
        let norm = (value - y_min) / (y_max - y_min);
        rect.max.y - (norm as f32 * rect.height())
    }

    fn calculate_nice_step(&self, range: f64, target_lines: usize) -> f64 {
        let raw_step = range / target_lines as f64;
        let magnitude = 10f64.powf(raw_step.log10().floor());
        let normalized = raw_step / magnitude;

        let nice = if normalized <= 1.0 {
            1.0
        } else if normalized <= 2.0 {
            2.0
        } else if normalized <= 5.0 {
            5.0
        } else {
            10.0
        };

        nice * magnitude
    }

    fn draw_dashed_line(
        &self,
        painter: &egui::Painter,
        from: Pos2,
        to: Pos2,
        color: Color32,
        width: f32,
        dash_length: f32,
        gap_length: f32,
    ) {
        let dir = to - from;
        let len = dir.length();
        if len == 0.0 {
            return;
        }
        let dir = dir / len;

        let mut pos = 0.0;
        let stroke = Stroke::new(width, color);

        while pos < len {
            let start = from + dir * pos;
            let end_pos = (pos + dash_length).min(len);
            let end = from + dir * end_pos;
            painter.line_segment([start, end], stroke);
            pos += dash_length + gap_length;
        }
    }
}