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
/*!
*Views* are plotlib's way of combining multiple representations into a single plot.
It is analogous to a *subplot* in other plotting libraries.

In essence, a view is a collection of representations along with some metadata describing the
extent to plot and information about the axes. It knows how to render itself.
*/

use std;
use std::f64;

use failure::format_err;
use svg::Node;

use crate::axis;
use crate::errors::Result;
use crate::grid::{Grid, GridType};
use crate::repr::{CategoricalRepresentation, ContinuousRepresentation};
use crate::svg_render;
use crate::text_render;

pub trait View {
    fn to_svg(&self, face_width: f64, face_height: f64) -> Result<svg::node::element::Group>;
    fn to_text(&self, face_width: u32, face_height: u32) -> Result<String>;
    fn add_grid(&mut self, grid: Grid);
    fn grid(&self) -> &Option<Grid>;
}

/// Standard 1-dimensional view with a continuous x-axis
#[derive(Default)]
pub struct ContinuousView {
    representations: Vec<Box<dyn ContinuousRepresentation>>,
    x_range: Option<axis::Range>,
    y_range: Option<axis::Range>,
    x_max_ticks: usize,
    y_max_ticks: usize,
    x_label: Option<String>,
    y_label: Option<String>,
    grid: Option<Grid>,
}

impl ContinuousView {
    /// Create an empty view
    pub fn new() -> ContinuousView {
        ContinuousView {
            representations: vec![],
            x_range: None,
            y_range: None,
            x_max_ticks: 6,
            y_max_ticks: 6,
            x_label: None,
            y_label: None,
            grid: None,
        }
    }
    /// Set the maximum number of ticks along the x axis.
    pub fn x_max_ticks(mut self, val: usize) -> Self {
        self.x_max_ticks = val;
        self
    }
    /// Set the maximum number of ticks along the y axis.
    pub fn y_max_ticks(mut self, val: usize) -> Self {
        self.y_max_ticks = val;
        self
    }

    /// Add a representation to the view
    pub fn add<R: ContinuousRepresentation + 'static>(mut self, repr: R) -> Self {
        self.representations.push(Box::new(repr));
        self
    }

    /// Set the x range for the view
    pub fn x_range(mut self, min: f64, max: f64) -> Self {
        self.x_range = Some(axis::Range::new(min, max));
        self
    }

    /// Set the y range for the view
    pub fn y_range(mut self, min: f64, max: f64) -> Self {
        self.y_range = Some(axis::Range::new(min, max));
        self
    }

    /// Set the label for the x-axis
    pub fn x_label<T>(mut self, value: T) -> Self
    where
        T: Into<String>,
    {
        self.x_label = Some(value.into());
        self
    }

    /// Set the label for the y-axis
    pub fn y_label<T>(mut self, value: T) -> Self
    where
        T: Into<String>,
    {
        self.y_label = Some(value.into());
        self
    }

    fn default_x_range(&self) -> axis::Range {
        let mut x_min = f64::INFINITY;
        let mut x_max = f64::NEG_INFINITY;
        for repr in &self.representations {
            let (this_x_min, this_x_max) = repr.range(0);
            x_min = x_min.min(this_x_min);
            x_max = x_max.max(this_x_max);
        }
        axis::Range::new(x_min, x_max)
    }

    fn default_y_range(&self) -> axis::Range {
        let mut y_min = f64::INFINITY;
        let mut y_max = f64::NEG_INFINITY;
        for repr in &self.representations {
            let (this_y_min, this_y_max) = repr.range(1);
            y_min = y_min.min(this_y_min);
            y_max = y_max.max(this_y_max);
        }
        axis::Range::new(y_min, y_max)
    }

    fn create_axes(&self) -> Result<(axis::ContinuousAxis, axis::ContinuousAxis)> {
        let default_x_range = self.default_x_range();
        let x_range = self.x_range.as_ref().unwrap_or(&default_x_range);
        if !x_range.is_valid() {
            return Err(format_err!(
                "Invalid x_range: {} >= {}. Please specify the x_range manually.",
                x_range.lower,
                x_range.upper
            ));
        }

        let default_y_range = self.default_y_range();
        let y_range = self.y_range.as_ref().unwrap_or(&default_y_range);
        if !y_range.is_valid() {
            return Err(format_err!(
                "Invalid y_range: {} >= {}. Please specify the y_range manually.",
                y_range.lower,
                y_range.upper
            ));
        }

        let x_label: String = self.x_label.clone().unwrap_or_else(|| "".to_string());
        let y_label: String = self.y_label.clone().unwrap_or_else(|| "".to_string());

        let x_axis = axis::ContinuousAxis::new(x_range.lower, x_range.upper, self.x_max_ticks)
            .label(x_label);
        let y_axis = axis::ContinuousAxis::new(y_range.lower, y_range.upper, self.y_max_ticks)
            .label(y_label);

        Ok((x_axis, y_axis))
    }
}

