Hello there,
First of all I'd like to thank everyone one here for the sheer amount of information in this forum regarding help for JFreeChart. Its one of the best support forums Ive seen for a project. I have hunted through the forum and so far havent been able to decipher any info I have garnered that may help i my problem.
I am a student who is just learning to get to grips with Java and is designing a stock control database as an assignment with a Java GUI client, and wish to include a chart that includes details like count and quantity from the database.
I do not know how to include the data from the SQL database into the chart. My class for the chart so far looks like the below code. I only want to represent the total values from the three columns stated below in a barchart. This may be very easy to the folks on here but I am a beginner at this and cannot find the step by step guide tutorial I need and I do understand these details may well be in the developers guide but being s student I cannot afford to buy the guide at this stage so Ill kindly ask for help in this forum regarding my problem.
The code below currently outputs the data values I want in the chart to a table, what I am asking for some help for is to include this data into a bar chart.
Any information would be much appreciated and thank you for your time.
public class NewChart extends javax.swing.JFrame{
private Connection connection;
//table headers
private String[] columnNames = {"Count",
"Min Stock Level",
"Quantity"};
//stores the data in the table
private Object[][] data;
public NewChart( Connection conn ) {
initComponents();
this.connection = conn;
//create the table and data initialisation
try{
//read data from table
ResultSet res = executeQuery( "SELECT * FROM mainpage" );
//we have to find a way to count the number of records in the result set
int counter = 0;
//put data into object 2d array
while( res.next() ){
counter++;
}
data = new Object[ 120 ][ 3 ];
counter = 0;
res = executeQuery( "SELECT * FROM mainpage" );
while( res.next() ){
data[ counter ][ 0 ] = res.getInt( "count" );
data[ counter ][ 1 ] = res.getInt( "minstocklevel" );
data[ counter ][ 2 ] = res.getInt( "quantity" );
// data[ counter ][ 3 ] =
// data[ counter ][ 4 ] =
// data[ counter ][ 5 ] =
counter++;
}
}catch(Exception ex){
System.err.println( "Error in constructor -> " + ex );
}
//put data in table
partstable.setModel(new javax.swing.table.DefaultTableModel( data, columnNames ) );
// new JFreeChart createChart(data, columnNames);
// this.setVisible(true);
}
//method used when a query is made to the database
public ResultSet executeQuery( String query ){
try{
Statement st = connection.createStatement();
//execute the string query
st.executeQuery( query );
//get the result set
ResultSet res = st.getResultSet();
return res;
}catch(Exception ex){
System.err.println( "Error in executeQuery() method -> " + ex );
}
//return nothing
return null;
}
//
//
/**
* Creates new form NewChart
*/
public NewChart(final String title) {
// initComponents();
}
SQL and JFreechart barchart
I just had an issue with a 3d BArchart and wrote a testcase for it. Have a look here.
http://www.jfree.org/phpBB2/viewtopic.php?t=20430
For your needs, i would consider to use just one dataset. If you look at the addValue method you can see how the chart is filled with data.
The null series are used to plot the bars side by side.
So if you use a normal BarRenderer instead of the BarRenderer3D, you will not run into the Problem I had in this post.
That should be all you need, fell free to use the code there...
Greets, Tom
http://www.jfree.org/phpBB2/viewtopic.php?t=20430
For your needs, i would consider to use just one dataset. If you look at the addValue method you can see how the chart is filled with data.
The null series are used to plot the bars side by side.
So if you use a normal BarRenderer instead of the BarRenderer3D, you will not run into the Problem I had in this post.
That should be all you need, fell free to use the code there...
Greets, Tom
-
- Posts: 3
- Joined: Tue Feb 27, 2007 1:37 pm
Thanks for the information, but I really am no further ahead, I think this may be too difficult for me to implement as a beginner. I know its probably easy for someone to see all I have to do is use the array I already have that has stored the data values from the SQL table and then implement these values into a chart.
With someone such as myself I need some sample dataset code using my own array code to learn what it does and then try and implement it. I never knew it would be as difficult as this as a beginner to implement a chart. Im also using Netbeans 5.5, ill keep trying as I wont give up until Ive cracked it
With someone such as myself I need some sample dataset code using my own array code to learn what it does and then try and implement it. I never knew it would be as difficult as this as a beginner to implement a chart. Im also using Netbeans 5.5, ill keep trying as I wont give up until Ive cracked it

-
- JFreeChart Project Leader
- Posts: 11734
- Joined: Fri Mar 14, 2003 10:29 am
- antibot: No, of course not.
- Contact:
I'd suggest that you start with a new instance of DefaultCategoryDataset, then iterate through your data array and add each individual value to the dataset. You'll end up with a separate copy of your data, but it should get you up and running with a chart.
Once you have more experience with JFreeChart, you could consider writing your own class that implements the CategoryDataset interface and provides direct access to the underlying data array.
Once you have more experience with JFreeChart, you could consider writing your own class that implements the CategoryDataset interface and provides direct access to the underlying data array.
David Gilbert
JFreeChart Project Leader
Read my blog
Support JFree via the Github sponsorship program
JFreeChart Project Leader


-
- Posts: 3
- Joined: Tue Feb 27, 2007 1:37 pm