From 531659bfbd1c7e2857b8fc42b8f08b9d171c2e28 Mon Sep 17 00:00:00 2001 From: javadba Date: Sun, 2 Aug 2015 13:40:09 -0700 Subject: [PATCH 1/2] Support initializing MatrixSeries with an entire fully baked Array --- .../java/org/jfree/data/xy/MatrixSeries.java | 34 ++++++++++++++++++- .../data/xy/MatrixSeriesCollectionTest.java | 24 +++++++++++++ 2 files changed, 57 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/jfree/data/xy/MatrixSeries.java b/src/main/java/org/jfree/data/xy/MatrixSeries.java index 1e44dbc4..52280d7d 100644 --- a/src/main/java/org/jfree/data/xy/MatrixSeries.java +++ b/src/main/java/org/jfree/data/xy/MatrixSeries.java @@ -79,13 +79,41 @@ public MatrixSeries(String name, int rows, int columns) { zeroAll(); } + /** + * Constructs a new matrix series. + *

+ * By default, all matrix items are initialzed to 0. + *

+ * + * @param name series name (null not permitted). + * @param data array of doubles - possibly multidimensional - + * representing the input data + * The arrays must all have the same length. + */ + public MatrixSeries(String name,double[][] data) { + super(name); + if (data == null) { + data = new double[][]{}; + } else { + int ncols = data[0].length; + for (int i = 1; i < data.length; i++) { + if (data[i].length != ncols) { + throw new IllegalArgumentException( + "Row " + i + "has different length " + data[i].length + + " than the first row " + ncols); + } + } + } + this.data = data; + } + /** * Returns the number of columns in this matrix series. * * @return The number of columns in this matrix series. */ public int getColumnsCount() { - return this.data[0].length; + return this.data.length > 0 ? this.data[0].length : 0; } @@ -186,6 +214,10 @@ public void update(int i, int j, double mij) { fireSeriesChanged(); } + public void updateAll(double [][] data) { + this.data = data; + fireSeriesChanged(); + } /** * Sets all matrix values to zero and sends a diff --git a/src/test/java/org/jfree/data/xy/MatrixSeriesCollectionTest.java b/src/test/java/org/jfree/data/xy/MatrixSeriesCollectionTest.java index ad83d45b..cf8f7015 100644 --- a/src/test/java/org/jfree/data/xy/MatrixSeriesCollectionTest.java +++ b/src/test/java/org/jfree/data/xy/MatrixSeriesCollectionTest.java @@ -80,6 +80,30 @@ public void testEquals() { assertEquals(c1, c2); } + @Test + public void testUpdateAll() { + // Test happy case + double[][] data = { {1.0,1.1,1.2},{2.0,2.1,2.2}}; + MatrixSeries s1 = new MatrixSeries("Series", data); + assertEquals(2,s1.getRowCount()); + assertEquals(3,s1.getColumnsCount()); + assertEquals(2.2,s1.get(1,2), 1e-8); + MatrixSeriesCollection c1 = new MatrixSeriesCollection(); + c1.addSeries(s1); + + // Test differing lengths + try { + double[][] data2 = { {1.0,1.1,1,2},{2.0,2.1,2.2}}; + MatrixSeries ss = new MatrixSeries("Series", data); + throw new IllegalArgumentException("Did not detect differing array lengths"); + } catch (IllegalArgumentException ie) {} + + // Test null array + MatrixSeries s2 = new MatrixSeries("Empty Series", null); + assertEquals(s2.getRowCount(),0); + assertEquals(s2.getColumnsCount(),0); + } + /** * Confirm that cloning works. */ From 7252eda95834f7f786981145a04b1263313d1698 Mon Sep 17 00:00:00 2001 From: javadba Date: Sun, 2 Aug 2015 16:39:52 -0700 Subject: [PATCH 2/2] Array bulk add for XYSeries --- src/main/java/org/jfree/data/xy/XYSeries.java | 33 ++++++++++++++++ .../data/xy/MatrixSeriesCollectionTest.java | 2 +- .../java/org/jfree/data/xy/XYSeriesTest.java | 39 +++++++++++++++++++ 3 files changed, 73 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/jfree/data/xy/XYSeries.java b/src/main/java/org/jfree/data/xy/XYSeries.java index c112f748..28c1ddaf 100644 --- a/src/main/java/org/jfree/data/xy/XYSeries.java +++ b/src/main/java/org/jfree/data/xy/XYSeries.java @@ -491,6 +491,39 @@ public void add(Number x, Number y, boolean notify) { add(item, notify); } + /** + * Adds array of new data to the series and, if requested, sends a + * {@link SeriesChangeEvent} to all registered listeners. The + * x and y arrays must be not null and of the same length. + *

+ * Throws an exception if the x-value is a duplicate AND the + * allowDuplicateXValues flag is false. + * + * @param xarr array of x values (null not permitted). + * @param yarr array of y values (null permitted). + * @param notify a flag the controls whether or not a + * {@link SeriesChangeEvent} is sent to all registered + * listeners. + */ + public void add(double[] xarr, double[] yarr, boolean notify) { + if (xarr == null) { + throw new IllegalArgumentException("x array may not be null"); + } + if (yarr == null) { + throw new IllegalArgumentException("y array may not be null"); + } + if (xarr.length != yarr.length) { + throw new IllegalArgumentException("x arraty and y array must be same lengths"); + } + for (int i=0;i