impl View for ContinuousView {
    /**
    Create an SVG rendering of the view
    */
    fn to_svg(&self, face_width: f64, face_height: f64) -> Result<svg::node::element::Group> {
        let mut view_group = svg::node::element::Group::new();

        let (x_axis, y_axis) = self.create_axes()?;

        let (legend_x, mut legend_y) = (face_width - 100., -face_height);
        if let Some(grid) = &self.grid {
            view_group.append(svg_render::draw_grid(
                GridType::Both(grid),
                face_width,
                face_height,
            ));
        }

        // Then, based on those ranges, draw each repr as an SVG
        for repr in &self.representations {
            let repr_group = repr.to_svg(&x_axis, &y_axis, face_width, face_height);
            view_group.append(repr_group);

            if let Some(legend_group) = repr.legend_svg() {
                view_group.append(legend_group.set(
                    "transform",
                    format!("translate({}, {})", legend_x, legend_y),
                ));
                legend_y += 18.;
            }
        }

        // Add in the axes
        view_group.append(svg_render::draw_x_axis(&x_axis, face_width));
        view_group.append(svg_render::draw_y_axis(&y_axis, face_height));

        Ok(view_group)
    }

    /**
    Create a text rendering of the view
    */
    fn to_text(&self, face_width: u32, face_height: u32) -> Result<String> {
        let (x_axis, y_axis) = self.create_axes()?;

        let (y_axis_string, longest_y_label_width) =
            text_render::render_y_axis_strings(&y_axis, face_height);

        let (x_axis_string, start_offset) = text_render::render_x_axis_strings(&x_axis, face_width);

        let left_gutter_width = std::cmp::max(
            longest_y_label_width as i32 + 3,
            start_offset.wrapping_neg(),
        ) as u32;

        let view_width = face_width + 1 + left_gutter_width + 1;
        let view_height = face_height + 4;

        let blank: Vec<String> = (0..view_height)
            .map(|_| (0..view_width).map(|_| ' ').collect())
            .collect();
        let mut view_string = blank.join("\n");

        for repr in &self.representations {
            let face_string = repr.to_text(&x_axis, &y_axis, face_width, face_height);
            view_string =
                text_render::overlay(&view_string, &face_string, left_gutter_width as i32 + 1, 0);
        }

        let view_string = text_render::overlay(
            &view_string,
            &y_axis_string,
            left_gutter_width as i32 - 2 - longest_y_label_width,
            0,
        );
        let view_string = text_render::overlay(
            &view_string,
            &x_axis_string,
            left_gutter_width as i32,
            face_height as i32,
        );

        Ok(view_string)
    }

    fn add_grid(&mut self, grid: Grid) {
        self.grid = Some(grid)
    }

    fn grid(&self) -> &Option<Grid> {
        &self.grid
    }
}

/// A view with categorical entries along the x-axis and continuous values along the y-axis
#[derive(Default)]
pub struct CategoricalView {
    representations: Vec<Box<dyn CategoricalRepresentation>>,
    x_range: Option<Vec<String>>,
    y_range: Option<axis::Range>,
    x_label: Option<String>,
    y_label: Option<String>,
    grid: Option<Grid>,
}

impl CategoricalView {
    /**
    Create an empty view
    */
    pub fn new() -> CategoricalView {
        CategoricalView {
            representations: vec![],
            x_range: None,
            y_range: None,
            x_label: None,
            y_label: None,
            grid: None,
        }
    }

