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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
//! # termplot
//! An _extensible_ plotting library for CLI applications.
//!
//!  - [Quick Start](#quick-start)
//!  - [Documentation](#documentation)
//!  - [Examples](#examples)
//!     + [Plotting a function](#plotting-a-function)
//!     + [Histogram](#histogram)
//!     + [Composing multiple plots](#composing-multiple-plots)
//!
//! ## Quick start
//! To use `termplot`, add the crate to your `Cargo.toml`.
//! ```toml
//! [dependencies]
//! termplot = "0.1.0"
//! ```
//!
//! [`Plot`] is the entry point of the library. Start by creating a [`Plot`] and play with it!
//!
//! ## Examples
//!
//! ### Plotting a function
//! Here is a quick example of what plotting `sin(x) / x` looks like.
//!
//! ```rust
//! use termplot::*;
//!
//! let mut plot = Plot::default();
//! plot.set_domain(Domain(-10.0..10.0))
//!     .set_codomain(Domain(-0.3..1.2))
//!     .set_title("Graph title")
//!     .set_x_label("X axis")
//!     .set_y_label("Y axis")
//!     .set_size(Size::new(50, 25))
//!     .add_plot(Box::new(plot::Graph::new(|x| x.sin() / x)));
//!
//! println!("{plot}");
//! ```
//! Output of the previous example:
//!
//! ![Simple
//! example](https://raw.githubusercontent.com/xavierhamel/termplot/main/assets/example-composed.png)
//!
//! ### Histogram
//! ```rust
//! use termplot::*;
//! use rand::Rng;
//!
//! let mut rng = rand::thread_rng();
//! let values: Vec<f64> = (0..100).map(|_| rng.gen_range(0.0f64..10.0f64)).collect();
//!
//! let mut plot = Plot::default();
//!
//! plot.set_domain(Domain(0.0..11.0))
//!     .set_codomain(Domain(0.0..45.0))
//!     .set_title("Graph title")
//!     .set_x_label("X axis")
//!     .set_y_label("Y axis")
//!     .set_size(Size::new(50, 25))
//!     .add_plot(Box::new(plot::Histogram::new(
//!         values,
//!         vec![0.0..2.0, 2.0..4.0, 4.0..6.0, 6.0..8.0, 8.0..10.0], // buckets
//!     )));
//!
//! println!("{plot}");
//! ```
//! Output of the previous example:
//!
//! ![Histogram example](https://raw.githubusercontent.com/xavierhamel/termplot/main/assets/example-hist.png)
//!
//! ### Composing multiple plots
//! It is also possible to compose multiple plots together:
//!
//! ```rust
//! use termplot::*;
//! use rand::Rng;
//!
//! let mut rng = rand::thread_rng();
//! let values: Vec<f64> = (0..100).map(|_| rng.gen_range(0.0f64..10.0f64)).collect();
//!
//! let mut plot = Plot::default();
//!
//! plot.set_domain(Domain(0.0..11.0))
//!     .set_codomain(Domain(0.0..45.0))
//!     .set_title("Graph title")
//!     .set_x_label("X axis")
//!     .set_y_label("Y axis")
//!     .set_size(Size::new(50, 25))
//!     .add_plot(Box::new(plot::Histogram::new(
//!         values,
//!         vec![0.0..2.0, 2.0..4.0, 4.0..6.0, 6.0..8.0, 8.0..10.0],
//!     )))
//!     .add_plot(Box::new(plot::Graph::new(|x| {
//!         -2.0 * (x - 5.0).powf(2.0) + 40.0
//!     })));
//!
//! println!("{plot}");
//! ```
//!
//! Output of the previous example:
//!
//! ![Composed example](https://raw.githubusercontent.com/xavierhamel/termplot/main/assets/example-composed.png)
use std::fmt;
use std::ops;

pub mod plot;
mod ticks;

/// A drawable component on the view.
pub trait DrawView {
    /// Draw the component on the given canvas.
    ///
    /// Simply draw on the given canvas lines and/or points. See [`ViewCanvas`] for more
    /// informations.
    ///
    /// `view` is provided only to give context if needed. It gives access to the domain, codomain,
    /// size of the view, etc... See [`View`] for more informations.
    ///
    /// # Examples
    ///
    /// This draws a rectangle centered on both the x and y axis.
    ///
    /// ```rust
    /// use termplot::{ViewCanvas, DrawView, Domain, Size, Plot, View};
    ///
    /// struct Rect;
    ///
    /// impl DrawView for Rect {
    ///     fn draw(&self, _: &View, canvas: &mut ViewCanvas) {
    ///         canvas.line(-2.0, 2.0, 2.0, 2.0);
    ///         canvas.line(2.0, 2.0, 2.0, -2.0);
    ///         canvas.line(-2.0, -2.0, 2.0, -2.0);
    ///         canvas.line(-2.0, 2.0, -2.0, -2.0);
    ///     }
    /// }
    ///
    /// let mut plot = Plot::default();
    /// plot.set_domain(Domain(-5.0..5.0))
    ///     .set_codomain(Domain(-5.0..5.0))
    ///     .set_size(Size::new(50, 50))
    ///     .add_plot(Box::new(Rect));
    ///
    /// println!("{plot}");
    /// ```
    fn draw(&self, view: &View, canvas: &mut ViewCanvas);
}

