gpt4 book ai didi

java - jfreechart自定义滚动产生索引越界异常

转载 作者:行者123 更新时间:2023-11-30 06:43:38 25 4
gpt4 key购买 nike

我想实现一个具有自定义滚动方式的图表组件。到目前为止我得到了这个:

package test;

import java.awt.Color;
import java.util.*;
import javax.swing.JFrame;
import org.jfree.chart.*;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.*;
import org.jfree.chart.renderer.xy.DefaultXYItemRenderer;
import org.jfree.data.xy.*;
import org.jfree.ui.*;

public class MultipleStreamGraph extends ChartPanel {

private XYSeriesCollection dataset;
private int scroll = 25;

public MultipleStreamGraph(String title, String labelXAxis, String labelYAxis, double lowerBound, double upperBound) {
super(null);
dataset = new XYSeriesCollection();
setChart(ChartFactory.createXYLineChart(title, labelXAxis, labelYAxis,
dataset, PlotOrientation.VERTICAL, true, true, false));
XYPlot plot = (XYPlot) getChart().getPlot();
DefaultXYItemRenderer renderer = new DefaultXYItemRenderer();
plot.setRenderer(renderer);
plot.setBackgroundPaint(Color.white);
setDomainZoomable(true);
setRangeZoomable(false);
ValueAxis axis = plot.getDomainAxis();
axis.setFixedAutoRange(100);
axis = plot.getRangeAxis();
axis.setRange(lowerBound, upperBound);
XYSeries xys = new XYSeries("bogus");
for (int i = 0; i < 100; i++) {
xys.add(i, 0);
}
dataset.addSeries(xys);
renderer.setSeriesShapesVisible(0, false);
renderer.setSeriesLinesVisible(0, false);
renderer.setSeriesVisibleInLegend(0, false);
}

public void addZone(IntervalMarker marker) {
getChart().getXYPlot().addRangeMarker(marker, Layer.BACKGROUND);
}

public void addSeries(String name) {
XYSeries xys = new XYSeries(name);
xys.setMaximumItemCount(100);
dataset.addSeries(xys);
}

public void addData(String seriesName, double y) {
XYSeries xys = dataset.getSeries(seriesName);
int x = 0;
if (xys != null) {
if (xys.getItemCount() >= xys.getMaximumItemCount()) {
List<XYDataItem> items = xys.getItems();
double[] ys = new double[items.size() - scroll];
for (int i = 0; i < ys.length; i++) {
ys[i] = items.get(i + scroll).getYValue();
}
xys.delete(0, xys.getItemCount() - 1);
for (int i = 0; i < ys.length; i++) {
xys.add(new XYDataItem(i, ys[i]));
}
}
x = xys.getItemCount();
xys.add(new XYDataItem(x, y));
}
}

public static void main(String[] args) {
JFrame frm = new JFrame();
final MultipleStreamGraph msg = new MultipleStreamGraph("Title", "X Axis", "Y Axis", 0, 10);
msg.addZone(new IntervalMarker(8.0, 10.0, new Color(1, 0, 0, 1 / 4f)));
msg.addZone(new IntervalMarker(2.0, 8.0, new Color(0, 1, 0, 1 / 4f)));
msg.addZone(new IntervalMarker(0.0, 2.0, new Color(1, 0, 0, 1 / 4f)));
msg.addSeries("1");
msg.addSeries("2");
msg.addSeries("3");
msg.addSeries("4");
msg.addSeries("5");
msg.addSeries("6");
msg.addSeries("7");
msg.addSeries("8");
msg.addSeries("9");
msg.addSeries("0");
Timer timer = new Timer();
final Random rando = new Random();
timer.schedule(new TimerTask() {

@Override
public void run() {
msg.addData("1", rando.nextDouble() * 10);
msg.addData("2", rando.nextDouble() * 10);
msg.addData("3", rando.nextDouble() * 10);
msg.addData("4", rando.nextDouble() * 10);
msg.addData("5", rando.nextDouble() * 10);
msg.addData("6", rando.nextDouble() * 10);
msg.addData("7", rando.nextDouble() * 10);
msg.addData("8", rando.nextDouble() * 10);
msg.addData("9", rando.nextDouble() * 10);
msg.addData("0", rando.nextDouble() * 10);
}
}, 50, 50);
frm.setContentPane(msg);
frm.pack();
frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
RefineryUtilities.centerFrameOnScreen(frm);
frm.setVisible(true);
}
}

