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
//!
//! poloto - plot to SVG and style with CSS
//!
//! ### Usage
//!
//! Check out the [github examples](https://github.com/tiby312/poloto/tree/master/examples).
//! * Plots containing NaN or Infinity are ignored.
//! * After 8 plots, the colors cycle back and are repeated.
//!
use core::fmt::Write;

pub use tagger;
mod build;
mod util;
pub use build::default_tags;
pub use build::Names;
use core::borrow::Borrow;

use build::*;

/// The poloto prelude.
pub mod prelude {
    pub use super::move_format;
}

use core::fmt;

mod render;

trait PlotTrait {
    fn write_name(&self, a: &mut fmt::Formatter) -> fmt::Result;
    fn iter_first(&mut self) -> &mut dyn Iterator<Item = [f64; 2]>;
    fn iter_second(&mut self) -> &mut dyn Iterator<Item = [f64; 2]>;
}

use fmt::Display;
struct PlotStruct<I: Iterator<Item = [f64; 2]> + Clone, F: Display> {
    first: I,
    second: I,
    func: F,
}

impl<I: Iterator<Item = [f64; 2]> + Clone, F: Display> PlotStruct<I, F> {
    fn new(it: I, func: F) -> Self {
        let it2 = it.clone();
        PlotStruct {
            first: it,
            second: it2,
            func,
        }
    }
}

impl<D: Iterator<Item = [f64; 2]> + Clone, F: Display> PlotTrait for PlotStruct<D, F> {
    fn write_name(&self, a: &mut fmt::Formatter) -> fmt::Result {
        self.func.fmt(a)
    }
    fn iter_first(&mut self) -> &mut dyn Iterator<Item = [f64; 2]> {
        &mut self.first
    }

    fn iter_second(&mut self) -> &mut dyn Iterator<Item = [f64; 2]> {
        &mut self.second
    }
}


enum PlotType {
    Scatter,
    Line,
    Histo,
    LineFill,
}

struct Plot<'a> {
    plot_type: PlotType,
    plots: Box<dyn PlotTrait + 'a>,
}

/// Shorthand for `moveable_format(move |w|write!(w,...))`
/// Similar to `format_args!()` except has a more flexible lifetime.
#[macro_export]
macro_rules! move_format {
    ($($arg:tt)*) => {
        $crate::moveable_format(move |w| write!(w,$($arg)*))
    }
}

/// Convert a moved closure into a impl fmt::Display.
/// This is useful because std's `format_args!()` macro
/// has a shorter lifetime.
pub fn moveable_format(func: impl Fn(&mut fmt::Formatter) -> fmt::Result) -> impl fmt::Display {
    struct Foo<F>(F);
    impl<F: Fn(&mut fmt::Formatter) -> fmt::Result> fmt::Display for Foo<F> {
        fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
            (self.0)(formatter)
        }
    }
    Foo(func)
}

/// Default theme using css variables (with light theme defaults if the variables are not set).
pub const HTML_CONFIG_CSS_VARIABLE_DEFAULT: &str = r###"<style>.poloto {
    font-family: "Arial";
    stroke-width:2;
    }
    .poloto_text{fill: var(--poloto_fg_color,black);  }
    .poloto_axis_lines{stroke: var(--poloto_fg_color,black);stoke-width:3;fill:none}
    .poloto_background{fill: var(--poloto_bg_color,aliceblue); }
    .poloto0stroke{stroke:  var(--poloto_color0,blue); }
    .poloto1stroke{stroke:  var(--poloto_color1,red); }
    .poloto2stroke{stroke:  var(--poloto_color2,green); }
    .poloto3stroke{stroke:  var(--poloto_color3,gold); }
    .poloto4stroke{stroke:  var(--poloto_color4,aqua); }
    .poloto5stroke{stroke:  var(--poloto_color5,brown); }
    .poloto6stroke{stroke:  var(--poloto_color6,lime); }
    .poloto7stroke{stroke:  var(--poloto_color7,chocolate); }
    .poloto0fill{fill:var(--poloto_color0,blue);}
    .poloto1fill{fill:var(--poloto_color1,red);}
    .poloto2fill{fill:var(--poloto_color2,green);}
    .poloto3fill{fill:var(--poloto_color3,gold);}
    .poloto4fill{fill:var(--poloto_color4,aqua);}
    .poloto5fill{fill:var(--poloto_color5,brown);}
    .poloto6fill{fill:var(--poloto_color6,lime);}
    .poloto7fill{fill:var(--poloto_color7,chocolate);}</style>"###;

