criterion_plot/
filledcurve.rs1use 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
9pub 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 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 fn set(&mut self, axes: Axes) -> &mut Properties {
67 self.axes = Some(axes);
68 self
69 }
70}
71
72impl Set<Color> for Properties {
73 fn set(&mut self, color: Color) -> &mut Properties {
75 self.color = Some(color);
76 self
77 }
78}
79
80impl Set<Label> for Properties {
81 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 fn set(&mut self, opacity: Opacity) -> &mut Properties {
97 self.opacity = Some(opacity.0);
98 self
99 }
100}
101
102pub struct FilledCurve<X, Y1, Y2> {
104 pub x: X,
106 pub y1: Y1,
108 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}