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

时间:2012-08-22 11:25:23

标签: java jfreechart

我制作了一个用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]);

        }
.....

谢谢。

3 个答案:

答案 0 :(得分:2)

您是否在将数据添加到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,之后会花费更长的时间来添加它们。

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

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

答案 1 :(得分:1)

我只提出了一些建议,你必须尝试一下:你可以在100个左右的数据集中为你的系列添加数据。您将不得不使用单独的线程添加到模型中,然后重新绘制图表,等待一段时间并添加另一个等等。

但是,我过去曾使用过JFreeChart,而且我认为它只有2500多个值才能正常运行。

答案 2 :(得分:1)

使 JFreechart 运行良好。设置前系列数据集: axisR.setAutoRange(false); axisH.setAutoRange(false); 它就像这样:

JFreeChart chart1 = ChartFactory.createXYLineChart(null, null, "Level", dataset1);
XYPlot plot1 = chart1.getXYPlot();
axisR = (NumberAxis)plot.getRangeAxis();
axisH = (NumberAxis)plot.getDomainAxis();
axisR.setAutoRange(false);
axisH.setAutoRange(false);

数据设置后

axisR.setAutoRange(true);
axisH.setAutoRange(true);