Random errors when changing series using JFreeChart

你离开我真会死。 提交于 2019-11-25 22:06:30

问题


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:

 DefaultXYDataset dataset = new DefaultXYDataset();
@Override
        public void run() {
                // TODO Auto-generated method stub
                for (int i = 0; i < periods; i++) {
                        series[0][i] = (double) i;
                        series[1][i] = 0;
                }
                dataset.addSeries(\"Series0\", series);
                for (int it = 0; it < 10; it++) {
                        series[1][random.nextInt(periods)] =  random.nextInt(100) / 2;
                        double[][] d = new double[2][periods];
                        for (int i = 0; i < periods; i++) {
                                d[0][i] = series[0][i];
                                d[1][i] = series[1][i];
                        }
                        dataset.removeSeries(\"Series0\");
                        dataset.addSeries(\"Series0\", series);
//                      try {
//                              Thread.sleep(100);
//                      } catch (java.lang.InterruptedException ex) {
//                      }
                }

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?


回答1:


Your snippet is incorrectly synchronized; you should update your dataset from the process() method of a SwingWorker, as shown here. Instead of a DateAxis, use a NumberAxis, as shown in ChartFactory.createXYLineChart().

Addendum: This variation on the example cited plots the worker's progress on a line chart. Note that createXYLineChart() uses NumberAxis for both domain and range.

private XYSeriesCollection collection = new XYSeriesCollection();
private XYSeries series = new XYSeries("Result");
...
collection.addSeries(series);
JFreeChart chart = ChartFactory.createXYLineChart(
    "Newton's Method", "X", "Y", collection,
    PlotOrientation.VERTICAL, false, true, false);
XYPlot plot = (XYPlot) chart.getPlot();
plot.getRangeAxis().setRange(1.4, 1.51);
plot.getDomainAxis().setStandardTickUnits(
    NumberAxis.createIntegerTickUnits());
XYLineAndShapeRenderer renderer =
    (XYLineAndShapeRenderer) plot.getRenderer();
renderer.setSeriesShapesVisible(0, true);
this.add(new ChartPanel(chart), BorderLayout.CENTER);
...
private int n;
@Override
protected void process(List<Double> chunks) {
    for (double d : chunks) {
        label.setText(df.format(d));
        series.add(++n, d);
    }
}


来源:https://stackoverflow.com/questions/13205251/random-errors-when-changing-series-using-jfreechart

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!