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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
|
//use std::io::IsTerminal;
//dbg!(std::io::stdout().is_terminal());
const ASCII_0: char = '─';
const ASCII_1: char = '│';
const ASCII_2: char = '╭';
const ASCII_3: char = '╰';
const ASCII_4: char = '╮';
const ASCII_7: char = '╯';
#[derive(Clone)]
#[allow(dead_code)]
enum GraphPixel<T> {
Normal(T),
Green(T),
Blue(T),
Red(T),
Blank,
}
impl<T> std::default::Default for GraphPixel<T> {
fn default() -> Self {
GraphPixel::Blank
}
}
impl<T: std::fmt::Display> std::fmt::Display for GraphPixel<T> {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(
f,
"{}",
match self {
GraphPixel::Normal(c) => format!("{}", c),
GraphPixel::Green(c) => format!("\x1b[32m{}\x1b[0m", c),
GraphPixel::Blue(c) => format!("\x1b[33m{}\x1b[0m", c),
GraphPixel::Red(c) => format!("\x1b[31m{}\x1b[0m", c),
GraphPixel::Blank => String::from(" "),
}
)
}
}
/// Available options for how the graph should look
#[derive(Clone)]
pub enum GraphType {
/// Use only * symbols
Star,
/// Use pretty characters from the ascii range
Ascii,
}
impl std::default::Default for GraphType {
fn default() -> Self {
GraphType::Star
}
}
/// Temporary variables used while building a graph
#[allow(dead_code)]
pub struct GraphBuilder {
/// A array of pixels, this will ultimately be turned to a string, is initialized to width * height
elements: Vec<GraphPixel<char>>,
/// Width of canvas
width: usize,
/// Height of canvas
height: usize,
/// Width of the area of the canvas left for the actual graph
draw_width: usize,
/// Height of the area of the canvas left for the actual graph
draw_height: usize,
/// x-offset for where the graph draw area begins
col_offset: usize,
/// y-offset for where the graph draw area begins
row_offset: usize,
/// The values of the x-axis of the graph
x_values: Vec<f64>,
/// The values of the y-axis of the graph
y_values: Vec<f64>,
/// Decides whether axis will be drawn on the resulting graph
enable_axis: bool,
/// Which GraphType to use when the graph is drawn
graph_type: GraphType,
}
impl GraphBuilder {
/// Create a new canvas with desired width and height
///
/// # Arguments
///
/// * `width` - Width of the output canvas
/// * `height` - Height of the output canvas
pub fn new(x_values: &[f64], y_values: &[f64], width: usize, height: usize) -> Self {
GraphBuilder {
elements: vec![GraphPixel::default(); width * height],
width,
height,
draw_width: width,
draw_height: height,
col_offset: 0,
row_offset: 0,
x_values: x_values.to_vec(),
y_values: y_values.to_vec(),
enable_axis: false,
graph_type: GraphType::default(),
}
}
/// Enable or disable axis in output
pub fn axis(&mut self, enable_axis: bool) -> &Self {
self.enable_axis = enable_axis;
self
}
/// Set graph type
pub fn graph_type(&mut self, graph_type: GraphType) -> &Self {
self.graph_type = graph_type;
self
}
/// Delete all saved samples before the last n
/// Assumes that y_values and x_values has the same length
///
/// # Arguments
///
/// * `n` - Number of samples to keep
pub fn keep_tail(&mut self, n: usize) -> &Self {
if self.y_values.len() > n {
self.y_values = self.y_values[self.y_values.len() - n..].to_vec();
self.x_values = self.x_values[self.x_values.len() - n..].to_vec();
}
self
}
/// Build the actual graph,
/// this is potentially a heavy operation, and it will mutate &self!
/// If you want to only see the "current state", you should clone first!
pub fn build(&mut self) -> String {
//let min_x = self.x_values.iter().cloned().fold(f64::INFINITY, f64::min);
//let max_x = self
// .x_values
// .iter()
// .cloned()
// .fold(f64::NEG_INFINITY, f64::max);
let min_y = self.y_values.iter().cloned().fold(f64::INFINITY, f64::min);
let max_y = self
.y_values
.iter()
.cloned()
.fold(f64::NEG_INFINITY, f64::max);
if self.enable_axis {
self.draw_axis(
GraphPixel::Normal(ASCII_1),
GraphPixel::Normal(ASCII_0),
GraphPixel::Normal('└'),
GraphPixel::Normal('┌'),
GraphPixel::Normal('┘'),
GraphPixel::Normal('┐'),
);
}
if true {
// && x_values.windows(2).all(|w| w[1] - w[0] == w[0] - w[1]) {
if self.y_values.len() >= self.draw_width {
self.downsample();
}
} else {
// If the sample size is not consistent, we should interpolate
todo!("interpolation is not implemented");
//interpolate(&y_values, &x_values, graph.width())
};
// Scale the data
let scale_factor = (self.draw_height - 1) as f64 / (max_y - min_y);
for i in 0..self.y_values.len() {
self.y_values[i] = ((self.y_values[i] - min_y) * scale_factor).round();
}
match self.graph_type {
GraphType::Star => self.draw_star(),
GraphType::Ascii => self.draw_ascii(),
}
self.to_string()
}
// Downsample using a common downsampling, this allows us to avoid doing anything
// with the x values.
// Make sure to only use one downsampling-algorithm
fn downsample(&mut self) {
let factor = self.y_values.len() as f64 / self.draw_width as f64;
let mut new_values = Vec::with_capacity(self.draw_width);
for i in 0..self.draw_width {
let new_value = self.y_values[(i as f64 * factor) as usize];
new_values.push(new_value);
}
self.y_values = new_values;
}
/// Turn canvas into a string
pub fn to_string(&self) -> String {
let mut out = String::with_capacity(self.height * (self.width + 1));
for (i, px) in self.elements.iter().enumerate() {
out.push_str(&px.to_string());
if (i + 1) % self.width == 0 && i < (self.height * self.width - 1) {
out.push('\n');
}
}
out
}
/// Set a pixel at a absolute position in the canvas
///
/// # Argument
///
/// * `x` - X-position of pixel
/// * `y` - Y-position of pixel
/// * `px` - The pixel to set
fn draw_exact(&mut self, x: usize, y: usize, px: GraphPixel<char>) {
let pos = y * self.width + x;
self.elements[pos] = px;
}
/// Set a pixel in the drawable part of the canvas
///
/// # Argument
///
/// * `x` - Relative X-position of pixel
/// * `y` - Relative Y-position of pixel
/// * `px` - The pixel to set
fn draw(&mut self, x: usize, y: usize, px: GraphPixel<char>) {
let pos = (y + self.row_offset) * self.width + (x + self.col_offset);
self.elements[pos] = px;
}
/// Add axis to the canvas and move graph drawing area inside axis
///
/// # Arguments
///
/// * `c1` - Horizontal axis lines
/// * `c2` - Vertical axis lines
/// * `c4` - Bottom left axis pixel
/// * `c5` - Top left axis pixel
/// * `c6` - Bottom right axis pixel
/// * `c7` - Top right axis pixel
fn draw_axis(
&mut self,
c1: GraphPixel<char>,
c2: GraphPixel<char>,
c3: GraphPixel<char>,
c4: GraphPixel<char>,
c5: GraphPixel<char>,
c6: GraphPixel<char>,
) {
if self.height < 2 || self.width < 2 {
return;
}
for i in 0..self.height {
self.elements[i * self.width] = c1.clone();
self.elements[i * self.width + self.width - 1] = c1.clone();
}
for i in 1..self.width - 1 {
self.elements[i] = c2.clone();
self.elements[(self.height - 1) * self.width + i] = c2.clone();
}
self.elements[0] = c4.clone();
self.elements[self.width - 1] = c6.clone();
self.elements[(self.height - 1) * self.width] = c3.clone();
self.elements[self.height * self.width - 1] = c5.clone();
if self.draw_height > 2 {
self.draw_height = self.height - 2;
}
if self.draw_width > 2 {
self.draw_width = self.width - 2;
}
self.col_offset = 1;
self.row_offset = 1;
}
/// Draw a graph using * for the pixels of the graph
fn draw_star(&mut self) {
for i in 0..self.y_values.len() {
let y = self.draw_height - (self.y_values[i] as usize) - 1;
self.draw(i, y, GraphPixel::Normal('*'));
}
}
/// Draw a graph using somewhat pretty ascii characters for pixels of the graph
pub fn draw_ascii(&mut self) {
if self.enable_axis {
self.draw_exact(
0,
self.draw_height - self.y_values[0] as usize,
GraphPixel::Green('├'),
);
self.draw_exact(
self.width - 1,
self.draw_height - self.y_values[self.y_values.len() - 1] as usize,
GraphPixel::Green('┤'),
);
}
for i in 0..self.y_values.len() {
let y1 = self.draw_height - (self.y_values[i] as usize) - 1;
let y2 = if i < self.y_values.len() - 1 {
self.draw_height - (self.y_values[i + 1] as usize) - 1
} else {
y1
};
if y1 == y2 {
self.draw(i, y1, GraphPixel::Green(ASCII_0));
} else if y1 > y2 {
self.draw(i, y1, GraphPixel::Green(ASCII_7));
self.draw(i, y2, GraphPixel::Green(ASCII_2));
for j in (y2 + 1)..y1 {
self.draw(i, j, GraphPixel::Green(ASCII_1));
}
} else {
self.draw(i, y1, GraphPixel::Green(ASCII_4));
self.draw(i, y2, GraphPixel::Green(ASCII_3));
for j in (y1 + 1)..y2 {
self.draw(i, j, GraphPixel::Green(ASCII_1));
}
}
}
}
}
// /// A better way to downsize, heavier and more complex, but should be used when sample speed is uneven.
// ///
// /// # Arguments
// ///
// /// * `y_values` - The y values that should be downsampled
// /// * `x_values` - X values, needed to interpolate while keeping sample distance
// /// * `column_count` - Desired resolution of the output
// pub fn interpolate(y_values: &[f64], x_values: &[f64], column_count: usize) -> Vec<f64> {
// let min_x = x_values.iter().cloned().fold(f64::INFINITY, f64::min);
// let max_x = x_values.iter().cloned().fold(f64::NEG_INFINITY, f64::max);
// let step = (max_x - min_x) / (column_count as f64 - 1.0);
// let mut interpolated_data = Vec::new();
//
// for i in 0..column_count {
// let target_mark = min_x + i as f64 * step;
// let mut j = 0;
// while j < x_values.len() - 1 && x_values[j + 1] < target_mark {
// j += 1;
// }
// let t0 = x_values[j];
// let t1 = x_values[j + 1];
// let d0 = y_values[j];
// let d1 = y_values[j + 1];
// let value = d0 + (d1 - d0) * (target_mark - t0) / (t1 - t0);
// interpolated_data.push(value);
// }
//
// interpolated_data
// }
//const _BRAILLE_1: char = '⣿';
//const BRAILLE_1_0: char = '⡀';
//const BRAILLE_1_1: char = '⣀';
//const BRAILLE_1_2: char = '⣀';
//const BRAILLE_2_0: char = '⡄';
//const BRAILLE_3_0: char = '⡆';
//const BRAILLE_4_0: char = '⡇';
// pub fn braille(y_values: &Vec<f64>, options: &GraphOptions) -> String {
// let aspects = SeriesAspects::from(y_values);
// let canvas = String::with_capacity((options.width * options.height) as usize);
//
// /*
// r = (max - min)
// r' = (max' - min')
// y' = (((y - min) * r') / r) + min'
// */
// let r = aspects.max - aspects.min;
// let r_marked = options.height;
//
// let norm_after = options.height;
//
// //for (x, y) in y_values.iter().enumerate() {
// // let y = norm(y.clone(), 0.0, options.height);
// // let x = norm(x.clone(), 0.0, options.width);
// //}
//
// String::from("")
// }
|