Rendering issues with JFreeChart and JComboBox

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
amarnath578
Posts: 22
Joined: Tue Aug 25, 2009 8:02 pm
antibot: No, of course not.

Rendering issues with JFreeChart and JComboBox

Post by amarnath578 » Tue Aug 25, 2009 8:16 pm

Hi, I have come across a strange problem today while working on plotting the candlestick charts on a frame. Before getting into the description of the problem, let me explain you the layout of the Frame on which I am plotting. I am using a Frame with BorderLayout on it. The NORTH side of the frame has a Toolbar with 3 ComboBoxes and the CENTER portion is occupied with a Chart Panel.

The problem is that the 3 combo boxes have about 6-15 items in each of them. So when I expand the combo box (which is added to the tool bar), it displays the number of items which will be overlaid on the Chart Panel. After this operation, I am not able to see the highlighted item in the Combo Box as I move the mouse around and I also do not see the Scrool Panel's vertical stick moving up and down though I manually move it.

However, if I add the toolbar on the SOUTH region of the Border Layout, every thing seems to be working as expected. The selection of the items are highlighted and also the movement of the scroll panel is clearly visible. So I thought it might have something to do with rendering the Combo Box items on top of the Chart Panel. The Chart Panel has several plots and annotations on it hence it draws continuously.

It seems to be so weird to me and I have no clue as to how to resolve it.

Thanks,
Amaranath Allampati

skunk
Posts: 1087
Joined: Thu Jun 02, 2005 10:14 pm
Location: Brisbane, Australia

Re: Rendering issues with JFreeChart and JComboBox

Post by skunk » Tue Aug 25, 2009 9:30 pm

amarnath578 wrote:The Chart Panel has several plots and annotations on it hence it draws continuously.
The symptoms sound like the chart is being updated on the wrong thread. JFreeChart, like every other Swing component, is NOT thread safe. You must make sure in your own code that any operation performed on the chart that may result in a repaint only happens on the event dispatching thread.

amarnath578
Posts: 22
Joined: Tue Aug 25, 2009 8:02 pm
antibot: No, of course not.

Re: Rendering issues with JFreeChart and JComboBox

Post by amarnath578 » Tue Aug 25, 2009 11:00 pm

Hi Shunk,

Thanks for the quick response. I am positive that all the updates that I do are done on the Event Dispatch Thread because I am not instantiating any new threads to update the chart on listening to the changes to the XYDataSet. I am updating the Chart only from the callback methods which are getting executed on EDT. I shall check this once again anyways.

As expected, when I am plotting the static data, I do not see this problem existing. JComboBox is being rendered properly and the scrolling is alsofine. However, I also noticed that the CandleStickRenderer's draw() method is being executed continuously provided that the Chart Panel is being focused. Do Renderers have anything to do with this kind of behaviour?

skunk
Posts: 1087
Joined: Thu Jun 02, 2005 10:14 pm
Location: Brisbane, Australia

Re: Rendering issues with JFreeChart and JComboBox

Post by skunk » Wed Aug 26, 2009 1:08 am

Just to make sure. You do know that the chart automatically repaints itself whenever an item is added to a dataset?

amarnath578
Posts: 22
Joined: Tue Aug 25, 2009 8:02 pm
antibot: No, of course not.

Re: Rendering issues with JFreeChart and JComboBox

Post by amarnath578 » Wed Aug 26, 2009 2:15 pm

Thanks for the reply. I am in the process of making sure that all the updates to the data sets are happening on EDT. Please let me know if you think any other issue can cause this sort of behavior.

However, I see this rendering problem even if the dataset does not change.

amarnath578
Posts: 22
Joined: Tue Aug 25, 2009 8:02 pm
antibot: No, of course not.

Re: Rendering issues with JFreeChart and JComboBox

Post by amarnath578 » Wed Aug 26, 2009 3:23 pm

Hi Shunk,

I did make sure that all the updates are done on EDT, but I still see the same problem. I am just making sure that the whole update is happening on EDT as listed below:

SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
fireMarketDataUpdated(transaction);
}
});

long timeNow = Calendar.getInstance().getTimeInMillis();
if(timeNow - lastUpdate > UPDATE_INTERVAL)
{
lastUpdate = timeNow;
SwingUtilities.invokeLater(new Runnable(){
public void run()
{
fireDatasetChanged();
}
});
}

I know this is not good to execute the whole process, instead of executing the parts, I wanted to make sure that no update executes outside of EDT. I sense something else which is the rootcause for this. I still see the two problems I used to notice, as noted in my first post. 1) When I expand the Combobox that displays 10 items, overlaying most of them on to the chart, mouse over will not highlight the underlying item 2) Scroll bar is not visible properly, though it rools the items and the selection is working fine. It does not highlight the item that has the mouse hovering on it.

Unfortunately, I do not know how to post an image with my message that would make things clear to you. Please help resolving this issue.