这就是我想要的行为。问题是这段代码在某些时候会产生这个异常:

Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: 
Index: 75, Size: 0
at java.util.ArrayList.RangeCheck(ArrayList.java:547)
at java.util.ArrayList.get(ArrayList.java:322)
at org.jfree.data.xy.XYSeries.getDataItem(XYSeries.java:616)
at org.jfree.data.xy.XYSeriesCollection.getX(XYSeriesCollection.java:320)
at org.jfree.data.xy.AbstractXYDataset.getXValue(AbstractXYDataset.java:75)
at org.jfree.chart.renderer.xy.XYLineAndShapeRenderer.drawPrimaryLine(XYLineAndShapeRenderer.java:985)
at org.jfree.chart.renderer.xy.XYLineAndShapeRenderer.drawItem(XYLineAndShapeRenderer.java:911)
at org.jfree.chart.plot.XYPlot.render(XYPlot.java:3738)
at org.jfree.chart.plot.XYPlot.draw(XYPlot.java:3310)
at org.jfree.chart.JFreeChart.draw(JFreeChart.java:1235)
at org.jfree.chart.ChartPanel.paintComponent(ChartPanel.java:1668)
at javax.swing.JComponent.paint(JComponent.java:1037)
at javax.swing.JComponent.paintToOffscreen(JComponent.java:5132)
at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(RepaintManager.java:1523)
at javax.swing.RepaintManager$PaintManager.paint(RepaintManager.java:1454)
at javax.swing.RepaintManager.paint(RepaintManager.java:1257)
at javax.swing.JComponent._paintImmediately(JComponent.java:5080)
at javax.swing.JComponent.paintImmediately(JComponent.java:4890)
at javax.swing.RepaintManager$3.run(RepaintManager.java:814)
at javax.swing.RepaintManager$3.run(RepaintManager.java:802)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:802)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:745)
at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:725)
at javax.swing.RepaintManager.access$1000(RepaintManager.java:46)
at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1668)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:672)
at java.awt.EventQueue.access$400(EventQueue.java:81)
at java.awt.EventQueue$2.run(EventQueue.java:633)
at java.awt.EventQueue$2.run(EventQueue.java:631)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:642)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

这似乎是由于我删除了这些项目而触发的,但同时它似乎发生在一个单独的线程中,独立于我的线程。有没有更简单的方法来实现这种滚动方式?例如,有没有办法更新项目而不是用新项目替换它们?更重要的是,如何防止这种异常发生?

最佳答案

好的。我发现了问题。我需要在 EDT 中清除并添加项目。所以方法 addData() 应该如下所示:

public void addData(final String seriesName, final double y) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
XYSeries xys = dataset.getSeries(seriesName);
int x = 0;
if (xys != null) {
if (xys.getItemCount() >= xys.getMaximumItemCount()) {
List<XYDataItem> items = xys.getItems();
double[] ys = new double[items.size() - scroll];
for (int i = 0; i < ys.length; i++) {
ys[i] = items.get(i + scroll).getYValue();
}
xys.delete(0, xys.getItemCount() - 1);
for (int i = 0; i < ys.length; i++) {
xys.add(new XYDataItem(i, ys[i]));
}
}
x = xys.getItemCount();
xys.add(new XYDataItem(x, y));
}
}
});
}

关于java - jfreechart自定义滚动产生索引越界异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44007547/

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