/// A size.
pub struct Size {
    /// The width.
    w: usize,
    /// The height.
    h: usize,
}

impl Size {
    pub fn new(w: usize, h: usize) -> Self {
        Self { w, h }
    }
}

impl Default for Size {
    fn default() -> Self {
        Self { w: 100, h: 100 }
    }
}

/// A container used for adding plots.
///
/// # Examples
///
/// ```rust
/// use termplot::*;
///
/// let mut plot = Plot::default();
/// plot.set_domain(Domain(-10.0..10.0))
///     .set_codomain(Domain(-0.3..1.2))
///     .set_title("Graph title")
///     .set_x_label("X axis")
///     .set_y_label("Y axis")
///     .set_size(Size::new(50, 25))
///     .add_plot(Box::new(plot::Graph::new(|x| x.sin() / x)));
///
/// println!("{plot}");
/// ```
/// Output:
///
/// <img src="./assets/example-simple.png" width="500" alt="Simple example (plotting)">
pub struct Plot {
    title: String,
    x_label: String,
    y_label: String,
    view: View,
    with_decoration: bool,
}

impl Default for Plot {
    fn default() -> Self {
        Self {
            title: String::new(),
            x_label: String::new(),
            y_label: String::new(),
            view: View::default(),
            with_decoration: true,
        }
    }
}

impl Plot {
    /// Add a plot or graph to the view.
    ///
    /// Multiple types of plots and graphs are already implemented. See [`plot`] for all the types
    /// of available plots and graphs.
    ///
    /// To create a new type of plot, see [`DrawView`].
    ///
    /// # Examples
    ///
    /// ```rust
    /// use termplot::{Plot, plot};
    ///
    /// let mut plot = Plot::default();
    /// plot.add_plot(Box::new(plot::Graph::new(|x| x.sin() / x)));
    ///
    /// println!("{plot}");
    /// ```
    pub fn add_plot(&mut self, plot: Box<dyn DrawView>) -> &mut Self {
        self.view.plots.push(plot);
        self
    }

    /// Set the domain (range of the x axis) of the plot.
    ///
    /// By default the domain is from -10 to 10.
    ///
    /// This function sets the minimum and maximum x values in the graph.
    pub fn set_domain(&mut self, domain: Domain) -> &mut Self {
        self.view.domain = domain;
        self
    }

    /// Set the codomain (range of the y axis) of the plot.
    ///
    /// By default the codomain is from -10 to 10.
    ///
    /// This function sets the minimum and maximum y values in the graph.
    pub fn set_codomain(&mut self, codomain: Domain) -> &mut Self {
        self.view.codomain = codomain;
        self
    }

    /// Set the title of the plot.
    pub fn set_title(&mut self, title: &str) -> &mut Self {
        self.title = String::from(title);
        self
    }

    /// Set the label of the x axis.
    ///
    /// The label is shown at the bottom of the figure.
    pub fn set_x_label(&mut self, label: &str) -> &mut Self {
        self.x_label = String::from(label);
        self
    }

    /// Set the label of the y axis.
    ///
    /// The label is shown at the bottom of the figure.
    pub fn set_y_label(&mut self, label: &str) -> &mut Self {
        self.y_label = String::from(label);
        self
    }

    /// Set the size of the view. This does not include decorations around the plot.
    ///
    /// The size is not the number of chars but the number of pixels. Pixels are smaller than
    /// chars. A char in the terminal is 2 by 4 pixels.
    pub fn set_size(&mut self, size: Size) -> &mut Self {
        self.view.size = size;
        self
    }
}

impl fmt::Display for Plot {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let rows = self.view.drawing(self.with_decoration);
        if !self.with_decoration {
            return write!(f, "{}", rows.join("\n"));
        }
        let width = rows[0].chars().count();
        writeln!(f, "╭{:─^width$}╮", self.title)?;
        for row in rows.iter() {
            writeln!(f, "│{row}│")?;
        }
        writeln!(f, "╰{:─<width$}╯", "")?;
        writeln!(f, " {: ^width$} ", self.x_label)?;
        writeln!(f, " {: ^width$} ", self.y_label)
    }
}

