Deprecated: The each() function is deprecated. This message will be suppressed on further calls in /home/zhenxiangba/zhenxiangba.com/public_html/phproxy-improved-master/index.php on line 456
candlestick.rs - source
[go: Go Back, main page]

criterion_plot/
candlestick.rs

1//! "Candlestick" plots
2
3use std::borrow::Cow;
4
5use crate::data::Matrix;
6use crate::traits::{self, Data, Set};
7use crate::{Color, Default, Display, Figure, Label, LineType, LineWidth, Plot, Script};
8
9/// Properties common to candlestick plots
10pub struct Properties {
11    color: Option<Color>,
12    label: Option<Cow<'static, str>>,
13    line_type: LineType,
14    linewidth: Option<f64>,
15}
16
17impl Default for Properties {
18    fn default() -> Properties {
19        Properties {
20            color: None,
21            label: None,
22            line_type: LineType::Solid,
23            linewidth: None,
24        }
25    }
26}
27
28impl Script for Properties {
29    fn script(&self) -> String {
30        let mut script = String::from("with candlesticks ");
31
32        script.push_str(&format!("lt {} ", self.line_type.display()));
33
34        if let Some(lw) = self.linewidth {
35            script.push_str(&format!("lw {} ", lw))
36        }
37
38        if let Some(color) = self.color {
39            script.push_str(&format!("lc rgb '{}' ", color.display()));
40        }
41
42        if let Some(ref label) = self.label {
43            script.push_str("title '");
44            script.push_str(label);
45            script.push('\'')
46        } else {
47            script.push_str("notitle")
48        }
49
50        script
51    }
52}
53
54impl Set<Color> for Properties {
55    /// Sets the line color
56    fn set(&mut self, color: Color) -> &mut Properties {
57        self.color = Some(color);
58        self
59    }
60}
61
62impl Set<Label> for Properties {
63    /// Sets the legend label
64    fn set(&mut self, label: Label) -> &mut Properties {
65        self.label = Some(label.0);
66        self
67    }
68}
69
70impl Set<LineType> for Properties {
71    /// Changes the line type
72    ///
73    /// **Note** By default `Solid` lines are used
74    fn set(&mut self, lt: LineType) -> &mut Properties {
75        self.line_type = lt;
76        self
77    }
78}
79
80impl Set<LineWidth> for Properties {
81    /// Changes the width of the line
82    ///
83    /// # Panics
84    ///
85    /// Panics if `width` is a non-positive value
86    fn set(&mut self, lw: LineWidth) -> &mut Properties {
87        let lw = lw.0;
88
89        assert!(lw > 0.);
90
91        self.linewidth = Some(lw);
92        self
93    }
94}
95
96/// A candlestick consists of a box and two whiskers that extend beyond the box
97pub struct Candlesticks<X, WM, BM, BH, WH> {
98    /// X coordinate of the candlestick
99    pub x: X,
100    /// Y coordinate of the end point of the bottom whisker
101    pub whisker_min: WM,
102    /// Y coordinate of the bottom of the box
103    pub box_min: BM,
104    /// Y coordinate of the top of the box
105    pub box_high: BH,
106    /// Y coordinate of the end point of the top whisker
107    pub whisker_high: WH,
108}
109
110impl<X, WM, BM, BH, WH> traits::Plot<Candlesticks<X, WM, BM, BH, WH>> for Figure
111where
112    BH: IntoIterator,
113    BH::Item: Data,
114    BM: IntoIterator,
115    BM::Item: Data,
116    WH: IntoIterator,
117    WH::Item: Data,
118    WM: IntoIterator,
119    WM::Item: Data,
120    X: IntoIterator,
121    X::Item: Data,
122{
123    type Properties = Properties;
124
125    fn plot<F>(
126        &mut self,
127        candlesticks: Candlesticks<X, WM, BM, BH, WH>,
128        configure: F,
129    ) -> &mut Figure
130    where
131        F: FnOnce(&mut Properties) -> &mut Properties,
132    {
133        let (x_factor, y_factor) = crate::scale_factor(&self.axes, crate::Axes::BottomXLeftY);
134        let Candlesticks {
135            x,
136            whisker_min,
137            box_min,
138            box_high,
139            whisker_high,
140        } = candlesticks;
141
142        let data = Matrix::new(
143            itertools::izip!(x, box_min, whisker_min, whisker_high, box_high),
144            (x_factor, y_factor, y_factor, y_factor, y_factor),
145        );
146        self.plots
147            .push(Plot::new(data, configure(&mut Default::default())));
148        self
149    }
150}