    /**
    Add a representation to the view
    */
    pub fn add<R: CategoricalRepresentation + 'static>(mut self, repr: R) -> Self {
        self.representations.push(Box::new(repr));
        self
    }

    /**
    Set the x range for the view
    */
    pub fn x_ticks(mut self, ticks: &[String]) -> Self {
        self.x_range = Some(ticks.into());
        self
    }

    /**
    Set the y range for the view
    */
    pub fn y_range(mut self, min: f64, max: f64) -> Self {
        self.y_range = Some(axis::Range::new(min, max));
        self
    }

    /**
    Set the label for the x-axis
    */
    pub fn x_label<T>(mut self, value: T) -> Self
    where
        T: Into<String>,
    {
        self.x_label = Some(value.into());
        self
    }

    /**
    Set the label for the y-axis
    */
    pub fn y_label<T>(mut self, value: T) -> Self
    where
        T: Into<String>,
    {
        self.y_label = Some(value.into());
        self
    }

    fn default_x_ticks(&self) -> Vec<String> {
        let mut v = vec![];
        for repr in &self.representations {
            for l in repr.ticks() {
                if !v.contains(&l) {
                    v.push(l.clone());
                }
            }
        }
        v
    }

    fn default_y_range(&self) -> axis::Range {
        let mut y_min = f64::INFINITY;
        let mut y_max = f64::NEG_INFINITY;
        for repr in &self.representations {
            let (this_y_min, this_y_max) = repr.range();
            y_min = y_min.min(this_y_min);
            y_max = y_max.max(this_y_max);
        }
        let buffer = (y_max - y_min) / 10.;
        let y_min = if y_min == 0.0 { y_min } else { y_min - buffer };
        let y_max = y_max + buffer;
        axis::Range::new(y_min, y_max)
    }

    fn create_axes(&self) -> Result<(axis::CategoricalAxis, axis::ContinuousAxis)> {
        let default_x_ticks = self.default_x_ticks();
        let x_range = self.x_range.as_ref().unwrap_or(&default_x_ticks);

        let default_y_range = self.default_y_range();
        let y_range = self.y_range.as_ref().unwrap_or(&default_y_range);

        if !y_range.is_valid() {
            return Err(format_err!("invalid y_range: {:?}", y_range));
        }

        let default_x_label = "".to_string();
        let x_label: String = self.x_label.clone().unwrap_or(default_x_label);

        let default_y_label = "".to_string();
        let y_label: String = self.y_label.clone().unwrap_or(default_y_label);

        let x_axis = axis::CategoricalAxis::new(x_range).label(x_label);
        let y_axis = axis::ContinuousAxis::new(y_range.lower, y_range.upper, 6).label(y_label);

        Ok((x_axis, y_axis))
    }
}

impl View for CategoricalView {
    fn to_svg(&self, face_width: f64, face_height: f64) -> Result<svg::node::element::Group> {
        let mut view_group = svg::node::element::Group::new();

        let (x_axis, y_axis) = self.create_axes()?;

        if let Some(grid) = &self.grid {
            view_group.append(svg_render::draw_grid(
                GridType::HorizontalOnly(grid),
                face_width,
                face_height,
            ));
        }

        // Then, based on those ranges, draw each repr as an SVG
        for repr in &self.representations {
            let repr_group = repr.to_svg(&x_axis, &y_axis, face_width, face_height);
            view_group.append(repr_group);
        }

        // Add in the axes
        view_group.append(svg_render::draw_categorical_x_axis(&x_axis, face_width));
        view_group.append(svg_render::draw_y_axis(&y_axis, face_height));

        Ok(view_group)
    }

    fn to_text(&self, _face_width: u32, _face_height: u32) -> Result<String> {
        Ok("".into())
    }

    fn add_grid(&mut self, grid: Grid) {
        self.grid = Some(grid);
    }

    fn grid(&self) -> &Option<Grid> {
        &self.grid
    }
}

/*pub struct AnyView<'a> {
    representations: Vec<&'a Representation>,
    axes: Vec<>,
    x_range: Option<axis::Range>,
    y_range: Option<axis::Range>,
    x_label: Option<String>,
    y_label: Option<String>,
}*/