gpt4 book ai didi

java - 生成 JFreeChart 图形的巨大执行时间

转载 作者:行者123 更新时间:2023-11-29 05:56:06 24 4
gpt4 key购买 nike

我做了一个用JfreeChart生成图形的方法。我有超过 2500 个值可以绘制在我的散点图中,但这需要很多时间。那么我们该如何解决呢?

这是一段代码:

public NuagePointsFact(){
LectureFichierExcel lfe = new LectureFichierExcel("data/requete1Test.xls");
this.series = new TimeSeries[lfe.findDate().size()];
this.dataset = new TimeSeriesCollection();

for(int i=0; i<lfe.findDate().size(); i++){
this.series [i] = new TimeSeries(i, Day.class);
series[i].add(new Day(new Date(lfe.findDate().get(i))), lfe.findValues().get(i));

dataset.addSeries(series[i]);

}
.....

谢谢。

最佳答案

您是在将数据添加到 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 之后添加它们需要更长的时间。

您应该能够看到性能上的差异。从您的示例中不清楚您使用的是哪种方法。

如果您在加载数据之前显示图表,请尽可能尝试切换顺序。

关于java - 生成 JFreeChart 图形的巨大执行时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12072035/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com