I use jfreechart-1.0.13.jar and jcommon-1.0.16.jar. I am working with a chart which has a single dataset with multiple series in it. The series are private fields that change over time (get cleared and filled up again with different data). If these changes come in at a low rate, everything is fine, but when I send in new data at a fast rate, the System.out prints out an IndexOutOfBoundsException (which is not thrown at me). It must be catched inside JFC somewhere and printed out with e.printStackTrace() or so.
Here is an example:
Code: Select all
import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Rectangle;
import javax.swing.JFrame;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
public class PlotDemo extends JFrame implements Runnable {
private static final long serialVersionUID = 1L;
private XYSeries plotSeries1, plotSeries2;
public PlotDemo() {
XYSeriesCollection data = new XYSeriesCollection();
XYLineAndShapeRenderer r = new XYLineAndShapeRenderer();
plotSeries1 = new XYSeries("demoSeries1");
plotSeries2 = new XYSeries("demoSeries2");
for(int i = 0; i < 500; i++) {
plotSeries2.add(i, Math.sin(i / 50.0));
}
data.addSeries(plotSeries1);
data.addSeries(plotSeries2);
r.setSeriesStroke(0, new BasicStroke(1f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL));
r.setSeriesShape(0, new Rectangle(new Dimension(0, 0)));
r.setSeriesPaint(0, Color.blue);
r.setSeriesStroke(1, new BasicStroke(1f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL));
r.setSeriesShape(1, new Rectangle(new Dimension(0, 0)));
r.setSeriesPaint(1, Color.red);
JFreeChart chart = ChartFactory.createXYLineChart("", "", "",
data, PlotOrientation.VERTICAL, false, true, false);
XYPlot plot = chart.getXYPlot();
plot.setRenderer(r);
ValueAxis domain= new NumberAxis(), range = new NumberAxis();
plot.setDomainAxis(domain);
plot.setRangeAxis(range);
ChartPanel phnPlotPanel = new ChartPanel(chart);
add(phnPlotPanel, BorderLayout.CENTER);
setVisible(true);
setSize(new Dimension(400, 400));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
PlotDemo main = new PlotDemo();
new Thread(main).start();
}
@Override
public void run() {
for(int t = 0; t < 1000; t++) {
plotSeries1.clear();
for(int i = 0; i < 500; i++) {
plotSeries1.add(i, Math.random() * Math.sin(i / 30.0), false);
}
plotSeries1.fireSeriesChanged();
sleepMs(10); // <------- change time here to see the difference
}
}
private void sleepMs(int ms) {
try {
Thread.sleep(ms);
} catch(InterruptedException e) {}
}
}

Or is the whole concept I am using to present different datasets over time, totally wrong? Before, I just created a new plotseries every time and then removed the old one and added the new one from/to the plot. But I thought this was kind of senseless.
Cheers, Stefan