2

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

        }
.....

谢谢你。

4

3 回答 3

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,它会在之后添加它们需要更长的时间。

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

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

于 2012-08-22T14:01:18.473 回答
1

我只有一些建议,您必须尝试一下:您可以将数据以 100 个左右的值的形式添加到您的系列中。您将不得不使用单独的线程添加到模型中,然后重新绘制图表,等待一段时间并添加另一束,依此类推。

但是,我过去曾使用过 JFreeChart,我认为它可以很好地处理超过 2500 个值。

于 2012-08-22T11:32:37.507 回答
1

使 JFreechart 运行良好。设置前系列数据集:axisR.setAutoRange(false); 轴H.setAutoRange(假);它是这样的:

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);
于 2021-03-25T15:22:27.577 回答