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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
use crate::render::svg::*;
use crate::shape::axis::{Axis, AxisPosition};
use crate::view::View;
use crate::{BandScale, Error, LinearScale};
use std::path::Path;
use svg::Node;
const DEFAULT_MARGIN_TOP: i32 = 90;
const DEFAULT_MARGIN_BOTTOM: i32 = 50;
const DEFAULT_MARGIN_LEFT: i32 = 60;
const DEFAULT_MARGIN_RIGHT: i32 = 40;
const DEFAULT_WIDTH: i32 = 800;
const DEFAULT_HEIGHT: i32 = 600;
const DEFAULT_TITLE_FONT_SIZE: &str = "24px";
const DEFAULT_TITLE_Y_TRANSFORM: i32 = 25;
pub struct Chart<'a> {
margin_top: i32,
margin_bottom: i32,
margin_left: i32,
margin_right: i32,
width: i32,
height: i32,
x_axis_top: Option<Axis>,
x_axis_bottom: Option<Axis>,
y_axis_left: Option<Axis>,
y_axis_right: Option<Axis>,
views: Vec<&'a dyn View>,
title: String,
}
impl<'a> Chart<'a> {
pub fn new() -> Self {
Chart {
margin_top: DEFAULT_MARGIN_TOP,
margin_bottom: DEFAULT_MARGIN_BOTTOM,
margin_left: DEFAULT_MARGIN_LEFT,
margin_right: DEFAULT_MARGIN_RIGHT,
width: DEFAULT_WIDTH,
height: DEFAULT_HEIGHT,
x_axis_top: None,
x_axis_bottom: None,
y_axis_left: None,
y_axis_right: None,
views: Vec::new(),
title: String::new(),
}
}
pub fn view_width(&self) -> i32 {
self.width - self.margin_left - self.margin_right
}
pub fn view_height(&self) -> i32 {
self.height - self.margin_top - self.margin_bottom
}
pub fn set_margin_top(mut self, margin_top: i32) -> Self {
self.margin_top = margin_top;
self
}
pub fn set_margin_bottom(mut self, margin_bottom: i32) -> Self {
self.margin_bottom = margin_bottom;
self
}
pub fn set_margin_left(mut self, margin_left: i32) -> Self {
self.margin_left = margin_left;
self
}
pub fn set_margin_right(mut self, margin_right: i32) -> Self {
self.margin_right = margin_right;
self
}
pub fn set_width(mut self, width: i32) -> Self {
self.width = width;
self
}
pub fn set_height(mut self, height: i32) -> Self {
self.height = height;
self
}
pub fn set_axis_top_band(mut self, scale: BandScale) -> Self {
self.x_axis_top = Some(Axis::new(
&scale,
AxisPosition::Top,
self.view_width(),
self.view_height(),
));
self
}
pub fn set_axis_top_linear(mut self, scale: LinearScale) -> Self {
self.x_axis_top = Some(Axis::new(
&scale,
AxisPosition::Top,
self.view_width(),
self.view_height(),
));
self
}
pub fn set_axis_bottom_band(mut self, scale: BandScale) -> Self {
self.x_axis_bottom = Some(Axis::new(
&scale,
AxisPosition::Bottom,
self.view_width(),
self.view_height(),
));
self
}
pub fn set_axis_bottom_linear(mut self, scale: LinearScale) -> Self {
self.x_axis_bottom = Some(Axis::new(
&scale,
AxisPosition::Bottom,
self.view_width(),
self.view_height(),
));
self
}
pub fn set_axis_left_band(mut self, scale: BandScale) -> Self {
self.y_axis_left = Some(Axis::new(
&scale,
AxisPosition::Left,
self.view_width(),
self.view_height(),
));
self
}
pub fn set_axis_left_linear(mut self, scale: LinearScale) -> Self {
self.y_axis_left = Some(Axis::new(
&scale,
AxisPosition::Left,
self.view_width(),
self.view_height(),
));
self
}
pub fn set_axis_right_band(mut self, scale: BandScale) -> Self {
self.y_axis_right = Some(Axis::new(
&scale,
AxisPosition::Right,
self.view_width(),
self.view_height(),
));
self
}
pub fn set_axis_right_linear(mut self, scale: LinearScale) -> Self {
self.y_axis_right = Some(Axis::new(
&scale,
AxisPosition::Right,
self.view_width(),
self.view_height(),
));
self
}
pub fn set_axis_top_label(mut self, label: &str) -> Self {
if let Some(ref mut axis) = self.x_axis_top {
axis.set_label(label);
}
self
}
pub fn set_axis_bottom_label(mut self, label: &str) -> Self {
if let Some(ref mut axis) = self.x_axis_bottom {
axis.set_label(label);
}
self
}
pub fn set_axis_left_label(mut self, label: &str) -> Self {
if let Some(ref mut axis) = self.y_axis_left {
axis.set_label(label);
}
self
}
pub fn set_axis_right_label(mut self, label: &str) -> Self {
if let Some(ref mut axis) = self.y_axis_right {
axis.set_label(label);
}
self
}
pub fn set_title(mut self, title: &str) -> Self {
self.title = title.to_string();
self
}
pub fn add_view(mut self, view: &'a dyn View) -> Self {
self.views.push(view);
self
}
pub fn to_svg(&self) -> svg::Document {
let mut res = svg::node::element::Group::new().set(CLASS_ATTR, CLASS_CHART);
if let Some(ref axis) = self.x_axis_top {
let mut axis_group = axis.to_svg();
axis_group.assign(
TRANSFORM_ATTR,
translate_x_y(self.margin_left, self.margin_top),
);
res.append(axis_group);
};
if let Some(ref axis) = self.x_axis_bottom {
let mut axis_group = axis.to_svg();
axis_group.assign(
TRANSFORM_ATTR,
translate_x_y(self.margin_left, self.height - self.margin_bottom),
);
res.append(axis_group);
};
if let Some(ref axis) = self.y_axis_left {
let mut axis_group = axis.to_svg();
axis_group.assign(
TRANSFORM_ATTR,
translate_x_y(self.margin_left, self.margin_top),
);
res.append(axis_group);
};
if let Some(ref axis) = self.y_axis_right {
let mut axis_group = axis.to_svg();
axis_group.assign(
TRANSFORM_ATTR,
translate_x_y(self.width - self.margin_right, self.margin_top),
);
res.append(axis_group);
};
let mut views_group = svg::node::element::Group::new()
.set(CLASS_ATTR, CLASS_VIEWS)
.set(
TRANSFORM_ATTR,
translate_x_y(self.margin_left, self.margin_top),
);
for view in self.views.iter() {
views_group.append(view.to_svg());
}
res.append(views_group);
if !self.title.is_empty() {
let title_group = svg::node::element::Group::new()
.set(CLASS_ATTR, CLASS_TITLE)
.set(
TRANSFORM_ATTR,
translate_x_y(self.width / 2, DEFAULT_TITLE_Y_TRANSFORM),
)
.add(
svg::node::element::Text::new()
.set(X_ATTR, START)
.set(Y_ATTR, START)
.set(DY_ATTR, DEFAULT_DY)
.set(FILL_ATTR, DEFAULT_FONT_COLOR)
.set(TEXT_ANCHOR_ATTR, TEXT_ANCHOR_MIDDLE)
.set(FONT_SIZE_ATTR, DEFAULT_TITLE_FONT_SIZE)
.set(FONT_FAMILY_ATTR, DEFAULT_FONT_FAMILY)
.add(svg::node::Text::new(&self.title)),
);
res.append(title_group);
}
svg::Document::new()
.set(WIDTH_ATTR, self.width)
.set(HEIGHT_ATTR, self.height)
.set(VIEW_BOX_ATTR, (START, START, self.width, self.height))
.add(res)
}
pub fn save<P: AsRef<Path>>(&self, path: P) -> Result<(), Error> {
svg::save(path, &self.to_svg())?;
Ok(())
}
}
impl<'a> Default for Chart<'a> {
fn default() -> Self {
Self::new()
}
}