Repository: milliams/plotlib (language: rust)

Issue:
API for specifying page dimensions
Hi, I'm starting to use this crate to create plots as examples of my [point process simulation library](https://github.com/ManifoldFR/point-process-rust), and I thought it'd be nice if one could use custom dimensions for plots rather than the hard-coded ones.

Right now, I'm getting around it by getting the SVG document using the `as_view()` method from the `view::View` trait and manually saving the file.

If you'd like to, maybe I could make a pull request for this ?


Discussion / hints:
Hi @ManifoldFR, glad to hear you're using the library. I'd welcome a PR indeed!
Well then, I'll get onto it !

Fix patch:
diff --git a/.gitignore b/.gitignore
index f47f014..793d95c 100644
--- a/.gitignore
+++ b/.gitignore
@@ -3,3 +3,4 @@ Cargo.lock
 *.bk
 .vscode
 .idea
+/*.svg
\ No newline at end of file
diff --git a/.travis.yml b/.travis.yml
index 22761ba..2a6d259 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -1,1 +1,15 @@
 language: rust
+rust:
+  - stable
+  - nightly
+
+matrix:
+  allow_failures:
+    - rust: nightly
+  fast_finish: true
+
+cache: cargo
+
+script:
+  - cargo build --verbose
+  - cargo test --verbose
\ No newline at end of file
diff --git a/Cargo.toml b/Cargo.toml
index c769e7b..705687a 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -13,4 +13,4 @@ keywords = ["plotting", "plot", "graph", "chart", "histogram", "scatter"]
 travis-ci = { repository = "milliams/plotlib" }
 
 [dependencies]
-svg = "0.5.10"
+svg = "0.5.11"
diff --git a/examples/boxplot_svg.rs b/examples/boxplot_svg.rs
index f6b66ff..406ec6b 100644
--- a/examples/boxplot_svg.rs
+++ b/examples/boxplot_svg.rs
@@ -11,6 +11,7 @@ fn main() {
     let v = plotlib::view::DiscreteView::new()
         .add(&b1)
         .add(&b2)
-        .x_label("Experiment");
-    plotlib::page::Page::single(&v).save("boxplot.svg");
+        .x_label("Experiment")
+        .y_label("y");
+    plotlib::page::Page::single(&v).dimensions(400, 300).save("boxplot.svg");
 }
diff --git a/src/page.rs b/src/page.rs
index e7fe371..b15ff06 100644
--- a/src/page.rs
+++ b/src/page.rs
@@ -17,6 +17,7 @@ A single page page laying out the views in a grid
 pub struct Page<'a> {
     views: Vec<&'a View>,
     num_views: u32,
+    dimensions: (u32, u32)
 }
 
 impl<'a> Page<'a> {
@@ -27,18 +28,39 @@ impl<'a> Page<'a> {
         Page {
             views: vec![view],
             num_views: 1,
+            dimensions: (600, 400)
         }
     }
 
+    /// Set the dimensions of the plot.
+    pub fn dimensions(mut self, x: u32, y: u32) -> Self {
+        self.dimensions = (x,y);
+        self
+    }
+
+    /// Add a view to the plot
+    pub fn add_plot(mut self, view: &'a View) -> Self {
+        self.views.push(view);
+        self.num_views += 1;
+        self
+    }
+
     /**
     Render the plot to an svg document
     */
     pub fn to_svg(&self) -> svg::Document {
-        let mut document = Document::new().set("viewBox", (0, 0, 600, 400));
+        let (width, height) = self.dimensions;
+        let mut document = Document::new().set("viewBox", (0, 0, width, height));
+
+        let x_margin = 80;
+        let y_margin = 60;
+        let x_offset = 0.6*x_margin as f64;
+        let y_offset = 0.6*y_margin as f64;
+
         // TODO put multiple views in correct places
         for &view in &self.views {
-            let view_group = view.to_svg(500., 340.)
-                .set("transform", format!("translate({}, {})", 50, 360));
+            let view_group = view.to_svg((width-x_margin) as f64, (height-y_margin) as f64)
+                .set("transform", format!("translate({}, {})", x_offset, height as f64-y_offset));
             document.append(view_group);
         }
         document


Test patch: