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 Ohlc<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")]
38 line: Option<Line>,
39 #[serde(skip_serializing_if = "Option::is_none")]
40 increasing: Option<Direction>,
41 #[serde(skip_serializing_if = "Option::is_none")]
42 decreasing: Option<Direction>,
43 #[serde(skip_serializing_if = "Option::is_none", rename = "hoverlabel")]
44 hover_label: Option<Label>,
45 #[serde(skip_serializing_if = "Option::is_none", rename = "tickwidth")]
46 tick_width: Option<f64>,
47 #[serde(skip_serializing_if = "Option::is_none", rename = "xcalendar")]
48 x_calendar: Option<Calendar>,
49}
50
51impl<T, O> Ohlc<T, O>
52where
53 T: Serialize + Default,
54 O: Serialize + Default,
55{
56 pub fn new(
57 x: Vec<T>,
58 open: Vec<O>,
59 high: Vec<O>,
60 low: Vec<O>,
61 close: Vec<O>,
62 ) -> Box<Ohlc<T, O>> {
63 let iline = Line::new().width(2.0).color(NamedColor::Green);
64 let dline = Line::new().width(2.0).color(NamedColor::Red);
65 Box::new(Ohlc {
66 r#type: PlotType::Ohlc,
67 x,
68 open,
69 high,
70 low,
71 close,
72 increasing: Some(Direction::Increasing { line: iline }),
73 decreasing: Some(Direction::Decreasing { line: dline }),
74 ..Default::default()
75 })
76 }
77
78 pub fn name(mut self, name: &str) -> Box<Ohlc<T, O>> {
79 self.name = Some(name.to_owned());
80 Box::new(self)
81 }
82
83 pub fn visible(mut self, visible: bool) -> Box<Ohlc<T, O>> {
84 self.visible = Some(visible);
85 Box::new(self)
86 }
87
88 pub fn show_legend(mut self, show_legend: bool) -> Box<Ohlc<T, O>> {
89 self.show_legend = Some(show_legend);
90 Box::new(self)
91 }
92
93 pub fn legend_group(mut self, legend_group: &str) -> Box<Ohlc<T, O>> {
94 self.legend_group = Some(legend_group.to_owned());
95 Box::new(self)
96 }
97
98 pub fn opacity(mut self, opacity: f64) -> Box<Ohlc<T, O>> {
99 self.opacity = Some(opacity);
100 Box::new(self)
101 }
102
103 pub fn text(mut self, text: &str) -> Box<Ohlc<T, O>> {
104 self.text = Some(Dim::Scalar(text.to_owned()));
105 Box::new(self)
106 }
107
108 pub fn text_array<S: AsRef<str>>(mut self, text: Vec<S>) -> Box<Ohlc<T, O>> {
109 let text = private::owned_string_vector(text);
110 self.text = Some(Dim::Vector(text));
111 Box::new(self)
112 }
113
114 pub fn hover_text(mut self, hover_text: &str) -> Box<Ohlc<T, O>> {
115 self.hover_text = Some(Dim::Scalar(hover_text.to_owned()));
116 Box::new(self)
117 }
118
119 pub fn hover_text_array<S: AsRef<str>>(mut self, hover_text: Vec<S>) -> Box<Ohlc<T, O>> {
120 let hover_text = private::owned_string_vector(hover_text);
121 self.hover_text = Some(Dim::Vector(hover_text));
122 Box::new(self)
123 }
124
125 pub fn hover_info(mut self, hover_info: HoverInfo) -> Box<Ohlc<T, O>> {
126 self.hover_info = Some(hover_info);
127 Box::new(self)
128 }
129
130 pub fn line(mut self, line: Line) -> Box<Ohlc<T, O>> {
131 self.line = Some(line);
132 Box::new(self)
133 }
134
135 pub fn increasing(mut self, increasing: Direction) -> Box<Ohlc<T, O>> {
136 self.increasing = Some(increasing);
137 Box::new(self)
138 }
139
140 pub fn decreasing(mut self, decreasing: Direction) -> Box<Ohlc<T, O>> {
141 self.decreasing = Some(decreasing);
142 Box::new(self)
143 }
144
145 pub fn hover_label(mut self, hover_label: Label) -> Box<Ohlc<T, O>> {
146 self.hover_label = Some(hover_label);
147 Box::new(self)
148 }
149
150 pub fn tick_width(mut self, tick_width: f64) -> Box<Ohlc<T, O>> {
151 self.tick_width = Some(tick_width);
152 Box::new(self)
153 }
154
155 pub fn x_calendar(mut self, x_calendar: Calendar) -> Box<Ohlc<T, O>> {
156 self.x_calendar = Some(x_calendar);
157 Box::new(self)
158 }
159}
160
161impl<X, Y> Trace for Ohlc<X, Y>
162where
163 X: Serialize + Default,
164 Y: Serialize + Default,
165{
166 fn serialize(&self) -> String {
167 serde_json::to_string(&self).unwrap()
168 }
169}