Hi. I am very rusty on multi-dimensional arrays and am trying to do the following: (pseudo code)
database.getData() // returns Number[][]
... /not sure how to convert here
Object[][][] = [don't want to specify series] (data appended [][])
XYDataSource = new DefaultXYDataSource(Object[][][]);
plot xydatasource.
Am I approaching this wrong? I can plot the XYdatasource fine until I try to merge the two demensional number array into the 3 dimensional object array. I know the array size of the Number[][] array ([resultsetsize][2 (x,y)]). Can anyone help?
Thanks!
-Glenn
XYDataSource creation
RE: XYDataSource creation
I realized that my post my not be clear. The end result I'm looking for is to plot the Number[][] where each Number[][0]=x and Number[][1]=y.
Thanks!
Thanks!
RE: XYDataSource creation
Hi Glenn,
What you should do is create your own data source...just a regular class that implements the XYDataSource interface. Your class should hold a reference to your two-dimensional array, which you can pass in via the constructor. Your implementation of each method in XYDataSource should just fetch the required data from the array.
Your finished class will look quite similar to DefaultXYDataSource, but you'll avoid copying data from one array to another...
Regards,
DG.
What you should do is create your own data source...just a regular class that implements the XYDataSource interface. Your class should hold a reference to your two-dimensional array, which you can pass in via the constructor. Your implementation of each method in XYDataSource should just fetch the required data from the array.
Your finished class will look quite similar to DefaultXYDataSource, but you'll avoid copying data from one array to another...
Regards,
DG.
RE: XYDataSource creation
First, I did get the chart to work....and it works GREAT I might add.
However, I still have a bug in my implementation of the XYDataSource.
I created the source and am getting a null pointer exception. It is when getXValue and getYValue are called, so I know it has to be with my implementation of the internal data structure. If anyone can help I'd appreciate it. Here's my code.
I removed the comments to make it easier to read.
//database information is read into a vector.
//the datasource is initialized with the vector with the
//method initData(Vector v) after that, I hand it over to
//JFreeChart
//The reported number of items (from initData() is 512.)
import com.jrefinery.chart.*;
import java.util.Vector;
public class PQXYDataSource extends AbstractDataSource implements XYDataSource
{
Float[][] data = null;
int items = 0;
public PQXYDataSource()
{
System.out.println("DataSource initialized");
}
public Number getXValue(int series, int item)
{
return data[item][0];
}
public Number getYValue(int series, int item)
{
return data[item][1];
}
public int getSeriesCount()
{
return 1;
}
public String getSeriesName(int series)
{
if (series==0)
{
return "Series 1";
}
else return "Error, series " + series + " not defined.";
}
public int getItemCount(int series)
{
return items;
}
/**
* Initializes the internal array structure for the series.
* @param v The vector containing the series;
*/
public void initData(Vector v)
{
items = v.size();
if (v.size() > 0)
return;
data = new Float[v.size()][2];
for (int i = 0; i < v.size(); i++)
{
float tmp[] = (float[])v.elementAt(i);
data[0] = new Float(tmp[0]);
data[1] = new Float(tmp[1]);
}
System.out.println("Items=" + items);
}
}
However, I still have a bug in my implementation of the XYDataSource.
I created the source and am getting a null pointer exception. It is when getXValue and getYValue are called, so I know it has to be with my implementation of the internal data structure. If anyone can help I'd appreciate it. Here's my code.
I removed the comments to make it easier to read.
//database information is read into a vector.
//the datasource is initialized with the vector with the
//method initData(Vector v) after that, I hand it over to
//JFreeChart
//The reported number of items (from initData() is 512.)
import com.jrefinery.chart.*;
import java.util.Vector;
public class PQXYDataSource extends AbstractDataSource implements XYDataSource
{
Float[][] data = null;
int items = 0;
public PQXYDataSource()
{
System.out.println("DataSource initialized");
}
public Number getXValue(int series, int item)
{
return data[item][0];
}
public Number getYValue(int series, int item)
{
return data[item][1];
}
public int getSeriesCount()
{
return 1;
}
public String getSeriesName(int series)
{
if (series==0)
{
return "Series 1";
}
else return "Error, series " + series + " not defined.";
}
public int getItemCount(int series)
{
return items;
}
/**
* Initializes the internal array structure for the series.
* @param v The vector containing the series;
*/
public void initData(Vector v)
{
items = v.size();
if (v.size() > 0)
return;
data = new Float[v.size()][2];
for (int i = 0; i < v.size(); i++)
{
float tmp[] = (float[])v.elementAt(i);
data[0] = new Float(tmp[0]);
data[1] = new Float(tmp[1]);
}
System.out.println("Items=" + items);
}
}
RE: XYDataSource creation
I fixed my own problem. I'll post the complete solutions (database code and all) later. It'll be in the form of a URL.
Thanks for your help, David.
-Glenn
Thanks for your help, David.
-Glenn