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
160
161
162
163
164
165
166
167
168
169
use crate::color::{COLOR_HEX_BLUE_3, COLOR_HEX_BLUE_4};
use crate::render::svg::*;
use crate::shape::point::Point;
use crate::{Color, Error, LinearScale, PointLabelPosition, PointType, Scale, View};
use svg::Node;
const DEFAULT_LABEL_VISIBLE: bool = true;
const DEFAULT_LABEL_POSITION: PointLabelPosition = PointLabelPosition::Top;
const DEFAULT_POINT_TYPE: PointType = PointType::Circle;
const DEFAULT_POINT_VISIBLE: bool = true;
#[derive(Clone)]
pub struct ScatterView {
x_scale: LinearScale,
y_scale: LinearScale,
point_fill_color: String,
point_stroke_color: String,
points: Vec<Point>,
point_type: PointType,
point_visible: bool,
point_label_visible: bool,
point_label_position: PointLabelPosition,
}
impl ScatterView {
pub fn new(x_scale: LinearScale, y_scale: LinearScale) -> Self {
Self {
x_scale,
y_scale,
point_fill_color: COLOR_HEX_BLUE_4.to_string(),
point_stroke_color: COLOR_HEX_BLUE_3.to_string(),
points: Vec::new(),
point_type: DEFAULT_POINT_TYPE,
point_visible: DEFAULT_POINT_VISIBLE,
point_label_visible: DEFAULT_LABEL_VISIBLE,
point_label_position: DEFAULT_LABEL_POSITION,
}
}
pub fn set_point_fill_color(mut self, point_fill_color: Color) -> Self {
self.point_fill_color = point_fill_color.to_string();
self
}
pub fn set_point_stroke_color(mut self, point_stroke_color: Color) -> Self {
self.point_stroke_color = point_stroke_color.to_string();
self
}
pub fn set_point_type(mut self, point_type: PointType) -> Self {
self.point_type = point_type;
self
}
pub fn set_point_visible(mut self, point_visible: bool) -> Self {
self.point_visible = point_visible;
self
}
pub fn set_point_label_visible(mut self, point_label_visible: bool) -> Self {
self.point_label_visible = point_label_visible;
self
}
pub fn set_point_label_position(mut self, point_label_position: PointLabelPosition) -> Self {
self.point_label_position = point_label_position;
self
}
pub fn set_data(mut self, data: &[(f32, f32)]) -> Result<Self, Error> {
if data.is_empty() {
return Err(Error::DataIsEmpty);
}
let x_bandwidth_offset = {
if self.x_scale.is_range_reversed() {
-self.x_scale.tick_offset()
} else {
self.x_scale.tick_offset()
}
};
let y_bandwidth_offset = {
if self.y_scale.is_range_reversed() {
-self.y_scale.tick_offset()
} else {
self.y_scale.tick_offset()
}
};
let mut points = Vec::new();
for values in data.iter() {
let scaled_x = &self.x_scale.scale(&values.0);
let scaled_y = self.y_scale.scale(&values.1);
let point = Point::new(
scaled_x + x_bandwidth_offset,
scaled_y + y_bandwidth_offset,
self.point_type,
DEFAULT_POINT_SIZE,
&values.1.to_string(),
&self.point_fill_color.to_string(),
&self.point_stroke_color.to_string(),
)
.set_point_visible(self.point_visible)
.set_x_label(&values.0.to_string())
.set_label_visible(self.point_label_visible)
.set_label_position(self.point_label_position);
points.push(point);
}
self.points = points;
Ok(self)
}
}
impl View for ScatterView {
fn to_svg(&self) -> svg::node::element::Group {
let mut res = svg::node::element::Group::new();
for point in self.points.iter() {
res.append(point.to_svg());
}
res
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn scatter_basic() {
let expected_svg_group = r##"<g>
<g class="point" transform="translate(5.125,4.7249985)">
<circle cx="0" cy="0" fill="#5095e5" r="5" stroke="#3a88e2"/>
<text dy=".35em" fill="#080808" font-family="sans-serif" font-size="14px" text-anchor="middle" x="0" y="-17">
(20.5,90.55)
</text>
</g>
<g class="point" transform="translate(23.9,29.67)">
<circle cx="0" cy="0" fill="#5095e5" r="5" stroke="#3a88e2"/>
<text dy=".35em" fill="#080808" font-family="sans-serif" font-size="14px" text-anchor="middle" x="0" y="-17">
(95.6,40.66)
</text>
</g>
</g>"##;
let x_scale = LinearScale::new(0.0, 200.0, 0, 50);
let y_scale = LinearScale::new(0.0, 100.0, 50, 0);
let data = vec![(20.5, 90.55), (95.6, 40.66)];
let scatter = ScatterView::new(x_scale, y_scale)
.set_data(&data)
.expect("unable to set data");
let scatter_svg = scatter.to_svg();
assert_eq!(scatter_svg.to_string(), expected_svg_group);
}
}