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
//!
//! Build blocks for making custom plots
//!
use super::*;

///The number of unique colors.
pub const NUM_COLORS: usize = 8;

///Used internally to implement [`Names`]
pub struct NamesStruct<A, B, C, D> {
    title: A,
    xname: B,
    yname: C,
    header: D,
}
impl<A: Display, B: Display, C: Display, D: Display> Names for NamesStruct<A, B, C, D> {
    fn write_header(&self, fm: &mut fmt::Formatter) -> fmt::Result {
        self.header.fmt(fm)
    }
    fn write_title(&self, fm: &mut fmt::Formatter) -> fmt::Result {
        self.title.fmt(fm)
    }
    fn write_xname(&self, fm: &mut fmt::Formatter) -> fmt::Result {
        self.xname.fmt(fm)
    }
    fn write_yname(&self, fm: &mut fmt::Formatter) -> fmt::Result {
        self.yname.fmt(fm)
    }
}

///Used internally to write out the header/title/xname/yname.
pub trait Names {
    fn write_header(&self, fm: &mut fmt::Formatter) -> fmt::Result;
    fn write_title(&self, fm: &mut fmt::Formatter) -> fmt::Result;
    fn write_xname(&self, fm: &mut fmt::Formatter) -> fmt::Result;
    fn write_yname(&self, fm: &mut fmt::Formatter) -> fmt::Result;
}

///Contains building blocks for create the default svg an styling tags from scratch.
pub mod default_tags {
    use core::fmt;

    ///The class of the svg tag.
    pub const CLASS: &str = "poloto";
    ///The width of the svg tag.
    pub const WIDTH: f64 = 800.0;
    ///The height of the svg tag.
    pub const HEIGHT: f64 = 500.0;
    ///The xmlns: `http://www.w3.org/2000/svg`
    pub const XMLNS: &str = "http://www.w3.org/2000/svg";

    ///Write default svg tag attributes.
    pub fn default_svg_attrs<'a, 'b, T: fmt::Write>(
        w: &'a mut tagger::AttributeWriter<'b, T>,
    ) -> Result<&'a mut tagger::AttributeWriter<'b, T>, fmt::Error> {
        use tagger::prelude::*;

        w.attr("class", CLASS)?
            .attr("width", WIDTH)?
            .attr("height", HEIGHT)?
            .with_attr("viewBox", wr!("0 0 {} {}", WIDTH, HEIGHT))?
            .attr("xmlns", XMLNS)
    }
}
///Create a custom style poloto style
pub struct StyleBuilder<A, B, C> {
    text_color: A,
    back_color: B,
    colors: [C; NUM_COLORS],
}
impl Default for StyleBuilder<&'static str, &'static str, &'static str> {
    fn default() -> Self {
        Self::new()
    }
}

impl StyleBuilder<&'static str, &'static str, &'static str> {
    pub fn new() -> Self {
        StyleBuilder {
            text_color: "black",
            back_color: "aliceblue",
            colors: [
                "blue",
                "red",
                "green",
                "gold",
                "aqua",
                "brown",
                "lime",
                "chocolate",
            ],
        }
    }
}
impl<A: Display, B: Display, C: Display> StyleBuilder<A, B, C> {
    pub fn with_text_color<X: Display>(self, text_color: X) -> StyleBuilder<X, B, C> {
        StyleBuilder {
            text_color,
            back_color: self.back_color,
            colors: self.colors,
        }
    }
    pub fn with_back_color<X: Display>(self, back_color: X) -> StyleBuilder<A, X, C> {
        StyleBuilder {
            text_color: self.text_color,
            back_color,
            colors: self.colors,
        }
    }

    pub fn with_colors<X: Display>(self, colors: [X; NUM_COLORS]) -> StyleBuilder<A, B, X> {
        StyleBuilder {
            text_color: self.text_color,
            back_color: self.back_color,
            colors,
        }
    }
    pub fn build_with_css_variables(self) -> impl Display {
        let StyleBuilder {
            text_color,
            back_color,
            colors,
        } = self;
        moveable_format(move |w| {
            write!(
                w,
                r###"<style>.poloto {{
                    font-family: "Arial";
                    stroke-width:2;
                    }}
                    .poloto_text{{fill: var(--poloto_fg_color,{0});  }}
                    .poloto_axis_lines{{stroke: var(--poloto_fg_color,{0});stoke-width:3;fill:none}}
                    .poloto_background{{fill: var(--poloto_bg_color,{1}); }}
                    .poloto0stroke{{stroke:  var(--poloto_color0,{2}); }}
                    .poloto1stroke{{stroke:  var(--poloto_color1,{3}); }}
                    .poloto2stroke{{stroke:  var(--poloto_color2,{4}); }}
                    .poloto3stroke{{stroke:  var(--poloto_color3,{5}); }}
                    .poloto4stroke{{stroke:  var(--poloto_color4,{6}); }}
                    .poloto5stroke{{stroke:  var(--poloto_color5,{7}); }}
                    .poloto6stroke{{stroke:  var(--poloto_color6,{8}); }}
                    .poloto7stroke{{stroke:  var(--poloto_color7,{9}); }}
                    .poloto0fill{{fill:var(--poloto_color0,{2});}}
                    .poloto1fill{{fill:var(--poloto_color1,{3});}}
                    .poloto2fill{{fill:var(--poloto_color2,{4});}}
                    .poloto3fill{{fill:var(--poloto_color3,{5});}}
                    .poloto4fill{{fill:var(--poloto_color4,{6});}}
                    .poloto5fill{{fill:var(--poloto_color5,{7});}}
                    .poloto6fill{{fill:var(--poloto_color6,{8});}}
                    .poloto7fill{{fill:var(--poloto_color7,{9});}}</style>"###,
                text_color,
                back_color,
                colors[0],
                colors[1],
                colors[2],
                colors[3],
                colors[4],
                colors[5],
                colors[6],
                colors[7]
            )
        })
    }
    pub fn build(self) -> impl Display {
        let StyleBuilder {
            text_color,
            back_color,
            colors,
        } = self;
        moveable_format(move |w| {
            write!(
                w,
                r###"<style>.poloto {{
                font-family: "Arial";
                stroke-width:2;
                }}
                .poloto_text{{fill: {0};  }}
                .poloto_axis_lines{{stroke: {0};stoke-width:3;fill:none}}
                .poloto_background{{fill: {1}; }}
                .poloto0stroke{{stroke:  {2}; }}
                .poloto1stroke{{stroke:  {3}; }}
                .poloto2stroke{{stroke:  {4}; }}
                .poloto3stroke{{stroke:  {5}; }}
                .poloto4stroke{{stroke:  {6}; }}
                .poloto5stroke{{stroke:  {7}; }}
                .poloto6stroke{{stroke:  {8}; }}
                .poloto7stroke{{stroke:  {9}; }}
                .poloto0fill{{fill:{2};}}
                .poloto1fill{{fill:{3};}}
                .poloto2fill{{fill:{4};}}
                .poloto3fill{{fill:{5};}}
                .poloto4fill{{fill:{6};}}
                .poloto5fill{{fill:{7};}}
                .poloto6fill{{fill:{8};}}
                .poloto7fill{{fill:{9};}}</style>"###,
                text_color,
                back_color,
                colors[0],
                colors[1],
                colors[2],
                colors[3],
                colors[4],
                colors[5],
                colors[6],
                colors[7],
            )
        })
    }
}

