Skip to content
Advertisement

Random errors when changing series using JFreeChart

I’m making a GUI that display result of background calculations. But before that, I wanted to test changing the dataset. Here is my code:

JavaScript

As you can see, I want to change points on the graph (every time it finishes ‘some complicated computations’) – this change is in the thread invoked by me in another class. My problem is that this whole concept is not working. It throws ‘Series index out of bounds’-IllegalArgumentException, ‘index out of bounds’ – of some library inner arraylist etc.. I’m not using DynamicTimeSeriesCollection because I need the X axis to be the number of my inner iterations not the time period, and also update when ‘some computations’ are finished not every some time period. Can you tell me what I’m doing wrong? Or is there a better way to update/refresh the graph?

Advertisement

Answer

Your snippet is incorrectly synchronized; you should update your dataset from the process() method of a SwingWorker, as shown here. Because your domain is “the number of my inner iterations”, don’t use a DateAxis; instead, use a NumberAxis, as shown in ChartFactory.createXYLineChart().

Addendum: This variation on the example plots the worker’s progress on a line chart. Note that createXYLineChart() uses NumberAxis for both domain and range. Given a series in the line chart’s dataset, note also how the implementation of process() can safely update the dataset as new data arrives; the listening chart will update itself in response.

JavaScript

chart

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement