1use crate::common::color::NamedColor;
4use crate::common::{Calendar, Dim, Direction, HoverInfo, Label, Line, PlotType};
5use crate::private;
6use crate::Trace;
7use serde::Serialize;
8
9#[derive(Serialize, Debug, Default)]
10pub struct Candlestick<T, O>
11where
12 T: Serialize + Default,
13 O: Serialize + Default,
14{
15 r#type: PlotType,
16 x: Vec<T>,
17 open: Vec<O>,
18 high: Vec<O>,
19 low: Vec<O>,
20 close: Vec<O>,
21 #[serde(skip_serializing_if = "Option::is_none")]
22 name: Option<String>,
23 #[serde(skip_serializing_if = "Option::is_none")]
24 visible: Option<bool>,
25 #[serde(skip_serializing_if = "Option::is_none", rename = "showlegend")]
26 show_legend: Option<bool>,
27 #[serde(skip_serializing_if = "Option::is_none", rename = "legendgroup")]
28 legend_group: Option<String>,
29 #[serde(skip_serializing_if = "Option::is_none")]
30 opacity: Option<f64>,
31 #[serde(skip_serializing_if = "Option::is_none")]
32 text: Option<Dim<String>>,
33 #[serde(skip_serializing_if = "Option::is_none", rename = "hovertext")]
34 hover_text: Option<Dim<String>>,
35 #[serde(skip_serializing_if = "Option::is_none", rename = "hoverinfo")]
36 hover_info: Option<HoverInfo>,
37 #[serde(skip_serializing_if = "Option::is_none", rename = "xaxis")]
38 x_axis: Option<String>,
39 #[serde(skip_serializing_if = "Option::is_none", rename = "yaxis")]
40 y_axis: Option<String>,
41 #[serde(skip_serializing_if = "Option::is_none")]
42 line: Option<Line>,
43 #[serde(skip_serializing_if = "Option::is_none", rename = "whiskerwidth")]
44 whisker_width: Option<f64>,
45 #[serde(skip_serializing_if = "Option::is_none")]
46 increasing: Option<Direction>,
47 #[serde(skip_serializing_if = "Option::is_none")]
48 decreasing: Option<Direction>,
49 #[serde(skip_serializing_if = "Option::is_none", rename = "hoverlabel")]
50 hover_label: Option<Label>,
51 #[serde(skip_serializing_if = "Option::is_none", rename = "xcalendar")]
52 x_calendar: Option<Calendar>,
53}
54
55impl<T, O> Candlestick<T, O>
56where
57 T: Serialize + Default,
58 O: Serialize + Default,
59{
60 pub fn new(
61 x: Vec<T>,
62 open: Vec<O>,
63 high: Vec<O>,
64 low: Vec<O>,
65 close: Vec<O>,
66 ) -> Box<Candlestick<T, O>> {
67 let iline = Line::new().width(1.0).color(NamedColor::Green);
68 let dline = Line::new().width(1.0).color(NamedColor::Red);
69 Box::new(Candlestick {
70 r#type: PlotType::Candlestick,
71 x,
72 open,
73 high,
74 low,
75 close,
76 increasing: Some(Direction::Increasing { line: iline }),
77 decreasing: Some(Direction::Decreasing { line: dline }),
78 ..Default::default()
79 })
80 }
81
82 pub fn name(mut self, name: &str) -> Box<Candlestick<T, O>> {
83 self.name = Some(name.to_owned());
84 Box::new(self)
85 }
86
87 pub fn visible(mut self, visible: bool) -> Box<Candlestick<T, O>> {
88 self.visible = Some(visible);
89 Box::new(self)
90 }
91
92 pub fn show_legend(mut self, show_legend: bool) -> Box<Candlestick<T, O>> {
93 self.show_legend = Some(show_legend);
94 Box::new(self)
95 }
96
97 pub fn legend_group(mut self, legend_group: &str) -> Box<Candlestick<T, O>> {
98 self.legend_group = Some(legend_group.to_owned());
99 Box::new(self)
100 }
101
102 pub fn opacity(mut self, opacity: f64) -> Box<Candlestick<T, O>> {
103 self.opacity = Some(opacity);
104 Box::new(self)
105 }
106
107 pub fn text(mut self, text: &str) -> Box<Candlestick<T, O>> {
108 self.text = Some(Dim::Scalar(text.to_owned()));
109 Box::new(self)
110 }
111
112 pub fn text_array<S: AsRef<str>>(mut self, text: Vec<S>) -> Box<Candlestick<T, O>> {
113 let text = private::owned_string_vector(text);
114 self.text = Some(Dim::Vector(text));
115 Box::new(self)
116 }
117
118 pub fn hover_text(mut self, hover_text: &str) -> Box<Candlestick<T, O>> {
119 self.hover_text = Some(Dim::Scalar(hover_text.to_owned()));
120 Box::new(self)
121 }
122
123 pub fn hover_text_array<S: AsRef<str>>(mut self, hover_text: Vec<S>) -> Box<Candlestick<T, O>> {
124 let hover_text = private::owned_string_vector(hover_text);
125 self.hover_text = Some(Dim::Vector(hover_text));
126 Box::new(self)
127 }
128
129 pub fn hover_info(mut self, hover_info: HoverInfo) -> Box<Candlestick<T, O>> {
130 self.hover_info = Some(hover_info);
131 Box::new(self)
132 }
133
134 pub fn x_axis(mut self, axis: &str) -> Box<Candlestick<T, O>> {
135 self.x_axis = Some(axis.to_owned());
136 Box::new(self)
137 }
138
139 pub fn y_axis(mut self, axis: &str) -> Box<Candlestick<T, O>> {
140 self.y_axis = Some(axis.to_owned());
141 Box::new(self)
142 }
143
144 pub fn line(mut self, line: Line) -> Box<Candlestick<T, O>> {
145 self.line = Some(line);
146 Box::new(self)
147 }
148
149 pub fn whisker_width(mut self, whisker_width: f64) -> Box<Candlestick<T, O>> {
150 self.whisker_width = Some(whisker_width);
151 Box::new(self)
152 }
153
154 pub fn increasing(mut self, increasing: Direction) -> Box<Candlestick<T, O>> {
155 self.increasing = Some(increasing);
156 Box::new(self)
157 }
158
159 pub fn decreasing(mut self, decreasing: Direction) -> Box<Candlestick<T, O>> {
160 self.decreasing = Some(decreasing);
161 Box::new(self)
162 }
163
164 pub fn hover_label(mut self, hover_label: Label) -> Box<Candlestick<T, O>> {
165 self.hover_label = Some(hover_label);
166 Box::new(self)
167 }
168
169 pub fn x_calendar(mut self, x_calendar: Calendar) -> Box<Candlestick<T, O>> {
170 self.x_calendar = Some(x_calendar);
171 Box::new(self)
172 }
173}
174
175impl<X, Y> Trace for Candlestick<X, Y>
176where
177 X: Serialize + Default,
178 Y: Serialize + Default,
179{
180 fn serialize(&self) -> String {
181 serde_json::to_string(&self).unwrap()
182 }
183}