Displaying all XYSeries at once in jFreeChart for improving speed

╄→尐↘猪︶ㄣ 提交于 2019-12-08 06:17:37

问题


Suppose that we need to display multiple XYSeries in a single XYSeriesCollection. My problem is that every time I add a XYSeries, the JFreeChart wants to update the chart and that slows down the process of displaying multiple XYSeries.

What I want is similar to this:

// Do not update the chart
XYSeriesCollection.add(XYSeries1)
XYSeriesCollection.add(XYSeries2) 
...
XYSeriesCollection.add(XYSeries10)
// Update the chart

How can I do this?


回答1:


Construct a new XYSeriesCollection having the desired series, and invoke setDataset() on the XYPlot. This will generate a single DatasetChangeEvent.

Addendum: Here's an SSCCE that updates N series, each having N2 values. As this is a performance question, the example may be helpful in profiling.

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.util.Random;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import org.jfree.chart.*;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.time.Day;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.xy.XYDataset;

public class ChartPanelTest {

    private static final int N = 16;
    private static final Random random = new Random();

    private static XYDataset createDataset() {
        TimeSeriesCollection tsc = new TimeSeriesCollection();
        for (int j = 0; j < N; j++) {
            TimeSeries series = new TimeSeries("Data" + j);
            Day current = new Day();
            for (int i = 0; i < N * N; i++) {
                series.add(current, random.nextGaussian());
                current = (Day) current.next();
            }
            tsc.addSeries(series);
        }
        return tsc;
    }

    private static JFreeChart createChart(final XYDataset dataset) {
        JFreeChart chart = ChartFactory.createTimeSeriesChart(
            "Test", "Day", "Value", dataset, false, false, false);
        return chart;
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame f = new JFrame();
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                XYDataset dataset = createDataset();
                JFreeChart chart = createChart(dataset);
                final XYPlot plot = chart.getXYPlot();
                ChartPanel chartPanel = new ChartPanel(chart) {
                    @Override
                    public Dimension getPreferredSize() {
                        return new Dimension(600, 300);
                    }
                };
                f.add(chartPanel);
                JPanel p = new JPanel();
                p.add(new JButton(new AbstractAction("New") {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        plot.setDataset(createDataset());
                    }
                }));
                f.add(p, BorderLayout.SOUTH);
                f.pack();
                f.setLocationRelativeTo(null);
                f.setVisible(true);
            }
        });
    }
}



回答2:


Looking at the documentation for XYSeriesCollection (assuming add should be addSeries) there is no method addAll (or similar).

If you want, you could extend XYSeriesCollection and implement addAll. In this method, you could temporarily disable all listeners before adding and re-add them after. However, this would probably be a bad idea, and would definitely need to be synchronized:

  • it would produce unpredictable polymorphism behavior
  • it would require reflection hacks, as AbstractDataset.listenerList is private
  • it could generate a security exception
  • it would not be thread safe

So the short answer is no, it is not feasible.


… but this is a starting point for how you could do it:

Field field = getClass().getDeclaredField("listenerList");
field.setAccessible(true);
EventListenerList ell = field.get(this);
// go from here
field.setAccessible(false);


来源:https://stackoverflow.com/questions/16552270/displaying-all-xyseries-at-once-in-jfreechart-for-improving-speed

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!