Is This a Bug?

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
allquixotic
Posts: 3
Joined: Mon Jul 18, 2005 4:41 pm

Is This a Bug?

Post by allquixotic » Mon Jul 18, 2005 7:16 pm

EDIT: As far as answering the question in the post title, I think the answer should be "yes" - and here's why. The exception occurs within an ArrayList in the JFreeChart library. In my opinion, all exceptions within an API should provide System.out feedback on why they occurred (assuming the API can't catch the exception and work around it). Apparently I've managed to do something unexpected that the library can't recover from, and this needs to turn into a "caught" exception in a future release. So yes, it's a bug, but it doesn't help me resolve my problem knowing that. If you want to either work on a fix for the bug, or help me with my problem, please read on. Thank you for your time!

Hello,

I'm trying to generate a bar chart with a variable number of series, dependent on the various unique values of a single record in my JDBC ResultSet. However, I think I may have found a bug within the JFreeChart source, since I can't attribute the error to anything else.

On a related note, the API doesn't specify what happens when you make multiple calls to executeQuery on a single dataset - is it creating a new series each time; is it trying to append the data to the one-and-only series; etc. Ideally I'd be able to specify a column that, becomes the criteria for defining new series - but as you can see here I've attempted to work around this limitation by finding the unique values with the JdbcRowSetImpl query "uniquebus".

By the way, I bought the Developer's Guide. It was nice for getting me started in the right direction, but, like all general-purpose documents, it is too general to answer my questions once I get into fully using the API. I guess there's no way that I could avoid posting to the forums because it's impossible to cover all the usages of this API in a mere 551 pages. ;)


The following code, generates the exception below, at the statement civil.executeQuery(buildQuery).

Code: Select all

 JFreeChart jfc;
              ArrayList<String> bus = new ArrayList<String> ();
          try {
            JdbcRowSetImpl uniquebus = customSql("SELECT * FROM disbus",true,true);
            while(uniquebus.next())
            {
              bus.add (uniquebus.getString("BusinessUnit"));
            }
            
              JDBCCategoryDataset civil = new JDBCCategoryDataset(getConnection());
              String buildQuery = null;
              for(String ase : bus)
              {

                if(ase == null || ase.length() < 1)
                  buildQuery = "SELECT holy2.symed, holy2.amount FROM holy2 WHERE exposee Is Null";
                else
                  buildQuery = "SELECT holy2.symed, holy2.amount FROM holy2 WHERE exposee=\'" + ase + "\';";
                System.out.println(buildQuery);
                civil.executeQuery(buildQuery);
                System.out.println("heh");
              }
              
              jfc =  ChartFactory.createBarChart("YTD WAR Value","Month","Dollars",civil,PlotOrientation.VERTICAL,true,false,false);
              CategoryPlot plot = (CategoryPlot) jfc.getPlot();
              BarRenderer renderer = (BarRenderer) plot.getRenderer();
              renderer.setSeriesPaint(0,Color.blue);
              renderer.setSeriesPaint(1,Color.yellow);
              return jfc;
          }
          catch(SQLException se)
          {
                  se.printStackTrace();
          }

