Updating Chart Data

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
Stever
Posts: 3
Joined: Fri Oct 29, 2010 1:05 am
antibot: No, of course not.
Location: Vancouver Island, British Columbia, Canada

Updating Chart Data

Post by Stever » Fri Oct 29, 2010 1:18 am

Hi, no doubt I will slap my forehead when I hear the answer, but I have been unable to find out how to do something very simple. I have created a statistical line chart with ChartFactory.createLineChart. The dataset is a DefaultStatisticalCategoryDataset. I need to update the data every minute or so. I have checked the JavaDoc and this forum, there are lots of answers to more complicated questions, but not apparently to this one. Do I:
1) Completely recreate the chart every time I need to update the plot. Doing this is not really a problem, as I only update every minute or so and there is not a lot of data, but this seems rather heavy handed.
2) Just add data to the dataset, assuming the chart has registered itself with the dataset and when the data changes the chart is notified and redraws. If this is so, how do I remove the old data? use the Remove call with the row and column key?
3) Do something far more sophisticated whose function is not obvious from the API.

Many thanks in advance for your help, and the promise of a local brew should you ever visit Vancouver Island in Canada.. :)
Steve

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

Re: Updating Chart Data

Post by skunk » Fri Oct 29, 2010 2:05 am

#2 The chart should automatically redraw itself every time the data changes. Just remember that ChartPanel, like every other Swing component, MUST be updated on the event dispatching thread, else there will be spurious display/repaint glitches. If you are updating the data asynchronously, just use SwingUtilities.invokeLater() to ensure that the update happens on the correct thread.

newuser
Posts: 1
Joined: Fri Oct 29, 2010 6:13 pm
antibot: No, of course not.

Re: Updating Chart Data

Post by newuser » Fri Oct 29, 2010 6:29 pm

I'm also interested in the original #2 question regarding old data, especial if you set a fixed range for the domain. If you keep adding to the dataset every minute and leave the chart up and running for a long time (like a week), old data will simply move to the left outside of the visible plot area. Eventually, the dataset gets very big and effects performance, doesn't it? Do you have to manually remove the old values in the dataset yourself to keep it to a reasonable size?

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

Re: Updating Chart Data

Post by skunk » Fri Oct 29, 2010 7:47 pm

Some dataset implementations have built-in support for a maximum number of items. For example in XYSeries you can call

Code: Select all

public void setMaximumItemCount(int maximum)
and the dataset will automatically delete old items whenever a new item is added.

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

Re: Updating Chart Data

Post by paradoxoff » Sat Oct 30, 2010 9:40 am

Stever wrote: 2) Just add data to the dataset, assuming the chart has registered itself with the dataset and when the data changes the chart is notified and redraws. If this is so, how do I remove the old data? use the Remove call with the row and column key?
3) Do something far more sophisticated whose function is not obvious from the API.
#2: If "adding data to the dataset" means "adding new values for existing column and row keys", you can simply call the add() method. The new data (mean and standard deviation) at the given column/row key will overwrite the existing data. The dataset itself will not grow.

If you add new column and/or row keys, the dataset will grow, and at one point the plot might become illegible due to the high number of column and/or row keys. In that case, you could extend SlidingCategoryDataset and create a SlidingStaticticalCategoryDataset that wraps a StatisticalCategoryDataset. The original dataset wil then still grow, but you can define which part of the dataset is actually shown in the plot, and the plot will remain legible.

#3: Not sure whether this is "not obvious": you can write your own StatisticalCategoryDataset implementation from scratch. This will give you full control about what data is shown. Fortunately, all datasets are based on interfaces, and JFreeChart offers a range of abstract dataset implementations that you can use as a starting point.

Locked