假设我们需要在一个XYSeries
中显示多个XYSeriesCollection
。我的问题是,每次添加XYSeries
时,JFreeChart
都希望更新图表,这会减慢显示多个XYSeries
的过程。
我想要的是这样的:
// Do not update the chart
XYSeriesCollection.add(XYSeries1)
XYSeriesCollection.add(XYSeries2)
...
XYSeriesCollection.add(XYSeries10)
// Update the chart
我该怎么做?
答案 0 :(得分:1)
查看the documentation for XYSeriesCollection
(假设add
应为addSeries
),没有方法addAll
(或类似)。
如果您愿意,可以扩展XYSeriesCollection
并实施addAll
。在此方法中,您可以在添加之前暂时禁用所有侦听器,然后重新添加它们。但是,这可能是一个坏主意,肯定需要synchronized
:
AbstractDataset.listenerList
是private
所以简短的答案是不,这是不可行的。
...但这是
Field field = getClass().getDeclaredField("listenerList");
field.setAccessible(true);
EventListenerList ell = field.get(this);
// go from here
field.setAccessible(false);
答案 1 :(得分:1)
构建具有所需系列的新XYSeriesCollection
,并在setDataset()
上调用XYPlot
。这将生成一个DatasetChangeEvent
。
附录:这是一个更新 N 系列的SSCCE,每个系列都有 N 2 值。由于这是性能问题,因此该示例在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);
}
});
}
}