1- use std:: { cell:: RefCell , rc:: Rc } ;
2-
31use crate :: {
4- chart_data:: ChartData , chart_renderer:: ChartRenderer , info_bar:: InfoBar , y_axis:: YAxis ,
2+ chart_data:: ChartData , chart_renderer:: ChartRenderer , info_bar:: InfoBar ,
3+ volume_pane:: VolumePane , y_axis:: YAxis ,
54} ;
5+ use std:: cell:: RefCell ;
6+ use std:: rc:: Rc ;
67
78#[ derive( Debug , Clone ) ]
89#[ cfg_attr( feature = "serde" , derive( serde:: Deserialize ) ) ]
@@ -11,25 +12,36 @@ pub struct Candle {
1112 pub high : f64 ,
1213 pub low : f64 ,
1314 pub close : f64 ,
15+ pub volume : Option < f64 > ,
16+ pub timestamp : Option < i64 > ,
1417}
1518
16- pub enum CandleType {
19+ pub ( crate ) enum CandleType {
1720 Bearish ,
1821 Bullish ,
1922}
2023
2124impl Candle {
2225 #[ allow( dead_code) ]
23- pub fn new ( open : f64 , high : f64 , low : f64 , close : f64 ) -> Candle {
26+ pub fn new (
27+ open : f64 ,
28+ high : f64 ,
29+ low : f64 ,
30+ close : f64 ,
31+ volume : Option < f64 > ,
32+ timestamp : Option < i64 > ,
33+ ) -> Candle {
2434 Candle {
2535 open,
2636 high,
2737 low,
2838 close,
39+ volume,
40+ timestamp,
2941 }
3042 }
3143
32- pub fn get_type ( & self ) -> CandleType {
44+ pub ( crate ) fn get_type ( & self ) -> CandleType {
3345 match self . open < self . close {
3446 true => CandleType :: Bullish ,
3547 false => CandleType :: Bearish ,
@@ -42,6 +54,7 @@ pub struct Chart {
4254 pub ( crate ) y_axis : YAxis ,
4355 pub ( crate ) chart_data : Rc < RefCell < ChartData > > ,
4456 pub ( crate ) info_bar : InfoBar ,
57+ pub ( crate ) volume_pane : VolumePane ,
4558}
4659
4760impl Chart {
@@ -51,27 +64,69 @@ impl Chart {
5164 let y_axis = YAxis :: new ( chart_data. clone ( ) ) ;
5265 let info_bar = InfoBar :: new ( "APPLE" . to_string ( ) , chart_data. clone ( ) ) ;
5366
67+ let volume_pane = VolumePane :: new (
68+ chart_data. clone ( ) ,
69+ ( chart_data. borrow ( ) . terminal_size . 1 / 6 ) as i64 ,
70+ ) ;
71+
72+ chart_data. borrow_mut ( ) . compute_height ( & volume_pane) ;
73+
5474 Chart {
5575 renderer,
5676 y_axis,
5777 chart_data,
5878 info_bar,
79+ volume_pane,
5980 }
6081 }
6182
83+ /// Draws the chart by outputting multiples strings in the terminal.
6284 pub fn draw ( & self ) {
6385 self . renderer . render ( self ) ;
6486 }
6587
88+ /// Set the name of the chart in the info bar.
6689 pub fn set_name ( & mut self , name : String ) {
6790 self . info_bar . name = name;
6891 }
6992
93+ /// Set the color of the bearish candle
94+ /// The default color is (234, 74, 90).
7095 pub fn set_bear_color ( & mut self , r : u8 , g : u8 , b : u8 ) {
7196 self . renderer . bearish_color = ( r, g, b) ;
7297 }
7398
99+ /// Set the color of the bullish candle
100+ /// The default color is (52, 208, 88).
74101 pub fn set_bull_color ( & mut self , r : u8 , g : u8 , b : u8 ) {
75102 self . renderer . bullish_color = ( r, g, b) ;
76103 }
104+
105+ /// Sets the color of the volume when the candle is bearish.
106+ /// The default color is (234, 74, 90).
107+ pub fn set_vol_bear_color ( & mut self , r : u8 , g : u8 , b : u8 ) {
108+ self . volume_pane . bearish_color = ( r, g, b) ;
109+ }
110+
111+ /// Sets the color of the volume when the candle is bullish.
112+ /// The default color is (52, 208, 88).
113+ pub fn set_vol_bull_color ( & mut self , r : u8 , g : u8 , b : u8 ) {
114+ self . volume_pane . bullish_color = ( r, g, b) ;
115+ }
116+
117+ /// Hide or show the volume pane.
118+ pub fn set_volume_pane_enabled ( & mut self , enabled : bool ) {
119+ self . volume_pane . enabled = enabled;
120+ }
121+
122+ /// Set the character for drawing the volume bars.
123+ pub fn set_volume_pane_unicode_fill ( & mut self , unicode_fill : char ) {
124+ self . volume_pane . unicode_fill = unicode_fill;
125+ }
126+
127+ /// Set the volume pane height.
128+ /// Default is 1/6 of the terminal height.
129+ pub fn set_volume_pane_height ( & mut self , height : i64 ) {
130+ self . volume_pane . height = height;
131+ }
77132}
0 commit comments