/// Default light theme
pub const HTML_CONFIG_LIGHT_DEFAULT: &str = r###"<style>.poloto {
    font-family: "Arial";
    stroke-width:2;
    }
    .poloto_text{fill: black;  }
    .poloto_axis_lines{stroke: black;stoke-width:3;fill:none}
    .poloto_background{fill: aliceblue; }
    .poloto0stroke{stroke:  blue; }
    .poloto1stroke{stroke:  red; }
    .poloto2stroke{stroke:  green; }
    .poloto3stroke{stroke:  gold; }
    .poloto4stroke{stroke:  aqua; }
    .poloto5stroke{stroke:  brown; }
    .poloto6stroke{stroke:  lime; }
    .poloto7stroke{stroke:  chocolate; }
    .poloto0fill{fill:blue;}
    .poloto1fill{fill:red;}
    .poloto2fill{fill:green;}
    .poloto3fill{fill:gold;}
    .poloto4fill{fill:aqua;}
    .poloto5fill{fill:brown;}
    .poloto6fill{fill:lime;}
    .poloto7fill{fill:chocolate;}</style>"###;

/// Default dark theme
pub const HTML_CONFIG_DARK_DEFAULT: &str = r###"<style>.poloto {
    font-family: "Arial";
    stroke-width:2;
    }
    .poloto_text{fill: white;  }
    .poloto_axis_lines{stroke: white;stoke-width:3;fill:none}
    .poloto_background{fill: black; }
    .poloto0stroke{stroke:  blue; }
    .poloto1stroke{stroke:  red; }
    .poloto2stroke{stroke:  green; }
    .poloto3stroke{stroke:  gold; }
    .poloto4stroke{stroke:  aqua; }
    .poloto5stroke{stroke:  brown; }
    .poloto6stroke{stroke:  lime; }
    .poloto7stroke{stroke:  chocolate; }
    .poloto0fill{fill:blue;}
    .poloto1fill{fill:red;}
    .poloto2fill{fill:green;}
    .poloto3fill{fill:gold;}
    .poloto4fill{fill:aqua;}
    .poloto5fill{fill:brown;}
    .poloto6fill{fill:lime;}
    .poloto7fill{fill:chocolate;}</style>"###;

/// Create a [`Plotter`] with the specified title,xname,yname, and custom html
/// Consider using some of the default html tags.
pub fn plot_with_html<'a>(
    title: impl Display,
    xname: impl Display,
    yname: impl Display,
    style: impl Display,
) -> Plotter<'a, impl Names> {
    build::PlotterBuilder::new()
        .with_header(style)
        .build(title, xname, yname)
}

/// Convenience function for `plot_with_html(title,xnam,yname,HTML_CONFIG_LIGHT_DEFAULT)`
pub fn plot<'a>(
    title: impl Display,
    xname: impl Display,
    yname: impl Display,
) -> Plotter<'a, impl Names> {
    plot_with_html(title, xname, yname, HTML_CONFIG_LIGHT_DEFAULT)
}

#[derive(Copy, Clone)]
enum SvgTagOption {
    Svg,
    NoSvg,
}

/// Keeps track of plots.
/// User supplies iterators that will be iterated on when
/// render is called.
pub struct Plotter<'a, D: Names> {
    names: D,
    plots: Vec<Plot<'a>>,
    svgtag: SvgTagOption,
}

