Display all XYSeries at once in jFreeChart to increase speed

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

What I want looks like this:

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

How can i do this?

+1
source share
2 answers

Create a new XYSeriesCollection with the desired row and call setDataset() on XYPlot . This will create one DatasetChangeEvent .

Appendix: Here is an SSCCE that updates the N series, each of which has N 2 values. Since this is a performance issue, this example may be useful in profiling .

test image

 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); } }); } } 
+1
source

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

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

  • this will lead to unpredictable polymorphism behavior.
  • this requires reflective hacks, and AbstractDataset.listenerList is private
  • it can throw a security exception
  • it will not be thread safe

So, the short answer is no, this is not possible [/ strong>.


& hellip; but this is the starting point for how you could do this:

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

Source: https://habr.com/ru/post/1369165/


All Articles