PDA

View Full Version : [Java Programming] Anyone know anything about JFreeChart?



mikeejimbo
2009-07-06, 07:07 AM
I seem to be having trouble getting my charts to update when I want them to do so. I know that when you add a dataseries to a chart it registers with that chart so that whenever the data is updated, the chart gets updated. But I have a weird problem with that. I've created my chart and my dataseries, and I have a loop like this:


for(int i = 0; i < 100; i++)
series.add(i, i*i);

But the chart doesn't update until it runs through the loop entirely. I have to wait until the loop is completed to see anything. I thought maybe my computer, being slow, was just not getting around to showing the chart until the loop was done anyway, so I tried making that '100' into '1000'. All that did was make it take 10x as long to show the chart.

So I thought I could trick it, by forcing it to update. I made my loop do this:


for(int i = 0; i < 100; i++)
{
series.add(i, i * i);

JFreeChart chart = ChartFactory.createScatterPlot(
"Scatter Plot Demo", // title
"X", "Y", // axis labels
dataset, // dataset
PlotOrientation.VERTICAL,
true, // legend? yes
true, // tooltips? yes
false // URLs? no
);
BufferedImage image = chart.createBufferedImage(500,300);
jLabel.setIcon(image);
}

I thought that by creating a new chart and showing a new image I could force it to update, even if it was very slow. But it showed the exact same behavior, the chart wouldn't update until my loop was finished. I even tried creating an entirely new dataset each time through a loop, creating a new series, adding all my data to the series, adding the series to the dataset, plotting a new chart with that dataset, creating a new image from the chart, and then updating the jlabel with that image. This too showed the same behavior. Maybe I'm just a Java greenhorn, but it doesn't make much sense to me.

Thanks for your advice in advance.

RMS Oceanic
2009-07-06, 08:15 AM
Have you tried clearing jLabel of all images before you add the new one on?

mikeejimbo
2009-07-06, 08:32 AM
Have you tried clearing jLabel of all images before you add the new one on?

That would be a good thing to try.

As for the thing with the first loop, I suspected that it might be a threading issue, and some other programmers also saw that this might be a problem. So I made a thread like this:


new Thread() {
public void run() {

int i = 0;
while(i < 10000)
{
series.add(i, i*i);
i++;
}
}
}.start();

That actually updated it the way I wanted.