jfreechart自定义滚动生成索引超出范围的异常

时间:2017-05-16 17:04:27

标签: java charts jfreechart

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

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)

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

1 个答案:

答案 0 :(得分:1)

行。我发现了这个问题。我需要在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));
            }
        }
    });
}