From 5c8353b2bbc1d6850b65441502429f00e5143b18 Mon Sep 17 00:00:00 2001
From: timmolter <tim.molter@gmail.com>
Date: Fri, 24 Aug 2012 16:03:57 +0200
Subject: [PATCH] new Constructor - The number of rows and columns will be
 calculated automatically

---
 .../com/xeiam/xchart/swing/SwingWrapper.java  | 19 +++++++++++++++++++
 .../com/xeiam/xchart/example/Example4.java    | 11 ++++-------
 2 files changed, 23 insertions(+), 7 deletions(-)

diff --git a/src/main/java/com/xeiam/xchart/swing/SwingWrapper.java b/src/main/java/com/xeiam/xchart/swing/SwingWrapper.java
index 37d374fe..f571d3dd 100644
--- a/src/main/java/com/xeiam/xchart/swing/SwingWrapper.java
+++ b/src/main/java/com/xeiam/xchart/swing/SwingWrapper.java
@@ -51,6 +51,8 @@ public class SwingWrapper {
    * Deprecated Constructor - use the one that takes a Collection! This will be removed in next version.
    * 
    * @param charts
+   * @param numRows
+   * @param numColumns
    */
   @Deprecated
   public SwingWrapper(Chart[] charts, int numRows, int numColumns) {
@@ -62,10 +64,27 @@ public class SwingWrapper {
     this.numColumns = numColumns;
   }
 
+  /**
+   * Constructor - The number of rows and columns will be calculated automatically
+   * 
+   * @param charts
+   * @param numRows
+   * @param numColumns
+   */
+  public SwingWrapper(List<Chart> charts) {
+
+    this.charts = charts;
+
+    this.numRows = (int) (Math.sqrt(charts.size()) + .5);
+    this.numColumns = (int) ((double) charts.size() / this.numRows + 1);
+  }
+
   /**
    * Constructor
    * 
    * @param charts
+   * @param numRows - the number of rows
+   * @param numColumns - the number of columns
    */
   public SwingWrapper(List<Chart> charts, int numRows, int numColumns) {
 
diff --git a/src/test/java/com/xeiam/xchart/example/Example4.java b/src/test/java/com/xeiam/xchart/example/Example4.java
index 1978556a..61ea43f5 100644
--- a/src/test/java/com/xeiam/xchart/example/Example4.java
+++ b/src/test/java/com/xeiam/xchart/example/Example4.java
@@ -31,17 +31,14 @@ public class Example4 {
 
   public static void main(String[] args) {
 
-    int numRows = 2;
-    int numCols = 2;
+    int numCharts = 4;
 
     List<Chart> charts = new ArrayList<Chart>();
 
-    for (int i = 0; i < numRows; i++) {
-      for (int j = 0; j < numCols; j++) {
-        charts.add(QuickChart.getChart(i + "," + j, "X", "Y", null, null, getRandomWalk(1000)));
-      }
+    for (int i = 0; i < numCharts; i++) {
+      charts.add(QuickChart.getChart("" + i, "X", "Y", null, null, getRandomWalk(1000)));
     }
-    new SwingWrapper(charts, numRows, numCols).displayChartMatrix();
+    new SwingWrapper(charts).displayChartMatrix();
   }
 
   /**
-- 
GitLab