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
filledcurve.rs - source
[go: Go Back, main page]

criterion_plot/
filledcurve.rs

1//! Filled curve plots
2
3use std::borrow::Cow;
4
5use crate::data::Matrix;
6use crate::traits::{self, Data, Set};
7use crate::{Axes, Color, Default, Display, Figure, Label, Opacity, Plot, Script};
8
9/// Properties common to filled curve plots
10pub struct Properties {
11    axes: Option<Axes>,
12    color: Option<Color>,
13    label: Option<Cow<'static, str>>,
14    opacity: Option<f64>,
15}
16
17impl Default for Properties {
18    fn default() -> Properties {
19        Properties {
20            axes: None,
21            color: None,
22            label: None,
23            opacity: None,
24        }
25    }
26}
27
28impl Script for Properties {
29    fn script(&self) -> String {
30        let mut script = if let Some(axes) = self.axes {
31            format!("axes {} ", axes.display())
32        } else {
33            String::new()
34        };
35        script.push_str("with filledcurves ");
36
37        script.push_str("fillstyle ");
38
39        if let Some(opacity) = self.opacity {
40            script.push_str(&format!("solid {} ", opacity))
41        }
42
43        // TODO border shoulde be configurable
44        script.push_str("noborder ");
45
46        if let Some(color) = self.color {
47            script.push_str(&format!("lc rgb '{}' ", color.display()));
48        }
49
50        if let Some(ref label) = self.label {
51            script.push_str("title '");
52            script.push_str(label);
53            script.push('\'')
54        } else {
55            script.push_str("notitle")
56        }
57
58        script
59    }
60}
61
62impl Set<Axes> for Properties {
63    /// Select axes to plot against
64    ///
65    /// **Note** By default, the `BottomXLeftY` axes are used
66    fn set(&mut self, axes: Axes) -> &mut Properties {
67        self.axes = Some(axes);
68        self
69    }
70}
71
72impl Set<Color> for Properties {
73    /// Sets the fill color
74    fn set(&mut self, color: Color) -> &mut Properties {
75        self.color = Some(color);
76        self
77    }
78}
79
80impl Set<Label> for Properties {
81    /// Sets the legend label
82    fn set(&mut self, label: Label) -> &mut Properties {
83        self.label = Some(label.0);
84        self
85    }
86}
87
88impl Set<Opacity> for Properties {
89    /// Changes the opacity of the fill color
90    ///
91    /// **Note** By default, the fill color is totally opaque (`opacity = 1.0`)
92    ///
93    /// # Panics
94    ///
95    /// Panics if `opacity` is outside the range `[0, 1]`
96    fn set(&mut self, opacity: Opacity) -> &mut Properties {
97        self.opacity = Some(opacity.0);
98        self
99    }
100}
101
102/// Fills the area between two curves
103pub struct FilledCurve<X, Y1, Y2> {
104    /// X coordinate of the data points of both curves
105    pub x: X,
106    /// Y coordinate of the data points of the first curve
107    pub y1: Y1,
108    /// Y coordinate of the data points of the second curve
109    pub y2: Y2,
110}
111
112impl<X, Y1, Y2> traits::Plot<FilledCurve<X, Y1, Y2>> for Figure
113where
114    X: IntoIterator,
115    X::Item: Data,
116    Y1: IntoIterator,
117    Y1::Item: Data,
118    Y2: IntoIterator,
119    Y2::Item: Data,
120{
121    type Properties = Properties;
122
123    fn plot<F>(&mut self, fc: FilledCurve<X, Y1, Y2>, configure: F) -> &mut Figure
124    where
125        F: FnOnce(&mut Properties) -> &mut Properties,
126    {
127        let FilledCurve { x, y1, y2 } = fc;
128
129        let mut props = Default::default();
130        configure(&mut props);
131
132        let (x_factor, y_factor) =
133            crate::scale_factor(&self.axes, props.axes.unwrap_or(crate::Axes::BottomXLeftY));
134
135        let data = Matrix::new(itertools::izip!(x, y1, y2), (x_factor, y_factor, y_factor));
136        self.plots.push(Plot::new(data, &props));
137        self
138    }
139}