您是在将数据添加到之前创建图表TimeSeries
吗?如果您在创建/显示图表后添加数据,则在plot
添加每个点后将重新绘制。
考虑这个例子:
import java.text.SimpleDateFormat;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
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.time.TimeSeriesDataItem;
import org.jfree.data.xy.XYDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
public class TimeSeriesDemo1 extends ApplicationFrame {
private static TimeSeries s1;
private static boolean loadFirst;
public TimeSeriesDemo1(String title) {
super(title);
JPanel chartPanel = createDemoPanel();
chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
setContentPane(chartPanel);
if (!loadFirst){
Runnable task = new Runnable() {
@Override
public void run() {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {}
int count = 0;
for ( int year = 2000 ; year < 2010 ; year++){
for (int month = 1 ; month < 13 ; month++){
for (int day = 1 ; day < 29 ; day++){
final TimeSeriesDataItem di = new TimeSeriesDataItem(new Day(day, month, year), Math.random() * 20);
count++;
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
s1.add(di);
}
});
}
}
}
System.out.println("After the chart is created " + count);
}
};
Thread th = new Thread(task,"Load Data"){
};
th.start();
}
}
private static JFreeChart createChart(XYDataset dataset) {
JFreeChart chart = ChartFactory.createTimeSeriesChart(
"Large Dataset Demo", // title
"Date", // x-axis label
"Value", // y-axis label
dataset, // data
true, // create legend?
true, // generate tooltips?
false // generate URLs?
);
XYPlot plot = (XYPlot) chart.getPlot();
DateAxis domainAaxis = (DateAxis) plot.getDomainAxis();
domainAaxis.setDateFormatOverride(new SimpleDateFormat("MMM-yyyy"));
return chart;
}
private static XYDataset createDataset() {
s1 = new TimeSeries("Random Data");
int count = 0;
if (loadFirst){
for ( int year = 2000 ; year < 2010 ; year++){
for (int month = 1 ; month < 13 ; month++){
for (int day = 1 ; day < 29 ; day++){
final TimeSeriesDataItem di = new TimeSeriesDataItem(new Day(day, month, year), Math.random() * 20);
count++;
s1.add(di);
}
}
}
System.out.println("Before the chart is created " + count);
}
TimeSeriesCollection dataset = new TimeSeriesCollection();
dataset.addSeries(s1);
return dataset;
}
public static JPanel createDemoPanel() {
JFreeChart chart = createChart(createDataset());
return new ChartPanel(chart);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
loadFirst = true;
TimeSeriesDemo1 demo = new TimeSeriesDemo1("Large Time Series Demo ");
demo.pack();
RefineryUtilities.centerFrameOnScreen(demo);
demo.setVisible(true);
}
});
}
}
如果您设置loadFirst = true
图表在屏幕上绘制之前添加大约 3k 个数据点。
设置loadFirst = false
,它会在之后添加它们需要更长的时间。
您应该能够看到性能上的差异。从您的示例中不清楚您使用的是哪种方法。
如果您在加载数据之前显示图表,则尽可能尝试切换顺序。