/// A `View` is where the graph and plots are drawn. The view does not includes decorations around
/// the plot (labels, title, border, etc..).
#[derive(Default)]
pub struct View {
    /// Domain (range of the x axis) of the plot or graph.
    pub domain: Domain,
    /// Codomain (range of the y axis) of the plot or graph.
    pub codomain: Domain,

    /// The size of the view. This does not include decorations around the plot.
    ///
    /// The size is not the number of chars but the number of pixels. Pixels are smaller than
    /// chars. A char in the terminal is 2 by 4 pixels.
    pub size: Size,
    plots: Vec<Box<dyn DrawView>>,
}

impl View {
    /// Draw x and y axis onto the view.
    fn draw_axis(&self, canvas: &mut ViewCanvas) {
        canvas.line(self.domain.min(), 0.0, self.domain.max(), 0.0);
        canvas.line(0.0, self.codomain.min(), 0.0, self.codomain.max());
    }

    /// Draw the plots and graphs that were added.
    fn draw_plots(&self, canvas: &mut ViewCanvas) {
        for plot in self.plots.iter() {
            plot.draw(&self, canvas);
        }
    }

    /// Return the plot with labels as a vector of strings.
    ///
    /// This function create a [`ViewCanvas`] and draw elements (like axis and plots) onto the
    /// canvas. It also generates and add the label of the axis.
    pub(crate) fn drawing(&self, with_decoration: bool) -> Vec<String> {
        let mut canvas = ViewCanvas::new(&self);
        self.draw_axis(&mut canvas);
        self.draw_plots(&mut canvas);
        let rows = canvas.rows();
        if !with_decoration {
            return rows;
        }
        let width = rows[0].chars().count();
        let mut out = Vec::new();
        let y_ticks = ticks::YTicks::new(&self.codomain, rows.len(), 2);
        let offset = y_ticks.display_width();
        let x_ticks = ticks::XTicks::new(&self.domain, width, 2);
        for (index, row) in rows.iter().enumerate() {
            out.push(format!("{: >offset$}{row}", y_ticks.get(index)));
        }
        out.push(format!("{: >offset$}{x_ticks}", ""));
        out
    }
}

/// Domain or codomain of a graph.
///
/// [`Domain`] is needed for implementing [`DrawView`] for a new type of plot.
pub struct Domain(pub std::ops::Range<f64>);

impl Default for Domain {
    fn default() -> Self {
        Self(-10.0..10.0)
    }
}

impl Domain {
    /// The smallest value of the domain.
    ///
    /// # Examples
    /// ```rust
    /// use termplot::Domain;
    ///
    /// let domain = Domain(-10.0..10.0);
    /// assert_eq!(domain.min(), -10.0);
    pub fn min(&self) -> f64 {
        self.0.start
    }

    /// The largest value of the domain.
    ///
    /// # Examples
    /// ```rust
    /// use termplot::Domain;
    ///
    /// let domain = Domain(-10.0..10.0);
    /// assert_eq!(domain.max(), 10.0);
    /// ```
    pub fn max(&self) -> f64 {
        self.0.end
    }

    /// The range of the domain as an absolute value.
    ///
    /// # Examples
    /// ```rust
    /// use termplot::Domain;
    ///
    /// let domain = Domain(-10.0..10.0);
    /// assert_eq!(domain.range(), 20.0);
    ///
    /// let domain = Domain(8.0..-8.0);
    /// assert_eq!(domain.range(), 16.0);
    /// ```
    pub fn range(&self) -> f64 {
        (self.0.end - self.0.start).abs()
    }

    /// An iterator over the range where a number of steps.
    ///
    /// For example, use this if it's needed to compute the value of each pixel for a plot.
    ///
    /// # Examples
    /// ```rust
    /// use termplot::{Domain};
    ///
    /// let domain = Domain(-10.0..10.0);
    /// let view_width = 100;
    ///
    /// domain
    ///     .iter(view_width)
    ///     .for_each(|x| {
    ///         // draw into the canvas
    ///     });
    /// ```
    /// See how [`plot::Graph`] is implemented for an in depth example.
    pub fn iter(&self, steps: usize) -> DomainIterator {
        DomainIterator::new(self.0.clone(), self.range() / steps as f64)
    }
}

/// An iterator over the domain with a number of steps.
///
/// This is used to only compute specific points of a continuous graph.
///
/// See [`Domain`] for more informations.
pub struct DomainIterator {
    current: f64,
    domain: ops::Range<f64>,
    step_by: f64,
}

impl DomainIterator {
    pub fn new(domain: ops::Range<f64>, step_by: f64) -> Self {
        Self {
            current: domain.start,
            domain,
            step_by,
        }
    }
}