Code: Select all

Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Index: 1, Size: 1

	at java.util.ArrayList.RangeCheck(Unknown Source)

	at java.util.ArrayList.remove(Unknown Source)

	at org.jfree.data.DefaultKeyedValues2D.removeRow(DefaultKeyedValues2D.java:350)

	at org.jfree.data.category.DefaultCategoryDataset.removeRow(DefaultCategoryDataset.java:265)

	at org.jfree.data.jdbc.JDBCCategoryDataset.executeQuery(JDBCCategoryDataset.java:215)

	at org.jfree.data.jdbc.JDBCCategoryDataset.executeQuery(JDBCCategoryDataset.java:179)

	at warproject.formEngine.AccessDBEngine.ytdWarValue(AccessDBEngine.java:75)
	at warproject.formEngine.BasicFormModel.menuAction(BasicFormModel.java:565)
	at warproject.formEngine.FormMenuBarMenuListener.actionPerformed(FormMenuBar.java:230)
	at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
	at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
	at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
	at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
	at javax.swing.AbstractButton.doClick(Unknown Source)
	at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Unknown Source)
	at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(Unknown Source)
	at java.awt.Component.processMouseEvent(Unknown Source)
	at javax.swing.JComponent.processMouseEvent(Unknown Source)
	at java.awt.Component.processEvent(Unknown Source)
	at java.awt.Container.processEvent(Unknown Source)
	at java.awt.Component.dispatchEventImpl(Unknown Source)
	at java.awt.Container.dispatchEventImpl(Unknown Source)
	at java.awt.Component.dispatchEvent(Unknown Source)
	at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
	at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
	at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
	at java.awt.Container.dispatchEventImpl(Unknown Source)
	at java.awt.Window.dispatchEventImpl(Unknown Source)
	at java.awt.Component.dispatchEvent(Unknown Source)
	at java.awt.EventQueue.dispatchEvent(Unknown Source)
	at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.run(Unknown Source)
As a way of attempting to troubleshoot this error, I went ahead and used my native database engine (MS Access) to run the SQL statements that are dynamically generated, and for each one I got (what appears to be) valid results. Further, if I call executeQuery exactly once using the same columns but without distinguishing based on the desired series-delimeter, I actually get a chart displayed on my screen as intended.

Regards,
Sean

mhilpert
Posts: 497
Joined: Wed Apr 02, 2003 1:57 pm
Location: Germany

Re: Is This a Bug?

Post by mhilpert » Tue Jul 19, 2005 1:04 pm

