ResultSet to XYDataset

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
padaek

ResultSet to XYDataset

Post by padaek » Mon Feb 25, 2002 8:56 pm

Hi, I'm trying to convert a ResultSet to XYDataset, then create a scatter plot, but encountered numerous error messages, namely java.lang.NullPointerException. Any help would be greatly appreciated.

The main function is:
public static void main(String[] args) {

java.sql.ResultSet rs = jdbc.getResults2("select te,tce from table8 where te > 5000");
MyXYDataset xyData = new MyXYDataset(rs);
JFreeChart sChart = ChartFactory.createScatterPlot("Scatter Plot","xLabel","yLabel",xyData,true);
JFreeChartFrame frame = new JFreeChartFrame("Scatter Plot", sChart, true);
frame.pack();
frame.setVisible(true);
}


The constructor of MyXYDataset(rs) is:
--note that the print out of xValues and yValues does show that the arrays are filled with valid values:

public MyXYDataset(java.sql.ResultSet rs) {
try{
int item = 0;
while(rs.next()){
xValues[0][item] = new Double(rs.getDouble("te")/1000); yValues[0][item] = new Double(rs.getDouble("tce")/10000);
System.out.println(xValues[0][item] + " , " + yValues[0][item]);
item++;
}
System.out.println("done");
}catch(java.sql.SQLException e){
System.out.println(e.getMessage());
}


}

padaek

Re: ResultSet to XYDataset

Post by padaek » Tue Feb 26, 2002 2:56 pm

Never mind, I found and solved the problem. If anyone wants the solution, conctact me.

Victor

Re: ResultSet to XYDataset

Post by Victor » Sun Mar 03, 2002 6:05 pm

I have gotten requests for my solution, so here it is:

1. Create a new version of the XYDataset class so that ITEM_COUNT is public, then make the constructor take a ResultSet as an argument. I called this new class MyXYDataset.

2. In another class, MyFirstChart, produce a ResultSet, instatiate MyXYDataset with the ResultSet as an argument, set ITEM_COUNT to the number of observations from the ResultSet.

Sorry for the lengthy inclusion of code, but I was asked for it. If there's a more efficient solution than mine, please let me know.



//MyXYDataset
package com.jrefinery.chart.demo;

import com.jrefinery.data.*;

public class MyXYDataset extends AbstractSeriesDataset implements XYDataset {

public static int SERIES_COUNT = 1;
public static int ITEM_COUNT = 0;
public static double RANGE = 0;
public Double[][] xValues = new Double[SERIES_COUNT][ITEM_COUNT];
public Double[][] yValues = new Double[SERIES_COUNT][ITEM_COUNT];


public MyXYDataset(java.sql.ResultSet rs) {
try{
int item = 0;
while(rs.next()){
xValues[0][item] = new Double(rs.getDouble(1));
yValues[0][item] = new Double(rs.getDouble(2));
//System.out.println(xValues[0][item] + " , " + yValues[0][item]);
item++;
}
System.out.println("done");
}catch(java.sql.SQLException e){
System.out.println(e.getMessage());
}


}
/**
* Returns the number of items in the specified series.
* @param series The index (zero-based) of the series;
* @return The number of items in the specified series.
*/
public int getItemCount(int series) {
return ITEM_COUNT;
}
/**
* Returns the number of series in the data source.
* @return The number of series in the data source.
*/
public int getSeriesCount() {
return SERIES_COUNT;
}
/**
* Returns the name of the series.
* @param series The index (zero-based) of the series;
* @return The name of the series.
*/
public String getSeriesName(int series) {
return "Series" + series;
}
/**
* Returns the x-value for the specified series and item. Series are numbered 0, 1, ...
* @param series The index (zero-based) of the series;
* @param item The index (zero-based) of the required item;
* @return The x-value for the specified series and item.
*/
public Number getXValue(int series, int item) {
return xValues[series][item];
}
/**
* Returns the y-value for the specified series and item. Series are numbered 0, 1, ...
* @param series The index (zero-based) of the series;
* @param item The index (zero-based) of the required item;
* @return The y-value for the specified series and item.
*/
public Number getYValue(int series, int item) {
return yValues[series][item];
}
/**
* Insert the method's description here.
* Creation date: (2/25/02 1:22:47 PM)
* @return com.jrefinery.data.XYDataset
*/
public XYDataset newMethod() {
return null;
}
}



//MY FIRST CHART
package com.jrefinery.chart.demo;

import com.jrefinery.data.*;
import com.jrefinery.chart.ChartFactory;
import com.jrefinery.chart.JFreeChart;
import com.jrefinery.chart.JFreeChartFrame;

public class MyFirstChart {

public static void main(String[] args) {
JDBC jdbc = new JDBC();
JDBC jdbc1 = new JDBC();
int count = 0;
jdbc.connect("sqldb");
jdbc1.connect("sqldb");
java.sql.ResultSet rs = jdbc.getResults2("select varA,varB from tableA ");
java.sql.ResultSet rs1 = jdbc1.getResults2("select count(varA) as count from tableA");
try{
while(rs1.next()){
count = rs1.getInt(1);
System.out.println(count);
}
}catch(java.sql.SQLException e){}
MyXYDataset.ITEM_COUNT = count;
XYDataset xyData = new MyXYDataset(rs);
jdbc.close();
jdbc1.close();
JFreeChart sPlot = ChartFactory.createScatterPlot("Scatter Plot","TE","TCE",xyData,true);
JFreeChartFrame frame = new JFreeChartFrame("Scatter Plot", sPlot, true);
frame.pack();
frame.setVisible(true);

}
}

David Gilbert

Re: ResultSet to XYDataset

Post by David Gilbert » Mon Mar 04, 2002 10:00 am

Hi Victor,

Thanks for posting your code. I've also uploaded some code (from Bryan Scott), for reading data via JDBC, to the contributions section on the JFreeChart web page. When I get a chance, I'll work through both (and anything else that anyone wants to contribute) and see what I can put in a future version of JFreeChart.

Interested parties should post comments here - I don't work with JDBC a lot, so I'm happy to take advice on the best course...

Regards,

Dave Gilbert

victor souphom

Re: ResultSet to XYDataset

Post by victor souphom » Mon Mar 04, 2002 5:48 pm

Dave, thanks for the feedback. I'm only concerned that this approach may be slow for large datasets because first it loops through the ResultSet to create xValues & yValues arrays, then creates the chart. If only the process could be reduced to where the ResultSet is immediately plotted without going through a conversion process. I think it takes a lot more work to achieve that level of efficiency than what I suggested.

Victor

Locked