impl<'a, D: Names> Plotter<'a, D> {
    /// Create a line from plots.
    ///
    /// # Example
    ///
    /// ```
    /// let data=[
    ///         [1.0f64,4.0],
    ///         [2.0,5.0],
    ///         [3.0,6.0]
    /// ];
    /// 
    /// let mut plotter = poloto::plot("title","x","y");
    /// plotter.line("",&data);
    /// ```
    pub fn line<I,J>(&mut self, name: impl Display + 'a, plots: I) -> &mut Self
    where
        I: IntoIterator<Item = J>,
        I::IntoIter: Clone + 'a,
        J:Borrow<[f64;2]>
    {
        self.plots.push(Plot {
            plot_type: PlotType::Line,
            plots: Box::new(PlotStruct::new(plots.into_iter().map(|x|*x.borrow() ), name)),
        });
        self
    }

    /// Create a line from plots that will be filled underneath.
    ///
    /// # Example
    ///
    /// ```
    /// let data=[
    ///         [1.0f64,4.0],
    ///         [2.0,5.0],
    ///         [3.0,6.0]
    /// ];
    /// 
    /// let mut plotter = poloto::plot("title","x","y");
    /// plotter.line_fill("",&data);
    /// ```
    pub fn line_fill<I,J>(&mut self, name: impl Display + 'a, plots: I) -> &mut Self
    where
        I: IntoIterator<Item = J>,
        I::IntoIter: Clone + 'a,
        J:Borrow<[f64;2]>
    {
        self.plots.push(Plot {
            plot_type: PlotType::LineFill,
            plots: Box::new(PlotStruct::new(plots.into_iter().map(|x|*x.borrow()), name)),
        });
        self
    }

    /// Create a scatter plot from plots.
    ///
    /// # Example
    ///
    /// ```
    /// let data=[
    ///         [1.0f64,4.0],
    ///         [2.0,5.0],
    ///         [3.0,6.0]
    /// ];
    /// 
    /// let mut plotter = poloto::plot("title","x","y");
    /// plotter.scatter("",&data);
    /// ```
    pub fn scatter<I,J>(&mut self, name: impl Display + 'a, plots: I) -> &mut Self
    where
        I: IntoIterator<Item = J>,
        I::IntoIter: Clone + 'a,
        J:Borrow<[f64;2]>
    {
        self.plots.push(Plot {
            plot_type: PlotType::Scatter,
            plots: Box::new(PlotStruct::new(plots.into_iter().map(|x|*x.borrow()), name)),
        });
        self
    }

    /// Create a histogram from plots.
    /// Each bar's left side will line up with a point
    ///
    /// # Example
    ///
    /// ```
    /// let data=[
    ///         [1.0f64,4.0],
    ///         [2.0,5.0],
    ///         [3.0,6.0]
    /// ];
    /// 
    /// let mut plotter = poloto::plot("title","x","y");
    /// plotter.histogram("",&data);
    /// ```
    pub fn histogram<I,J>(&mut self, name: impl Display + 'a, plots: I) -> &mut Self
    where
        I: IntoIterator<Item = J>,
        I::IntoIter: Clone + 'a,
        J:Borrow<[f64;2]>
    {
        self.plots.push(Plot {
            plot_type: PlotType::Histo,
            plots: Box::new(PlotStruct::new(plots.into_iter().map(|x|*x.borrow()), name)),
        });
        self
    }

    /// When render is called, do not add the default svg tag at the
    /// start and end.
    pub fn without_svg(&mut self) -> &mut Self {
        self.svgtag = SvgTagOption::NoSvg;
        self
    }

    /// Render to a `String`
    pub fn render_to_string(self) -> Result<String, fmt::Error> {
        let mut s = String::new();
        self.render(&mut s)?;
        Ok(s)
    }

    /// Render to a `std::io::Write`
    pub fn render_io<T: std::io::Write>(self, writer: T) -> Result<T, fmt::Error> {
        self.render(tagger::upgrade(writer)).map(|x| x.inner)
    }

    /// Render the svg to the writer.
    ///
    /// Up until now, nothing has been written to the writer. We
    /// have just accumulated a list of commands and closures. This call will
    /// actually call all the closures and consume all the plot iterators.
    pub fn render<T: fmt::Write>(self, writer: T) -> Result<T, fmt::Error> {
        let Plotter {
            names,
            plots,
            svgtag,
        } = self;
        let mut root = tagger::Element::new(writer);

        use crate::build::default_tags::*;

        match svgtag {
            SvgTagOption::Svg => {
                root.elem("svg", |writer| {
                    let (svg, ()) = writer.write(|w| default_svg_attrs(w)?.empty_ok())?;

                    render::render(svg.get_writer(), plots, names)?;
                    svg.empty_ok()
                })?;
            }
            SvgTagOption::NoSvg => {
                render::render(root.get_writer(), plots, names)?;
            }
        }
        Ok(root.into_writer())
    }
}