allquixotic wrote:In my opinion, all exceptions within an API should provide System.out feedback on why they occurred (assuming the API can't catch the exception and work around it).
Nope, System.out is bad style. Exceptions are the best way to tell an application that something went wrong. (E.g. if the application does not show a console or is even running as a service (yes, we run a Java application as a service!), those messages just get lost.) Exceptions are the only ways to get error handling into an application during design time. Exceptions are objects and hold all required data you need to know (including such feedback as you wish).

Other than that: don't use JDBCCategoryDatasets but write your own interface of getting data via JDBC and create the specific JFreeCHart datasets. This way you are flexible enough with your datasets and can work around possible restrictions of JDBCCategoryDataset.
Java 11, JFreeChart 1.0.15, JFreeSVG 4.0

allquixotic
Posts: 3
Joined: Mon Jul 18, 2005 4:41 pm

Re: Is This a Bug?

Post by allquixotic » Tue Jul 19, 2005 5:31 pm

mhilpert wrote:Other than that: don't use JDBCCategoryDatasets but write your own interface of getting data via JDBC and create the specific JFreeCHart datasets. This way you are flexible enough with your datasets and can work around possible restrictions of JDBCCategoryDataset.
That's what I ended up doing... I don't see much point in the existence of JDBCCategoryDataset unless you want exactly one series of data and it comes out of JDBC just the way you want it in the chart... in my case there is no SQL expression that can pull out my data the way I need it so that the JDBCCategoryDataset can just enumerate through a ResultSet, pull values and lay them down in a chart.

So I used a CategoryDataset instead, at the expense of even more code :p

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Post by david.gilbert » Tue Jul 19, 2005 10:37 pm

Any suggestions for making JDBCCategoryDataset more flexible? Should it just be dropped? Does it serve as a useful starting point? I don't use JDBC myself, so I'm interested in what others think.
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

kevdev
Posts: 3
Joined: Fri Jul 15, 2005 1:30 pm

Post by kevdev » Wed Jul 20, 2005 2:48 pm

I think jdbc dataset is a good Idea as I is quick, easy and tidy to use but in my experience it is limited.

I have used the JDBCXYDataset in the past but I was limited to one time series. When I wanted to add more TimeSeries I found that I had to take out the JDBC and create a new TimeSeriesCollection and add the TimeSeries separately after I had pulled my ResultSet. The result of this was more code!

The reason I had to do this was because with JDBCXY, it expects the first column to be the Date, second to be first series, third column to be next series eg.

X | SER1 | SER2 | SER3
-----------+---------+---------+--------
1-Aug-2002 | 54.3 | 32.1 | 53.4
2-Aug-2002 | 43.4 | 54.3 | 75.2
3-Aug-2002 | 39.6 | 55.9 | 37.1
4-Aug-2002 | 35.4 | 55.2 | 27.5
5-Aug-2002 | 33.9 | 49.8 | 22.3
6-Aug-2002 | 35.2 | 48.4 | 17.7
7-Aug-2002 | 38.9 | 49.7 | 15.3
8-Aug-2002 | 36.3 | 44.4 | 12.1
9-Aug-2002 | 31.0 | 46.3 | 11.0

But with my table I have date as the first column, the value as the next column with an extra ID column.. Eg..

X | val | ID
-----------+---------+---------+--------
1-Aug-2002 | 54.3 | SER1
2-Aug-2002 | 43.4 | SER1
3-Aug-2002 | 39.6 | SER1
4-Aug-2002 | 35.4 | SER1
5-Aug-2002 | 33.9 | SER1
1-Aug-2002 | 54.3 | SER2
2-Aug-2002 | 43.4 | SER2
3-Aug-2002 | 39.6 | SER2
4-Aug-2002 | 35.4 | SER2
5-Aug-2002 | 33.9 | SER2
1-Aug-2002 | 54.3 | SER3
2-Aug-2002 | 43.4 | SER3
3-Aug-2002 | 39.6 | SER3
4-Aug-2002 | 35.4 | SER3
5-Aug-2002 | 33.9 | SER3

Obviously this give the advantage of not having to have a separate columns for each timeseries you add, for example, what happens when you want to add a new series - you would have to add SER4 column, which is no good.

My suggestion would be to make JDBC not dependant on 1 column per series…

jumper18
Posts: 3
Joined: Wed Mar 08, 2006 7:19 am

A real bug in JDBCCategoryDataset.java:215

Post by jumper18 » Wed Mar 08, 2006 7:33 am

The for loop that tries to remove the old values starting at JDBCCategoryDataset.java:215 has flawed logic

Code: Select all

            // Remove any previous old data
            int i = getRowCount();
            for(;i >= 0;--i) {
                removeRow(i);
            }
won't work, it will always throw IndexOutOfBoundsException because i the first time around will be size not size-1. It needs to be more like

Code: Select all

            // Remove any previous old data
            int i = getRowCount();
            while (--i >= 0) {
                removeRow(i);
            }

angel
Posts: 899
Joined: Thu Jan 15, 2004 12:07 am
Location: Germany - Palatinate

Post by angel » Wed Mar 08, 2006 9:14 am

My suggestion: deprecate the JDBC-Datasets. Until now I haven't found any benefit.

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Post by david.gilbert » Wed Mar 08, 2006 3:53 pm

angel wrote:My suggestion: deprecate the JDBC-Datasets. Until now I haven't found any benefit.
Yes, I think you are right.
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Re: A real bug in JDBCCategoryDataset.java:215

Post by david.gilbert » Wed Mar 08, 2006 5:03 pm

jumper18 wrote:The for loop that tries to remove the old values starting at JDBCCategoryDataset.java:215 has flawed logic

Code: Select all

            // Remove any previous old data
            int i = getRowCount();
            for(;i >= 0;--i) {
                removeRow(i);
            }
won't work, it will always throw IndexOutOfBoundsException because i the first time around will be size not size-1. It needs to be more like

Code: Select all

            // Remove any previous old data
            int i = getRowCount();
            while (--i >= 0) {
                removeRow(i);
            }
Thanks! I entered this in the bug database and it should be fixed for the next release (1.0.2):

http://sourceforge.net/tracker/index.ph ... tid=115494
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

Locked