问题描述
假设我们需要在单个 XYSeriesCollection
中显示多个 XYSeries
。我的问题是,每次添加 XYSeries
时, JFreeChart
都想更新图表,这会减慢流程显示多个 XYSeries
。
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
.
我想要的是这样的:
// Do not update the chart
XYSeriesCollection.add(XYSeries1)
XYSeriesCollection.add(XYSeries2)
...
XYSeriesCollection.add(XYSeries10)
// Update the chart
我怎样才能这样做?
推荐答案
构造一个具有所需系列的新 XYSeriesCollection
,并在 XYPlot
上调用 setDataset()
。这将生成一个 DatasetChangeEvent
。
Construct a new XYSeriesCollection
having the desired series, and invoke setDataset()
on the XYPlot
. This will generate a single DatasetChangeEvent
.
附录:这是一个更新 N 的SSCCE系列,每个都有 N 值。由于这是性能问题,因此该示例可能对有所帮助。
Addendum: Here's an SSCCE that updates N series, each having N 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);
}
});
}
}
这篇关于在jFreeChart中一次显示所有XYSeries以提高速度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!