Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion src/main/java/org/jfree/data/xy/MatrixSeries.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,41 @@ public MatrixSeries(String name, int rows, int columns) {
zeroAll();
}

/**
* Constructs a new matrix series.
* <p>
* By default, all matrix items are initialzed to 0.
* </p>
*
* @param name series name (<code>null</code> 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;
}


Expand Down Expand Up @@ -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
Expand Down
33 changes: 33 additions & 0 deletions src/main/java/org/jfree/data/xy/XYSeries.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
* <P>
* Throws an exception if the x-value is a duplicate AND the
* allowDuplicateXValues flag is false.
*
* @param xarr array of x values (<code>null</code> not permitted).
* @param yarr array of y values (<code>null</code> 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<xarr.length;i++) {
add(new XYDataItem(xarr[i], yarr[i]), false);
}
if (notify) {
fireSeriesChanged();
}

}

/**
* Adds a data item to the series and, if requested, sends a
* {@link SeriesChangeEvent} to all registered listeners.
Expand Down
24 changes: 24 additions & 0 deletions src/test/java/org/jfree/data/xy/MatrixSeriesCollectionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,30 @@ public void testEquals() {
assertEquals(c1, c2);
}

@Test
public void testAddEntireArray() {
// 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.
*/
Expand Down
39 changes: 39 additions & 0 deletions src/test/java/org/jfree/data/xy/XYSeriesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,45 @@ public void testEquals() {
assertEquals(s1, s2);
}

@Test
public void testAddEntireArray() {
// Test happy case with autosort =true
// The first element should be pushed to the end
// since autosort is set to true
XYSeries s1 = new XYSeries("Series",true);
double[] x= {1.4,1.0,1.1,1.2,1.3};
double[] y = { 32.0,2.0,4.0,8.0,16.0};
s1.add(x,y, true);
double [][] sarr = s1.toArray();
assert sarr[0].length == x.length;
assert sarr[1].length == y.length;

double[] xexpect= {1.0,1.1,1.2,1.3, 1.4};
double[] yexpect = { 2.0,4.0,8.0,16.0, 32.0};
for (int i=0;i< x.length; i++) {
assertEquals(xexpect[i],sarr[0][i], 1e-8);
assertEquals(yexpect[i],sarr[1][i], 1e-8);
}
assertEquals(1.4, s1.getX(x.length - 1).doubleValue(),1e-8);
assertEquals(32.0,s1.getY(x.length - 1).doubleValue(),1e-8);

// Test differing lengths
try {
double[] x2= {1.4,1.0,1.1,1.2,1.3};
double[] y2 = { 32.0,2.0,4.0,8.0,16.0};
s1.add(x2,y2, true);
throw new IllegalArgumentException("Did not detect differing array lengths");
} catch (IllegalArgumentException ie) {}

// Test null array
try {
double[] x3= null;
double[] y3 = {};
s1.add(x3,y3, true);
throw new IllegalArgumentException("Did not detect null array(s)");
} catch (IllegalArgumentException ie) {}
}

/**
* Some simple checks for the hashCode() method.
*/
Expand Down