impl Iterator for DomainIterator {
    type Item = f64;

    fn next(&mut self) -> Option<Self::Item> {
        if self.current >= self.domain.end {
            return None;
        }
        let result = self.current;
        self.current += self.step_by;
        Some(result)
    }
}

/// The view where graphs are graphed and plots are plotted.
///
/// Braille characters are use to draw on the canvas. `termplot` uses [`drawille::Canvas`] for
/// darwing.
pub struct ViewCanvas<'view> {
    canvas: drawille::Canvas,
    view: &'view View,
}

impl<'view> ViewCanvas<'view> {
    pub(crate) fn new(view: &'view View) -> Self {
        Self {
            canvas: drawille::Canvas::new(view.size.w as u32, view.size.h as u32),
            view,
        }
    }

    pub(crate) fn rows(&self) -> Vec<String> {
        let rows = self.canvas.rows();
        // println!("{:?}", rows[0].chars().count());
        rows
    }

    fn project_on_canvas(&self, x: f64, y: f64) -> (u32, u32) {
        let height = self.view.size.h as f64;
        let y_tmp = (y - self.view.codomain.min()) / self.view.codomain.range();
        let y = (height - y_tmp * height).round().clamp(0.0, height - 1.0);

        let width = self.view.size.w as f64;
        let x_tmp = (x - self.view.domain.min()) / self.view.domain.range();
        let x = (x_tmp * width).round().clamp(0.0, width - 1.0);

        (x as u32, y as u32)
    }

    /// Draw a line from the point (`x0`, `y0`) to (`x1`, `y1`).
    ///
    /// The coordinate are of the plotting space, and **not the actual pixel's coordinate.**
    ///
    /// This function uses the domain and codomain of the [`View`] to determine which pixels should
    /// be drawn. Therefor the drawn shape is relative to the position of the domain and codomain
    /// of the plotting space.
    pub fn line(&mut self, x0: f64, y0: f64, x1: f64, y1: f64) {
        let (x0, y0) = self.project_on_canvas(x0, y0);
        let (x1, y1) = self.project_on_canvas(x1, y1);
        self.canvas.line(x0, y0, x1, y1);
    }

    /// Draw a point at (`x`, `y`).
    ///
    /// The coordinate are of the plotting space, and **not the actual pixel's coordinate.**
    ///
    /// This function uses the domain and codomain of the [`View`] to determine which pixels should
    /// be drawn. Therefor the drawn shape is relative to the position of the domain and codomain
    /// of the plotting space.
    pub fn point(&mut self, x: f64, y: f64) {
        let (x, y) = self.project_on_canvas(x, y);
        self.canvas.set(x, y);
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use rand::Rng;

    #[test]
    fn simple() {
        let mut plot = Plot::default();
        plot.set_domain(Domain(-10.0..10.0))
            .set_codomain(Domain(-0.3..1.2))
            .set_title("Graph title")
            .set_x_label("X axis")
            .set_y_label("Y axis")
            .set_size(Size::new(100, 25))
            .add_plot(Box::new(plot::Graph::new(|x| x.sin() / x)));

        println!("{plot}");
    }

    #[test]
    fn histogram() {
        let mut rng = rand::thread_rng();
        let values: Vec<f64> = (0..100).map(|_| rng.gen_range(0.0f64..10.0f64)).collect();
        let mut plot = Plot::default();
        plot.set_domain(Domain(0.0..11.0))
            .set_codomain(Domain(0.0..45.0))
            .set_title("Graph title")
            .set_x_label("X axis")
            .set_y_label("Y axis")
            .set_size(Size::new(100, 25))
            .add_plot(Box::new(plot::Histogram::new(
                values,
                vec![0.0..2.0, 2.0..4.0, 4.0..6.0, 6.0..8.0, 8.0..10.0],
            )));
        println!("{plot}");
    }

    #[test]
    fn composition() {
        let mut rng = rand::thread_rng();
        let values: Vec<f64> = (0..100).map(|_| rng.gen_range(0.0f64..10.0f64)).collect();
        let mut plot = Plot::default();
        plot.set_domain(Domain(0.0..11.0))
            .set_codomain(Domain(0.0..45.0))
            .set_title("Graph title")
            .set_x_label("X axis")
            .set_y_label("Y axis")
            .set_size(Size::new(100, 25))
            .add_plot(Box::new(plot::Histogram::new(
                values,
                vec![0.0..2.0, 2.0..4.0, 4.0..6.0, 6.0..8.0, 8.0..10.0],
            )))
            .add_plot(Box::new(plot::Graph::new(|x| {
                -2.0 * (x - 5.0).powf(2.0) + 40.0
            })));
        println!("{plot}");
    }
}