gpt4 book ai didi

java - 使用 JFreeChart 更改系列时出现随机错误

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:02:57 25 4
gpt4 key购买 nike

我正在制作一个显示后台计算结果的 GUI。但在此之前,我想测试更改数据集。这是我的代码:

 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 轴是我的内部迭代次数而不是时间期间,并且在“某些计算”完成时也不是每隔一段时间更新一次。你能告诉我我做错了什么吗?或者是否有更好的方法来更新/刷新图形?

最佳答案

您的代码段未正确同步;你应该从 SwingWorkerprocess() 方法更新你的 dataset ,如图here .因为您的域是“我的内部迭代次数”,所以不要使用 DateAxis;相反,使用 NumberAxis,如 ChartFactory.createXYLineChart() 中所示.

附录:example 的变体在折线图上绘制 worker 的进度。请注意,createXYLineChart() 对域和范围都使用了 NumberAxis。给定折线图数据集中的 series,还要注意 process() 的实现如何在新数据到达时安全地更新数据集;收听图表将自行更新以做出响应。

private XYSeries series = new XYSeries("Result");

@Override
protected void process(List<Double> chunks) {
for (double d : chunks) {
label.setText(df.format(d));
series.add(++n, d);
}
}

chart

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.beans.PropertyChangeEvent;
import java.text.DecimalFormat;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JProgressBar;
import javax.swing.SwingWorker;
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.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

/**
* @see https://stackoverflow.com/a/13205322/230513
* @see https://stackoverflow.com/questions/4637215
*/
public final class ChartWorker {

private static final String S = "0.000000000000000";
private final JProgressBar progressBar = new JProgressBar();
private final JLabel label = new JLabel(S, JLabel.CENTER);
private final XYSeries series = new XYSeries("Result");
private final XYDataset dataset = new XYSeriesCollection(series);

private void create() {
JFrame f = new JFrame("√2");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(progressBar, BorderLayout.NORTH);
JFreeChart chart = ChartFactory.createXYLineChart(
"Newton's Method", "X", "Y", dataset,
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);
f.add(new ChartPanel(chart) {
@Override
public Dimension getPreferredSize() {
return new Dimension(640, 480);
}
}, BorderLayout.CENTER);
f.add(label, BorderLayout.SOUTH);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
runCalc();
}

private void runCalc() {
progressBar.setIndeterminate(true);
TwoWorker task = new TwoWorker();
task.addPropertyChangeListener((PropertyChangeEvent e) -> {
if ("progress".equals(e.getPropertyName())) {
progressBar.setIndeterminate(false);
progressBar.setValue((Integer) e.getNewValue());
}
});
task.execute();
}

private class TwoWorker extends SwingWorker<Double, Double> {

private static final int N = 5;
private final DecimalFormat df = new DecimalFormat(S);
double x = 1;
private int n;

@Override
protected Double doInBackground() throws Exception {
for (int i = 1; i <= N; i++) {
x = x - (((x * x - 2) / (2 * x)));
setProgress(i * (100 / N));
publish(x);
Thread.sleep(1000); // simulate latency
}
return x;
}

@Override
protected void process(List<Double> chunks) {
for (double d : chunks) {
label.setText(df.format(d));
series.add(++n, d);
}
}
}

public static void main(String[] args) {
EventQueue.invokeLater(new ChartWorker()::create);
}
}

关于java - 使用 JFreeChart 更改系列时出现随机错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13205251/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com