-
Notifications
You must be signed in to change notification settings - Fork 27
/
boxplot.rs
159 lines (134 loc) · 3.06 KB
/
boxplot.rs
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*!
Box plot
# Examples
```
# use plotlib::boxplot::BoxPlot;
# use plotlib::view::DiscreteView;
let b1 = BoxPlot::from_slice(&[0., 2., 3., 4.]);
let b2 = BoxPlot::from_vec(vec![0., 2., 3., 4.]);
let v = DiscreteView::new().add(&b1);
```
*/
use std::f64;
use svg;
use axis;
use representation::DiscreteRepresentation;
use svg_render;
use style;
use utils;
#[derive(Debug, Default)]
pub struct Style {
fill: Option<String>,
}
impl Style {
pub fn new() -> Self {
Style { fill: None }
}
pub fn overlay(&mut self, other: &Self) {
if let Some(ref v) = other.fill {
self.fill = Some(v.clone())
}
}
}
impl style::BoxPlot for Style {
fn fill<T>(&mut self, value: T) -> &mut Self
where
T: Into<String>,
{
self.fill = Some(value.into());
self
}
fn get_fill(&self) -> &Option<String> {
&self.fill
}
}
enum BoxData<'a> {
Owned(Vec<f64>),
Ref(&'a [f64]),
}
pub struct BoxPlot<'a> {
data: BoxData<'a>,
label: String,
style: Style,
}
impl<'a> BoxPlot<'a> {
pub fn from_slice(v: &'a [(f64)]) -> Self {
BoxPlot {
data: BoxData::Ref(v),
style: Style::new(),
label: String::new(),
}
}
pub fn from_vec(v: Vec<f64>) -> Self {
BoxPlot {
data: BoxData::Owned(v),
style: Style::new(),
label: String::new(),
}
}
pub fn style(mut self, style: &Style) -> Self {
self.style.overlay(style);
self
}
pub fn get_style(&self) -> &Style {
&self.style
}
pub fn label<T>(mut self, label: T) -> Self
where
T: Into<String>,
{
self.label = label.into();
self
}
pub fn get_label(&self) -> &String {
&self.label
}
fn get_data(&'a self) -> &'a [f64] {
match self.data {
BoxData::Owned(ref v) => v,
BoxData::Ref(v) => v,
}
}
fn range(&self) -> (f64, f64) {
match self.data {
BoxData::Owned(ref v) => utils::range(v),
BoxData::Ref(v) => utils::range(v),
}
}
}
impl<'a> DiscreteRepresentation for BoxPlot<'a> {
/// The maximum range. Used for auto-scaling axis
fn range(&self) -> (f64, f64) {
self.range()
}
/// The ticks that this representation covers. Used to collect all ticks for display
fn ticks(&self) -> Vec<String> {
vec![self.label.clone()]
}
fn to_svg(
&self,
x_axis: &axis::DiscreteAxis,
y_axis: &axis::ContinuousAxis,
face_width: f64,
face_height: f64,
) -> svg::node::element::Group {
svg_render::draw_face_boxplot(
self.get_data(),
&self.label,
x_axis,
y_axis,
face_width,
face_height,
&self.style,
)
}
fn to_text(
&self,
x_axis: &axis::DiscreteAxis,
y_axis: &axis::ContinuousAxis,
face_width: u32,
face_height: u32,
) -> String {
"".into()
}
}