1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
use std::borrow::Cow;
use std::iter::IntoIterator;
use data::Matrix;
use traits::{self, Data};
use {Axes, Color, Default, Display, Figure, Plot, Script};
pub struct Properties {
axes: Option<Axes>,
color: Option<Color>,
label: Option<Cow<'static, str>>,
opacity: Option<f64>,
}
impl Properties {
pub fn axes(&mut self, axes: Axes) -> &mut Properties {
self.axes = Some(axes);
self
}
pub fn color(&mut self, color: Color) -> &mut Properties {
self.color = Some(color);
self
}
pub fn label<S>(&mut self, label: S) -> &mut Properties
where
S: Into<Cow<'static, str>>,
{
self.label = Some(label.into());
self
}
pub fn opacity(&mut self, opacity: f64) -> &mut Properties {
self.opacity = Some(opacity);
self
}
}
impl Default for Properties {
fn default() -> Properties {
Properties {
axes: None,
color: None,
label: None,
opacity: None,
}
}
}
impl Script for Properties {
fn script(&self) -> String {
let mut script = if let Some(axes) = self.axes {
format!("axes {} ", axes.display())
} else {
String::new()
};
script.push_str("with filledcurves ");
script.push_str("fillstyle ");
if let Some(opacity) = self.opacity {
script.push_str(&format!("solid {} ", opacity))
}
script.push_str("noborder ");
if let Some(color) = self.color {
script.push_str(&format!("lc rgb '{}' ", color.display()));
}
if let Some(ref label) = self.label {
script.push_str("title '");
script.push_str(label);
script.push('\'')
} else {
script.push_str("notitle")
}
script
}
}
pub struct FilledCurve<X, Y1, Y2> {
pub x: X,
pub y1: Y1,
pub y2: Y2,
}
impl<X, Y1, Y2> traits::Plot<FilledCurve<X, Y1, Y2>> for Figure
where
X: IntoIterator,
X::Item: Data,
Y1: IntoIterator,
Y1::Item: Data,
Y2: IntoIterator,
Y2::Item: Data,
{
type Properties = Properties;
fn plot<F>(&mut self, fc: FilledCurve<X, Y1, Y2>, configure: F) -> &mut Figure
where
F: FnOnce(&mut Properties) -> &mut Properties,
{
let FilledCurve { x, y1, y2 } = fc;
let mut props = Default::default();
configure(&mut props);
let (x_factor, y_factor) =
::scale_factor(&self.axes, props.axes.unwrap_or(::Axes::BottomXLeftY));
let data = Matrix::new(izip!(x, y1, y2), (x_factor, y_factor, y_factor));
self.plots.push(Plot::new(data, &props));
self
}
}