#1009 IndexOutOfBoundsException, setMaximumItemAge,setFixedAutoRa

1.0.x
closed-invalid
David Gilbert
None
5
2013-11-23
2010-08-20
egrof
No

Using method setMaximumItemAge from TimeSeries together with setFixedAutoRange from DateAxis causes some IndexOutOfBoundsExceptions.

Discussion

  •  egrof
    egrof
    2010-08-20

    Full example programm which causes Exception

     
    Attachments
  •  egrof
    egrof
    2010-08-20

    The Exception:

    Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Index: 687, Size: 687
    at java.util.ArrayList.RangeCheck(ArrayList.java:547)
    at java.util.ArrayList.get(ArrayList.java:322)
    at org.jfree.data.time.TimeSeries.getDataItem(TimeSeries.java:330)
    at org.jfree.data.time.TimeSeriesCollection.getY(TimeSeriesCollection.java:502)
    at org.jfree.data.xy.AbstractXYDataset.getYValue(AbstractXYDataset.java:92)
    at org.jfree.data.xy.XYBarDataset.getYValue(XYBarDataset.java:204)
    at org.jfree.chart.renderer.xy.XYBarRenderer.drawItem(XYBarRenderer.java:826)
    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:1029)
    at javax.swing.JComponent.paintToOffscreen(JComponent.java:5124)
    at javax.swing.BufferStrategyPaintManager.paint(BufferStrategyPaintManager.java:278)
    at javax.swing.RepaintManager.paint(RepaintManager.java:1224)
    at javax.swing.JComponent._paintImmediately(JComponent.java:5072)
    at javax.swing.JComponent.paintImmediately(JComponent.java:4882)
    at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:785)
    at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:713)
    at javax.swing.RepaintManager.seqPaintDirtyRegions(RepaintManager.java:693)
    at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(SystemEventQueueUtilities.java:125)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
    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)

     
  • David Gilbert
    David Gilbert
    2013-11-23

    This is not a bug, the problem is that your code is updating the dataset off the EDT, and the chart is being repainted on the EDT. JFreeChart is not thread safe, so this will cause problems occasionally. Here is a revised version of your code that works fine (the important changes are (1) launching the app on the EDT and (2) using a Swing Timer to update the dataset, then everything happens on the EDT):

    public class Charttest {
    
    public Charttest() {
    
        JFrame frame = new JFrame();
        JPanel panel = createChart("Name");
        frame.add(panel);
    
        frame.validate();
        frame.setSize(new Dimension(800,600));
        frame.setVisible(true);
        addValues(panel);
    }
    
    public void addValues(JPanel panel) {
        JFreeChart chart1 = ((ChartPanel) panel).getChart();
        XYBarDataset intervallXYDataset1 = (XYBarDataset) ((XYPlot) chart1.getPlot()).getDataset();
        TimeSeriesCollection tsc1 = (TimeSeriesCollection) intervallXYDataset1.getUnderlyingDataset();
        final TimeSeries ts1 = tsc1.getSeries("Name");
    
        double d;
    
        Timer t = new Timer(50, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Double d = Math.random();
                ts1.add(new Millisecond(new Date(System.currentTimeMillis())), d);
                System.out.println("" + d + " : " + ts1.getItemCount());
            }
        });
        t.start();
    }
    
    private static JPanel createChart(String id) {
    
        long shownPeriod = 1000 * 15; // Milliseconds
        TimeSeries ts = new TimeSeries(id);
        ts.setMaximumItemAge(shownPeriod);
    
        JFreeChart chart = ChartFactory.createXYBarChart(
                null,
                null,
                true,
                id,
                //  new TimeSeriesCollection(new TimeSeries(id)),
                (IntervalXYDataset) new XYBarDataset(new TimeSeriesCollection(ts), 50),
                //  new TimePeriodValuesCollection(new TimePeriodValues(id)),
                PlotOrientation.VERTICAL,
                false,
                true,
                false);
    
        XYPlot plot = (XYPlot) chart.getPlot();
        plot.setDomainPannable(true);
        plot.setRangePannable(true);
        plot.setForegroundAlpha(0.85f);
        chart.setBackgroundPaint(new Color(238, 238, 238));
    
        DateAxis axis = (DateAxis) plot.getDomainAxis();
        axis.setTickMarkPosition(DateTickMarkPosition.MIDDLE);
        axis.setAutoRange(true);
        axis.setFixedAutoRange(shownPeriod); 
        axis.createStandardDateTickUnits();
    
        NumberAxis yAxis = (NumberAxis) plot.getRangeAxis();
        yAxis.setRange(0, 1.05);
        XYBarRenderer renderer = (XYBarRenderer) plot.getRenderer();
    
        renderer.setDrawBarOutline(false);
        renderer.setBarPainter(new StandardXYBarPainter());
        renderer.setShadowVisible(false);
        renderer.setSeriesPaint(0, Color.red);
    
        return new ChartPanel(chart);
    }
    
    public static void main(String args[]) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
    
                new Charttest();
            }
        });
    
    }
    

    }

     
  • David Gilbert
    David Gilbert
    2013-11-23

    • status: open --> closed-invalid
    • assigned_to: David Gilbert
    • Group: --> 1.0.x