///Insert svg data after the svg element, but before the plot elements.
pub struct HeaderBuilder<D: Display> {
    header: D,
}

impl Default for HeaderBuilder<&'static str> {
    fn default() -> Self {
        Self::new()
    }
}

impl HeaderBuilder<&'static str> {
    pub fn new() -> Self {
        HeaderBuilder { header: "" }
    }
}
impl<D: Display> HeaderBuilder<D> {
    ///Push the default poloto css styling.
    pub fn push_css_default(self) -> HeaderBuilder<impl Display> {
        HeaderBuilder {
            header: concatenate_display("", self.header, StyleBuilder::new().build()),
        }
    }

    /// Instead of the default style, use one that adds variables.
    ///
    /// This injects what is produced by [`StyleBuilder::build_with_css_variables`] instead of
    /// the default [`StyleBuilder::build`].
    ///
    /// If you embed the generated svg into a html file,
    /// then you can add this example:
    /// ```css
    /// .poloto{
    ///    --poloto_bg_color:"black";
    ///    --poloto_fg_color:"white;
    ///    --poloto_color0:"red";
    ///    --poloto_color1:"green";
    ///    --poloto_color2:"yellow";
    ///    --poloto_color3:"orange";
    ///    --poloto_color4:"purple";
    ///    --poloto_color5:"pink";
    ///    --poloto_color6:"aqua";
    ///    --poloto_color7:"red";
    /// }
    /// ```  
    /// By default these variables are not defined, so the svg falls back on some default colors.
    pub fn push_default_css_with_variable(self) -> HeaderBuilder<impl Display> {
        HeaderBuilder {
            header: concatenate_display(
                "",
                self.header,
                StyleBuilder::new().build_with_css_variables(),
            ),
        }
    }
    /// User can inject some svg elements using this function.
    /// They will be inserted right after the svg and default svg tags.
    ///
    /// You can override the css in regular html if you embed the generated svg.
    /// This gives you a lot of flexibility giving your the power to dynamically
    /// change the theme of your svg.
    ///
    /// However, if you want to embed the svg as an image, you lose this ability.
    /// If embedding as IMG is desired, instead the user can insert a custom style into the generated svg itself.
    ///
    pub fn push(self, a: impl fmt::Display) -> HeaderBuilder<impl Display> {
        HeaderBuilder {
            header: concatenate_display("", self.header, a),
        }
    }
    pub fn build(self) -> D {
        self.header
    }
}

///If [`plot`] isn't good enough, use this struct for more control.
pub struct PlotterBuilder<D: fmt::Display> {
    header: D,
    svgtag: bool,
}
impl Default for PlotterBuilder<&'static str> {
    fn default() -> Self {
        Self::new()
    }
}
impl PlotterBuilder<&'static str> {
    pub fn new() -> Self {
        PlotterBuilder {
            header: "",
            svgtag: true,
        }
    }
    pub fn with_header<J: Display>(self, header: J) -> PlotterBuilder<J> {
        PlotterBuilder {
            header,
            svgtag: self.svgtag,
        }
    }
    pub fn with_svg(mut self, svg: bool) -> Self {
        self.svgtag = svg;
        self
    }
}

impl<'a, D: Display + 'a> PlotterBuilder<D> {
    pub fn build<A: Display + 'a, B: Display + 'a, C: Display + 'a>(
        self,
        title: A,
        xname: B,
        yname: C,
    ) -> Plotter<'a, NamesStruct<A, B, C, D>> {
        let svgtag = if self.svgtag {
            SvgTagOption::Svg
        } else {
            SvgTagOption::NoSvg
        };

        Plotter {
            names: NamesStruct {
                title,
                xname,
                yname,
                header: self.header,
            },
            plots: Vec::new(),
            svgtag,
        }
    }
}