skunk
Posts: 1087
Joined: Thu Jun 02, 2005 10:14 pm
Location: Brisbane, Australia

Re: Rendering issues with JFreeChart and JComboBox

Post by skunk » Wed Aug 26, 2009 4:17 pm

The issue is that the Dataset will by default automatically call fireDatasetChanged() (which will trigger a repaint) whenever data is added to the dataset.

Also, adding or removing an Annotation will also trigger a repaint.

amarnath578
Posts: 22
Joined: Tue Aug 25, 2009 8:02 pm
antibot: No, of course not.

Re: Rendering issues with JFreeChart and JComboBox

Post by amarnath578 » Wed Aug 26, 2009 6:17 pm

You are right. But I do not want to draw my chart every milli second.... so I have this condition of updating the chart only once in a selected time period.... Do you mean you see any problems with the code I sent you?

I still did not understand why the Combo Box is not behaving as it has to..... Because my only concern now is that why should there be any repainting when my dataset does not change at all.... I am working on a trading product that needs to plot the price trends dynamically in production. However, the environment on which I am working does not update the data set unless I do a fake trade....

There are no annotations added to the plot as of now.... There are no updates to the data set either.... I assume there should not be any repainting at all.... still I don't see the scroll bar moving or the mouse hovered item highlighted. Please help me resolve it.

Thanks.

paradoxoff
Posts: 1634
Joined: Sat Feb 17, 2007 1:51 pm

Re: Rendering issues with JFreeChart and JComboBox

Post by paradoxoff » Wed Aug 26, 2009 9:41 pm

A guess: maybe repaints are triggered by the AWT to clean the dirty region If the JComboBox overlays the ChartPanel and partly hides it?
Are you using the chartBuffer to display the panel?
Tome, it looks as if your app simply hangs. Is it possible that frequent redraws (from wherever they might come) consume too much cpu power and memory?

skunk
Posts: 1087
Joined: Thu Jun 02, 2005 10:14 pm
Location: Brisbane, Australia

Re: Rendering issues with JFreeChart and JComboBox

Post by skunk » Wed Aug 26, 2009 9:43 pm

amarnath578 wrote:I assume there should not be any repainting at all....
But it clearly is repainting when it shouldn't be.

You are going to have to post more code than just the two fragments above.

skunk
Posts: 1087
Joined: Thu Jun 02, 2005 10:14 pm
Location: Brisbane, Australia

Re: Rendering issues with JFreeChart and JComboBox

Post by skunk » Wed Aug 26, 2009 11:08 pm

Something other ideas that may help

Does the same problem occur if you right click on the ChartPanel to display the popup menu?

When the combobox is overlaying the ChartPanel and the program become unresponsive, can you run Task Manager on Windows or top on linux to see if the java program is hogging a cpu. If so it would suggest that some event handler is receiving a notification that painting is complete and immediately forcing a repaint for some reason. Do you have any timers in your program that are periodically repainting the chart?

amarnath578
Posts: 22
Joined: Tue Aug 25, 2009 8:02 pm
antibot: No, of course not.

Re: Rendering issues with JFreeChart and JComboBox

Post by amarnath578 » Thu Aug 27, 2009 3:21 pm

Hi Shunk,

Your observation is absolutely right, but not always. I see that sometimes it does not even display the pop up menu on right clicking the chart.... I did not consider this issue as major as the popup appears 90% of the time as expected. Again, I have no clue why this happens provided that there are no annotations added or no timers added to the chart.... Do you have any straight forward solution as to why the pop up is not visible correctly always?

amarnath578
Posts: 22
Joined: Tue Aug 25, 2009 8:02 pm
antibot: No, of course not.

Re: Rendering issues with JFreeChart and JComboBox

Post by amarnath578 » Fri Aug 28, 2009 2:54 pm

Hi,
At last, we decided to add a menu in place of adding combo boxes to the toolbar, to avoid this kind of rendering problems. Menu is working fine and does not have any issues so far.

I am currently working on developing a charting framework for a trading product (Mostly the domain axis is Time and the range axis varies among Price, Volume or any other indicators). One of the major requirements requires me to display several charts as a time in the Chart Panel (this is something which is already working). However, I need to enhance this feature to display the price of the multiple charts as an annotation on the Chart Panel i.e. if there are 4 data sets plotted on the Chart, I need to display 4 different prices one per chart on top of the chart as annotations. More over, I need to indicate the mouse position on these four charts in terms of a painted circle and this circle needs to continuously move on the trend lines as I move the cursor.

Please have a look at the url specified below to get a better understanding of what I am talking about: http://www.google.com/finance?q=NYSE:ICE

Please visit this URL and hover the mouse on the chart and see how it moves continuously on the chart. Please let me know how I can implement this functionality using JFreeChart.

Thanks.
Amar

Locked