使用JFreeChart更改系列时的随机错误 [英] Random errors when changing series using JFreeChart

查看:179
本文介绍了使用JFreeChart更改系列时的随机错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做一个GUI来显示背景计算的结果。但在此之前,我想测试改变数据集。这是我的代码:

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) {
//                      }
                }

正如你所看到的,我想改变图上的点(每次完成'一些复杂的计算') - 这个改变是由我在另一个类中调用的线程。我的问题是这个整个概念不工作。它抛出'系列索引超出边界'IllegalArgumentException,'索引超出边界' - 一些库内部数组列表等。我不使用DynamicTimeSeriesCollection,因为我需要X轴是我的内部迭代数,而不是时间周期,并且当一些计算不是每个时间周期都完成时更新。你能告诉我我做错了什么吗?

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?

推荐答案

您的代码段不正确同步;您应该从 SwingWorker 进程()更新数据集 $ c>,如此处所示。而不是 DateAxis ,请使用 NumberAxis ,如 ChartFactory.createXYLineChart()

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().

附录:引用的示例中的变体绘制了工作人员在折线图上的进度。请注意, createXYLineChart()对域和范围使用 NumberAxis

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);
    }
}

这篇关于使用JFreeChart更改系列时的随机错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